Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -10,8 +10,7 @@ import asyncio
|
|
| 10 |
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
|
| 11 |
from groq import Groq
|
| 12 |
|
| 13 |
-
# API ํค
|
| 14 |
-
GENAI_KEY = os.getenv("GEMINI_KEY") # ๊ธฐ์กด ํค ์ ์ง ์
|
| 15 |
GROQ_API_KEY = os.getenv("GROQ_API_KEY")
|
| 16 |
|
| 17 |
class ModelManager:
|
|
@@ -22,7 +21,6 @@ class ModelManager:
|
|
| 22 |
@classmethod
|
| 23 |
def get_qwen(cls):
|
| 24 |
if cls._llm_pipeline is None:
|
| 25 |
-
# CPU ์ต์ ํ ๋ฒ์ Qwen 0.5B
|
| 26 |
model_id = "Qwen/Qwen2.5-0.5B-Instruct"
|
| 27 |
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
| 28 |
model = AutoModelForCausalLM.from_pretrained(model_id, device_map="cpu", torch_dtype=torch.float32)
|
|
@@ -41,7 +39,6 @@ class ModelManager:
|
|
| 41 |
cls._music_pipeline = pipeline("text-to-audio", "facebook/musicgen-small", device="cpu")
|
| 42 |
return cls._music_pipeline
|
| 43 |
|
| 44 |
-
# 8์ธ ๋ฉค๋ฒ ๋ณด์ด์ค ๋งคํ
|
| 45 |
MEMBERS_VOICE = {
|
| 46 |
"์์ค (Korea)": "ko-KR-SunHiNeural", "Chloe (USA)": "en-US-AriaNeural",
|
| 47 |
"Naomi (Japan)": "ja-JP-NanamiNeural", "Beatrice (Brazil)": "pt-BR-FranciscaNeural",
|
|
@@ -49,15 +46,19 @@ MEMBERS_VOICE = {
|
|
| 49 |
"Liwei (China)": "zh-CN-XiaoxiaoNeural", "Sophie (France)": "fr-FR-DeniseNeural"
|
| 50 |
}
|
| 51 |
|
| 52 |
-
async def band_consulting(user_input, member_name, lang_code):
|
| 53 |
req_id = str(uuid.uuid4())[:8]
|
| 54 |
voice_path = f"/tmp/v_{req_id}.mp3"
|
| 55 |
music_path = f"/tmp/m_{req_id}.wav"
|
| 56 |
|
| 57 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
|
| 59 |
ai_text_raw = ""
|
| 60 |
-
# 1. Groq ์๋
|
| 61 |
groq_client = ModelManager.get_groq()
|
| 62 |
if groq_client:
|
| 63 |
try:
|
|
@@ -68,38 +69,42 @@ async def band_consulting(user_input, member_name, lang_code):
|
|
| 68 |
ai_text_raw = res.choices[0].message.content
|
| 69 |
except: pass
|
| 70 |
|
| 71 |
-
# 2. ๋ก์ปฌ Qwen Fallback
|
| 72 |
if not ai_text_raw:
|
| 73 |
qwen = ModelManager.get_qwen()
|
| 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=
|
| 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 |
-
tab_display = tab_match.group(1).strip() if tab_match else "
|
| 81 |
clean_text = re.sub(r'\[TAB\].*?(\[|$)', '', ai_text_raw, flags=re.DOTALL | re.IGNORECASE)
|
| 82 |
clean_text = re.sub(r'\[MUSIC:.*?\]', '', clean_text, flags=re.IGNORECASE).strip()
|
| 83 |
|
| 84 |
-
#
|
| 85 |
-
tts_text = "\n".join(clean_text.split('\n')[:5])
|
| 86 |
voice_name = MEMBERS_VOICE.get(member_name, "ko-KR-SunHiNeural")
|
| 87 |
-
communicate = edge_tts.Communicate(re.sub(r'[\*\#\-\_\~\|]', '',
|
| 88 |
await communicate.save(voice_path)
|
| 89 |
|
| 90 |
-
#
|
| 91 |
music_gen = ModelManager.get_music()
|
| 92 |
-
music_p = music_match.group(1).strip() if music_match else "
|
| 93 |
music_output = music_gen(music_p, forward_params={"max_new_tokens": 512})
|
| 94 |
audio_data = np.squeeze(music_output["audio"])
|
| 95 |
audio_int16 = (audio_data * 32767).astype(np.int16)
|
| 96 |
scipy.io.wavfile.write(music_path, music_output["sampling_rate"], audio_int16)
|
| 97 |
|
| 98 |
-
return
|
| 99 |
|
| 100 |
-
with gr.Blocks() as demo:
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 104 |
|
| 105 |
demo.queue().launch()
|
|
|
|
| 10 |
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
|
| 11 |
from groq import Groq
|
| 12 |
|
| 13 |
+
# API ํค ์ค์
|
|
|
|
| 14 |
GROQ_API_KEY = os.getenv("GROQ_API_KEY")
|
| 15 |
|
| 16 |
class ModelManager:
|
|
|
|
| 21 |
@classmethod
|
| 22 |
def get_qwen(cls):
|
| 23 |
if cls._llm_pipeline is None:
|
|
|
|
| 24 |
model_id = "Qwen/Qwen2.5-0.5B-Instruct"
|
| 25 |
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
| 26 |
model = AutoModelForCausalLM.from_pretrained(model_id, device_map="cpu", torch_dtype=torch.float32)
|
|
|
|
| 39 |
cls._music_pipeline = pipeline("text-to-audio", "facebook/musicgen-small", device="cpu")
|
| 40 |
return cls._music_pipeline
|
| 41 |
|
|
|
|
| 42 |
MEMBERS_VOICE = {
|
| 43 |
"์์ค (Korea)": "ko-KR-SunHiNeural", "Chloe (USA)": "en-US-AriaNeural",
|
| 44 |
"Naomi (Japan)": "ja-JP-NanamiNeural", "Beatrice (Brazil)": "pt-BR-FranciscaNeural",
|
|
|
|
| 46 |
"Liwei (China)": "zh-CN-XiaoxiaoNeural", "Sophie (France)": "fr-FR-DeniseNeural"
|
| 47 |
}
|
| 48 |
|
| 49 |
+
async def band_consulting(user_input, member_name, lang_code, g_inst, b_inst, d_inst, chords):
|
| 50 |
req_id = str(uuid.uuid4())[:8]
|
| 51 |
voice_path = f"/tmp/v_{req_id}.mp3"
|
| 52 |
music_path = f"/tmp/m_{req_id}.wav"
|
| 53 |
|
| 54 |
+
# 1. JAM ์์ฒญ์ฌํญ ํตํฉ ํ๋กฌํํธ (์ค๋ช
๋ณด๊ฐ ์ง์ ์ถ๊ฐ)
|
| 55 |
+
jam_info = f"๊ธฐํ ์คํ์ผ: {g_inst}, ๋ฒ ์ด์ค: {b_inst}, ๋๋ผ: {d_inst}, ์ฝ๋์งํ: {chords}"
|
| 56 |
+
system_prompt = f"""๋น์ ์ ๋ฝ๋ฐด๋ ๋ฉค๋ฒ {member_name}์
๋๋ค. {lang_code}๋ก ๋ต๋ณํ์ธ์.
|
| 57 |
+
๋จ์ ์์ฝ์ด ์๋๋ผ ๋ฎค์ง์
์ผ๋ก์ ์ ๋ฌธ์ ์ด๊ณ ๊น์ด ์๋ ์๋ด์ 5~7๋ฌธ์ฅ์ผ๋ก ์์ธํ ๋งํด์ฃผ์ธ์.
|
| 58 |
+
[TAB] ์น์
์๋ ํ๋ธ๋ผ์
๋ณด๋ ์์ธ ์ฝ๋ ์งํ์ ๋ฐ๋์ ํฌํจํ์ธ์.
|
| 59 |
+
[MUSIC] ์น์
์๋ ๋ค์ JAM ์์ฒญ์ ๋ฐ์ํ ์์ด ํ๋กฌํํธ๋ฅผ ์์ฑํ์ธ์: {jam_info}"""
|
| 60 |
|
| 61 |
ai_text_raw = ""
|
|
|
|
| 62 |
groq_client = ModelManager.get_groq()
|
| 63 |
if groq_client:
|
| 64 |
try:
|
|
|
|
| 69 |
ai_text_raw = res.choices[0].message.content
|
| 70 |
except: pass
|
| 71 |
|
|
|
|
| 72 |
if not ai_text_raw:
|
| 73 |
qwen = ModelManager.get_qwen()
|
| 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 |
+
tab_display = tab_match.group(1).strip() if tab_match else "No Score Data"
|
| 81 |
clean_text = re.sub(r'\[TAB\].*?(\[|$)', '', ai_text_raw, flags=re.DOTALL | re.IGNORECASE)
|
| 82 |
clean_text = re.sub(r'\[MUSIC:.*?\]', '', clean_text, flags=re.IGNORECASE).strip()
|
| 83 |
|
| 84 |
+
# 2. TTS ์์ฑ (5~7๋ฌธ์ฅ ๋ฐ์)
|
|
|
|
| 85 |
voice_name = MEMBERS_VOICE.get(member_name, "ko-KR-SunHiNeural")
|
| 86 |
+
communicate = edge_tts.Communicate(re.sub(r'[\*\#\-\_\~\|]', '', clean_text), voice_name)
|
| 87 |
await communicate.save(voice_path)
|
| 88 |
|
| 89 |
+
# 3. ์์
์์ฑ (12์ด ์ฐ์ฃผ)
|
| 90 |
music_gen = ModelManager.get_music()
|
| 91 |
+
music_p = music_match.group(1).strip() if music_match else "modern rock band sound"
|
| 92 |
music_output = music_gen(music_p, forward_params={"max_new_tokens": 512})
|
| 93 |
audio_data = np.squeeze(music_output["audio"])
|
| 94 |
audio_int16 = (audio_data * 32767).astype(np.int16)
|
| 95 |
scipy.io.wavfile.write(music_path, music_output["sampling_rate"], audio_int16)
|
| 96 |
|
| 97 |
+
return clean_text, voice_path, music_path, tab_display
|
| 98 |
|
| 99 |
+
with gr.Blocks(css="#neon-glow { transition: all 0.5s; }") as demo:
|
| 100 |
+
# Hidden Inputs for logic
|
| 101 |
+
i_input = gr.Textbox(visible=False); i_mem = gr.Textbox(visible=False); i_lang = gr.Textbox(visible=False)
|
| 102 |
+
# JAM Inputs
|
| 103 |
+
i_g = gr.Textbox(visible=False); i_b = gr.Textbox(visible=False); i_d = gr.Textbox(visible=False); i_c = gr.Textbox(visible=False)
|
| 104 |
+
|
| 105 |
+
o_text = gr.Textbox(visible=False); o_voice = gr.Audio(visible=False); o_music = gr.Audio(visible=False); o_tab = gr.Textbox(visible=False)
|
| 106 |
+
|
| 107 |
+
btn = gr.Button("API", visible=False)
|
| 108 |
+
btn.click(band_consulting, [i_input, i_mem, i_lang, i_g, i_b, i_d, i_c], [o_text, o_voice, o_music, o_tab], api_name="predict")
|
| 109 |
|
| 110 |
demo.queue().launch()
|