Spaces:
Sleeping
Sleeping
File size: 3,719 Bytes
da89e23 440060b da89e23 8ee2277 da89e23 eef5842 8ee2277 26c3944 8ee2277 eef5842 440060b 26c3944 440060b 1359090 26c3944 84daabb 26c3944 8ee2277 26c3944 eef5842 8ee2277 26c3944 3f6565c 26c3944 8ee2277 eef5842 1359090 3f6565c eef5842 fb6ce6a eef5842 1359090 eef5842 84daabb eef5842 26c3944 eef5842 84daabb eef5842 8ee2277 3f6565c 5473ad3 26c3944 1359090 8ee2277 eef5842 84daabb 26c3944 84daabb f2609ff 26c3944 5724da8 f2609ff | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 | 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() |