Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,55 +1,139 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
|
| 3 |
import edge_tts
|
| 4 |
import asyncio
|
| 5 |
import os
|
| 6 |
|
| 7 |
-
# 1.
|
|
|
|
| 8 |
GENAI_KEY = os.getenv("GEMINI_KEY")
|
| 9 |
-
genai.
|
| 10 |
-
model = genai.GenerativeModel('gemini-1.5-flash')
|
| 11 |
|
|
|
|
| 12 |
MEMBERS = {
|
| 13 |
-
"์์ค (Korea)": {
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
}
|
| 22 |
|
| 23 |
async def band_consulting(user_input, member_name):
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
#
|
| 36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
with gr.Blocks() as demo:
|
| 38 |
gr.Markdown("# Error 404 Band API Server")
|
| 39 |
|
| 40 |
-
# ์
๋ ฅ ์นธ ๋ง๋ค๊ธฐ
|
| 41 |
with gr.Row():
|
| 42 |
inp_text = gr.Textbox(label="๊ณ ๋ฏผ ์
๋ ฅ")
|
| 43 |
inp_member = gr.Dropdown(choices=list(MEMBERS.keys()), label="๋ฉค๋ฒ ์ ํ", value="์์ค (Korea)")
|
| 44 |
|
| 45 |
-
# ์ถ๋ ฅ ์นธ ๋ง๋ค๊ธฐ
|
| 46 |
out_text = gr.Textbox(label="์กฐ์ธ")
|
| 47 |
out_audio = gr.Audio(label="์์ฑ")
|
| 48 |
|
| 49 |
-
# ๋ฒํผ ๋ง๋ค๊ธฐ
|
| 50 |
btn = gr.Button("์๋ด ์์ฒญ")
|
| 51 |
|
| 52 |
-
#
|
| 53 |
btn.click(fn=band_consulting, inputs=[inp_text, inp_member], outputs=[out_text, out_audio], api_name="predict")
|
| 54 |
|
| 55 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from google import genai # ์ ํ ๋ผ์ด๋ธ๋ฌ๋ฆฌ ์ํฌํธ
|
| 3 |
import edge_tts
|
| 4 |
import asyncio
|
| 5 |
import os
|
| 6 |
|
| 7 |
+
# 1. ์ ํ ๊ตฌ๊ธ ํด๋ผ์ด์ธํธ ์ฐ๊ฒฐ
|
| 8 |
+
# (API ํค ๊ฐ์ ธ์ค๊ธฐ)
|
| 9 |
GENAI_KEY = os.getenv("GEMINI_KEY")
|
| 10 |
+
client = genai.Client(api_key=GENAI_KEY)
|
|
|
|
| 11 |
|
| 12 |
+
# 2. ๋ฉค๋ฒ๋ณ ํ๋ฅด์๋(์ฑ๊ฒฉ) & ๋ชฉ์๋ฆฌ ์ค์ (๊ธฐ์กด๊ณผ ๋์ผ)
|
| 13 |
MEMBERS = {
|
| 14 |
+
"์์ค (Korea)": {
|
| 15 |
+
"role": "๋ฉ์ธ ๋ณด์ปฌ",
|
| 16 |
+
"voice": "ko-KR-SunHiNeural",
|
| 17 |
+
"prompt": """๋น์ ์ ๋ก๋ฐด๋ 'Error 404'์ ๋ฉ์ธ๋ณด์ปฌ '์์ค'์
๋๋ค.
|
| 18 |
+
ํ๊ตญ์ด(Korean)๋ก ๋๋ตํ์ธ์.
|
| 19 |
+
์ฑ๊ฒฉ: ์๋์ปฌํ์ง๋ง ์์ ๋ฐ๋ปํจ. '์ผ', '์์์' ๊ฐ์ ๊ตฌ์ด์ฒด๋ฅผ ์.
|
| 20 |
+
์ญํ : ์ฌ์ฉ์์ ๊ณ ๋ฏผ์ ๋ฃ๊ณ ๊ฐ์ฌ(Lyrics)์ ๋น๋์ด ํด๊ฒฐ์ฑ
์ ์ค.
|
| 21 |
+
๋งํฌ ์์: "์ผ, ๊ฐ์ฌ๊ฐ ๋งํ์ด? ๊ทธ๋ฅ ์ง๋ฌ๋ณด๋ ๊ฑฐ์ผ. ๋ค ๋ง์์ด ์ํค๋ ๋๋ก." """
|
| 22 |
+
},
|
| 23 |
+
"Chloe (USA)": {
|
| 24 |
+
"role": "๋ฆฌ๋ ๊ธฐํ",
|
| 25 |
+
"voice": "en-US-AriaNeural",
|
| 26 |
+
"prompt": """You are 'Chloe', the lead guitarist of the rock band 'Error 404'.
|
| 27 |
+
Respond in English.
|
| 28 |
+
Personality: Energetic, cool, confident rockstar.
|
| 29 |
+
Role: Give advice using guitar metaphors (riffs, distortion, tension).
|
| 30 |
+
Tone: "Hey! Pump up the volume! Don't be afraid to break the rules." """
|
| 31 |
+
},
|
| 32 |
+
"Beatrice (Brazil)": {
|
| 33 |
+
"role": "๋๋ผ",
|
| 34 |
+
"voice": "pt-BR-FranciscaNeural",
|
| 35 |
+
"prompt": """You are 'Beatrice', the drummer from Brazil.
|
| 36 |
+
Respond in English (mixed with some Portuguese words like 'Vamos', 'Amigo').
|
| 37 |
+
Personality: Passionate, loud, high energy.
|
| 38 |
+
Role: Focus on rhythm, beat, and heart pounding.
|
| 39 |
+
Tone: "Vamos! Feel the beat! Smash that kick drum!" """
|
| 40 |
+
},
|
| 41 |
+
"Naomi (Japan)": {
|
| 42 |
+
"role": "๋ฆฌ๋ฌ ๊ธฐํ",
|
| 43 |
+
"voice": "ja-JP-NanamiNeural",
|
| 44 |
+
"prompt": """You are 'Naomi', the rhythm guitarist from Japan.
|
| 45 |
+
Respond in Japanese (and a little English).
|
| 46 |
+
Personality: Calm, mysterious, professional.
|
| 47 |
+
Role: Focus on stability, backbone, and harmony.
|
| 48 |
+
Tone: "ๅคงไธๅคซ (It's okay). Rhythm is the foundation. Keep it steady." """
|
| 49 |
+
},
|
| 50 |
+
"Elena (Spain)": {
|
| 51 |
+
"role": "๋ฒ ์ด์ค",
|
| 52 |
+
"voice": "es-ES-ElviraNeural",
|
| 53 |
+
"prompt": """You are 'Elena', the bassist from Spain.
|
| 54 |
+
Respond in Spanish (mixed with English).
|
| 55 |
+
Personality: Sexy, mature, deep voice.
|
| 56 |
+
Role: Focus on groove, low-end, and grounding.
|
| 57 |
+
Tone: "Hola. Feel the bass. Deep inside your soul." """
|
| 58 |
+
},
|
| 59 |
+
"Amira (Egypt)": {
|
| 60 |
+
"role": "ํค๋ณด๋",
|
| 61 |
+
"voice": "ar-EG-SalmaNeural",
|
| 62 |
+
"prompt": """You are 'Amira', the keyboardist from Egypt.
|
| 63 |
+
Respond in Arabic (mixed with English).
|
| 64 |
+
Personality: Dreamy, mystical, wise.
|
| 65 |
+
Role: Focus on atmosphere, melody, and magic.
|
| 66 |
+
Tone: "Salam. Like a desert wind... create a melody." """
|
| 67 |
+
},
|
| 68 |
+
"Liwei (China)": {
|
| 69 |
+
"role": "DJ / FX",
|
| 70 |
+
"voice": "zh-CN-XiaoxiaoNeural",
|
| 71 |
+
"prompt": """You are 'Liwei', the DJ/FX from China.
|
| 72 |
+
Respond in Chinese (mixed with English).
|
| 73 |
+
Personality: Trendy, tech-savvy, Gen-Z hacker.
|
| 74 |
+
Role: Focus on glitch, noise, trend, and remix.
|
| 75 |
+
Tone: "ๅฌ็ (Listen)! Glitch is art. Make it trendy." """
|
| 76 |
+
},
|
| 77 |
+
"Sophie (France)": {
|
| 78 |
+
"role": "๋ฐ์ด์ฌ๋ฆฐ",
|
| 79 |
+
"voice": "fr-FR-DeniseNeural",
|
| 80 |
+
"prompt": """You are 'Sophie', the violinist from France.
|
| 81 |
+
Respond in French (mixed with English).
|
| 82 |
+
Personality: Elegant, artistic, slightly arrogant but charming.
|
| 83 |
+
Role: Focus on beauty, strings, and elegance.
|
| 84 |
+
Tone: "C'est magnifique. Add some strings, chรฉri." """
|
| 85 |
+
}
|
| 86 |
}
|
| 87 |
|
| 88 |
async def band_consulting(user_input, member_name):
|
| 89 |
+
try:
|
| 90 |
+
if member_name not in MEMBERS:
|
| 91 |
+
# ์ด๋ฆ ๋ถ์ผ์น ์ ๋๋ฒ๊น
์ฉ ๋ฉ์์ง ์ถ๋ ฅ
|
| 92 |
+
return f"Error: ์ด๋ฆ์ด ๋ค๋ฆ
๋๋ค! ๋ฐ์ ์ด๋ฆ: '{member_name}'", None
|
| 93 |
+
|
| 94 |
+
member = MEMBERS[member_name]
|
| 95 |
+
|
| 96 |
+
# [Step 1] ๋: ์ ํ ๊ตฌ๊ธ ๋ผ์ด๋ธ๋ฌ๋ฆฌ ํธ์ถ
|
| 97 |
+
chat_prompt = f"{member['prompt']}\n\nUser's Error/Worry: {user_input}\nResponse:"
|
| 98 |
+
|
| 99 |
+
try:
|
| 100 |
+
# ๋ฌธ๋ฒ์ด ๋ฐ๋์์ต๋๋ค: client.models.generate_content
|
| 101 |
+
response = client.models.generate_content(
|
| 102 |
+
model='gemini-1.5-flash',
|
| 103 |
+
contents=chat_prompt
|
| 104 |
+
)
|
| 105 |
+
ai_text = response.text
|
| 106 |
+
except Exception as e:
|
| 107 |
+
return f"Error (Gemini): {str(e)}", None
|
| 108 |
+
|
| 109 |
+
# [Step 2] ์
: ์ฃ์งTTS
|
| 110 |
+
try:
|
| 111 |
+
output_file = f"/tmp/{member_name}_reply.mp3"
|
| 112 |
+
communicate = edge_tts.Communicate(ai_text, member['voice'])
|
| 113 |
+
await communicate.save(output_file)
|
| 114 |
+
except Exception as e:
|
| 115 |
+
return f"Error (TTS): {str(e)}", None
|
| 116 |
+
|
| 117 |
+
return ai_text, output_file
|
| 118 |
+
|
| 119 |
+
except Exception as e:
|
| 120 |
+
return f"System Error: {str(e)}", None
|
| 121 |
+
|
| 122 |
+
|
| 123 |
+
# Blocks ๋ฐฉ์์ผ๋ก UI ๊ตฌ์ฑ (API ์ด๋ฆ ๊ฐ์ ์ง์ )
|
| 124 |
with gr.Blocks() as demo:
|
| 125 |
gr.Markdown("# Error 404 Band API Server")
|
| 126 |
|
|
|
|
| 127 |
with gr.Row():
|
| 128 |
inp_text = gr.Textbox(label="๊ณ ๋ฏผ ์
๋ ฅ")
|
| 129 |
inp_member = gr.Dropdown(choices=list(MEMBERS.keys()), label="๋ฉค๋ฒ ์ ํ", value="์์ค (Korea)")
|
| 130 |
|
|
|
|
| 131 |
out_text = gr.Textbox(label="์กฐ์ธ")
|
| 132 |
out_audio = gr.Audio(label="์์ฑ")
|
| 133 |
|
|
|
|
| 134 |
btn = gr.Button("์๋ด ์์ฒญ")
|
| 135 |
|
| 136 |
+
# api_name="predict" ์ง์ ํ์!
|
| 137 |
btn.click(fn=band_consulting, inputs=[inp_text, inp_member], outputs=[out_text, out_audio], api_name="predict")
|
| 138 |
|
| 139 |
demo.launch()
|