error404-api / app.py
benjamin5607's picture
Update app.py
84daabb verified
Raw History Blame
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()