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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -20
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
- # ํ”„๋กฌํ”„ํŠธ ๊ฐ•ํ™”: 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()
@@ -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 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)
 
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)