benjamin5607 commited on
Commit
5724da8
Β·
verified Β·
1 Parent(s): 4f60ef0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -81
app.py CHANGED
@@ -8,102 +8,64 @@ import scipy.io.wavfile
8
  import numpy as np
9
  from transformers import pipeline
10
 
11
- # 1. API ν‚€ μ„€μ • (Hugging Face Settings -> Variables에 등둝 ν•„μˆ˜)
12
  GENAI_KEY = os.getenv("GEMINI_KEY")
13
 
14
- # 2. [μž‘κ³‘κ°€] MusicGen λͺ¨λΈ λ‘œλ“œ (음질 보정 μ„€μ •)
15
- print("⏳ μž‘κ³‘κ°€(MusicGen)λ₯Ό μ†Œμ§‘ μ€‘μž…λ‹ˆλ‹€... (1~2λΆ„ μ†Œμš”)")
16
  try:
17
- # facebook/musicgen-small λͺ¨λΈ μ‚¬μš©
18
  music_synthesiser = pipeline("text-to-audio", "facebook/musicgen-small")
19
- print("βœ… μž‘κ³‘κ°€ μ€€λΉ„ μ™„λ£Œ! 잼 μ„Έμ…˜μ„ μ‹œμž‘ν•  수 μžˆμŠ΅λ‹ˆλ‹€.")
20
  except Exception as e:
21
  print(f"⚠️ λͺ¨λΈ λ‘œλ“œ μ‹€νŒ¨: {e}")
22
  music_synthesiser = None
23
 
24
- # 3. 졜적의 Gemini λͺ¨λΈ μžλ™ 탐색
25
  def find_working_model():
26
  url = f"https://generativelanguage.googleapis.com/v1beta/models?key={GENAI_KEY}"
27
  try:
28
  response = requests.get(url)
29
  data = response.json()
30
  candidates = [m['name'].replace("models/", "") for m in data.get('models', []) if 'generateContent' in m.get('supportedGenerationMethods', [])]
31
- preferred = ["gemini-1.5-flash", "gemini-1.5-flash-latest", "gemini-pro"]
32
  for p in preferred:
33
  if p in candidates: return p
34
  return candidates[0] if candidates else "gemini-pro"
35
- except:
36
- return "gemini-pro"
37
 
38
  ACTIVE_MODEL = find_working_model()
39
 
40
- # 4. λ°΄λ“œ 멀버 데이터 (이름 μ˜€νƒ€ μ™„λ²½ μˆ˜μ •: "μ„œμœ€ (Korea)")
41
  MEMBERS = {
42
- "μ„œμœ€ (Korea)": {
43
- "voice": "ko-KR-SunHiNeural",
44
- "prompt": "당신은 λ‘λ°΄λ“œ 보컬 'μ„œμœ€'μž…λ‹ˆλ‹€. ν•œκ΅­μ–΄λ‘œ λŒ€λ‹΅ν•˜μ„Έμš”. 성격: μ‹œλ‹ˆμ»¬, 츀데레, μŒμ•…μ— 진심. 전문적인 μŒμ•… 쑰언을 μ œκ³΅ν•˜μ„Έμš”."
45
- },
46
- "Chloe (USA)": {
47
- "voice": "en-US-AriaNeural",
48
- "prompt": "You are 'Chloe', the lead guitarist. Respond in English. Focus on guitar tone and riffs."
49
- },
50
- "Beatrice (Brazil)": {
51
- "voice": "pt-BR-FranciscaNeural",
52
- "prompt": "You are 'Beatrice', the drummer. Focus on rhythm patterns and energy."
53
- },
54
- "Naomi (Japan)": {
55
- "voice": "ja-JP-NanamiNeural",
56
- "prompt": "You are 'Naomi'. Respond in Japanese. Focus on harmony and chord progressions."
57
- },
58
- "Elena (Spain)": {
59
- "voice": "es-ES-ElviraNeural",
60
- "prompt": "You are 'Elena'. Focus on bass lines and groove."
61
- },
62
- "Amira (Egypt)": {
63
- "voice": "ar-EG-SalmaNeural",
64
- "prompt": "You are 'Amira'. Focus on keyboards and atmosphere."
65
- },
66
- "Liwei (China)": {
67
- "voice": "zh-CN-XiaoxiaoNeural",
68
- "prompt": "You are 'Liwei'. Focus on electronic sound and FX."
69
- },
70
- "Sophie (France)": {
71
- "voice": "fr-FR-DeniseNeural",
72
- "prompt": "You are 'Sophie'. Focus on emotional melodies and violin."
73
- }
74
  }
75
 
76
- # 5. 메인 둜직 ν•¨μˆ˜
77
  async def band_consulting(user_input, member_name, consult_category):
78
  try:
79
  member = MEMBERS.get(member_name, MEMBERS["μ„œμœ€ (Korea)"])
80
 
81
- # β˜… μ–Έμ–΄ κ²°μ • 둜직 보강: μΉ΄ν…Œκ³ λ¦¬ ν…μŠ€νŠΈμ—μ„œ μ–Έμ–΄ 정보λ₯Ό ν™•μ‹€νžˆ μΆ”μΆœ
82
- # 예: "πŸ‡ΊπŸ‡Έ English (Global Random 🎲) / Guitar" -> "English"
83
  requested_lang = "English" if "English" in consult_category else "Korean"
84
 
85
  system_instruction = f"""
86
  {member['prompt']}
 
 
87
 
88
- [CRITICAL RULE]
89
- The user wants to consult in {requested_lang}.
90
- Regardless of your origin, you MUST respond ENTIRELY in {requested_lang}.
91
- Do not use your native language if it's different from {requested_lang}.
92
-
93
- [Current Category: {consult_category}]
94
-
95
- 쑰언을 μ œκ³΅ν•œ λ’€ λ°˜λ“œμ‹œ λ§ˆοΏ½οΏ½λ§‰μ— λ‹€μŒ ν˜•μ‹μ„ ν¬ν•¨ν•˜μ„Έμš”:
96
- 1. [TAB]: μŒμ•…μ˜ κ°„λ‹¨ν•œ μ½”λ“œ μ§„ν–‰μ΄λ‚˜ ν…μŠ€νŠΈ 악보λ₯Ό κ·Έλ¦¬μ„Έμš”.
97
- 2. [MUSIC]: 뢄야와 μ–΄μšΈλ¦¬λŠ” 고음질 μŒμ•… ν”„λ‘¬ν”„νŠΈλ₯Ό μ˜μ–΄λ‘œ μž‘μ„±ν•˜μ„Έμš”.
98
  """
99
 
100
  url = f"https://generativelanguage.googleapis.com/v1beta/models/{ACTIVE_MODEL}:generateContent?key={GENAI_KEY}"
101
  payload = {"contents": [{"parts": [{"text": f"{system_instruction}\n질문: {user_input}"}]}]}
102
  res = requests.post(url, json=payload, headers={'Content-Type': 'application/json'})
103
-
104
- if res.status_code != 200:
105
- return "Gemini API 연결에 μ‹€νŒ¨ν–ˆμŠ΅λ‹ˆλ‹€.", None, None, "No Tab"
106
-
107
  ai_text_raw = res.json()['candidates'][0]['content']['parts'][0]['text']
108
 
109
  tab_match = re.search(r'\[TAB\](.*?)(\[|$)', ai_text_raw, re.DOTALL)
@@ -112,42 +74,26 @@ async def band_consulting(user_input, member_name, consult_category):
112
 
113
  clean_text = re.sub(r'\[TAB\].*?(\[|$)', '', ai_text_raw, flags=re.DOTALL)
114
  clean_text = re.sub(r'\[MUSIC:.*?\]', '', clean_text).strip()
115
- tts_text = re.sub(r'[\*\#\-\_\~]', '', clean_text)
116
 
117
  voice_path = f"/tmp/v_{member_name.replace(' ', '_')}.mp3"
118
  await edge_tts.Communicate(tts_text, member['voice']).save(voice_path)
119
 
120
  music_path = None
121
  if music_match and music_synthesiser:
122
- music_p = music_match.group(1).strip() + ", high quality, studio recording, power rock, intense drums"
123
  music_output = music_synthesiser(music_p, forward_params={"max_new_tokens": 512, "guidance_scale": 4.5, "do_sample": True})
124
  music_path = f"/tmp/m_{member_name.replace(' ', '_')}.wav"
125
  scipy.io.wavfile.write(music_path, music_output["sampling_rate"], music_output["audio"][0].T)
126
 
127
  return clean_text, voice_path, music_path, tab_display
128
-
129
  except Exception as e:
130
- print(f"❌ Error: {e}")
131
- return f"λ°΄λ“œ μ‹œμŠ€ν…œ 였λ₯˜: {str(e)}", None, None, "Error"
132
 
133
- # 8. Gradio μΈν„°νŽ˜μ΄μŠ€ μ •μ˜ (API용)
134
  with gr.Blocks() as demo:
135
  with gr.Row():
136
- inp_text = gr.Textbox()
137
- inp_member = gr.Textbox()
138
- inp_cat = gr.Textbox()
139
-
140
- out_advice = gr.Textbox()
141
- out_voice = gr.Audio()
142
- out_music = gr.Audio()
143
- out_tab = gr.Textbox()
144
-
145
- btn = gr.Button("Predict")
146
- btn.click(
147
- fn=band_consulting,
148
- inputs=[inp_text, inp_member, inp_cat],
149
- outputs=[out_advice, out_voice, out_music, out_tab],
150
- api_name="predict"
151
- )
152
 
153
  demo.launch()
 
8
  import numpy as np
9
  from transformers import pipeline
10
 
11
+ # 1. API ν‚€ μ„€μ •
12
  GENAI_KEY = os.getenv("GEMINI_KEY")
13
 
14
+ # 2. [μž‘κ³‘κ°€] MusicGen λ‘œλ“œ
15
+ print("⏳ μž‘κ³‘κ°€ μ†Œμ§‘ 쀑...")
16
  try:
 
17
  music_synthesiser = pipeline("text-to-audio", "facebook/musicgen-small")
18
+ print("βœ… μž‘κ³‘κ°€ μ€€λΉ„ μ™„λ£Œ!")
19
  except Exception as e:
20
  print(f"⚠️ λͺ¨λΈ λ‘œλ“œ μ‹€νŒ¨: {e}")
21
  music_synthesiser = None
22
 
 
23
  def find_working_model():
24
  url = f"https://generativelanguage.googleapis.com/v1beta/models?key={GENAI_KEY}"
25
  try:
26
  response = requests.get(url)
27
  data = response.json()
28
  candidates = [m['name'].replace("models/", "") for m in data.get('models', []) if 'generateContent' in m.get('supportedGenerationMethods', [])]
29
+ preferred = ["gemini-1.5-flash", "gemini-pro"]
30
  for p in preferred:
31
  if p in candidates: return p
32
  return candidates[0] if candidates else "gemini-pro"
33
+ except: return "gemini-pro"
 
34
 
35
  ACTIVE_MODEL = find_working_model()
36
 
37
+ # 8인 멀버 데이터
38
  MEMBERS = {
39
+ "μ„œμœ€ (Korea)": { "voice": "ko-KR-SunHiNeural", "prompt": "당신은 보컬 'μ„œμœ€'μž…λ‹ˆλ‹€. μ‹œλ‹ˆμ»¬ν•˜κ³  μŒμ•…μ— μ§„μ‹¬μž…λ‹ˆλ‹€." },
40
+ "Chloe (USA)": { "voice": "en-US-AriaNeural", "prompt": "You are 'Chloe', the lead guitarist. Cool rockstar vibe." },
41
+ "Beatrice (Brazil)": { "voice": "pt-BR-FranciscaNeural", "prompt": "You are 'Beatrice', the drummer. Focus on rhythm." },
42
+ "Naomi (Japan)": { "voice": "ja-JP-NanamiNeural", "prompt": "You are 'Naomi'. Focus on harmony." },
43
+ "Elena (Spain)": { "voice": "es-ES-ElviraNeural", "prompt": "You are 'Elena'. Focus on groove." },
44
+ "Amira (Egypt)": { "voice": "ar-EG-SalmaNeural", "prompt": "You are 'Amira'. Focus on atmosphere." },
45
+ "Liwei (China)": { "voice": "zh-CN-XiaoxiaoNeural", "prompt": "You are 'Liwei'. Focus on sound design." },
46
+ "Sophie (France)": { "voice": "fr-FR-DeniseNeural", "prompt": "You are 'Sophie'. Focus on melody." }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
47
  }
48
 
 
49
  async def band_consulting(user_input, member_name, consult_category):
50
  try:
51
  member = MEMBERS.get(member_name, MEMBERS["μ„œμœ€ (Korea)"])
52
 
53
+ # μ–Έμ–΄ κ°•μ œ 둜직
 
54
  requested_lang = "English" if "English" in consult_category else "Korean"
55
 
56
  system_instruction = f"""
57
  {member['prompt']}
58
+ [CRITICAL RULE] Respond ENTIRELY in {requested_lang}.
59
+ Even if you are global, the user selected {requested_lang} as the consultation language.
60
 
61
+ μ‘°μ–Έ ν›„ λ°˜λ“œμ‹œ 포함:
62
+ 1. [TAB]: μ½”λ“œ μ§„ν–‰ λ˜λŠ” ν…μŠ€νŠΈ 악보.
63
+ 2. [MUSIC]: 뢄야와 μ–΄μšΈλ¦¬λŠ” μ˜μ–΄ μŒμ•… ν”„λ‘¬ν”„νŠΈ.
 
 
 
 
 
 
 
64
  """
65
 
66
  url = f"https://generativelanguage.googleapis.com/v1beta/models/{ACTIVE_MODEL}:generateContent?key={GENAI_KEY}"
67
  payload = {"contents": [{"parts": [{"text": f"{system_instruction}\n질문: {user_input}"}]}]}
68
  res = requests.post(url, json=payload, headers={'Content-Type': 'application/json'})
 
 
 
 
69
  ai_text_raw = res.json()['candidates'][0]['content']['parts'][0]['text']
70
 
71
  tab_match = re.search(r'\[TAB\](.*?)(\[|$)', ai_text_raw, re.DOTALL)
 
74
 
75
  clean_text = re.sub(r'\[TAB\].*?(\[|$)', '', ai_text_raw, flags=re.DOTALL)
76
  clean_text = re.sub(r'\[MUSIC:.*?\]', '', clean_text).strip()
77
+ tts_text = re.sub(r'[\*\#\-\_\~]', '', clean_text) # 특수문자 제거
78
 
79
  voice_path = f"/tmp/v_{member_name.replace(' ', '_')}.mp3"
80
  await edge_tts.Communicate(tts_text, member['voice']).save(voice_path)
81
 
82
  music_path = None
83
  if music_match and music_synthesiser:
84
+ music_p = music_match.group(1).strip() + ", high quality, studio recording, master sound"
85
  music_output = music_synthesiser(music_p, forward_params={"max_new_tokens": 512, "guidance_scale": 4.5, "do_sample": True})
86
  music_path = f"/tmp/m_{member_name.replace(' ', '_')}.wav"
87
  scipy.io.wavfile.write(music_path, music_output["sampling_rate"], music_output["audio"][0].T)
88
 
89
  return clean_text, voice_path, music_path, tab_display
 
90
  except Exception as e:
91
+ return f"Error: {str(e)}", None, None, "Error"
 
92
 
 
93
  with gr.Blocks() as demo:
94
  with gr.Row():
95
+ i1 = gr.Textbox(); i2 = gr.Textbox(); i3 = gr.Textbox()
96
+ o1 = gr.Textbox(); o2 = gr.Audio(); o3 = gr.Audio(); o4 = gr.Textbox()
97
+ btn = gr.Button("GO"); btn.click(band_consulting, [i1, i2, i3], [o1, o2, o3, o4], api_name="predict")
 
 
 
 
 
 
 
 
 
 
 
 
 
98
 
99
  demo.launch()