Spaces:
Sleeping
Sleeping
Download app.py from benjamin5607/error404-api: direct link, hf CLI and curl.
- Browser
- Download file 5.6 kB
-
https://huggingface.co/spaces/benjamin5607/error404-api/resolve/84daabb92cd7d1d9609dae066ca7c54886a103c7/app.py
- Command line
-
hf download hf://spaces/benjamin5607/error404-api@84daabb92cd7d1d9609dae066ca7c54886a103c7/app.py
-
curl -L -o app.py https://huggingface.co/spaces/benjamin5607/error404-api/resolve/84daabb92cd7d1d9609dae066ca7c54886a103c7/app.py
5.6 kB
| import gradio as gr | |
| from google import genai # ์ ํ ๋ผ์ด๋ธ๋ฌ๋ฆฌ ์ํฌํธ | |
| import edge_tts | |
| import asyncio | |
| import os | |
| # 1. ์ ํ ๊ตฌ๊ธ ํด๋ผ์ด์ธํธ ์ฐ๊ฒฐ | |
| # (API ํค ๊ฐ์ ธ์ค๊ธฐ) | |
| GENAI_KEY = os.getenv("GEMINI_KEY") | |
| client = genai.Client(api_key=GENAI_KEY) | |
| # 2. ๋ฉค๋ฒ๋ณ ํ๋ฅด์๋(์ฑ๊ฒฉ) & ๋ชฉ์๋ฆฌ ์ค์ (๊ธฐ์กด๊ณผ ๋์ผ) | |
| MEMBERS = { | |
| "์์ค (Korea)": { | |
| "role": "๋ฉ์ธ ๋ณด์ปฌ", | |
| "voice": "ko-KR-SunHiNeural", | |
| "prompt": """๋น์ ์ ๋ก๋ฐด๋ 'Error 404'์ ๋ฉ์ธ๋ณด์ปฌ '์์ค'์ ๋๋ค. | |
| ํ๊ตญ์ด(Korean)๋ก ๋๋ตํ์ธ์. | |
| ์ฑ๊ฒฉ: ์๋์ปฌํ์ง๋ง ์์ ๋ฐ๋ปํจ. '์ผ', '์์์' ๊ฐ์ ๊ตฌ์ด์ฒด๋ฅผ ์. | |
| ์ญํ : ์ฌ์ฉ์์ ๊ณ ๋ฏผ์ ๋ฃ๊ณ ๊ฐ์ฌ(Lyrics)์ ๋น๋์ด ํด๊ฒฐ์ฑ ์ ์ค. | |
| ๋งํฌ ์์: "์ผ, ๊ฐ์ฌ๊ฐ ๋งํ์ด? ๊ทธ๋ฅ ์ง๋ฌ๋ณด๋ ๊ฑฐ์ผ. ๋ค ๋ง์์ด ์ํค๋ ๋๋ก." """ | |
| }, | |
| "Chloe (USA)": { | |
| "role": "๋ฆฌ๋ ๊ธฐํ", | |
| "voice": "en-US-AriaNeural", | |
| "prompt": """You are 'Chloe', the lead guitarist of the rock band 'Error 404'. | |
| Respond in English. | |
| Personality: Energetic, cool, confident rockstar. | |
| Role: Give advice using guitar metaphors (riffs, distortion, tension). | |
| Tone: "Hey! Pump up the volume! Don't be afraid to break the rules." """ | |
| }, | |
| "Beatrice (Brazil)": { | |
| "role": "๋๋ผ", | |
| "voice": "pt-BR-FranciscaNeural", | |
| "prompt": """You are 'Beatrice', the drummer from Brazil. | |
| Respond in English (mixed with some Portuguese words like 'Vamos', 'Amigo'). | |
| Personality: Passionate, loud, high energy. | |
| Role: Focus on rhythm, beat, and heart pounding. | |
| Tone: "Vamos! Feel the beat! Smash that kick drum!" """ | |
| }, | |
| "Naomi (Japan)": { | |
| "role": "๋ฆฌ๋ฌ ๊ธฐํ", | |
| "voice": "ja-JP-NanamiNeural", | |
| "prompt": """You are 'Naomi', the rhythm guitarist from Japan. | |
| Respond in Japanese (and a little English). | |
| Personality: Calm, mysterious, professional. | |
| Role: Focus on stability, backbone, and harmony. | |
| Tone: "ๅคงไธๅคซ (It's okay). Rhythm is the foundation. Keep it steady." """ | |
| }, | |
| "Elena (Spain)": { | |
| "role": "๋ฒ ์ด์ค", | |
| "voice": "es-ES-ElviraNeural", | |
| "prompt": """You are 'Elena', the bassist from Spain. | |
| Respond in Spanish (mixed with English). | |
| Personality: Sexy, mature, deep voice. | |
| Role: Focus on groove, low-end, and grounding. | |
| Tone: "Hola. Feel the bass. Deep inside your soul." """ | |
| }, | |
| "Amira (Egypt)": { | |
| "role": "ํค๋ณด๋", | |
| "voice": "ar-EG-SalmaNeural", | |
| "prompt": """You are 'Amira', the keyboardist from Egypt. | |
| Respond in Arabic (mixed with English). | |
| Personality: Dreamy, mystical, wise. | |
| Role: Focus on atmosphere, melody, and magic. | |
| Tone: "Salam. Like a desert wind... create a melody." """ | |
| }, | |
| "Liwei (China)": { | |
| "role": "DJ / FX", | |
| "voice": "zh-CN-XiaoxiaoNeural", | |
| "prompt": """You are 'Liwei', the DJ/FX from China. | |
| Respond in Chinese (mixed with English). | |
| Personality: Trendy, tech-savvy, Gen-Z hacker. | |
| Role: Focus on glitch, noise, trend, and remix. | |
| Tone: "ๅฌ็ (Listen)! Glitch is art. Make it trendy." """ | |
| }, | |
| "Sophie (France)": { | |
| "role": "๋ฐ์ด์ฌ๋ฆฐ", | |
| "voice": "fr-FR-DeniseNeural", | |
| "prompt": """You are 'Sophie', the violinist from France. | |
| Respond in French (mixed with English). | |
| Personality: Elegant, artistic, slightly arrogant but charming. | |
| Role: Focus on beauty, strings, and elegance. | |
| Tone: "C'est magnifique. Add some strings, chรฉri." """ | |
| } | |
| } | |
| async def band_consulting(user_input, member_name): | |
| try: | |
| if member_name not in MEMBERS: | |
| # ์ด๋ฆ ๋ถ์ผ์น ์ ๋๋ฒ๊น ์ฉ ๋ฉ์์ง ์ถ๋ ฅ | |
| return f"Error: ์ด๋ฆ์ด ๋ค๋ฆ ๋๋ค! ๋ฐ์ ์ด๋ฆ: '{member_name}'", None | |
| member = MEMBERS[member_name] | |
| # [Step 1] ๋: ์ ํ ๊ตฌ๊ธ ๋ผ์ด๋ธ๋ฌ๋ฆฌ ํธ์ถ | |
| chat_prompt = f"{member['prompt']}\n\nUser's Error/Worry: {user_input}\nResponse:" | |
| try: | |
| # ๋ฌธ๋ฒ์ด ๋ฐ๋์์ต๋๋ค: client.models.generate_content | |
| response = client.models.generate_content( | |
| model='gemini-1.5-flash', | |
| contents=chat_prompt | |
| ) | |
| ai_text = response.text | |
| except Exception as e: | |
| return f"Error (Gemini): {str(e)}", None | |
| # [Step 2] ์ : ์ฃ์งTTS | |
| try: | |
| output_file = f"/tmp/{member_name}_reply.mp3" | |
| communicate = edge_tts.Communicate(ai_text, member['voice']) | |
| await communicate.save(output_file) | |
| except Exception as e: | |
| return f"Error (TTS): {str(e)}", None | |
| return ai_text, output_file | |
| except Exception as e: | |
| return f"System Error: {str(e)}", None | |
| # Blocks ๋ฐฉ์์ผ๋ก UI ๊ตฌ์ฑ (API ์ด๋ฆ ๊ฐ์ ์ง์ ) | |
| with gr.Blocks() as demo: | |
| gr.Markdown("# Error 404 Band API Server") | |
| with gr.Row(): | |
| inp_text = gr.Textbox(label="๊ณ ๋ฏผ ์ ๋ ฅ") | |
| inp_member = gr.Dropdown(choices=list(MEMBERS.keys()), label="๋ฉค๋ฒ ์ ํ", value="์์ค (Korea)") | |
| out_text = gr.Textbox(label="์กฐ์ธ") | |
| out_audio = gr.Audio(label="์์ฑ") | |
| btn = gr.Button("์๋ด ์์ฒญ") | |
| # api_name="predict" ์ง์ ํ์! | |
| btn.click(fn=band_consulting, inputs=[inp_text, inp_member], outputs=[out_text, out_audio], api_name="predict") | |
| demo.launch() |