Spaces:
Sleeping
Sleeping
File size: 5,600 Bytes
da89e23 84daabb da89e23 84daabb da89e23 84daabb da89e23 84daabb da89e23 84daabb da89e23 84daabb f2609ff 84daabb f2609ff da89e23 f2609ff | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 | 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() |