Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,6 +4,7 @@ import gradio as gr
|
|
| 4 |
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
|
| 5 |
from groq import Groq
|
| 6 |
|
|
|
|
| 7 |
class ModelManager:
|
| 8 |
_llm_pipeline = None
|
| 9 |
_music_pipeline = None
|
|
@@ -31,7 +32,7 @@ class ModelManager:
|
|
| 31 |
cls._music_pipeline = pipeline("text-to-audio", "facebook/musicgen-small", device="cpu")
|
| 32 |
return cls._music_pipeline
|
| 33 |
|
| 34 |
-
# ์ธ์ด๋ณ
|
| 35 |
LANG_MEMBER_MAP = {
|
| 36 |
"Korean": {"name": "์์ค (Korea)", "voice": "ko-KR-SunHiNeural"},
|
| 37 |
"English": {"name": "Chloe (USA)", "voice": "en-US-AriaNeural"},
|
|
@@ -48,12 +49,14 @@ async def band_consulting(user_input, selected_lang, g_inst, b_inst, d_inst, cho
|
|
| 48 |
voice_path = f"/tmp/v_{req_id}.mp3"
|
| 49 |
music_path = f"/tmp/m_{req_id}.wav"
|
| 50 |
|
| 51 |
-
|
| 52 |
|
| 53 |
-
|
| 54 |
-
|
|
|
|
|
|
|
| 55 |
[TAB] Section: Detailed chords/tabs.
|
| 56 |
-
[MUSIC] Section: English prompt
|
| 57 |
|
| 58 |
ai_text_raw = ""
|
| 59 |
groq_client = ModelManager.get_groq()
|
|
@@ -71,15 +74,23 @@ async def band_consulting(user_input, selected_lang, g_inst, b_inst, d_inst, cho
|
|
| 71 |
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)
|
| 72 |
ai_text_raw = out[0]['generated_text'].split("assistant\n")[-1]
|
| 73 |
|
|
|
|
| 74 |
tab_match = re.search(r'\[TAB\](.*?)(\[|$)', ai_text_raw, re.DOTALL | re.IGNORECASE)
|
| 75 |
music_match = re.search(r'\[MUSIC:(.*?)\]', ai_text_raw, re.IGNORECASE)
|
| 76 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
clean_text = re.sub(r'\[TAB\].*?(\[|$)', '', ai_text_raw, flags=re.DOTALL | re.IGNORECASE)
|
| 78 |
-
clean_text = re.sub(r'\[MUSIC:.*?\]', '', clean_text, flags=re.IGNORECASE)
|
|
|
|
| 79 |
|
| 80 |
-
|
|
|
|
| 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"
|
| 85 |
music_output = music_gen(music_p, forward_params={"max_new_tokens": 512})
|
|
@@ -87,11 +98,12 @@ async def band_consulting(user_input, selected_lang, g_inst, b_inst, d_inst, cho
|
|
| 87 |
audio_int16 = (audio_data * 32767).astype(np.int16)
|
| 88 |
scipy.io.wavfile.write(music_path, music_output["sampling_rate"], audio_int16)
|
| 89 |
|
| 90 |
-
|
|
|
|
| 91 |
|
| 92 |
with gr.Blocks() as demo:
|
| 93 |
inputs = [gr.Textbox(visible=False) for _ in range(6)]
|
| 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 |
|
|
|
|
| 4 |
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
|
| 5 |
from groq import Groq
|
| 6 |
|
| 7 |
+
# ๋ชจ๋ธ ๊ด๋ฆฌ ์ฑ๊ธํค
|
| 8 |
class ModelManager:
|
| 9 |
_llm_pipeline = None
|
| 10 |
_music_pipeline = None
|
|
|
|
| 32 |
cls._music_pipeline = pipeline("text-to-audio", "facebook/musicgen-small", device="cpu")
|
| 33 |
return cls._music_pipeline
|
| 34 |
|
| 35 |
+
# ์ธ์ด๋ณ ๋ฉค๋ฒ/๋ณด์ด์ค ๋งคํ
|
| 36 |
LANG_MEMBER_MAP = {
|
| 37 |
"Korean": {"name": "์์ค (Korea)", "voice": "ko-KR-SunHiNeural"},
|
| 38 |
"English": {"name": "Chloe (USA)", "voice": "en-US-AriaNeural"},
|
|
|
|
| 49 |
voice_path = f"/tmp/v_{req_id}.mp3"
|
| 50 |
music_path = f"/tmp/m_{req_id}.wav"
|
| 51 |
|
| 52 |
+
m_info = LANG_MEMBER_MAP.get(selected_lang, LANG_MEMBER_MAP["Korean"])
|
| 53 |
|
| 54 |
+
# [TRANSLATION] ์น์
์ ์ถ๊ฐํ๋๋ก ํ๋กฌํํธ ์์
|
| 55 |
+
system_prompt = f"""You are {m_info['name']}. Respond ONLY in {selected_lang}.
|
| 56 |
+
Provide 5-7 sentences of music advice.
|
| 57 |
+
At the end, add [TRANSLATION] followed by the English translation of your advice.
|
| 58 |
[TAB] Section: Detailed chords/tabs.
|
| 59 |
+
[MUSIC] Section: English prompt for MusicGen: Guitar:{g_inst}, Bass:{b_inst}, Drums:{d_inst}, Chords:{chords}"""
|
| 60 |
|
| 61 |
ai_text_raw = ""
|
| 62 |
groq_client = ModelManager.get_groq()
|
|
|
|
| 74 |
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)
|
| 75 |
ai_text_raw = out[0]['generated_text'].split("assistant\n")[-1]
|
| 76 |
|
| 77 |
+
# ๋ฐ์ดํฐ ํ์ฑ (๋ณธ๋ฌธ, ๋ฒ์ญ, ์
๋ณด, ์์
)
|
| 78 |
tab_match = re.search(r'\[TAB\](.*?)(\[|$)', ai_text_raw, re.DOTALL | re.IGNORECASE)
|
| 79 |
music_match = re.search(r'\[MUSIC:(.*?)\]', ai_text_raw, re.IGNORECASE)
|
| 80 |
+
trans_match = re.search(r'\[TRANSLATION\](.*?)(\[|$)', ai_text_raw, re.DOTALL | re.IGNORECASE)
|
| 81 |
+
|
| 82 |
+
tab_display = tab_match.group(1).strip() if tab_match else "No Data"
|
| 83 |
+
translation = trans_match.group(1).strip() if trans_match else ""
|
| 84 |
+
|
| 85 |
clean_text = re.sub(r'\[TAB\].*?(\[|$)', '', ai_text_raw, flags=re.DOTALL | re.IGNORECASE)
|
| 86 |
+
clean_text = re.sub(r'\[MUSIC:.*?\]', '', clean_text, flags=re.IGNORECASE)
|
| 87 |
+
clean_text = re.sub(r'\[TRANSLATION\].*?(\[|$)', '', clean_text, flags=re.DOTALL | re.IGNORECASE).strip()
|
| 88 |
|
| 89 |
+
# TTS ์์ฑ (๋ค์ดํฐ๋ธ ์ธ์ด๋ก)
|
| 90 |
+
communicate = edge_tts.Communicate(re.sub(r'[\*\#\-\_\~\|]', '', clean_text), m_info["voice"])
|
| 91 |
await communicate.save(voice_path)
|
| 92 |
|
| 93 |
+
# MusicGen (12์ด)
|
| 94 |
music_gen = ModelManager.get_music()
|
| 95 |
music_p = music_match.group(1).strip() if music_match else "rock"
|
| 96 |
music_output = music_gen(music_p, forward_params={"max_new_tokens": 512})
|
|
|
|
| 98 |
audio_int16 = (audio_data * 32767).astype(np.int16)
|
| 99 |
scipy.io.wavfile.write(music_path, music_output["sampling_rate"], audio_int16)
|
| 100 |
|
| 101 |
+
# ๋ฆฌํด ์ ๋ฒ์ญ ํฌํจ (clean_text, voice_path, music_path, tab_display, translation)
|
| 102 |
+
return clean_text, voice_path, music_path, tab_display, translation
|
| 103 |
|
| 104 |
with gr.Blocks() as demo:
|
| 105 |
inputs = [gr.Textbox(visible=False) for _ in range(6)]
|
| 106 |
+
outputs = [gr.Textbox(visible=False), gr.Audio(visible=False), gr.Audio(visible=False), gr.Textbox(visible=False), gr.Textbox(visible=False)]
|
| 107 |
btn = gr.Button("API", visible=False)
|
| 108 |
btn.click(band_consulting, inputs, outputs, api_name="predict")
|
| 109 |
|