Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -16,8 +16,8 @@ except:
|
|
| 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",
|
|
@@ -29,22 +29,28 @@ async def band_consulting(user_input, member_name, lang_code):
|
|
| 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 |
-
system_instruction = f""
|
| 33 |
-
λΉμ μ λ°΄λ λ©€λ² '{member_name}'μ
λλ€.
|
| 34 |
-
1. λ°λμ '{target_lang}'λ‘λ§ λ΅λ³νμΈμ.
|
| 35 |
-
2. μμ± λ΅λ³μ© λ³Έλ¬Έμ μμ£Ό μΉμ νκ³ μμΈνκ² μμ±νμΈμ.
|
| 36 |
-
3. λͺ¨λ μ
보μ μ°μ£Όλ²μ λ°λμ [TAB] μΉμ
μ ν¬ν¨νμΈμ.
|
| 37 |
-
4. λ§μ§λ§μ [MUSIC: μμ΄ ν둬ννΈ]λ₯Ό ν¬ν¨νμΈμ.
|
| 38 |
-
"""
|
| 39 |
|
| 40 |
url = f"https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash:generateContent?key={GENAI_KEY}"
|
| 41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
response = requests.post(url, json=payload)
|
| 44 |
res_data = response.json()
|
| 45 |
|
| 46 |
-
|
| 47 |
-
|
|
|
|
|
|
|
| 48 |
|
| 49 |
ai_text_raw = res_data['candidates'][0]['content']['parts'][0]['text']
|
| 50 |
|
|
@@ -52,15 +58,12 @@ async def band_consulting(user_input, member_name, lang_code):
|
|
| 52 |
music_match = re.search(r'\[MUSIC:(.*?)\]', ai_text_raw, re.IGNORECASE)
|
| 53 |
tab_display = tab_match.group(1).strip() if tab_match else "μμΈ μ
보 μμ"
|
| 54 |
|
| 55 |
-
# TTSμμ μ
보 λλ €λ΄κΈ°
|
| 56 |
clean_text = re.sub(r'\[TAB\].*?(\[|$)', '', ai_text_raw, flags=re.DOTALL)
|
| 57 |
clean_text = re.sub(r'\[MUSIC:.*?\]', '', clean_text).strip()
|
| 58 |
|
| 59 |
-
# μμ± μμ±
|
| 60 |
voice_path = f"/tmp/v_{member_name.split()[0]}.mp3"
|
| 61 |
await edge_tts.Communicate(clean_text, MEMBERS.get(member_name, "ko-KR-SunHiNeural")).save(voice_path)
|
| 62 |
|
| 63 |
-
# μμ
μμ±
|
| 64 |
music_path = None
|
| 65 |
if music_match and music_synthesiser:
|
| 66 |
p = music_match.group(1).strip()
|
|
@@ -70,7 +73,7 @@ async def band_consulting(user_input, member_name, lang_code):
|
|
| 70 |
|
| 71 |
return clean_text, voice_path, music_path, tab_display
|
| 72 |
except Exception as e:
|
| 73 |
-
return str(e), None, None, "Error"
|
| 74 |
|
| 75 |
with gr.Blocks() as demo:
|
| 76 |
i1 = gr.Textbox(); i2 = gr.Textbox(); i3 = gr.Textbox()
|
|
|
|
| 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",
|
|
|
|
| 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 |
+
system_instruction = f"λΉμ μ {member_name}μ
λλ€. λ°λμ {target_lang}λ‘λ§ λ΅λ³νμΈμ. μμ±μ 5μ€ μ΄λ΄ μμ½, μμΈ μ€λͺ
μ [TAB]μ, μμ
ν둬ννΈλ [MUSIC: ν둬ννΈ]μ λ£μΌμΈμ."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
url = f"https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash:generateContent?key={GENAI_KEY}"
|
| 35 |
+
|
| 36 |
+
# β
μμ νν° ν΄μ λ° μ€μ κ°ν
|
| 37 |
+
payload = {
|
| 38 |
+
"contents": [{"parts": [{"text": f"{system_instruction}\nμ§λ¬Έ: {user_input}"}]}],
|
| 39 |
+
"safetySettings": [
|
| 40 |
+
{"category": "HARM_CATEGORY_HARASSMENT", "threshold": "BLOCK_NONE"},
|
| 41 |
+
{"category": "HARM_CATEGORY_HATE_SPEECH", "threshold": "BLOCK_NONE"},
|
| 42 |
+
{"category": "HARM_CATEGORY_SEXUALLY_EXPLICIT", "threshold": "BLOCK_NONE"},
|
| 43 |
+
{"category": "HARM_CATEGORY_DANGEROUS_CONTENT", "threshold": "BLOCK_NONE"}
|
| 44 |
+
]
|
| 45 |
+
}
|
| 46 |
|
| 47 |
response = requests.post(url, json=payload)
|
| 48 |
res_data = response.json()
|
| 49 |
|
| 50 |
+
# candidates μλ¬ μΈλΆ μ§λ¨
|
| 51 |
+
if 'candidates' not in res_data or not res_data['candidates']:
|
| 52 |
+
reason = res_data.get('promptFeedback', {}).get('blockReason', 'Unknown')
|
| 53 |
+
return f"νν°μ μν΄ μ°¨λ¨λμμ΅λλ€ (μ¬μ : {reason}). λ€λ₯Έ λ°©μμΌλ‘ μ§λ¬Έν΄μ£ΌμΈμ.", None, None, "Score Error"
|
| 54 |
|
| 55 |
ai_text_raw = res_data['candidates'][0]['content']['parts'][0]['text']
|
| 56 |
|
|
|
|
| 58 |
music_match = re.search(r'\[MUSIC:(.*?)\]', ai_text_raw, re.IGNORECASE)
|
| 59 |
tab_display = tab_match.group(1).strip() if tab_match else "μμΈ μ
보 μμ"
|
| 60 |
|
|
|
|
| 61 |
clean_text = re.sub(r'\[TAB\].*?(\[|$)', '', ai_text_raw, flags=re.DOTALL)
|
| 62 |
clean_text = re.sub(r'\[MUSIC:.*?\]', '', clean_text).strip()
|
| 63 |
|
|
|
|
| 64 |
voice_path = f"/tmp/v_{member_name.split()[0]}.mp3"
|
| 65 |
await edge_tts.Communicate(clean_text, MEMBERS.get(member_name, "ko-KR-SunHiNeural")).save(voice_path)
|
| 66 |
|
|
|
|
| 67 |
music_path = None
|
| 68 |
if music_match and music_synthesiser:
|
| 69 |
p = music_match.group(1).strip()
|
|
|
|
| 73 |
|
| 74 |
return clean_text, voice_path, music_path, tab_display
|
| 75 |
except Exception as e:
|
| 76 |
+
return f"Backend Error: {str(e)}", None, None, "Error"
|
| 77 |
|
| 78 |
with gr.Blocks() as demo:
|
| 79 |
i1 = gr.Textbox(); i2 = gr.Textbox(); i3 = gr.Textbox()
|