error404-api / app.py
benjamin5607's picture
Update app.py
eef5842 verified
Raw History Blame
3.72 kB
import gradio as gr
import requests
import edge_tts
import os
import re
import scipy.io.wavfile
from transformers import pipeline
GENAI_KEY = os.getenv("GEMINI_KEY")
try:
music_synthesiser = pipeline("text-to-audio", "facebook/musicgen-small")
except:
music_synthesiser = None
# 8인 멀버 보이슀 λ§€μΉ­
MEMBERS = {
"μ„œμœ€ (Korea)": "ko-KR-SunHiNeural",
"Chloe (USA)": "en-US-AriaNeural",
"Beatrice (Brazil)": "pt-BR-FranciscaNeural",
"Naomi (Japan)": "ja-JP-NanamiNeural",
"Elena (Spain)": "es-ES-ElviraNeural",
"Amira (Egypt)": "ar-EG-SalmaNeural",
"Liwei (China)": "zh-CN-XiaoxiaoNeural",
"Sophie (France)": "fr-FR-DeniseNeural"
}
async def band_consulting(user_input, member_name, consult_lang_code):
try:
lang_map = {"ko": "Korean", "en": "English", "ja": "Japanese", "pt": "Portuguese", "es": "Spanish", "ar": "Arabic", "zh": "Chinese", "fr": "French"}
target_lang = lang_map.get(consult_lang_code, "Korean")
system_instruction = f"""
당신은 λ°΄λ“œ 멀버 '{member_name}'μž…λ‹ˆλ‹€.
1. λ°˜λ“œμ‹œ '{target_lang}'둜만 λ‹΅λ³€ν•˜μ„Έμš”. μ˜μ–΄λ©΄ μ˜μ–΄, 일본어면 μΌλ³Έμ–΄λ§Œ μ‚¬μš©ν•©λ‹ˆλ‹€.
2. μŒμ„± λ‹΅λ³€(TTS)은 5쀄 μ΄λ‚΄λ‘œ ν•΅μ‹¬λ§Œ μš”μ•½ν•˜μ„Έμš”.
3. λͺ¨λ“  상세 μ„€λͺ…, ν™”μ„±ν•™ μ‘°μ–Έ, ν…μŠ€νŠΈ μ•…λ³΄λŠ” [TAB] μ„Ήμ…˜μ— λ„£μœΌμ„Έμš”.
4. μŒμ•… μƒμ„±μš© μ˜μ–΄ ν”„λ‘¬ν”„νŠΈλŠ” λ§ˆμ§€λ§‰μ— [MUSIC: ν”„λ‘¬ν”„νŠΈ] ν˜•μ‹μœΌλ‘œ ν¬ν•¨ν•˜μ„Έμš”.
"""
url = f"https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash:generateContent?key={GENAI_KEY}"
payload = {"contents": [{"parts": [{"text": f"{system_instruction}\n질문: {user_input}"}]}]}
res = requests.post(url, json=payload).json()
ai_text_raw = res['candidates'][0]['content']['parts'][0]['text']
# 데이터 μ •λ°€ νŒŒμ‹±
tab_match = re.search(r'\[TAB\](.*?)(\[|$)', ai_text_raw, re.DOTALL)
music_match = re.search(r'\[MUSIC:(.*?)\]', ai_text_raw, re.IGNORECASE)
tab_display = tab_match.group(1).strip() if tab_match else "No Detailed Score"
# β˜… 악보 읽기 λ°©μ§€: [TAB] 이후 λ‚΄μš©μ„ ν†΅μ§Έλ‘œ μ‚­μ œν•˜μ—¬ λͺ©μ†Œλ¦¬μš© ν…μŠ€νŠΈ 생성
clean_text = re.sub(r'\[TAB\].*?(\[|$)', '', ai_text_raw, flags=re.DOTALL)
clean_text = re.sub(r'\[MUSIC:.*?\]', '', clean_text).strip()
tts_lines = clean_text.split('\n')
tts_final = "\n".join(tts_lines[:5]) # 5쀄 μš”μ•½
# μŒμ„± 생성 (특수기호 제거)
tts_input = re.sub(r'[\*\#\-\_\~\|]', '', tts_final)
voice_path = f"/tmp/v_{member_name.split()[0]}.mp3"
await edge_tts.Communicate(tts_input, MEMBERS.get(member_name, "ko-KR-SunHiNeural")).save(voice_path)
# μŒμ•… 생성 (512 tokens = μ•½ 10~12초)
music_path = None
if music_match and music_synthesiser:
p = music_match.group(1).strip() + ", high quality, studio recording"
music_output = music_synthesiser(p, forward_params={"max_new_tokens": 512, "do_sample": True})
music_path = f"/tmp/m_{member_name.split()[0]}.wav"
scipy.io.wavfile.write(music_path, music_output["sampling_rate"], music_output["audio"][0].T)
return tts_final, voice_path, music_path, tab_display
except Exception as e:
return str(e), None, None, "Error"
with gr.Blocks() as demo:
i1 = gr.Textbox(); i2 = gr.Textbox(); i3 = gr.Textbox()
o1 = gr.Textbox(); o2 = gr.Audio(); o3 = gr.Audio(); o4 = gr.Textbox()
btn = gr.Button("GO"); btn.click(band_consulting, [i1, i2, i3], [o1, o2, o3, o4], api_name="predict")
demo.launch()