benjamin5607 commited on
Commit
6ee7640
Β·
verified Β·
1 Parent(s): eef5842

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -23
app.py CHANGED
@@ -8,12 +8,13 @@ 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",
@@ -30,49 +31,58 @@ async def band_consulting(user_input, member_name, consult_lang_code):
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"
66
- music_output = music_synthesiser(p, forward_params={"max_new_tokens": 512, "do_sample": True})
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()
77
- btn = gr.Button("GO"); btn.click(band_consulting, [i1, i2, i3], [o1, o2, o3, o4], api_name="predict")
 
78
  demo.launch()
 
8
 
9
  GENAI_KEY = os.getenv("GEMINI_KEY")
10
 
11
+ # μŒμ•… 생성 λͺ¨λΈ λ‘œλ“œ
12
  try:
13
  music_synthesiser = pipeline("text-to-audio", "facebook/musicgen-small")
14
+ except Exception as e:
15
+ print(f"Music Model Load Error: {e}")
16
  music_synthesiser = None
17
 
 
18
  MEMBERS = {
19
  "μ„œμœ€ (Korea)": "ko-KR-SunHiNeural",
20
  "Chloe (USA)": "en-US-AriaNeural",
 
31
  lang_map = {"ko": "Korean", "en": "English", "ja": "Japanese", "pt": "Portuguese", "es": "Spanish", "ar": "Arabic", "zh": "Chinese", "fr": "French"}
32
  target_lang = lang_map.get(consult_lang_code, "Korean")
33
 
34
+ # β˜… λ¦¬λ”λ‹˜ μš”μ²­: μ„€λͺ… μΆ©λΆ„νžˆ, 악보 λ°˜λ“œμ‹œ 포함
35
  system_instruction = f"""
36
+ 당신은 λ½λ°΄λ“œ 멀버 '{member_name}'μž…λ‹ˆλ‹€.
37
+ 1. λ°˜λ“œμ‹œ '{target_lang}'둜 λ‹΅λ³€ν•˜μ„Έμš”.
38
+ 2. 고민에 λŒ€ν•΄ μ•„μ£Ό μƒμ„Έν•˜κ³  μΉœμ ˆν•˜κ²Œ μ„€λͺ…ν•˜μ„Έμš”. (λ¬Έμž₯ 수 μ œν•œ μ—†μŒ)
39
+ 3. λ°˜λ“œμ‹œ [TAB] μ„Ήμ…˜μ„ λ§Œλ“€μ–΄ μƒμ„Έν•œ ν…μŠ€νŠΈ μ•…λ³΄λ‚˜ 연주법을 ν¬ν•¨ν•˜μ„Έμš”.
40
+ 4. μŒμ•… μƒμ„±μš© ν”„λ‘¬ν”„νŠΈλŠ” λ§ˆμ§€λ§‰μ— [MUSIC: μ˜μ–΄ν”„λ‘¬ν”„νŠΈ] ν˜•μ‹μœΌλ‘œ λ„£μœΌμ„Έμš”.
41
  """
42
 
43
  url = f"https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash:generateContent?key={GENAI_KEY}"
44
  payload = {"contents": [{"parts": [{"text": f"{system_instruction}\n질문: {user_input}"}]}]}
 
 
45
 
46
+ response = requests.post(url, json=payload)
47
+ res_data = response.json()
48
+
49
+ # 'candidates' μ—λŸ¬ λ°©μ–΄
50
+ if 'candidates' not in res_data:
51
+ return "API 응닡 였λ₯˜κ°€ λ°œμƒν–ˆμŠ΅λ‹ˆλ‹€. ν‚€λ₯Ό ν™•μΈν•΄μ£Όμ„Έμš”.", None, None, "Score Error"
52
+
53
+ ai_text_raw = res_data['candidates'][0]['content']['parts'][0]['text']
54
+
55
+ # 데이터 νŒŒμ‹±
56
  tab_match = re.search(r'\[TAB\](.*?)(\[|$)', ai_text_raw, re.DOTALL)
57
  music_match = re.search(r'\[MUSIC:(.*?)\]', ai_text_raw, re.IGNORECASE)
 
58
 
59
+ tab_display = tab_match.group(1).strip() if tab_match else "상세 악보λ₯Ό μƒμ„±ν•˜μ§€ λͺ»ν–ˆμŠ΅λ‹ˆλ‹€."
60
+
61
+ # TTS용 ν…μŠ€νŠΈ (악보 기호 μ œμ™Έν•˜κ³  순수 μ„€λͺ…λ§Œ)
62
  clean_text = re.sub(r'\[TAB\].*?(\[|$)', '', ai_text_raw, flags=re.DOTALL)
63
  clean_text = re.sub(r'\[MUSIC:.*?\]', '', clean_text).strip()
 
 
64
 
65
+ # μŒμ„± 생성 (μ„€λͺ…이 κΈΈ 경우 처음 λͺ‡ λ¬Έμž₯만 읽도둝 쑰절 κ°€λŠ₯)
 
66
  voice_path = f"/tmp/v_{member_name.split()[0]}.mp3"
67
+ tts_text = " ".join(clean_text.split('.')[:3]) # λ„ˆλ¬΄ κΈΈλ©΄ 였λ₯˜λ‚˜λ―€λ‘œ μ•ž 3λ¬Έμž₯만 TTS
68
+ await edge_tts.Communicate(tts_text, MEMBERS.get(member_name, "ko-KR-SunHiNeural")).save(voice_path)
69
 
70
+ # μŒμ•… 생성
71
  music_path = None
72
  if music_match and music_synthesiser:
73
+ p = music_match.group(1).strip()
74
+ music_output = music_synthesiser(p, forward_params={"max_new_tokens": 512})
75
  music_path = f"/tmp/m_{member_name.split()[0]}.wav"
76
  scipy.io.wavfile.write(music_path, music_output["sampling_rate"], music_output["audio"][0].T)
77
 
78
+ return clean_text, voice_path, music_path, tab_display
79
+
80
  except Exception as e:
81
+ return f"였λ₯˜ λ°œμƒ: {str(e)}", None, None, "Error"
82
 
83
  with gr.Blocks() as demo:
84
  i1 = gr.Textbox(); i2 = gr.Textbox(); i3 = gr.Textbox()
85
  o1 = gr.Textbox(); o2 = gr.Audio(); o3 = gr.Audio(); o4 = gr.Textbox()
86
+ btn = gr.Button("GO"); btn.click(band_consulting, [i1, i2, i3], [o1, o2, o3, o4])
87
+
88
  demo.launch()