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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -16
app.py CHANGED
@@ -7,12 +7,13 @@ 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",
@@ -26,39 +27,39 @@ MEMBERS = {
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"
@@ -66,11 +67,10 @@ async def band_consulting(user_input, member_name, consult_lang_code):
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()
 
7
  from transformers import pipeline
8
 
9
  GENAI_KEY = os.getenv("GEMINI_KEY")
10
+
11
  try:
12
  music_synthesiser = pipeline("text-to-audio", "facebook/musicgen-small")
13
  except:
14
  music_synthesiser = None
15
 
16
+ # 8์ธ ๋ฉค๋ฒ„ ๋ณด์ด์Šค ๋งค์นญ
17
  MEMBERS = {
18
  "์„œ์œค (Korea)": "ko-KR-SunHiNeural",
19
  "Chloe (USA)": "en-US-AriaNeural",
 
27
 
28
  async def band_consulting(user_input, member_name, consult_lang_code):
29
  try:
 
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. ์Œ์„ฑ ๋‹ต๋ณ€(TTS)์€ 5์ค„ ์ด๋‚ด๋กœ ํ•ต์‹ฌ๋งŒ ์š”์•ฝํ•˜์„ธ์š”.
37
+ 3. ๋ชจ๋“  ์ƒ์„ธ ์„ค๋ช…, ํ™”์„ฑํ•™ ์กฐ์–ธ, ํ…์ŠคํŠธ ์•…๋ณด๋Š” [TAB] ์„น์…˜์— ๋„ฃ์œผ์„ธ์š”.
38
+ 4. ์Œ์•… ์ƒ์„ฑ์šฉ ์˜์–ด ํ”„๋กฌํ”„ํŠธ๋Š” ๋งˆ์ง€๋ง‰์— [MUSIC: ํ”„๋กฌํ”„ํŠธ] ํ˜•์‹์œผ๋กœ ํฌํ•จํ•˜์„ธ์š”.
39
  """
40
 
 
41
  url = f"https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash:generateContent?key={GENAI_KEY}"
42
  payload = {"contents": [{"parts": [{"text": f"{system_instruction}\n์งˆ๋ฌธ: {user_input}"}]}]}
43
  res = requests.post(url, json=payload).json()
44
  ai_text_raw = res['candidates'][0]['content']['parts'][0]['text']
45
 
46
+ # ๋ฐ์ดํ„ฐ ์ •๋ฐ€ ํŒŒ์‹ฑ
47
  tab_match = re.search(r'\[TAB\](.*?)(\[|$)', ai_text_raw, re.DOTALL)
48
  music_match = re.search(r'\[MUSIC:(.*?)\]', ai_text_raw, re.IGNORECASE)
49
+ tab_display = tab_match.group(1).strip() if tab_match else "No Detailed Score"
50
 
51
+ # โ˜… ์•…๋ณด ์ฝ๊ธฐ ๋ฐฉ์ง€: [TAB] ์ดํ›„ ๋‚ด์šฉ์„ ํ†ต์งธ๋กœ ์‚ญ์ œํ•˜์—ฌ ๋ชฉ์†Œ๋ฆฌ์šฉ ํ…์ŠคํŠธ ์ƒ์„ฑ
52
  clean_text = re.sub(r'\[TAB\].*?(\[|$)', '', ai_text_raw, flags=re.DOTALL)
53
  clean_text = re.sub(r'\[MUSIC:.*?\]', '', clean_text).strip()
54
+ tts_lines = clean_text.split('\n')
55
+ tts_final = "\n".join(tts_lines[:5]) # 5์ค„ ์š”์•ฝ
56
 
57
+ # ์Œ์„ฑ ์ƒ์„ฑ (ํŠน์ˆ˜๊ธฐํ˜ธ ์ œ๊ฑฐ)
58
+ tts_input = re.sub(r'[\*\#\-\_\~\|]', '', tts_final)
59
  voice_path = f"/tmp/v_{member_name.split()[0]}.mp3"
60
+ await edge_tts.Communicate(tts_input, MEMBERS.get(member_name, "ko-KR-SunHiNeural")).save(voice_path)
61
 
62
+ # ์Œ์•… ์ƒ์„ฑ (512 tokens = ์•ฝ 10~12์ดˆ)
63
  music_path = None
64
  if music_match and music_synthesiser:
65
  p = music_match.group(1).strip() + ", high quality, studio recording"
 
67
  music_path = f"/tmp/m_{member_name.split()[0]}.wav"
68
  scipy.io.wavfile.write(music_path, music_output["sampling_rate"], music_output["audio"][0].T)
69
 
70
+ return tts_final, voice_path, music_path, tab_display
71
  except Exception as e:
72
  return str(e), None, None, "Error"
73
 
 
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()