Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import requests | |
| import edge_tts | |
| import os | |
| import re | |
| import scipy.io.wavfile | |
| from transformers import pipeline | |
| # β GPU μ§μμ μν λΌμ΄λΈλ¬λ¦¬ (Hugging Face νκ²½μΈ κ²½μ°) | |
| try: | |
| import spaces | |
| has_spaces = True | |
| except: | |
| has_spaces = False | |
| GENAI_KEY = os.getenv("GEMINI_KEY") | |
| # λͺ¨λΈ λ‘λ (Hugging Face μ΅μ ν) | |
| try: | |
| music_synthesiser = pipeline("text-to-audio", "facebook/musicgen-small", device=0 if has_spaces else -1) | |
| except: | |
| music_synthesiser = None | |
| # 8μΈ λ©€λ² λ° λ°μ΄ν° (μμ /μΆμ μμ) | |
| 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" } | |
| # β GPU κ°μ λ°μ½λ μ΄ν° (Hugging Face λ¬΄λ£ GPU νμ±ν) | |
| def generate_music_safe(prompt): | |
| if not music_synthesiser: return None, None | |
| try: | |
| # 리λλ μμ²λλ‘ 512(μ½ 12μ΄) ν νμ! | |
| output = music_synthesiser(prompt, forward_params={"max_new_tokens": 512, "do_sample": True}) | |
| return output["sampling_rate"], output["audio"][0].T | |
| except: | |
| return None, None | |
| async def band_consulting(user_input, member_name, 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(lang_code, "Korean") | |
| system_instruction = f"λΉμ μ {member_name}μ λλ€. λ°λμ {target_lang}λ‘λ§ λ΅λ³νμΈμ. μμ±μ 5μ€ μ΄λ΄, μμΈ μ€λͺ μ [TAB]μ, μμ ν둬ννΈλ [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 "μμΈ μ 보 μμ" | |
| clean_text = re.sub(r'\[TAB\].*?(\[|$)', '', ai_text_raw, flags=re.DOTALL) | |
| clean_text = re.sub(r'\[MUSIC:.*?\]', '', clean_text).strip() | |
| tts_final = "\n".join(clean_text.split('\n')[:5]) | |
| # μμ± μμ± | |
| voice_path = f"/tmp/v_{member_name.split()[0]}.mp3" | |
| await edge_tts.Communicate(re.sub(r'[\*\#\-\_\~\|]', '', tts_final), MEMBERS.get(member_name, "ko-KR-SunHiNeural")).save(voice_path) | |
| # μμ μμ± (νμμμ λ°©μ§ λ‘μ§ ν¬ν¨) | |
| music_path = None | |
| if music_match: | |
| sr, audio = generate_music_safe(music_match.group(1).strip()) | |
| if sr is not None: | |
| music_path = f"/tmp/m_{member_name.split()[0]}.wav" | |
| scipy.io.wavfile.write(music_path, sr, audio) | |
| return tts_final, voice_path, music_path, tab_display | |
| except Exception as e: | |
| return f"System Error: {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() | |
| # β ν(Queue)λ₯Ό νμ±ννμ¬ μλ²κ° λκΈ°μ§ μκ² ν¨ | |
| btn = gr.Button("GO"); btn.click(band_consulting, [i1, i2, i3], [o1, o2, o3, o4], api_name="predict") | |
| # β queue()λ₯Ό λ°λμ λΆμ¬μΌ κΈ΄ μμ μ λΈλΌμ°μ κ° κΈ°λ€λ €μ€λλ€. | |
| demo.queue().launch() |