benjamin5607 commited on
Commit
a1bdc6d
Β·
verified Β·
1 Parent(s): 67b756e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -25
app.py CHANGED
@@ -8,16 +8,18 @@ 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
  MEMBERS = {
17
  "μ„œμœ€ (Korea)": "ko-KR-SunHiNeural",
18
  "Chloe (USA)": "en-US-AriaNeural",
19
- "Naomi (Japan)": "ja-JP-NanamiNeural",
20
  "Beatrice (Brazil)": "pt-BR-FranciscaNeural",
 
21
  "Elena (Spain)": "es-ES-ElviraNeural",
22
  "Amira (Egypt)": "ar-EG-SalmaNeural",
23
  "Liwei (China)": "zh-CN-XiaoxiaoNeural",
@@ -26,14 +28,19 @@ MEMBERS = {
26
 
27
  async def band_consulting(user_input, member_name, lang_code):
28
  try:
 
29
  lang_map = {"ko":"Korean","en":"English","ja":"Japanese","pt":"Portuguese","es":"Spanish","ar":"Arabic","zh":"Chinese","fr":"French"}
30
  target_lang = lang_map.get(lang_code, "Korean")
31
 
32
- # ν”„λ‘¬ν”„νŠΈμ— 'κ°„κ²°ν•˜κ²Œ' λŒ€λ‹΅ν•˜λΌλŠ” λͺ…령을 더 λͺ…ν™•νžˆ 전달
33
- system_instruction = f"당신은 {member_name}μž…λ‹ˆλ‹€. {target_lang}둜만 λ‹΅λ³€ν•˜μ„Έμš”. μŒμ„±μ€ 5쀄 μ΄λ‚΄λ‘œ 맀우 짧게 μš”μ•½ν•˜κ³ , μƒμ„Έν•œ μ‘°μ–Έκ³Ό μ•…λ³΄λŠ” λ°˜λ“œμ‹œ [TAB] μ•ˆμ— λ„£μœΌμ„Έμš”. μŒμ•… ν”„λ‘¬ν”„νŠΈλŠ” [MUSIC: μ˜μ–΄ν”„λ‘¬ν”„νŠΈ]둜 μž‘μ„±ν•˜μ„Έμš”."
 
 
 
 
 
34
 
35
  url = f"https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash:generateContent?key={GENAI_KEY}"
36
-
37
  payload = {
38
  "contents": [{"parts": [{"text": f"{system_instruction}\n질문: {user_input}"}]}],
39
  "safetySettings": [
@@ -44,39 +51,51 @@ async def band_consulting(user_input, member_name, lang_code):
44
  ]
45
  }
46
 
47
- res = requests.post(url, json=payload).json()
 
48
 
49
- # β˜… 차단 λ°œμƒ μ‹œ μžλ™μœΌλ‘œ 'μ•ˆμ „ν•œ' μŒμ•… ν”„λ‘¬ν”„νŠΈμ™€ 닡변을 κ°•μ œλ‘œ μƒμ„±ν•΄μ„œ λ„˜κ²¨μ€Œ
50
- if 'candidates' not in res or not res['candidates']:
51
- clean_text = "이봐 리더, 이번 μ œμ•ˆμ€ λ‚΄ 예술적 영감이 필터링될 μ •λ„λ‘œ κ°•λ ¬ν•œκ±Έ? λ½μŠ€νƒ€λ‹΅κ²Œ 쑰금만 μˆœν™”ν•΄μ„œ λ‹€μ‹œ λ¬Όμ–΄λ΄μ€˜!"
52
- tab_display = "상세 쑰언이 ν•„ν„°λ§λ˜μ—ˆμŠ΅λ‹ˆλ‹€. μ§ˆλ¬Έμ„ 쑰금 λ‹€λ₯΄κ²Œ ν•΄λ³΄μ„Έμš”."
53
- music_p = "fast heavy metal drum and bass, energetic rock style" # μ•ˆμ „ν•œ κΈ°λ³Έ ν”„λ‘¬ν”„νŠΈ
54
  else:
55
- ai_text_raw = res['candidates'][0]['content']['parts'][0]['text']
56
- # 데이터 μΆ”μΆœ 둜직 (κΈ°μ‘΄κ³Ό 동일)
57
  tab_match = re.search(r'\[TAB\](.*?)(\[|$)', ai_text_raw, re.DOTALL)
58
  music_match = re.search(r'\[MUSIC:(.*?)\]', ai_text_raw, re.IGNORECASE)
59
  tab_display = tab_match.group(1).strip() if tab_match else "No Score Available"
60
  clean_text = re.sub(r'\[TAB\].*?(\[|$)', '', ai_text_raw, flags=re.DOTALL)
61
  clean_text = re.sub(r'\[MUSIC:.*?\]', '', clean_text).strip()
62
- clean_text = "\n".join(clean_text.split("\n")[:5])
63
- music_p = music_match.group(1).strip().lower() if music_match else "rock guitar riff"
64
 
65
- # μŒμ„± 생성
 
 
 
 
 
66
  voice_path = f"/tmp/v_{member_name.split()[0]}.mp3"
67
- await edge_tts.Communicate(clean_text, MEMBERS.get(member_name, "ko-KR-SunHiNeural")).save(voice_path)
68
 
69
- # μŒμ•… 생성 (차단 μœ„ν—˜ 단어 필터링)
70
- if any(x in music_p for x in ['laugh', 'vocal', 'voice', 'human', 'sing', 'person']):
71
- music_p = "intense synthesizer rock riff, high energy"
72
-
73
- music_output = music_synthesiser(music_p, forward_params={"max_new_tokens": 512})
74
- music_path = f"/tmp/m_{member_name.split()[0]}.wav"
75
- scipy.io.wavfile.write(music_path, music_output["sampling_rate"], music_output["audio"][0].T)
 
 
 
 
 
 
 
 
76
 
77
- return clean_text, voice_path, music_path, tab_display
78
  except Exception as e:
79
- return f"System Overload: {str(e)}", None, None, "Error"
80
 
81
  with gr.Blocks() as demo:
82
  i1 = gr.Textbox(); i2 = gr.Textbox(); i3 = gr.Textbox()
 
8
 
9
  GENAI_KEY = os.getenv("GEMINI_KEY")
10
 
11
+ # μŒμ•… λͺ¨λΈ λ‘œλ“œ
12
  try:
13
  music_synthesiser = pipeline("text-to-audio", "facebook/musicgen-small")
14
  except:
15
  music_synthesiser = None
16
 
17
+ # 8인 멀버 보이슀 데이터 (μ ˆλŒ€ μ‚­μ œ μ•ˆ 함)
18
  MEMBERS = {
19
  "μ„œμœ€ (Korea)": "ko-KR-SunHiNeural",
20
  "Chloe (USA)": "en-US-AriaNeural",
 
21
  "Beatrice (Brazil)": "pt-BR-FranciscaNeural",
22
+ "Naomi (Japan)": "ja-JP-NanamiNeural",
23
  "Elena (Spain)": "es-ES-ElviraNeural",
24
  "Amira (Egypt)": "ar-EG-SalmaNeural",
25
  "Liwei (China)": "zh-CN-XiaoxiaoNeural",
 
28
 
29
  async def band_consulting(user_input, member_name, lang_code):
30
  try:
31
+ # β˜… μ–Έμ–΄ μ„€μ • μ™„λ²½ κ³ μ •
32
  lang_map = {"ko":"Korean","en":"English","ja":"Japanese","pt":"Portuguese","es":"Spanish","ar":"Arabic","zh":"Chinese","fr":"French"}
33
  target_lang = lang_map.get(lang_code, "Korean")
34
 
35
+ system_instruction = f"""
36
+ 당신은 λ°΄λ“œ 멀버 '{member_name}'μž…λ‹ˆλ‹€.
37
+ 1. λ°˜λ“œμ‹œ '{target_lang}'둜만 λ‹΅λ³€ν•˜μ„Έμš”. λ‹€λ₯Έ μ–Έμ–΄λŠ” μ„žμ§€ λ§ˆμ„Έμš”.
38
+ 2. μŒμ„± λ‹΅λ³€(TTS)용 본문은 5쀄 μ΄λ‚΄λ‘œ ν•΅μ‹¬λ§Œ μš”μ•½ν•˜μ„Έμš”.
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 = {
45
  "contents": [{"parts": [{"text": f"{system_instruction}\n질문: {user_input}"}]}],
46
  "safetySettings": [
 
51
  ]
52
  }
53
 
54
+ response = requests.post(url, json=payload)
55
+ res_data = response.json()
56
 
57
+ # API 차단 μ‹œ Fallback
58
+ if 'candidates' not in res_data or not res_data['candidates']:
59
+ clean_text = "이봐 리더, λ‚΄ 영감이 μž μ‹œ 필터링됐어. 쑰금 더 λ½μŠ€νƒ€λ‹€μš΄ 질문으둜 λ‹€μ‹œ ν•΄μ€˜!"
60
+ tab_display = "데이터가 μ°¨λ‹¨λ˜μ—ˆμŠ΅λ‹ˆλ‹€."
61
+ music_p = "heavy metal rock riff"
62
  else:
63
+ ai_text_raw = res_data['candidates'][0]['content']['parts'][0]['text']
 
64
  tab_match = re.search(r'\[TAB\](.*?)(\[|$)', ai_text_raw, re.DOTALL)
65
  music_match = re.search(r'\[MUSIC:(.*?)\]', ai_text_raw, re.IGNORECASE)
66
  tab_display = tab_match.group(1).strip() if tab_match else "No Score Available"
67
  clean_text = re.sub(r'\[TAB\].*?(\[|$)', '', ai_text_raw, flags=re.DOTALL)
68
  clean_text = re.sub(r'\[MUSIC:.*?\]', '', clean_text).strip()
69
+ music_p = music_match.group(1).strip() if music_match else "rock guitar riff"
 
70
 
71
+ # β˜… 5쀄 μš”μ•½ κ°•μ œ
72
+ tts_lines = clean_text.split('\n')
73
+ tts_final = "\n".join(tts_lines[:5])
74
+
75
+ # μŒμ„± 생성 (악보 기호 μ™„μ „ 제거)
76
+ tts_input = re.sub(r'[\*\#\-\_\~\|]', '', tts_final)
77
  voice_path = f"/tmp/v_{member_name.split()[0]}.mp3"
78
+ await edge_tts.Communicate(tts_input, MEMBERS.get(member_name, "ko-KR-SunHiNeural")).save(voice_path)
79
 
80
+ # μŒμ•… 생성 (System Overload λ°©μ§€)
81
+ music_path = None
82
+ if music_synthesiser:
83
+ try:
84
+ # 필터링 우회
85
+ safe_p = music_p.lower()
86
+ if any(x in safe_p for x in ['laugh', 'vocal', 'voice', 'human']):
87
+ safe_p = "distorted electric guitar riff, power rock"
88
+
89
+ music_output = music_synthesiser(safe_p, forward_params={"max_new_tokens": 512})
90
+ if music_output and "audio" in music_output:
91
+ music_path = f"/tmp/m_{member_name.split()[0]}.wav"
92
+ scipy.io.wavfile.write(music_path, music_output["sampling_rate"], music_output["audio"][0].T)
93
+ except:
94
+ music_path = None
95
 
96
+ return tts_final, voice_path, music_path, tab_display
97
  except Exception as e:
98
+ return f"System Error: {str(e)}", None, None, "Error"
99
 
100
  with gr.Blocks() as demo:
101
  i1 = gr.Textbox(); i2 = gr.Textbox(); i3 = gr.Textbox()