Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -8,24 +8,34 @@ import asyncio
|
|
| 8 |
import numpy as np
|
| 9 |
from transformers import pipeline
|
| 10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
GENAI_KEY = os.getenv("GEMINI_KEY")
|
| 12 |
|
| 13 |
# μμ
λͺ¨λΈ λ‘λ
|
| 14 |
try:
|
| 15 |
music_synthesiser = pipeline("text-to-audio", "facebook/musicgen-small")
|
| 16 |
-
except:
|
|
|
|
| 17 |
music_synthesiser = None
|
| 18 |
|
| 19 |
-
# 8μΈ λ©€λ² λ°μ΄ν° (
|
| 20 |
MEMBERS = {
|
| 21 |
-
"μμ€ (Korea)": { "voice": "ko-KR-SunHiNeural", "prompt": "
|
| 22 |
-
"Chloe (USA)": { "voice": "en-US-AriaNeural", "prompt": "
|
| 23 |
-
"Beatrice (Brazil)": { "voice": "pt-BR-FranciscaNeural", "prompt": "
|
| 24 |
-
"Naomi (Japan)": { "voice": "ja-JP-NanamiNeural", "prompt": "
|
| 25 |
-
"Elena (Spain)": { "voice": "es-ES-ElviraNeural", "prompt": "
|
| 26 |
-
"Amira (Egypt)": { "voice": "ar-EG-SalmaNeural", "prompt": "
|
| 27 |
-
"Liwei (China)": { "voice": "zh-CN-XiaoxiaoNeural", "prompt": "
|
| 28 |
-
"Sophie (France)": { "voice": "fr-FR-DeniseNeural", "prompt": "
|
| 29 |
}
|
| 30 |
|
| 31 |
async def band_consulting(user_input, member_name, lang_code):
|
|
@@ -35,22 +45,22 @@ async def band_consulting(user_input, member_name, lang_code):
|
|
| 35 |
target_lang = lang_map.get(lang_code, "Korean")
|
| 36 |
|
| 37 |
system_instruction = f"""
|
| 38 |
-
|
| 39 |
1. λ°λμ {target_lang}λ‘λ§ λ΅λ³νμΈμ.
|
| 40 |
-
2. μμ± λ΅λ³μ 5μ€ μ΄λ΄ μμ½, μμΈ μ€λͺ
|
| 41 |
3. λ§μ§λ§μ [MUSIC: μμ΄ ν둬ννΈ]λ₯Ό ν¬ν¨νμΈμ.
|
| 42 |
"""
|
| 43 |
|
| 44 |
url = f"https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash:generateContent?key={GENAI_KEY}"
|
| 45 |
payload = {"contents": [{"parts": [{"text": f"{system_instruction}\nμ§λ¬Έ: {user_input}"}]}]}
|
| 46 |
|
| 47 |
-
response = requests.post(url, json=payload, timeout=
|
| 48 |
res_data = response.json()
|
| 49 |
|
| 50 |
if 'candidates' not in res_data:
|
| 51 |
-
clean_text = "μκ°μ΄
|
| 52 |
tab_display = "λ°μ΄ν° μ€λ₯"
|
| 53 |
-
music_p = "rock music"
|
| 54 |
else:
|
| 55 |
ai_text_raw = res_data['candidates'][0]['content']['parts'][0]['text']
|
| 56 |
tab_match = re.search(r'\[TAB\](.*?)(\[|$)', ai_text_raw, re.DOTALL)
|
|
@@ -58,24 +68,23 @@ async def band_consulting(user_input, member_name, lang_code):
|
|
| 58 |
tab_display = tab_match.group(1).strip() if tab_match else "μμΈ μ
보 μμ"
|
| 59 |
clean_text = re.sub(r'\[TAB\].*?(\[|$)', '', ai_text_raw, flags=re.DOTALL)
|
| 60 |
clean_text = re.sub(r'\[MUSIC:.*?\]', '', clean_text).strip()
|
| 61 |
-
music_p = music_match.group(1).strip() if music_match else "rock
|
| 62 |
|
| 63 |
-
# μμ± μμ± (
|
| 64 |
tts_final = "\n".join(clean_text.split('\n')[:5])
|
| 65 |
voice_path = f"/tmp/v_{member_name.split()[0]}.mp3"
|
|
|
|
| 66 |
communicate = edge_tts.Communicate(re.sub(r'[\*\#\-\_\~\|]', '', tts_final), member_data['voice'])
|
| 67 |
await communicate.save(voice_path)
|
| 68 |
|
| 69 |
-
# μμ
μμ± (
|
| 70 |
music_path = None
|
| 71 |
if music_synthesiser:
|
| 72 |
try:
|
| 73 |
-
# νν° λ¨μ΄ μ°ν
|
| 74 |
safe_p = music_p.lower()
|
| 75 |
if any(x in safe_p for x in ['laugh', 'vocal', 'voice', 'human']):
|
| 76 |
-
safe_p = "electric guitar riff
|
| 77 |
|
| 78 |
-
# CPU κ³ΌλΆνλ₯Ό 견λκΈ° μν΄ max_new_tokens 512 μ μ§
|
| 79 |
music_output = music_synthesiser(safe_p, forward_params={"max_new_tokens": 512})
|
| 80 |
if music_output and "audio" in music_output:
|
| 81 |
music_path = f"/tmp/m_{member_name.split()[0]}.wav"
|
|
@@ -86,14 +95,13 @@ async def band_consulting(user_input, member_name, lang_code):
|
|
| 86 |
return tts_final, voice_path, music_path, tab_display
|
| 87 |
|
| 88 |
except Exception as e:
|
| 89 |
-
print(f"
|
| 90 |
return f"System Overload: {str(e)}", None, None, "Error"
|
| 91 |
|
| 92 |
with gr.Blocks() as demo:
|
| 93 |
i1 = gr.Textbox(); i2 = gr.Textbox(); i3 = gr.Textbox()
|
| 94 |
o1 = gr.Textbox(); o2 = gr.Audio(); o3 = gr.Audio(); o4 = gr.Textbox()
|
| 95 |
-
# ν μμ€ν
νμ±νλ‘ λΈλΌμ°μ μ°κ²° μ μ§
|
| 96 |
btn = gr.Button("GO"); btn.click(band_consulting, [i1, i2, i3], [o1, o2, o3, o4], api_name="predict")
|
| 97 |
|
| 98 |
-
#
|
| 99 |
demo.queue().launch()
|
|
|
|
| 8 |
import numpy as np
|
| 9 |
from transformers import pipeline
|
| 10 |
|
| 11 |
+
# [ν΅μ¬] λΉλκΈ° νμΌ λμ€ν¬λ¦½ν° μλ¬ λ°©μ§ μ€μ
|
| 12 |
+
if os.name != 'nt': # 리λ
μ€/λ§₯ νκ²½ λμ
|
| 13 |
+
import selectors
|
| 14 |
+
selector = selectors.SelectSelector()
|
| 15 |
+
try:
|
| 16 |
+
asyncio.set_event_loop_policy(asyncio.DefaultEventLoopPolicy())
|
| 17 |
+
except:
|
| 18 |
+
pass
|
| 19 |
+
|
| 20 |
GENAI_KEY = os.getenv("GEMINI_KEY")
|
| 21 |
|
| 22 |
# μμ
λͺ¨λΈ λ‘λ
|
| 23 |
try:
|
| 24 |
music_synthesiser = pipeline("text-to-audio", "facebook/musicgen-small")
|
| 25 |
+
except Exception as e:
|
| 26 |
+
print(f"Music Model Load Error: {e}")
|
| 27 |
music_synthesiser = None
|
| 28 |
|
| 29 |
+
# 8μΈ λ©€λ² λ°μ΄ν° (μ λ μΆμ μμ)
|
| 30 |
MEMBERS = {
|
| 31 |
+
"μμ€ (Korea)": { "voice": "ko-KR-SunHiNeural", "prompt": "보컬 μμ€. μλ컬νμ§λ§ μ§μ¬μ." },
|
| 32 |
+
"Chloe (USA)": { "voice": "en-US-AriaNeural", "prompt": "Guitarist Chloe. Cool rockstar vibe." },
|
| 33 |
+
"Beatrice (Brazil)": { "voice": "pt-BR-FranciscaNeural", "prompt": "Drummer Beatrice. Rhythm focus." },
|
| 34 |
+
"Naomi (Japan)": { "voice": "ja-JP-NanamiNeural", "prompt": "Naomi. Harmony focus." },
|
| 35 |
+
"Elena (Spain)": { "voice": "es-ES-ElviraNeural", "prompt": "Elena. Groove focus." },
|
| 36 |
+
"Amira (Egypt)": { "voice": "ar-EG-SalmaNeural", "prompt": "Amira. Atmosphere focus." },
|
| 37 |
+
"Liwei (China)": { "voice": "zh-CN-XiaoxiaoNeural", "prompt": "Liwei. Sound design focus." },
|
| 38 |
+
"Sophie (France)": { "voice": "fr-FR-DeniseNeural", "prompt": "Sophie. Melody focus." }
|
| 39 |
}
|
| 40 |
|
| 41 |
async def band_consulting(user_input, member_name, lang_code):
|
|
|
|
| 45 |
target_lang = lang_map.get(lang_code, "Korean")
|
| 46 |
|
| 47 |
system_instruction = f"""
|
| 48 |
+
λΉμ μ '{member_name}'μ
λλ€.
|
| 49 |
1. λ°λμ {target_lang}λ‘λ§ λ΅λ³νμΈμ.
|
| 50 |
+
2. μμ± λ΅λ³μ 5μ€ μ΄λ΄ μμ½, μμΈ μ€λͺ
/μ
보λ [TAB]μ λ£μΌμΈμ.
|
| 51 |
3. λ§μ§λ§μ [MUSIC: μμ΄ ν둬ννΈ]λ₯Ό ν¬ν¨νμΈμ.
|
| 52 |
"""
|
| 53 |
|
| 54 |
url = f"https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash:generateContent?key={GENAI_KEY}"
|
| 55 |
payload = {"contents": [{"parts": [{"text": f"{system_instruction}\nμ§λ¬Έ: {user_input}"}]}]}
|
| 56 |
|
| 57 |
+
response = requests.post(url, json=payload, timeout=20)
|
| 58 |
res_data = response.json()
|
| 59 |
|
| 60 |
if 'candidates' not in res_data:
|
| 61 |
+
clean_text = "μκ°μ΄ κΌ¬μμ΄. λ€μ μλν΄μ€!"
|
| 62 |
tab_display = "λ°μ΄ν° μ€λ₯"
|
| 63 |
+
music_p = "rock music riff"
|
| 64 |
else:
|
| 65 |
ai_text_raw = res_data['candidates'][0]['content']['parts'][0]['text']
|
| 66 |
tab_match = re.search(r'\[TAB\](.*?)(\[|$)', ai_text_raw, re.DOTALL)
|
|
|
|
| 68 |
tab_display = tab_match.group(1).strip() if tab_match else "μμΈ μ
보 μμ"
|
| 69 |
clean_text = re.sub(r'\[TAB\].*?(\[|$)', '', ai_text_raw, flags=re.DOTALL)
|
| 70 |
clean_text = re.sub(r'\[MUSIC:.*?\]', '', clean_text).strip()
|
| 71 |
+
music_p = music_match.group(1).strip() if music_match else "rock guitar riff"
|
| 72 |
|
| 73 |
+
# μμ± μμ± (λΉλκΈ° 루ν μ§μ μ μ΄)
|
| 74 |
tts_final = "\n".join(clean_text.split('\n')[:5])
|
| 75 |
voice_path = f"/tmp/v_{member_name.split()[0]}.mp3"
|
| 76 |
+
# νμΌμ΄ μ΄λ €μλμ§ νμΈ ν μμ±
|
| 77 |
communicate = edge_tts.Communicate(re.sub(r'[\*\#\-\_\~\|]', '', tts_final), member_data['voice'])
|
| 78 |
await communicate.save(voice_path)
|
| 79 |
|
| 80 |
+
# μμ
μμ± (512 ν ν° ν νμ)
|
| 81 |
music_path = None
|
| 82 |
if music_synthesiser:
|
| 83 |
try:
|
|
|
|
| 84 |
safe_p = music_p.lower()
|
| 85 |
if any(x in safe_p for x in ['laugh', 'vocal', 'voice', 'human']):
|
| 86 |
+
safe_p = "heavy rock electric guitar riff"
|
| 87 |
|
|
|
|
| 88 |
music_output = music_synthesiser(safe_p, forward_params={"max_new_tokens": 512})
|
| 89 |
if music_output and "audio" in music_output:
|
| 90 |
music_path = f"/tmp/m_{member_name.split()[0]}.wav"
|
|
|
|
| 95 |
return tts_final, voice_path, music_path, tab_display
|
| 96 |
|
| 97 |
except Exception as e:
|
| 98 |
+
print(f"Error caught: {e}")
|
| 99 |
return f"System Overload: {str(e)}", None, None, "Error"
|
| 100 |
|
| 101 |
with gr.Blocks() as demo:
|
| 102 |
i1 = gr.Textbox(); i2 = gr.Textbox(); i3 = gr.Textbox()
|
| 103 |
o1 = gr.Textbox(); o2 = gr.Audio(); o3 = gr.Audio(); o4 = gr.Textbox()
|
|
|
|
| 104 |
btn = gr.Button("GO"); btn.click(band_consulting, [i1, i2, i3], [o1, o2, o3, o4], api_name="predict")
|
| 105 |
|
| 106 |
+
# ν μμ€ν
μΌλ‘ νμμμ λ°©μ§
|
| 107 |
demo.queue().launch()
|