Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,77 +4,96 @@ import edge_tts
|
|
| 4 |
import os
|
| 5 |
import re
|
| 6 |
import scipy.io.wavfile
|
|
|
|
|
|
|
| 7 |
from transformers import pipeline
|
| 8 |
-
# β
GPU μ§μμ μν λΌμ΄λΈλ¬λ¦¬ (Hugging Face νκ²½μΈ κ²½μ°)
|
| 9 |
-
try:
|
| 10 |
-
import spaces
|
| 11 |
-
has_spaces = True
|
| 12 |
-
except:
|
| 13 |
-
has_spaces = False
|
| 14 |
|
| 15 |
GENAI_KEY = os.getenv("GEMINI_KEY")
|
| 16 |
|
| 17 |
-
# λͺ¨λΈ λ‘λ
|
| 18 |
try:
|
| 19 |
-
music_synthesiser = pipeline("text-to-audio", "facebook/musicgen-small"
|
| 20 |
except:
|
| 21 |
music_synthesiser = None
|
| 22 |
|
| 23 |
-
# 8μΈ λ©€λ²
|
| 24 |
-
MEMBERS = {
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
return None, None
|
| 35 |
|
| 36 |
async def band_consulting(user_input, member_name, lang_code):
|
| 37 |
try:
|
|
|
|
| 38 |
lang_map = {"ko":"Korean","en":"English","ja":"Japanese","pt":"Portuguese","es":"Spanish","ar":"Arabic","zh":"Chinese","fr":"French"}
|
| 39 |
target_lang = lang_map.get(lang_code, "Korean")
|
| 40 |
|
| 41 |
-
system_instruction = f"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
-
|
| 47 |
-
|
| 48 |
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
|
| 57 |
-
# μμ± μμ±
|
|
|
|
| 58 |
voice_path = f"/tmp/v_{member_name.split()[0]}.mp3"
|
| 59 |
-
|
|
|
|
| 60 |
|
| 61 |
-
# μμ
μμ± (
|
| 62 |
music_path = None
|
| 63 |
-
if
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
|
| 69 |
return tts_final, voice_path, music_path, tab_display
|
|
|
|
| 70 |
except Exception as e:
|
| 71 |
-
|
|
|
|
| 72 |
|
| 73 |
with gr.Blocks() as demo:
|
| 74 |
i1 = gr.Textbox(); i2 = gr.Textbox(); i3 = gr.Textbox()
|
| 75 |
o1 = gr.Textbox(); o2 = gr.Audio(); o3 = gr.Audio(); o4 = gr.Textbox()
|
| 76 |
-
#
|
| 77 |
btn = gr.Button("GO"); btn.click(band_consulting, [i1, i2, i3], [o1, o2, o3, o4], api_name="predict")
|
| 78 |
|
| 79 |
-
# β
queue()λ₯Ό
|
| 80 |
demo.queue().launch()
|
|
|
|
| 4 |
import os
|
| 5 |
import re
|
| 6 |
import scipy.io.wavfile
|
| 7 |
+
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": "You are 'Chloe', the lead guitarist. Cool rockstar vibe." },
|
| 23 |
+
"Beatrice (Brazil)": { "voice": "pt-BR-FranciscaNeural", "prompt": "You are 'Beatrice', the drummer. Focus on rhythm." },
|
| 24 |
+
"Naomi (Japan)": { "voice": "ja-JP-NanamiNeural", "prompt": "You are 'Naomi'. Focus on harmony." },
|
| 25 |
+
"Elena (Spain)": { "voice": "es-ES-ElviraNeural", "prompt": "You are 'Elena'. Focus on groove." },
|
| 26 |
+
"Amira (Egypt)": { "voice": "ar-EG-SalmaNeural", "prompt": "You are 'Amira'. Focus on atmosphere." },
|
| 27 |
+
"Liwei (China)": { "voice": "zh-CN-XiaoxiaoNeural", "prompt": "You are 'Liwei'. Focus on sound design." },
|
| 28 |
+
"Sophie (France)": { "voice": "fr-FR-DeniseNeural", "prompt": "You are 'Sophie'. Focus on melody." }
|
| 29 |
+
}
|
|
|
|
| 30 |
|
| 31 |
async def band_consulting(user_input, member_name, lang_code):
|
| 32 |
try:
|
| 33 |
+
member_data = MEMBERS.get(member_name, MEMBERS["μμ€ (Korea)"])
|
| 34 |
lang_map = {"ko":"Korean","en":"English","ja":"Japanese","pt":"Portuguese","es":"Spanish","ar":"Arabic","zh":"Chinese","fr":"French"}
|
| 35 |
target_lang = lang_map.get(lang_code, "Korean")
|
| 36 |
|
| 37 |
+
system_instruction = f"""
|
| 38 |
+
{member_data['prompt']}
|
| 39 |
+
1. λ°λμ {target_lang}λ‘λ§ λ΅λ³νμΈμ.
|
| 40 |
+
2. μμ± λ΅λ³μ 5μ€ μ΄λ΄ μμ½, μμΈ μ€λͺ
μ [TAB]μ λ£μΌμΈμ.
|
| 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=15)
|
| 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)
|
| 57 |
+
music_match = re.search(r'\[MUSIC:(.*?)\]', ai_text_raw, re.IGNORECASE)
|
| 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 style"
|
| 62 |
|
| 63 |
+
# μμ± μμ± (5μ€ μμ½ λ° νν°λ§)
|
| 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 |
+
# μμ
μμ± (리λλ μμ²λλ‘ 512 ν ν° - κΈ΄ μ°μ£Ό)
|
| 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, rock style"
|
| 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"
|
| 82 |
+
scipy.io.wavfile.write(music_path, music_output["sampling_rate"], music_output["audio"][0].T)
|
| 83 |
+
except:
|
| 84 |
+
music_path = None
|
| 85 |
|
| 86 |
return tts_final, voice_path, music_path, tab_display
|
| 87 |
+
|
| 88 |
except Exception as e:
|
| 89 |
+
print(f"Server Catch: {e}")
|
| 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 |
+
# β
queue()λ₯Ό μΆκ°νμ¬ λκΈ°μ΄μ λ§λ€κ³ νμμμ(Invalid FD) μλ¬λ₯Ό λ°©μ§
|
| 99 |
demo.queue().launch()
|