Spaces:
Sleeping
Sleeping
Download app.py from benjamin5607/error404-api: direct link, hf CLI and curl.
- Browser
- Download file 3.72 kB
-
https://huggingface.co/spaces/benjamin5607/error404-api/resolve/eef58422ffa424b4ddae204c4f69de11fc0233ed/app.py
- Command line
-
hf download hf://spaces/benjamin5607/error404-api@eef58422ffa424b4ddae204c4f69de11fc0233ed/app.py
-
curl -L -o app.py https://huggingface.co/spaces/benjamin5607/error404-api/resolve/eef58422ffa424b4ddae204c4f69de11fc0233ed/app.py
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() |