Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,104 +4,77 @@ import edge_tts
|
|
| 4 |
import os
|
| 5 |
import re
|
| 6 |
import scipy.io.wavfile
|
| 7 |
-
import numpy as np
|
| 8 |
from transformers import pipeline
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
GENAI_KEY = os.getenv("GEMINI_KEY")
|
| 11 |
|
| 12 |
-
#
|
| 13 |
try:
|
| 14 |
-
music_synthesiser = pipeline("text-to-audio", "facebook/musicgen-small")
|
| 15 |
-
except
|
| 16 |
-
print(f"Music Model Load Error: {e}")
|
| 17 |
music_synthesiser = None
|
| 18 |
|
| 19 |
-
# 8μΈ λ©€λ²
|
| 20 |
-
MEMBERS = {
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 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 |
-
[LANGUAGE RULE] Respond 100% in {target_lang}. No other languages.
|
| 40 |
-
[STYLE RULE] 1. Verbal response MUST be under 5 lines (Concise).
|
| 41 |
-
2. Put ALL detailed musical explanations, chord progressions, and tips in [TAB].
|
| 42 |
-
3. End with [MUSIC: English prompt] for a high-quality rock sample.
|
| 43 |
-
"""
|
| 44 |
|
| 45 |
url = f"https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash:generateContent?key={GENAI_KEY}"
|
| 46 |
-
payload = {
|
| 47 |
-
"contents": [{"parts": [{"text": f"{system_instruction}\nμ§λ¬Έ: {user_input}"}]}],
|
| 48 |
-
"safetySettings": [
|
| 49 |
-
{"category": "HARM_CATEGORY_HARASSMENT", "threshold": "BLOCK_NONE"},
|
| 50 |
-
{"category": "HARM_CATEGORY_HATE_SPEECH", "threshold": "BLOCK_NONE"},
|
| 51 |
-
{"category": "HARM_CATEGORY_SEXUALLY_EXPLICIT", "threshold": "BLOCK_NONE"},
|
| 52 |
-
{"category": "HARM_CATEGORY_DANGEROUS_CONTENT", "threshold": "BLOCK_NONE"}
|
| 53 |
-
]
|
| 54 |
-
}
|
| 55 |
|
| 56 |
-
|
| 57 |
-
|
| 58 |
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
music_match = re.search(r'\[MUSIC:(.*?)\]', ai_text_raw, re.IGNORECASE)
|
| 67 |
-
tab_display = tab_match.group(1).strip() if tab_match else "No Score"
|
| 68 |
-
|
| 69 |
-
# TTSμμ μ
보 μ½κΈ° λ°©μ§
|
| 70 |
-
clean_text = re.sub(r'\[TAB\].*?(\[|$)', '', ai_text_raw, flags=re.DOTALL)
|
| 71 |
-
clean_text = re.sub(r'\[MUSIC:.*?\]', '', clean_text).strip()
|
| 72 |
-
clean_text = "\n".join(clean_text.split('\n')[:5])
|
| 73 |
-
music_p = music_match.group(1).strip() if music_match else "rock guitar riff"
|
| 74 |
|
| 75 |
# μμ± μμ±
|
| 76 |
-
tts_input = re.sub(r'[\*\#\-\_\~\|]', '', clean_text)
|
| 77 |
voice_path = f"/tmp/v_{member_name.split()[0]}.mp3"
|
| 78 |
-
await edge_tts.Communicate(
|
| 79 |
|
| 80 |
-
# μμ
μμ± (
|
| 81 |
music_path = None
|
| 82 |
-
if
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
safe_p = "distorted electric guitar, rock style"
|
| 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 clean_text, voice_path, music_path, tab_display
|
| 97 |
|
|
|
|
| 98 |
except Exception as e:
|
| 99 |
-
return f"System
|
| 100 |
|
| 101 |
with gr.Blocks() as demo:
|
| 102 |
-
|
| 103 |
-
i1 = gr.Textbox(); i2 = gr.Textbox(); i3 = gr.Textbox()
|
| 104 |
o1 = gr.Textbox(); o2 = gr.Audio(); o3 = gr.Audio(); o4 = gr.Textbox()
|
|
|
|
| 105 |
btn = gr.Button("GO"); btn.click(band_consulting, [i1, i2, i3], [o1, o2, o3, o4], api_name="predict")
|
| 106 |
|
| 107 |
-
|
|
|
|
|
|
| 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 |
+
# λͺ¨λΈ λ‘λ (Hugging Face μ΅μ ν)
|
| 18 |
try:
|
| 19 |
+
music_synthesiser = pipeline("text-to-audio", "facebook/musicgen-small", device=0 if has_spaces else -1)
|
| 20 |
+
except:
|
|
|
|
| 21 |
music_synthesiser = None
|
| 22 |
|
| 23 |
+
# 8μΈ λ©€λ² λ° λ°μ΄ν° (μμ /μΆμ μμ)
|
| 24 |
+
MEMBERS = { "μμ€ (Korea)": "ko-KR-SunHiNeural", "Chloe (USA)": "en-US-AriaNeural", "Naomi (Japan)": "ja-JP-NanamiNeural", "Beatrice (Brazil)": "pt-BR-FranciscaNeural", "Elena (Spain)": "es-ES-ElviraNeural", "Amira (Egypt)": "ar-EG-SalmaNeural", "Liwei (China)": "zh-CN-XiaoxiaoNeural", "Sophie (France)": "fr-FR-DeniseNeural" }
|
| 25 |
+
|
| 26 |
+
# β
GPU κ°μ λ°μ½λ μ΄ν° (Hugging Face λ¬΄λ£ GPU νμ±ν)
|
| 27 |
+
def generate_music_safe(prompt):
|
| 28 |
+
if not music_synthesiser: return None, None
|
| 29 |
+
try:
|
| 30 |
+
# 리λλ μμ²λλ‘ 512(μ½ 12μ΄) ν νμ!
|
| 31 |
+
output = music_synthesiser(prompt, forward_params={"max_new_tokens": 512, "do_sample": True})
|
| 32 |
+
return output["sampling_rate"], output["audio"][0].T
|
| 33 |
+
except:
|
| 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"λΉμ μ {member_name}μ
λλ€. λ°λμ {target_lang}λ‘λ§ λ΅λ³νμΈμ. μμ±μ 5μ€ μ΄λ΄, μμΈ μ€λͺ
μ [TAB]μ, μμ
ν둬ννΈλ [MUSIC: ν둬ννΈ]μ λ£μΌμΈμ."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
res = requests.post(url, json=payload).json()
|
| 47 |
+
ai_text_raw = res['candidates'][0]['content']['parts'][0]['text']
|
| 48 |
|
| 49 |
+
tab_match = re.search(r'\[TAB\](.*?)(\[|$)', ai_text_raw, re.DOTALL)
|
| 50 |
+
music_match = re.search(r'\[MUSIC:(.*?)\]', ai_text_raw, re.IGNORECASE)
|
| 51 |
+
tab_display = tab_match.group(1).strip() if tab_match else "μμΈ μ
보 μμ"
|
| 52 |
+
|
| 53 |
+
clean_text = re.sub(r'\[TAB\].*?(\[|$)', '', ai_text_raw, flags=re.DOTALL)
|
| 54 |
+
clean_text = re.sub(r'\[MUSIC:.*?\]', '', clean_text).strip()
|
| 55 |
+
tts_final = "\n".join(clean_text.split('\n')[:5])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
|
| 57 |
# μμ± μμ±
|
|
|
|
| 58 |
voice_path = f"/tmp/v_{member_name.split()[0]}.mp3"
|
| 59 |
+
await edge_tts.Communicate(re.sub(r'[\*\#\-\_\~\|]', '', tts_final), MEMBERS.get(member_name, "ko-KR-SunHiNeural")).save(voice_path)
|
| 60 |
|
| 61 |
+
# μμ
μμ± (νμμμ λ°©μ§ λ‘μ§ ν¬ν¨)
|
| 62 |
music_path = None
|
| 63 |
+
if music_match:
|
| 64 |
+
sr, audio = generate_music_safe(music_match.group(1).strip())
|
| 65 |
+
if sr is not None:
|
| 66 |
+
music_path = f"/tmp/m_{member_name.split()[0]}.wav"
|
| 67 |
+
scipy.io.wavfile.write(music_path, sr, audio)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
|
| 69 |
+
return tts_final, voice_path, music_path, tab_display
|
| 70 |
except Exception as e:
|
| 71 |
+
return f"System Error: {str(e)}", None, None, "Error"
|
| 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 |
+
# β
ν(Queue)λ₯Ό νμ±ννμ¬ μλ²κ° λκΈ°μ§ μκ² ν¨
|
| 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()
|