Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -32,6 +32,18 @@ class ModelManager:
|
|
| 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"},
|
|
@@ -51,13 +63,21 @@ async def band_consulting(user_input, selected_lang, g_inst, b_inst, d_inst, cho
|
|
| 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 |
-
#
|
| 56 |
-
system_prompt = f"""You are {m_info['name']}. Respond ONLY in {selected_lang}.
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
|
| 62 |
ai_text_raw = ""
|
| 63 |
groq_client = ModelManager.get_groq()
|
|
@@ -81,35 +101,32 @@ async def band_consulting(user_input, selected_lang, g_inst, b_inst, d_inst, cho
|
|
| 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
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
# TTS
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
|
| 92 |
-
#
|
| 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
|
| 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)
|
|
|
|
| 32 |
cls._music_pipeline = pipeline("text-to-audio", "facebook/musicgen-small", device="cpu")
|
| 33 |
return cls._music_pipeline
|
| 34 |
|
| 35 |
+
# ๋ฉค๋ฒ๋ณ ๊ณ ์ ์ฑ๊ฒฉ/๋งํฌ ํ๋ฅด์๋
|
| 36 |
+
PERSONA_MAP = {
|
| 37 |
+
"์์ค (Korea)": "์์ ์ ์ด๋ฉด์๋ ๊ฐ๋ ฌํ K-Rock ๋ฆฌ๋. ์ง์งํ๊ณ ์ฒ ํ์ ์ด๋ฉฐ ๋ฐ๋ปํ๊ฒ ์กฐ์ธํจ.",
|
| 38 |
+
"Chloe (USA)": "์์ ๋ถ๋ฐฉํ ์บ๋ฆฌํฌ๋์ ํํฌ ๋ฝ์คํ. ๊ฑฐ์นจ์๊ณ ์ฟจํ๋ฉฐ ์๋์ ํฑํ ๋งํฌ.",
|
| 39 |
+
"Naomi (Japan)": "์๋ฒฝ์ฃผ์ J-Rock ๊ธฐํ๋ฆฌ์คํธ. ์ฌ์ธํ๊ณ ์์ ๋ฐ๋ฅด๋ฉฐ ์์
์ด๋ก ์ ๋ ์นด๋ก์.",
|
| 40 |
+
"Beatrice (Brazil)": "์ด์ ์ ์ธ ์ผ๋ฐ ๋ฝ์ปค. ๊ธ์ ์ ์ด๊ณ ๋ฆฌ๋ฌ๊ฐ์ด ๋์น๋ฉฐ ์ธ์์ ์ฆ๊ฑฐ์์ ๊ฐ์กฐํจ.",
|
| 41 |
+
"Elena (Spain)": "๋๋ผ๋งํฑํ ๊ฐ์ฑ์ ์์ ์. ํ๋ผ๋ฉฉ์ฝ์ ์ ์ด๊ณผ ๋ฝ์ ํ๊ดด๋ ฅ์ ๋์์ ๊ฐ์ง.",
|
| 42 |
+
"Amira (Egypt)": "์ ๋น๋ก์ด ์ค๋ฆฌ์ํ ๋ฝ์ปค. ๊น์ ์งํ์ ๊ณ ์ ์ ์ธ ๋ฌด๊ฒ๊ฐ์ ๋ด์ ์กฐ์ธํจ.",
|
| 43 |
+
"Liwei (China)": "์ ํต๊ณผ ํ๋์ ์กฐํ๋ฅผ ์ค์ํ๋ ํจ์ ๋ฝ์ปค. ์ ์ ๋๊ณ ํ ์๋ ๋งํฌ.",
|
| 44 |
+
"Sophie (France)": "์์ ์ ์์กด์ฌ์ด ๊ฐํ ์๋ฐฉ๊ฐ๋ฅด๋ ๋ฝ์ปค. ์์ ์ด๊ณ ์ธ๋ จ๋ ํํ์ ์ฆ๊ฒจ ์."
|
| 45 |
+
}
|
| 46 |
+
|
| 47 |
# ์ธ์ด๋ณ ๋ฉค๋ฒ/๋ณด์ด์ค ๋งคํ
|
| 48 |
LANG_MEMBER_MAP = {
|
| 49 |
"Korean": {"name": "์์ค (Korea)", "voice": "ko-KR-SunHiNeural"},
|
|
|
|
| 63 |
music_path = f"/tmp/m_{req_id}.wav"
|
| 64 |
|
| 65 |
m_info = LANG_MEMBER_MAP.get(selected_lang, LANG_MEMBER_MAP["Korean"])
|
| 66 |
+
persona = PERSONA_MAP.get(m_info['name'], "์ด์ ์ ์ธ ๋ฝ์คํ")
|
| 67 |
|
| 68 |
+
# [์ ๊ตํ] ๋๋ผ ๋นํธ + ํ๋ธ ์๋ก ํฌํจ ํ๋กฌํํธ
|
| 69 |
+
system_prompt = f"""You are the rock star '{m_info['name']}'. Respond ONLY in {selected_lang}.
|
| 70 |
+
Persona: {persona}.
|
| 71 |
+
|
| 72 |
+
CRITICAL RULES:
|
| 73 |
+
1. Advice: 5-7 deep sentences in {selected_lang}. Speak according to your persona.
|
| 74 |
+
2. [TAB] SECTION: Must be highly detailed.
|
| 75 |
+
- Include Drum patterns (e.g., H|x-x-x-x| S|--o---o-|).
|
| 76 |
+
- Include Guitar Solo Tab with fret numbers (e.g., e|--12-15-12--|).
|
| 77 |
+
- Provide precise Chord fingerings.
|
| 78 |
+
3. [TRANSLATION]: English translation of your advice.
|
| 79 |
+
4. [MUSIC]: English prompt: Guitar:{g_inst}, Bass:{b_inst}, Drums:{d_inst}, Chords:{chords}
|
| 80 |
+
"""
|
| 81 |
|
| 82 |
ai_text_raw = ""
|
| 83 |
groq_client = ModelManager.get_groq()
|
|
|
|
| 101 |
music_match = re.search(r'\[MUSIC:(.*?)\]', ai_text_raw, re.IGNORECASE)
|
| 102 |
trans_match = re.search(r'\[TRANSLATION\](.*?)(\[|$)', ai_text_raw, re.DOTALL | re.IGNORECASE)
|
| 103 |
|
| 104 |
+
tab_display = tab_match.group(1).strip() if tab_match else "No Detailed Tab Generated"
|
| 105 |
+
eng_translation = trans_match.group(1).strip() if trans_match else ""
|
| 106 |
+
|
| 107 |
+
# [์์ ] TTS ์์ฑ์์ ๋ฒ์ญ๋ฌธ ์๋ฒฝ ์ ์ธ ๋ก์ง
|
| 108 |
+
speech_text = re.sub(r'\[TAB\].*?(\[|$)', '', ai_text_raw, flags=re.DOTALL | re.IGNORECASE)
|
| 109 |
+
speech_text = re.sub(r'\[MUSIC:.*?\]', '', speech_text, flags=re.IGNORECASE)
|
| 110 |
+
speech_text = re.sub(r'\[TRANSLATION\].*?(\[|$)', '', speech_text, flags=re.DOTALL | re.IGNORECASE).strip()
|
| 111 |
|
| 112 |
+
tts_text = re.sub(r'[\*\#\-\_\~\|]', '', speech_text)
|
|
|
|
| 113 |
communicate = edge_tts.Communicate(tts_text, m_info["voice"])
|
| 114 |
await communicate.save(voice_path)
|
| 115 |
|
| 116 |
+
# MusicGen ์ฐ์ฃผ ์์ฑ (12์ด)
|
| 117 |
music_gen = ModelManager.get_music()
|
| 118 |
+
music_p = music_match.group(1).strip() if music_match else "rock music"
|
| 119 |
music_output = music_gen(music_p, forward_params={"max_new_tokens": 512})
|
| 120 |
audio_data = np.squeeze(music_output["audio"])
|
| 121 |
audio_int16 = (audio_data * 32767).astype(np.int16)
|
| 122 |
scipy.io.wavfile.write(music_path, music_output["sampling_rate"], audio_int16)
|
| 123 |
|
| 124 |
+
return speech_text, voice_path, music_path, tab_display, eng_translation
|
| 125 |
|
| 126 |
except Exception as e:
|
|
|
|
| 127 |
return f"Error: {str(e)}", None, None, "No Tab", "Error occurred"
|
| 128 |
|
| 129 |
with gr.Blocks() as demo:
|
|
|
|
| 130 |
inputs = [gr.Textbox(visible=False) for _ in range(6)]
|
| 131 |
outputs = [gr.Textbox(visible=False), gr.Audio(visible=False), gr.Audio(visible=False), gr.Textbox(visible=False), gr.Textbox(visible=False)]
|
| 132 |
btn = gr.Button("API", visible=False)
|