benjamin5607 commited on
Commit
26c3944
ยท
verified ยท
1 Parent(s): 5473ad3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -56
app.py CHANGED
@@ -3,95 +3,76 @@ import requests
3
  import edge_tts
4
  import os
5
  import re
6
- import torch
7
  import scipy.io.wavfile
8
- import numpy as np
9
  from transformers import pipeline
10
 
11
  GENAI_KEY = os.getenv("GEMINI_KEY")
12
-
13
  try:
14
- # ์Œ์งˆ ๋ณด์ • ๋ชจ๋ธ ๋กœ๋“œ
15
  music_synthesiser = pipeline("text-to-audio", "facebook/musicgen-small")
16
- except Exception as e:
17
  music_synthesiser = None
18
 
19
- def find_working_model():
20
- url = f"https://generativelanguage.googleapis.com/v1beta/models?key={GENAI_KEY}"
21
- try:
22
- response = requests.get(url)
23
- data = response.json()
24
- candidates = [m['name'].replace("models/", "") for m in data.get('models', []) if 'generateContent' in m.get('supportedGenerationMethods', [])]
25
- preferred = ["gemini-1.5-flash", "gemini-pro"]
26
- for p in preferred:
27
- if p in candidates: return p
28
- return candidates[0] if candidates else "gemini-pro"
29
- except: return "gemini-pro"
30
-
31
- ACTIVE_MODEL = find_working_model()
32
-
33
  MEMBERS = {
34
- "์„œ์œค (Korea)": { "voice": "ko-KR-SunHiNeural", "prompt": "๋‹น์‹ ์€ ๋ณด์ปฌ '์„œ์œค'์ž…๋‹ˆ๋‹ค. ์‹œ๋‹ˆ์ปฌํ•˜์ง€๋งŒ ์Œ์•…์— ์ง„์‹ฌ์ž…๋‹ˆ๋‹ค." },
35
- "Chloe (USA)": { "voice": "en-US-AriaNeural", "prompt": "You are 'Chloe', the lead guitarist. Cool rockstar vibe." },
36
- "Beatrice (Brazil)": { "voice": "pt-BR-FranciscaNeural", "prompt": "You are 'Beatrice', the drummer. Focus on rhythm." },
37
- "Naomi (Japan)": { "voice": "ja-JP-NanamiNeural", "prompt": "You are 'Naomi'. Focus on harmony." },
38
- "Elena (Spain)": { "voice": "es-ES-ElviraNeural", "prompt": "You are 'Elena'. Focus on groove." },
39
- "Amira (Egypt)": { "voice": "ar-EG-SalmaNeural", "prompt": "You are 'Amira'. Focus on atmosphere." },
40
- "Liwei (China)": { "voice": "zh-CN-XiaoxiaoNeural", "prompt": "You are 'Liwei'. Focus on sound design." },
41
- "Sophie (France)": { "voice": "fr-FR-DeniseNeural", "prompt": "You are 'Sophie'. Focus on melody." }
42
  }
43
 
44
- async def band_consulting(user_input, member_name, consult_category):
45
  try:
46
- member = MEMBERS.get(member_name, MEMBERS["์„œ์œค (Korea)"])
47
- # ๋ฆฌ๋”๋‹˜ ์š”์ฒญ: ์˜์–ด ์ƒ๋‹ด ์‹œ ๋ฌด์กฐ๊ฑด ์˜์–ด๋งŒ ์‚ฌ์šฉ
48
- requested_lang = "English" if "English" in consult_category else "Korean"
49
-
50
  system_instruction = f"""
51
- {member['prompt']}
52
- [LANGUAGE RULE] Respond 100% in {requested_lang}. NEVER use other languages.
53
- [RESPONSE STYLE]
54
- 1. Keep your verbal response (main text) VERY CONCISE, under 5 short lines.
55
- 2. Put ALL technical details, scales, and long musical advice inside the [TAB] section.
56
- 3. Start with a charismatic rockstar greeting.
57
-
58
- After advice, you MUST include:
59
- 1. [TAB]: Detailed musical explanation + Chord/Tab progression.
60
- 2. [MUSIC]: English music prompt (high quality, studio recording).
61
  """
62
 
63
- url = f"https://generativelanguage.googleapis.com/v1beta/models/{ACTIVE_MODEL}:generateContent?key={GENAI_KEY}"
 
64
  payload = {"contents": [{"parts": [{"text": f"{system_instruction}\n์งˆ๋ฌธ: {user_input}"}]}]}
65
- res = requests.post(url, json=payload, headers={'Content-Type': 'application/json'})
66
- ai_text_raw = res.json()['candidates'][0]['content']['parts'][0]['text']
67
 
 
68
  tab_match = re.search(r'\[TAB\](.*?)(\[|$)', ai_text_raw, re.DOTALL)
69
  music_match = re.search(r'\[MUSIC:(.*?)\]', ai_text_raw, re.IGNORECASE)
70
- tab_display = tab_match.group(1).strip() if tab_match else "No Score Available"
71
 
 
72
  clean_text = re.sub(r'\[TAB\].*?(\[|$)', '', ai_text_raw, flags=re.DOTALL)
73
  clean_text = re.sub(r'\[MUSIC:.*?\]', '', clean_text).strip()
74
- tts_text = re.sub(r'[\*\#\-\_\~]', '', clean_text)
75
 
76
- voice_path = f"/tmp/v_{member_name.replace(' ', '_')}.mp3"
77
- await edge_tts.Communicate(tts_text, member['voice']).save(voice_path)
 
78
 
 
79
  music_path = None
80
  if music_match and music_synthesiser:
81
  p = music_match.group(1).strip() + ", high quality, studio recording"
82
- # ๋ฆฌ๋”๋‹˜ ์š”์ฒญ: ๊ธธ์ด ์•ฝ 10~12์ดˆ(512 tokens)
83
- music_output = music_synthesiser(p, forward_params={"max_new_tokens": 512, "guidance_scale": 4.5, "do_sample": True})
84
- music_path = f"/tmp/m_{member_name.replace(' ', '_')}.wav"
85
  scipy.io.wavfile.write(music_path, music_output["sampling_rate"], music_output["audio"][0].T)
86
 
87
  return clean_text, voice_path, music_path, tab_display
88
  except Exception as e:
89
- return f"Error: {str(e)}", None, None, "Error"
90
 
 
91
  with gr.Blocks() as demo:
92
- with gr.Row():
93
- i1 = gr.Textbox(); i2 = gr.Textbox(); i3 = gr.Textbox()
94
  o1 = gr.Textbox(); o2 = gr.Audio(); o3 = gr.Audio(); o4 = gr.Textbox()
95
  btn = gr.Button("GO"); btn.click(band_consulting, [i1, i2, i3], [o1, o2, o3, o4], api_name="predict")
96
-
97
  demo.launch()
 
3
  import edge_tts
4
  import os
5
  import re
 
6
  import scipy.io.wavfile
 
7
  from transformers import pipeline
8
 
9
  GENAI_KEY = os.getenv("GEMINI_KEY")
 
10
  try:
 
11
  music_synthesiser = pipeline("text-to-audio", "facebook/musicgen-small")
12
+ except:
13
  music_synthesiser = None
14
 
15
+ # ๋ฉค๋ฒ„ ์„ค์ • (๋ชฉ์†Œ๋ฆฌ ๋งค์นญ์šฉ)
 
 
 
 
 
 
 
 
 
 
 
 
 
16
  MEMBERS = {
17
+ "์„œ์œค (Korea)": "ko-KR-SunHiNeural",
18
+ "Chloe (USA)": "en-US-AriaNeural",
19
+ "Beatrice (Brazil)": "pt-BR-FranciscaNeural",
20
+ "Naomi (Japan)": "ja-JP-NanamiNeural",
21
+ "Elena (Spain)": "es-ES-ElviraNeural",
22
+ "Amira (Egypt)": "ar-EG-SalmaNeural",
23
+ "Liwei (China)": "zh-CN-XiaoxiaoNeural",
24
+ "Sophie (France)": "fr-FR-DeniseNeural"
25
  }
26
 
27
+ async def band_consulting(user_input, member_name, consult_lang_code):
28
  try:
29
+ # ์„ ํƒ๋œ ์–ธ์–ด ์ฝ”๋“œ(ko, en, ja ๋“ฑ)์— ๋”ฐ๋ผ ๋‹ต๋ณ€ ์–ธ์–ด ๊ฐ•์ œ
30
+ lang_map = {"ko": "Korean", "en": "English", "ja": "Japanese", "pt": "Portuguese", "es": "Spanish", "ar": "Arabic", "zh": "Chinese", "fr": "French"}
31
+ target_lang = lang_map.get(consult_lang_code, "Korean")
32
+
33
  system_instruction = f"""
34
+ ๋‹น์‹ ์€ ๋ฐด๋“œ ๋ฉค๋ฒ„ '{member_name}'์ž…๋‹ˆ๋‹ค.
35
+ [๊ทœ์น™ 1] ๋ฌด์กฐ๊ฑด '{target_lang}'๋กœ๋งŒ ๋‹ต๋ณ€ํ•˜์„ธ์š”.
36
+ [๊ทœ์น™ 2] ์Œ์„ฑ ๋‹ต๋ณ€์šฉ ๋ณธ๋ฌธ์€ 5์ค„ ์ด๋‚ด๋กœ ์งง๊ฒŒ ์š”์•ฝํ•˜์„ธ์š”.
37
+ [๊ทœ์น™ 3] ๋ชจ๋“  ์ƒ์„ธ ์„ค๋ช…๊ณผ ์•…๋ณด๋Š” [TAB] ์„น์…˜์— ๋„ฃ์œผ์„ธ์š”.
38
+ [๊ทœ์น™ 4] ์Œ์•… ์ƒ์„ฑ์šฉ ์˜์–ด ํ”„๋กฌํ”„ํŠธ๋Š” [MUSIC: ํ”„๋กฌํ”„ํŠธ] ํ˜•์‹์œผ๋กœ ๋์— ํฌํ•จํ•˜์„ธ์š”.
 
 
 
 
 
39
  """
40
 
41
+ # Gemini API ํ˜ธ์ถœ (๋ชจ๋ธ๋ช…์€ ํ™˜๊ฒฝ์— ๋งž๊ฒŒ ์ž๋™ ์กฐ์ ˆ๋œ๋‹ค๊ณ  ๊ฐ€์ •)
42
+ url = f"https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash:generateContent?key={GENAI_KEY}"
43
  payload = {"contents": [{"parts": [{"text": f"{system_instruction}\n์งˆ๋ฌธ: {user_input}"}]}]}
44
+ res = requests.post(url, json=payload).json()
45
+ ai_text_raw = res['candidates'][0]['content']['parts'][0]['text']
46
 
47
+ # ๋ฐ์ดํ„ฐ ํŒŒ์‹ฑ
48
  tab_match = re.search(r'\[TAB\](.*?)(\[|$)', ai_text_raw, re.DOTALL)
49
  music_match = re.search(r'\[MUSIC:(.*?)\]', ai_text_raw, re.IGNORECASE)
50
+ tab_display = tab_match.group(1).strip() if tab_match else "์ƒ์„ธ ์•…๋ณด ์—†์Œ"
51
 
52
+ # TTS์šฉ ํด๋ฆฐ ํ…์ŠคํŠธ (์•…๋ณด/ํ”„๋กฌํ”„ํŠธ ์ œ๊ฑฐ + 5์ค„ ์ปท)
53
  clean_text = re.sub(r'\[TAB\].*?(\[|$)', '', ai_text_raw, flags=re.DOTALL)
54
  clean_text = re.sub(r'\[MUSIC:.*?\]', '', clean_text).strip()
55
+ clean_text = "\n".join(clean_text.split("\n")[:5])
56
 
57
+ # ๋ชฉ์†Œ๋ฆฌ ์ƒ์„ฑ
58
+ voice_path = f"/tmp/v_{member_name.split()[0]}.mp3"
59
+ await edge_tts.Communicate(clean_text, MEMBERS.get(member_name, "ko-KR-SunHiNeural")).save(voice_path)
60
 
61
+ # ์Œ์•… ์ƒ์„ฑ
62
  music_path = None
63
  if music_match and music_synthesiser:
64
  p = music_match.group(1).strip() + ", high quality, studio recording"
65
+ music_output = music_synthesiser(p, forward_params={"max_new_tokens": 512, "do_sample": True})
66
+ music_path = f"/tmp/m_{member_name.split()[0]}.wav"
 
67
  scipy.io.wavfile.write(music_path, music_output["sampling_rate"], music_output["audio"][0].T)
68
 
69
  return clean_text, voice_path, music_path, tab_display
70
  except Exception as e:
71
+ return str(e), None, None, "Error"
72
 
73
+ # Gradio ์ธํ„ฐํŽ˜์ด์Šค๋Š” API์šฉ์œผ๋กœ ์ตœ์†Œํ™”
74
  with gr.Blocks() as demo:
75
+ i1 = gr.Textbox(); i2 = gr.Textbox(); i3 = gr.Textbox()
 
76
  o1 = gr.Textbox(); o2 = gr.Audio(); o3 = gr.Audio(); o4 = gr.Textbox()
77
  btn = gr.Button("GO"); btn.click(band_consulting, [i1, i2, i3], [o1, o2, o3, o4], api_name="predict")
 
78
  demo.launch()