benjamin5607 commited on
Commit
b1d0675
ยท
verified ยท
1 Parent(s): 13bbb40

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -21
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
- system_prompt = f"๋‹น์‹ ์€ ๋ฐด๋“œ ๋ฉค๋ฒ„ {member_name}์ž…๋‹ˆ๋‹ค. {lang_code}๋กœ ๋‹ต๋ณ€ํ•˜์„ธ์š”. 5์ค„ ์ด๋‚ด ์š”์•ฝ, ์ƒ์„ธ ์„ค๋ช…์€ [TAB]์—, ์Œ์•… ํ”„๋กฌํ”„ํŠธ๋Š” [MUSIC: ์˜์–ดํ”„๋กฌํ”„ํŠธ]์— ๋„ฃ์œผ์„ธ์š”."
 
 
 
 
 
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=512)
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
- # 3. TTS ์Œ์„ฑ ์ƒ์„ฑ (๋ณต๊ตฌ)
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'[\*\#\-\_\~\|]', '', tts_text), voice_name)
88
  await communicate.save(voice_path)
89
 
90
- # 4. ์Œ์•… ์ƒ์„ฑ (๊ธธ์ด ์—ฐ์žฅ: 512 ํ† ํฐ = ์•ฝ 12์ดˆ)
91
  music_gen = ModelManager.get_music()
92
- music_p = music_match.group(1).strip() if music_match else "energetic rock guitar solo"
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 tts_text, voice_path, music_path, tab_display
99
 
100
- with gr.Blocks() as demo:
101
- i1 = gr.Textbox(); i2 = gr.Textbox(); i3 = gr.Textbox()
102
- o1 = gr.Textbox(); o2 = gr.Audio(); o3 = gr.Audio(); o4 = gr.Textbox()
103
- btn = gr.Button("GO"); btn.click(band_consulting, [i1, i2, i3], [o1, o2, o3, o4], api_name="predict")
 
 
 
 
 
 
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()