Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -8,12 +8,13 @@ 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 |
-
# 8μΈ λ©€λ² λ³΄μ΄μ€ λ§€μΉ
|
| 17 |
MEMBERS = {
|
| 18 |
"μμ€ (Korea)": "ko-KR-SunHiNeural",
|
| 19 |
"Chloe (USA)": "en-US-AriaNeural",
|
|
@@ -30,49 +31,58 @@ async def band_consulting(user_input, member_name, consult_lang_code):
|
|
| 30 |
lang_map = {"ko": "Korean", "en": "English", "ja": "Japanese", "pt": "Portuguese", "es": "Spanish", "ar": "Arabic", "zh": "Chinese", "fr": "French"}
|
| 31 |
target_lang = lang_map.get(consult_lang_code, "Korean")
|
| 32 |
|
|
|
|
| 33 |
system_instruction = f"""
|
| 34 |
-
λΉμ μ λ°΄λ λ©€λ² '{member_name}'μ
λλ€.
|
| 35 |
-
1. λ°λμ '{target_lang}'λ‘
|
| 36 |
-
2.
|
| 37 |
-
3.
|
| 38 |
-
4. μμ
μμ±μ©
|
| 39 |
"""
|
| 40 |
|
| 41 |
url = f"https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash:generateContent?key={GENAI_KEY}"
|
| 42 |
payload = {"contents": [{"parts": [{"text": f"{system_instruction}\nμ§λ¬Έ: {user_input}"}]}]}
|
| 43 |
-
res = requests.post(url, json=payload).json()
|
| 44 |
-
ai_text_raw = res['candidates'][0]['content']['parts'][0]['text']
|
| 45 |
|
| 46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
tab_match = re.search(r'\[TAB\](.*?)(\[|$)', ai_text_raw, re.DOTALL)
|
| 48 |
music_match = re.search(r'\[MUSIC:(.*?)\]', ai_text_raw, re.IGNORECASE)
|
| 49 |
-
tab_display = tab_match.group(1).strip() if tab_match else "No Detailed Score"
|
| 50 |
|
| 51 |
-
|
|
|
|
|
|
|
| 52 |
clean_text = re.sub(r'\[TAB\].*?(\[|$)', '', ai_text_raw, flags=re.DOTALL)
|
| 53 |
clean_text = re.sub(r'\[MUSIC:.*?\]', '', clean_text).strip()
|
| 54 |
-
tts_lines = clean_text.split('\n')
|
| 55 |
-
tts_final = "\n".join(tts_lines[:5]) # 5μ€ μμ½
|
| 56 |
|
| 57 |
-
# μμ± μμ± (
|
| 58 |
-
tts_input = re.sub(r'[\*\#\-\_\~\|]', '', tts_final)
|
| 59 |
voice_path = f"/tmp/v_{member_name.split()[0]}.mp3"
|
| 60 |
-
|
|
|
|
| 61 |
|
| 62 |
-
# μμ
μμ±
|
| 63 |
music_path = None
|
| 64 |
if music_match and music_synthesiser:
|
| 65 |
-
p = music_match.group(1).strip()
|
| 66 |
-
music_output = music_synthesiser(p, forward_params={"max_new_tokens": 512
|
| 67 |
music_path = f"/tmp/m_{member_name.split()[0]}.wav"
|
| 68 |
scipy.io.wavfile.write(music_path, music_output["sampling_rate"], music_output["audio"][0].T)
|
| 69 |
|
| 70 |
-
return
|
|
|
|
| 71 |
except Exception as e:
|
| 72 |
-
return str(e), None, None, "Error"
|
| 73 |
|
| 74 |
with gr.Blocks() as demo:
|
| 75 |
i1 = gr.Textbox(); i2 = gr.Textbox(); i3 = gr.Textbox()
|
| 76 |
o1 = gr.Textbox(); o2 = gr.Audio(); o3 = gr.Audio(); o4 = gr.Textbox()
|
| 77 |
-
btn = gr.Button("GO"); btn.click(band_consulting, [i1, i2, i3], [o1, o2, o3, o4]
|
|
|
|
| 78 |
demo.launch()
|
|
|
|
| 8 |
|
| 9 |
GENAI_KEY = os.getenv("GEMINI_KEY")
|
| 10 |
|
| 11 |
+
# μμ
μμ± λͺ¨λΈ λ‘λ
|
| 12 |
try:
|
| 13 |
music_synthesiser = pipeline("text-to-audio", "facebook/musicgen-small")
|
| 14 |
+
except Exception as e:
|
| 15 |
+
print(f"Music Model Load Error: {e}")
|
| 16 |
music_synthesiser = None
|
| 17 |
|
|
|
|
| 18 |
MEMBERS = {
|
| 19 |
"μμ€ (Korea)": "ko-KR-SunHiNeural",
|
| 20 |
"Chloe (USA)": "en-US-AriaNeural",
|
|
|
|
| 31 |
lang_map = {"ko": "Korean", "en": "English", "ja": "Japanese", "pt": "Portuguese", "es": "Spanish", "ar": "Arabic", "zh": "Chinese", "fr": "French"}
|
| 32 |
target_lang = lang_map.get(consult_lang_code, "Korean")
|
| 33 |
|
| 34 |
+
# β
리λλ μμ²: μ€λͺ
μΆ©λΆν, μ
보 λ°λμ ν¬ν¨
|
| 35 |
system_instruction = f"""
|
| 36 |
+
λΉμ μ λ½λ°΄λ λ©€λ² '{member_name}'μ
λλ€.
|
| 37 |
+
1. λ°λμ '{target_lang}'λ‘ λ΅λ³νμΈμ.
|
| 38 |
+
2. κ³ λ―Όμ λν΄ μμ£Ό μμΈνκ³ μΉμ νκ² μ€λͺ
νμΈμ. (λ¬Έμ₯ μ μ ν μμ)
|
| 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 = {"contents": [{"parts": [{"text": f"{system_instruction}\nμ§λ¬Έ: {user_input}"}]}]}
|
|
|
|
|
|
|
| 45 |
|
| 46 |
+
response = requests.post(url, json=payload)
|
| 47 |
+
res_data = response.json()
|
| 48 |
+
|
| 49 |
+
# 'candidates' μλ¬ λ°©μ΄
|
| 50 |
+
if 'candidates' not in res_data:
|
| 51 |
+
return "API μλ΅ μ€λ₯κ° λ°μνμ΅λλ€. ν€λ₯Ό νμΈν΄μ£ΌμΈμ.", None, None, "Score Error"
|
| 52 |
+
|
| 53 |
+
ai_text_raw = res_data['candidates'][0]['content']['parts'][0]['text']
|
| 54 |
+
|
| 55 |
+
# λ°μ΄ν° νμ±
|
| 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 |
|
| 59 |
+
tab_display = tab_match.group(1).strip() if tab_match else "μμΈ μ
보λ₯Ό μμ±νμ§ λͺ»νμ΅λλ€."
|
| 60 |
+
|
| 61 |
+
# TTSμ© ν
μ€νΈ (μ
보 κΈ°νΈ μ μΈνκ³ μμ μ€λͺ
λ§)
|
| 62 |
clean_text = re.sub(r'\[TAB\].*?(\[|$)', '', ai_text_raw, flags=re.DOTALL)
|
| 63 |
clean_text = re.sub(r'\[MUSIC:.*?\]', '', clean_text).strip()
|
|
|
|
|
|
|
| 64 |
|
| 65 |
+
# μμ± μμ± (μ€λͺ
μ΄ κΈΈ κ²½μ° μ²μ λͺ λ¬Έμ₯λ§ μ½λλ‘ μ‘°μ κ°λ₯)
|
|
|
|
| 66 |
voice_path = f"/tmp/v_{member_name.split()[0]}.mp3"
|
| 67 |
+
tts_text = " ".join(clean_text.split('.')[:3]) # λ무 κΈΈλ©΄ μ€λ₯λλ―λ‘ μ 3λ¬Έμ₯λ§ TTS
|
| 68 |
+
await edge_tts.Communicate(tts_text, MEMBERS.get(member_name, "ko-KR-SunHiNeural")).save(voice_path)
|
| 69 |
|
| 70 |
+
# μμ
μμ±
|
| 71 |
music_path = None
|
| 72 |
if music_match and music_synthesiser:
|
| 73 |
+
p = music_match.group(1).strip()
|
| 74 |
+
music_output = music_synthesiser(p, forward_params={"max_new_tokens": 512})
|
| 75 |
music_path = f"/tmp/m_{member_name.split()[0]}.wav"
|
| 76 |
scipy.io.wavfile.write(music_path, music_output["sampling_rate"], music_output["audio"][0].T)
|
| 77 |
|
| 78 |
+
return clean_text, voice_path, music_path, tab_display
|
| 79 |
+
|
| 80 |
except Exception as e:
|
| 81 |
+
return f"μ€λ₯ λ°μ: {str(e)}", None, None, "Error"
|
| 82 |
|
| 83 |
with gr.Blocks() as demo:
|
| 84 |
i1 = gr.Textbox(); i2 = gr.Textbox(); i3 = gr.Textbox()
|
| 85 |
o1 = gr.Textbox(); o2 = gr.Audio(); o3 = gr.Audio(); o4 = gr.Textbox()
|
| 86 |
+
btn = gr.Button("GO"); btn.click(band_consulting, [i1, i2, i3], [o1, o2, o3, o4])
|
| 87 |
+
|
| 88 |
demo.launch()
|