Spaces:
Sleeping
Sleeping
Download app.py from benjamin5607/error404-api: direct link, hf CLI and curl.
- Browser
- Download file 4.16 kB
-
https://huggingface.co/spaces/benjamin5607/error404-api/resolve/bc8d2c41b578c3fe0c4db30d9f3e8cab2d9d42da/app.py
- Command line
-
hf download hf://spaces/benjamin5607/error404-api@bc8d2c41b578c3fe0c4db30d9f3e8cab2d9d42da/app.py
-
curl -L -o app.py https://huggingface.co/spaces/benjamin5607/error404-api/resolve/bc8d2c41b578c3fe0c4db30d9f3e8cab2d9d42da/app.py
4.16 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 | |
| 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" | |
| } | |
| 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}" | |
| # β μ°¨λ¨ λ°©μ§: λͺ¨λ μμ μΉ΄ν κ³ λ¦¬ BLOCK_NONE μ€μ | |
| payload = { | |
| "contents": [{"parts": [{"text": f"{system_instruction}\nμ§λ¬Έ: {user_input}"}]}], | |
| "safetySettings": [ | |
| {"category": "HARM_CATEGORY_HARASSMENT", "threshold": "BLOCK_NONE"}, | |
| {"category": "HARM_CATEGORY_HATE_SPEECH", "threshold": "BLOCK_NONE"}, | |
| {"category": "HARM_CATEGORY_SEXUALLY_EXPLICIT", "threshold": "BLOCK_NONE"}, | |
| {"category": "HARM_CATEGORY_DANGEROUS_CONTENT", "threshold": "BLOCK_NONE"} | |
| ], | |
| "generationConfig": { | |
| "temperature": 0.7, | |
| "maxOutputTokens": 1024 | |
| } | |
| } | |
| response = requests.post(url, json=payload) | |
| res_data = response.json() | |
| # 'candidates'κ° μμ κ²½μ°λ₯Ό μν μμΈ μ²λ¦¬ κ°ν | |
| if 'candidates' not in res_data or not res_data['candidates']: | |
| # νν°λ§λ κ²½μ°λ₯Ό λλΉν κΈ°λ³Έ λ΅λ³ μΈνΈ | |
| fallback_text = f"μ΄λ΄ 리λ, μ§κΈ λ΄ μκ°μ΄ μ μ μ°¨λ¨λμ΄. λ€μ λ¬Όμ΄λ΄μ€λ? [TAB] λ€μ μλν΄μ£ΌμΈμ. [MUSIC: powerful rock riff]" | |
| ai_text_raw = fallback_text | |
| else: | |
| ai_text_raw = res_data['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() | |
| # 5μ€ μμ½ | |
| clean_text = "\n".join(clean_text.split("\n")[:5]) | |
| voice_path = f"/tmp/v_{member_name.split()[0]}.mp3" | |
| await edge_tts.Communicate(clean_text, MEMBERS.get(member_name, "ko-KR-SunHiNeural")).save(voice_path) | |
| music_path = None | |
| if music_match and music_synthesiser: | |
| p = music_match.group(1).strip() | |
| music_output = music_synthesiser(p, forward_params={"max_new_tokens": 512}) | |
| 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 clean_text, voice_path, music_path, tab_display | |
| except Exception as e: | |
| return f"System Reset Needed: {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() |