benjamin5607 commited on
Commit
891bc28
ยท
verified ยท
1 Parent(s): 1c44d4e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +61 -30
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
- # ... (์ƒ๋žต: LLM ๋‹ต๋ณ€ ์ƒ์„ฑ ๋กœ์ง ๋™์ผ)
49
-
50
- # 1. ํŒŒ์‹ฑ ๋กœ์ง ์ •๊ตํ™”
51
- tab_match = re.search(r'\[TAB\](.*?)(\[|$)', ai_text_raw, re.DOTALL | re.IGNORECASE)
52
- music_match = re.search(r'\[MUSIC:(.*?)\]', ai_text_raw, re.IGNORECASE)
53
- trans_match = re.search(r'\[TRANSLATION\](.*?)(\[|$)', ai_text_raw, re.DOTALL | re.IGNORECASE)
54
-
55
- tab_display = tab_match.group(1).strip() if tab_match else "No Tab Data"
56
- translation = trans_match.group(1).strip() if trans_match else ""
57
-
58
- # โ˜… ํ•ต์‹ฌ: ๋งด๋ฒ„๊ฐ€ ์ฝ์„ ํ…์ŠคํŠธ(TTS)๋Š” ๋ฒˆ์—ญ๋ฌธ์„ ์ œ์™ธํ•œ clean_text๋งŒ ์‚ฌ์šฉ
59
- clean_text = re.sub(r'\[TAB\].*?(\[|$)', '', ai_text_raw, flags=re.DOTALL | re.IGNORECASE)
60
- clean_text = re.sub(r'\[MUSIC:.*?\]', '', clean_text, flags=re.IGNORECASE)
61
- clean_text = re.sub(r'\[TRANSLATION\].*?(\[|$)', '', clean_text, flags=re.DOTALL | re.IGNORECASE).strip()
62
-
63
- # TTS ์ƒ์„ฑ (๋ฒˆ์—ญ๋ฌธ ์—†์ด ๋„ค์ดํ‹ฐ๋ธŒ ์„ค๋ช…๋งŒ ์ฝ์Œ)
64
- m_info = LANG_MEMBER_MAP.get(selected_lang, LANG_MEMBER_MAP["Korean"])
65
- # ํŠน์ˆ˜๋ฌธ์ž ์ œ๊ฑฐ ํ›„ ์ˆœ์ˆ˜ ํ…์ŠคํŠธ๋งŒ ์ „๋‹ฌ
66
- tts_text = re.sub(r'[\*\#\-\_\~\|]', '', clean_text)
67
- communicate = edge_tts.Communicate(tts_text, m_info["voice"])
68
- await communicate.save(voice_path)
 
 
 
69
 
70
- # MusicGen (12์ดˆ)
71
- music_gen = ModelManager.get_music()
72
- music_p = music_match.group(1).strip() if music_match else "rock"
73
- music_output = music_gen(music_p, forward_params={"max_new_tokens": 512})
74
- audio_data = np.squeeze(music_output["audio"])
75
- audio_int16 = (audio_data * 32767).astype(np.int16)
76
- scipy.io.wavfile.write(music_path, music_output["sampling_rate"], audio_int16)
77
 
78
- # ๋ฆฌํ„ด ์‹œ ๋ฒˆ์—ญ ํฌํ•จ (clean_text, voice_path, music_path, tab_display, translation)
79
- return clean_text, voice_path, music_path, tab_display, translation
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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)