benjamin5607 commited on
Commit
3827fce
ยท
verified ยท
1 Parent(s): b1d0675

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -33
app.py CHANGED
@@ -1,18 +1,10 @@
1
- import os
2
- import re
3
- import uuid
4
- import torch
5
  import numpy as np
6
- import scipy.io.wavfile
7
  import gradio as gr
8
- import edge_tts
9
- import asyncio
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:
17
  _llm_pipeline = None
18
  _music_pipeline = None
@@ -29,13 +21,15 @@ class ModelManager:
29
 
30
  @classmethod
31
  def get_groq(cls):
32
- if cls._groq_client is None and GROQ_API_KEY:
33
- cls._groq_client = Groq(api_key=GROQ_API_KEY)
 
34
  return cls._groq_client
35
 
36
  @classmethod
37
  def get_music(cls):
38
  if cls._music_pipeline is None:
 
39
  cls._music_pipeline = pipeline("text-to-audio", "facebook/musicgen-small", device="cpu")
40
  return cls._music_pipeline
41
 
@@ -51,12 +45,12 @@ async def band_consulting(user_input, member_name, lang_code, g_inst, b_inst, d_
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()
@@ -71,24 +65,25 @@ async def band_consulting(user_input, member_name, lang_code, g_inst, b_inst, d_
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)
@@ -96,15 +91,11 @@ async def band_consulting(user_input, member_name, lang_code, g_inst, b_inst, d_
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()
 
1
+ import os, re, uuid, torch, scipy.io.wavfile, edge_tts, asyncio
 
 
 
2
  import numpy as np
 
3
  import gradio as gr
 
 
4
  from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
5
  from groq import Groq
6
 
7
+ # ์‹ฑ๊ธ€ํ†ค ๋ชจ๋ธ ๊ด€๋ฆฌ์ž: CPU ๋ฉ”๋ชจ๋ฆฌ ๋ถ€์กฑ์œผ๋กœ ์ธํ•œ ํฌ๋ž˜์‹œ ๋ฐฉ์ง€
 
 
8
  class ModelManager:
9
  _llm_pipeline = None
10
  _music_pipeline = None
 
21
 
22
  @classmethod
23
  def get_groq(cls):
24
+ key = os.getenv("GROQ_API_KEY")
25
+ if cls._groq_client is None and key:
26
+ cls._groq_client = Groq(api_key=key)
27
  return cls._groq_client
28
 
29
  @classmethod
30
  def get_music(cls):
31
  if cls._music_pipeline is None:
32
+ # CPU ํ™˜๊ฒฝ์—์„œ ๊ฐ€์žฅ ์•ˆ์ •์ ์ธ small ๋ชจ๋ธ ์‚ฌ์šฉ
33
  cls._music_pipeline = pipeline("text-to-audio", "facebook/musicgen-small", device="cpu")
34
  return cls._music_pipeline
35
 
 
45
  voice_path = f"/tmp/v_{req_id}.mp3"
46
  music_path = f"/tmp/m_{req_id}.wav"
47
 
48
+ # JAM ์ง€์‹œ์‚ฌํ•ญ๊ณผ ์ƒ๋‹ด ์–ธ์–ด ๋ฐ˜์˜
49
+ jam_context = f"Guitar: {g_inst}, Bass: {b_inst}, Drums: {d_inst}, Chords: {chords}"
50
+ system_prompt = f"""๋‹น์‹ ์€ ๋ฝ์Šคํƒ€ {member_name}์ž…๋‹ˆ๋‹ค. ๋ฐ˜๋“œ์‹œ {lang_code} ์–ธ์–ด๋กœ๋งŒ ๋‹ต๋ณ€ํ•˜์„ธ์š”.
51
+ ์ „๋ฌธ ๋ฎค์ง€์…˜์œผ๋กœ์„œ 5~7๋ฌธ์žฅ์˜ ๊นŠ์ด ์žˆ๊ณ  ์„ฑ์ˆ™ํ•œ ์ƒ๋‹ด์„ ์ œ๊ณตํ•˜์„ธ์š”.
52
+ [TAB] ์„น์…˜์—๋Š” ์ฝ”๋“œ ์ง„ํ–‰์ด๋‚˜ ํƒ€๋ธ”๋ผ์•…๋ณด๋ฅผ ์ƒ์„ธํžˆ ์ ์œผ์„ธ์š”.
53
+ [MUSIC] ์„น์…˜์—๋Š” ๋‹ค์Œ JAM ์š”์ฒญ์„ ๋ฐ˜์˜ํ•œ ์˜์–ด ํ”„๋กฌํ”„ํŠธ๋ฅผ ์ž‘์„ฑํ•˜์„ธ์š”: {jam_context}"""
54
 
55
  ai_text_raw = ""
56
  groq_client = ModelManager.get_groq()
 
65
 
66
  if not ai_text_raw:
67
  qwen = ModelManager.get_qwen()
68
+ input_text = f"<|im_start|>system\n{system_prompt}<|im_end|>\n<|im_start|>user\n{user_input}<|im_end|>\nassistant\n"
69
+ out = qwen(input_text, max_new_tokens=1024, do_sample=True)
70
  ai_text_raw = out[0]['generated_text'].split("assistant\n")[-1]
71
 
72
+ # ์ •๊ทœ์‹ ํŒŒ์‹ฑ
73
  tab_match = re.search(r'\[TAB\](.*?)(\[|$)', ai_text_raw, re.DOTALL | re.IGNORECASE)
74
  music_match = re.search(r'\[MUSIC:(.*?)\]', ai_text_raw, re.IGNORECASE)
75
+ tab_display = tab_match.group(1).strip() if tab_match else "No Tab Data"
76
  clean_text = re.sub(r'\[TAB\].*?(\[|$)', '', ai_text_raw, flags=re.DOTALL | re.IGNORECASE)
77
  clean_text = re.sub(r'\[MUSIC:.*?\]', '', clean_text, flags=re.IGNORECASE).strip()
78
 
79
+ # TTS ์ƒ์„ฑ
80
  voice_name = MEMBERS_VOICE.get(member_name, "ko-KR-SunHiNeural")
81
  communicate = edge_tts.Communicate(re.sub(r'[\*\#\-\_\~\|]', '', clean_text), voice_name)
82
  await communicate.save(voice_path)
83
 
84
+ # MusicGen ์ƒ์„ฑ (512ํ† ํฐ = ์•ฝ 12์ดˆ)
85
  music_gen = ModelManager.get_music()
86
+ music_p = music_match.group(1).strip() if music_match else "energetic rock guitar solo"
87
  music_output = music_gen(music_p, forward_params={"max_new_tokens": 512})
88
  audio_data = np.squeeze(music_output["audio"])
89
  audio_int16 = (audio_data * 32767).astype(np.int16)
 
91
 
92
  return clean_text, voice_path, music_path, tab_display
93
 
94
+ with gr.Blocks() as demo:
95
+ # 7๊ฐœ์˜ ์ž…๋ ฅ (์ƒ๋‹ด, ๋ฉค๋ฒ„, ์ƒ๋‹ด์–ธ์–ด, ๊ธฐํƒ€, ๋ฒ ์ด์Šค, ๋“œ๋Ÿผ, ์ฝ”๋“œ)
96
+ in_list = [gr.Textbox(visible=False) for _ in range(7)]
97
+ out_list = [gr.Textbox(visible=False), gr.Audio(visible=False), gr.Audio(visible=False), gr.Textbox(visible=False)]
 
 
 
 
98
  btn = gr.Button("API", visible=False)
99
+ btn.click(band_consulting, in_list, out_list, api_name="predict")
100
 
101
  demo.queue().launch()