Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
-
|
|
|
|
| 48 |
|
| 49 |
-
#
|
| 50 |
-
if 'candidates' not in
|
| 51 |
-
clean_text = "μ΄λ΄ 리λ,
|
| 52 |
-
tab_display = "
|
| 53 |
-
music_p = "
|
| 54 |
else:
|
| 55 |
-
ai_text_raw =
|
| 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 |
-
|
| 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(
|
| 68 |
|
| 69 |
-
# μμ
μμ± (
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
|
| 77 |
-
return
|
| 78 |
except Exception as e:
|
| 79 |
-
return f"System
|
| 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()
|