Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -45,40 +45,71 @@ LANG_MEMBER_MAP = {
|
|
| 45 |
}
|
| 46 |
|
| 47 |
async def band_consulting(user_input, selected_lang, g_inst, b_inst, d_inst, chords):
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
|
|
|
|
|
|
|
|
|
| 69 |
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
audio_int16 = (audio_data * 32767).astype(np.int16)
|
| 76 |
-
scipy.io.wavfile.write(music_path, music_output["sampling_rate"], audio_int16)
|
| 77 |
|
| 78 |
-
|
| 79 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
|
| 81 |
with gr.Blocks() as demo:
|
|
|
|
| 82 |
inputs = [gr.Textbox(visible=False) for _ in range(6)]
|
| 83 |
outputs = [gr.Textbox(visible=False), gr.Audio(visible=False), gr.Audio(visible=False), gr.Textbox(visible=False), gr.Textbox(visible=False)]
|
| 84 |
btn = gr.Button("API", visible=False)
|
|
|
|
| 45 |
}
|
| 46 |
|
| 47 |
async def band_consulting(user_input, selected_lang, g_inst, b_inst, d_inst, chords):
|
| 48 |
+
try:
|
| 49 |
+
req_id = str(uuid.uuid4())[:8]
|
| 50 |
+
voice_path = f"/tmp/v_{req_id}.mp3"
|
| 51 |
+
music_path = f"/tmp/m_{req_id}.wav"
|
| 52 |
+
|
| 53 |
+
m_info = LANG_MEMBER_MAP.get(selected_lang, LANG_MEMBER_MAP["Korean"])
|
| 54 |
+
|
| 55 |
+
# ํ๋กฌํํธ ๊ฐํ: 5-7์ค ์ค๋ช
๋ฐ ์์ด ๋ฒ์ญ ์น์
๊ฐ์
|
| 56 |
+
system_prompt = f"""You are {m_info['name']}. Respond ONLY in {selected_lang}.
|
| 57 |
+
Provide 5-7 lines of professional music advice.
|
| 58 |
+
[TAB] Section: Detailed chords/tabs.
|
| 59 |
+
[TRANSLATION] English translation of your advice.
|
| 60 |
+
[MUSIC] English prompt: Guitar:{g_inst}, Bass:{b_inst}, Drums:{d_inst}, Chords:{chords}"""
|
| 61 |
+
|
| 62 |
+
ai_text_raw = ""
|
| 63 |
+
groq_client = ModelManager.get_groq()
|
| 64 |
+
if groq_client:
|
| 65 |
+
try:
|
| 66 |
+
res = groq_client.chat.completions.create(
|
| 67 |
+
messages=[{"role": "system", "content": system_prompt}, {"role": "user", "content": user_input}],
|
| 68 |
+
model="llama-3.3-70b-versatile"
|
| 69 |
+
)
|
| 70 |
+
ai_text_raw = res.choices[0].message.content
|
| 71 |
+
except: pass
|
| 72 |
|
| 73 |
+
if not ai_text_raw:
|
| 74 |
+
qwen = ModelManager.get_qwen()
|
| 75 |
+
input_t = f"<|im_start|>system\n{system_prompt}<|im_end|>\n<|im_start|>user\n{user_input}<|im_end|>\nassistant\n"
|
| 76 |
+
out = qwen(input_t, max_new_tokens=1024)
|
| 77 |
+
ai_text_raw = out[0]['generated_text'].split("assistant\n")[-1]
|
|
|
|
|
|
|
| 78 |
|
| 79 |
+
# ํ์ฑ ๋ก์ง
|
| 80 |
+
tab_match = re.search(r'\[TAB\](.*?)(\[|$)', ai_text_raw, re.DOTALL | re.IGNORECASE)
|
| 81 |
+
music_match = re.search(r'\[MUSIC:(.*?)\]', ai_text_raw, re.IGNORECASE)
|
| 82 |
+
trans_match = re.search(r'\[TRANSLATION\](.*?)(\[|$)', ai_text_raw, re.DOTALL | re.IGNORECASE)
|
| 83 |
+
|
| 84 |
+
tab_display = tab_match.group(1).strip() if tab_match else "No Tab Data"
|
| 85 |
+
translation = trans_match.group(1).strip() if trans_match else ""
|
| 86 |
+
|
| 87 |
+
# TTS์ฉ ํด๋ฆฐ ํ
์คํธ (๋ฒ์ญ๋ฌธ/์์
ํ๋กฌํํธ/์
๋ณด ์ ์ธ)
|
| 88 |
+
clean_text = re.sub(r'\[TAB\].*?(\[|$)', '', ai_text_raw, flags=re.DOTALL | re.IGNORECASE)
|
| 89 |
+
clean_text = re.sub(r'\[MUSIC:.*?\]', '', clean_text, flags=re.IGNORECASE)
|
| 90 |
+
clean_text = re.sub(r'\[TRANSLATION\].*?(\[|$)', '', clean_text, flags=re.DOTALL | re.IGNORECASE).strip()
|
| 91 |
+
|
| 92 |
+
# TTS ์์ฑ
|
| 93 |
+
tts_text = re.sub(r'[\*\#\-\_\~\|]', '', clean_text)
|
| 94 |
+
communicate = edge_tts.Communicate(tts_text, m_info["voice"])
|
| 95 |
+
await communicate.save(voice_path)
|
| 96 |
+
|
| 97 |
+
# MusicGen (12์ด)
|
| 98 |
+
music_gen = ModelManager.get_music()
|
| 99 |
+
music_p = music_match.group(1).strip() if music_match else "rock"
|
| 100 |
+
music_output = music_gen(music_p, forward_params={"max_new_tokens": 512})
|
| 101 |
+
audio_data = np.squeeze(music_output["audio"])
|
| 102 |
+
audio_int16 = (audio_data * 32767).astype(np.int16)
|
| 103 |
+
scipy.io.wavfile.write(music_path, music_output["sampling_rate"], audio_int16)
|
| 104 |
+
|
| 105 |
+
return clean_text, voice_path, music_path, tab_display, translation
|
| 106 |
+
|
| 107 |
+
except Exception as e:
|
| 108 |
+
print(f"Error: {str(e)}")
|
| 109 |
+
return f"Error: {str(e)}", None, None, "No Tab", "Error occurred"
|
| 110 |
|
| 111 |
with gr.Blocks() as demo:
|
| 112 |
+
# ์ธํ 6๊ฐ (script.js์ predict ๋ฐฐ์ด๊ณผ ์ผ์น)
|
| 113 |
inputs = [gr.Textbox(visible=False) for _ in range(6)]
|
| 114 |
outputs = [gr.Textbox(visible=False), gr.Audio(visible=False), gr.Audio(visible=False), gr.Textbox(visible=False), gr.Textbox(visible=False)]
|
| 115 |
btn = gr.Button("API", visible=False)
|