Spaces:
Sleeping
Sleeping
Download app.py from benjamin5607/error404-api: direct link, hf CLI and curl.
- Browser
- Download file 5.38 kB
-
https://huggingface.co/spaces/benjamin5607/error404-api/resolve/d4006330614842e9b6a26a657bfbf468fffebf8c/app.py
- Command line
-
hf download hf://spaces/benjamin5607/error404-api@d4006330614842e9b6a26a657bfbf468fffebf8c/app.py
-
curl -L -o app.py https://huggingface.co/spaces/benjamin5607/error404-api/resolve/d4006330614842e9b6a26a657bfbf468fffebf8c/app.py
5.38 kB
| import gradio as gr | |
| import requests | |
| import edge_tts | |
| import os | |
| import re | |
| import scipy.io.wavfile | |
| import numpy as np | |
| from transformers import pipeline | |
| GENAI_KEY = os.getenv("GEMINI_KEY") | |
| # μμ μμ± λͺ¨λΈ λ‘λ (Hugging Face νκ²½ μ΅μ ν) | |
| try: | |
| music_synthesiser = pipeline("text-to-audio", "facebook/musicgen-small") | |
| except Exception as e: | |
| print(f"Music Model Load Error: {e}") | |
| music_synthesiser = None | |
| # 8μΈ λ©€λ² λ³΄μ΄μ€ λ°μ΄ν° λ° ν둬ννΈ (μ λ μ μ§) | |
| MEMBERS = { | |
| "μμ€ (Korea)": { "voice": "ko-KR-SunHiNeural", "prompt": "λΉμ μ 보컬 'μμ€'μ λλ€. μλ컬νμ§λ§ μμ μ μ§μ¬μ λλ€." }, | |
| "Chloe (USA)": { "voice": "en-US-AriaNeural", "prompt": "You are 'Chloe', the lead guitarist. Cool rockstar vibe." }, | |
| "Beatrice (Brazil)": { "voice": "pt-BR-FranciscaNeural", "prompt": "You are 'Beatrice', the drummer. Focus on rhythm." }, | |
| "Naomi (Japan)": { "voice": "ja-JP-NanamiNeural", "prompt": "You are 'Naomi'. Focus on harmony." }, | |
| "Elena (Spain)": { "voice": "es-ES-ElviraNeural", "prompt": "You are 'Elena'. Focus on groove." }, | |
| "Amira (Egypt)": { "voice": "ar-EG-SalmaNeural", "prompt": "You are 'Amira'. Focus on atmosphere." }, | |
| "Liwei (China)": { "voice": "zh-CN-XiaoxiaoNeural", "prompt": "You are 'Liwei'. Focus on sound design." }, | |
| "Sophie (France)": { "voice": "fr-FR-DeniseNeural", "prompt": "You are 'Sophie'. Focus on melody." } | |
| } | |
| async def band_consulting(user_input, member_name, lang_code): | |
| try: | |
| member_data = MEMBERS.get(member_name, MEMBERS["μμ€ (Korea)"]) | |
| 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_data['prompt']} | |
| [LANGUAGE RULE] Respond 100% in {target_lang}. No other languages. | |
| [STYLE RULE] 1. Verbal response MUST be under 5 lines (Concise). | |
| 2. Put ALL detailed musical explanations, chord progressions, and tips in [TAB]. | |
| 3. End with [MUSIC: English prompt] for a high-quality rock sample. | |
| """ | |
| 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}"}]}], | |
| "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"} | |
| ] | |
| } | |
| response = requests.post(url, json=payload) | |
| res_data = response.json() | |
| if 'candidates' not in res_data: | |
| clean_text = "μκ°μ΄ μ μ μ°¨λ¨λμ΄. λ€μ λ¬Όμ΄λ΄μ€!" | |
| tab_display = "νν°λ§μΌλ‘ μΈν΄ λ΄μ©μ νμν μ μμ΅λλ€." | |
| music_p = "heavy metal rock riff" | |
| 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 "No Score" | |
| # TTSμμ μ 보 μ½κΈ° λ°©μ§ | |
| clean_text = re.sub(r'\[TAB\].*?(\[|$)', '', ai_text_raw, flags=re.DOTALL) | |
| clean_text = re.sub(r'\[MUSIC:.*?\]', '', clean_text).strip() | |
| clean_text = "\n".join(clean_text.split('\n')[:5]) | |
| music_p = music_match.group(1).strip() if music_match else "rock guitar riff" | |
| # μμ± μμ± | |
| tts_input = re.sub(r'[\*\#\-\_\~\|]', '', clean_text) | |
| voice_path = f"/tmp/v_{member_name.split()[0]}.mp3" | |
| await edge_tts.Communicate(tts_input, member_data['voice']).save(voice_path) | |
| # μμ μμ± (μλ¬ λ°©μ§ κ°ν) | |
| music_path = None | |
| if music_synthesiser: | |
| try: | |
| # νν°λ§ μ°ν | |
| safe_p = music_p.lower() | |
| if any(x in safe_p for x in ['laugh', 'vocal', 'voice', 'human']): | |
| safe_p = "distorted electric guitar, rock style" | |
| music_output = music_synthesiser(safe_p, forward_params={"max_new_tokens": 512}) | |
| if music_output and "audio" in music_output: | |
| 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) | |
| except: | |
| music_path = None | |
| return clean_text, voice_path, music_path, tab_display | |
| except Exception as e: | |
| return f"System Overload: {str(e)}", None, None, "Error" | |
| with gr.Blocks() as demo: | |
| with gr.Row(): | |
| 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() |