Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
import os, re, uuid, torch, scipy.io.wavfile, edge_tts, asyncio
|
| 2 |
import numpy as np
|
| 3 |
import gradio as gr
|
| 4 |
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
|
|
@@ -31,16 +31,12 @@ class ModelManager:
|
|
| 31 |
cls._music_pipeline = pipeline("text-to-audio", "facebook/musicgen-small", device="cpu")
|
| 32 |
return cls._music_pipeline
|
| 33 |
|
| 34 |
-
# 8์ธ ๋ฉค๋ฒ
|
| 35 |
-
|
| 36 |
-
"์์ค (Korea)":
|
| 37 |
-
"
|
| 38 |
-
"
|
| 39 |
-
"
|
| 40 |
-
"Elena (Spain)": {"voice": "es-ES-ElviraNeural", "lang": "Spanish"},
|
| 41 |
-
"Amira (Egypt)": {"voice": "ar-EG-SalmaNeural", "lang": "Arabic"},
|
| 42 |
-
"Liwei (China)": {"voice": "zh-CN-XiaoxiaoNeural", "lang": "Chinese"},
|
| 43 |
-
"Sophie (France)": {"voice": "fr-FR-DeniseNeural", "lang": "French"}
|
| 44 |
}
|
| 45 |
|
| 46 |
async def band_consulting(user_input, member_name, consult_lang, g_inst, b_inst, d_inst, chords):
|
|
@@ -48,15 +44,11 @@ async def band_consulting(user_input, member_name, consult_lang, g_inst, b_inst,
|
|
| 48 |
voice_path = f"/tmp/v_{req_id}.mp3"
|
| 49 |
music_path = f"/tmp/m_{req_id}.wav"
|
| 50 |
|
| 51 |
-
# 1. ๋ฉค๋ฒ ์ ์ ๋ก์ง: ์์ด๊ฐ ์ ํ๋๋ฉด ๋๋ค ๋ฉค๋ฒ๊ฐ ๋ด๋น
|
| 52 |
-
actual_member = member_name
|
| 53 |
-
if consult_lang == "English":
|
| 54 |
-
actual_member = random.choice(list(MEMBERS_CONFIG.keys()))
|
| 55 |
-
|
| 56 |
-
target_lang = MEMBERS_CONFIG[actual_member]["lang"] if consult_lang == "Native" else consult_lang
|
| 57 |
-
|
| 58 |
jam_context = f"Guitar: {g_inst}, Bass: {b_inst}, Drums: {d_inst}, Chords: {chords}"
|
| 59 |
-
|
|
|
|
|
|
|
|
|
|
| 60 |
Provide 5-7 lines of professional music advice.
|
| 61 |
[TAB] Section: Detailed chords/tabs.
|
| 62 |
[MUSIC] Section: English prompt reflecting: {jam_context}"""
|
|
@@ -77,32 +69,30 @@ async def band_consulting(user_input, member_name, consult_lang, g_inst, b_inst,
|
|
| 77 |
out = qwen(f"<|im_start|>system\n{system_prompt}<|im_end|>\n<|im_start|>user\n{user_input}<|im_end|>\nassistant\n", max_new_tokens=1024)
|
| 78 |
ai_text_raw = out[0]['generated_text'].split("assistant\n")[-1]
|
| 79 |
|
| 80 |
-
# ํ์ฑ
|
| 81 |
tab_match = re.search(r'\[TAB\](.*?)(\[|$)', ai_text_raw, re.DOTALL | re.IGNORECASE)
|
| 82 |
music_match = re.search(r'\[MUSIC:(.*?)\]', ai_text_raw, re.IGNORECASE)
|
| 83 |
-
tab_display = tab_match.group(1).strip() if tab_match else "No Data"
|
| 84 |
clean_text = re.sub(r'\[TAB\].*?(\[|$)', '', ai_text_raw, flags=re.DOTALL | re.IGNORECASE)
|
| 85 |
clean_text = re.sub(r'\[MUSIC:.*?\]', '', clean_text, flags=re.IGNORECASE).strip()
|
| 86 |
|
| 87 |
-
# TTS
|
| 88 |
-
voice_name =
|
| 89 |
communicate = edge_tts.Communicate(re.sub(r'[\*\#\-\_\~\|]', '', clean_text), voice_name)
|
| 90 |
await communicate.save(voice_path)
|
| 91 |
|
| 92 |
-
# MusicGen (12์ด)
|
| 93 |
music_gen = ModelManager.get_music()
|
| 94 |
-
music_p = music_match.group(1).strip() if music_match else "rock"
|
| 95 |
music_output = music_gen(music_p, forward_params={"max_new_tokens": 512})
|
| 96 |
audio_data = np.squeeze(music_output["audio"])
|
| 97 |
audio_int16 = (audio_data * 32767).astype(np.int16)
|
| 98 |
scipy.io.wavfile.write(music_path, music_output["sampling_rate"], audio_int16)
|
| 99 |
|
| 100 |
-
return
|
| 101 |
|
| 102 |
with gr.Blocks() as demo:
|
| 103 |
-
|
| 104 |
-
|
| 105 |
btn = gr.Button("API", visible=False)
|
| 106 |
-
btn.click(band_consulting,
|
| 107 |
|
| 108 |
demo.queue().launch()
|
|
|
|
| 1 |
+
import os, re, uuid, torch, scipy.io.wavfile, edge_tts, asyncio
|
| 2 |
import numpy as np
|
| 3 |
import gradio as gr
|
| 4 |
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
|
|
|
|
| 31 |
cls._music_pipeline = pipeline("text-to-audio", "facebook/musicgen-small", device="cpu")
|
| 32 |
return cls._music_pipeline
|
| 33 |
|
| 34 |
+
# 8์ธ ๋ฉค๋ฒ ๋ณด์ด์ค (์๋ด ์ธ์ด์ ๊ด๊ณ์์ด ๋ฉค๋ฒ ๊ณ ์ ๋ณด์ด์ค ์ ์ง)
|
| 35 |
+
MEMBERS_VOICE = {
|
| 36 |
+
"์์ค (Korea)": "ko-KR-SunHiNeural", "Chloe (USA)": "en-US-AriaNeural",
|
| 37 |
+
"Naomi (Japan)": "ja-JP-NanamiNeural", "Beatrice (Brazil)": "pt-BR-FranciscaNeural",
|
| 38 |
+
"Elena (Spain)": "es-ES-ElviraNeural", "Amira (Egypt)": "ar-EG-SalmaNeural",
|
| 39 |
+
"Liwei (China)": "zh-CN-XiaoxiaoNeural", "Sophie (France)": "fr-FR-DeniseNeural"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
}
|
| 41 |
|
| 42 |
async def band_consulting(user_input, member_name, consult_lang, g_inst, b_inst, d_inst, chords):
|
|
|
|
| 44 |
voice_path = f"/tmp/v_{req_id}.mp3"
|
| 45 |
music_path = f"/tmp/m_{req_id}.wav"
|
| 46 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
jam_context = f"Guitar: {g_inst}, Bass: {b_inst}, Drums: {d_inst}, Chords: {chords}"
|
| 48 |
+
|
| 49 |
+
# ๋ฆฌ๋๋ ์ง์: ์ ํ๋ consult_lang์ผ๋ก ๋ต๋ณ ๊ฐ์
|
| 50 |
+
system_prompt = f"""You are {member_name}, a global rock star.
|
| 51 |
+
You MUST respond ONLY in the language: {consult_lang}.
|
| 52 |
Provide 5-7 lines of professional music advice.
|
| 53 |
[TAB] Section: Detailed chords/tabs.
|
| 54 |
[MUSIC] Section: English prompt reflecting: {jam_context}"""
|
|
|
|
| 69 |
out = qwen(f"<|im_start|>system\n{system_prompt}<|im_end|>\n<|im_start|>user\n{user_input}<|im_end|>\nassistant\n", max_new_tokens=1024)
|
| 70 |
ai_text_raw = out[0]['generated_text'].split("assistant\n")[-1]
|
| 71 |
|
|
|
|
| 72 |
tab_match = re.search(r'\[TAB\](.*?)(\[|$)', ai_text_raw, re.DOTALL | re.IGNORECASE)
|
| 73 |
music_match = re.search(r'\[MUSIC:(.*?)\]', ai_text_raw, re.IGNORECASE)
|
| 74 |
+
tab_display = tab_match.group(1).strip() if tab_match else "No Score Data"
|
| 75 |
clean_text = re.sub(r'\[TAB\].*?(\[|$)', '', ai_text_raw, flags=re.DOTALL | re.IGNORECASE)
|
| 76 |
clean_text = re.sub(r'\[MUSIC:.*?\]', '', clean_text, flags=re.IGNORECASE).strip()
|
| 77 |
|
| 78 |
+
# TTS ๋ณด์ด์ค (๋ฉค๋ฒ ์ด๋ฆ์ ๋ง๋ ๋ณด์ด์ค ์ ํ)
|
| 79 |
+
voice_name = MEMBERS_VOICE.get(member_name, "en-US-AriaNeural")
|
| 80 |
communicate = edge_tts.Communicate(re.sub(r'[\*\#\-\_\~\|]', '', clean_text), voice_name)
|
| 81 |
await communicate.save(voice_path)
|
| 82 |
|
|
|
|
| 83 |
music_gen = ModelManager.get_music()
|
| 84 |
+
music_p = music_match.group(1).strip() if music_match else "rock guitar riff"
|
| 85 |
music_output = music_gen(music_p, forward_params={"max_new_tokens": 512})
|
| 86 |
audio_data = np.squeeze(music_output["audio"])
|
| 87 |
audio_int16 = (audio_data * 32767).astype(np.int16)
|
| 88 |
scipy.io.wavfile.write(music_path, music_output["sampling_rate"], audio_int16)
|
| 89 |
|
| 90 |
+
return clean_text, voice_path, music_path, tab_display
|
| 91 |
|
| 92 |
with gr.Blocks() as demo:
|
| 93 |
+
inputs = [gr.Textbox(visible=False) for _ in range(7)]
|
| 94 |
+
outputs = [gr.Textbox(visible=False), gr.Audio(visible=False), gr.Audio(visible=False), gr.Textbox(visible=False)]
|
| 95 |
btn = gr.Button("API", visible=False)
|
| 96 |
+
btn.click(band_consulting, inputs, outputs, api_name="predict")
|
| 97 |
|
| 98 |
demo.queue().launch()
|