Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,13 +1,59 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
|
| 3 |
import edge_tts
|
| 4 |
import os
|
|
|
|
| 5 |
|
| 6 |
-
# 1. API
|
| 7 |
GENAI_KEY = os.getenv("GEMINI_KEY")
|
| 8 |
-
|
| 9 |
|
| 10 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
MEMBERS = {
|
| 12 |
"์์ค (Korea)": {
|
| 13 |
"role": "๋ฉ์ธ ๋ณด์ปฌ",
|
|
@@ -17,37 +63,37 @@ MEMBERS = {
|
|
| 17 |
"Chloe (USA)": {
|
| 18 |
"role": "๋ฆฌ๋ ๊ธฐํ",
|
| 19 |
"voice": "en-US-AriaNeural",
|
| 20 |
-
"prompt": "You are 'Chloe'
|
| 21 |
},
|
| 22 |
"Beatrice (Brazil)": {
|
| 23 |
"role": "๋๋ผ",
|
| 24 |
"voice": "pt-BR-FranciscaNeural",
|
| 25 |
-
"prompt": "You are 'Beatrice'
|
| 26 |
},
|
| 27 |
"Naomi (Japan)": {
|
| 28 |
"role": "๋ฆฌ๋ฌ ๊ธฐํ",
|
| 29 |
"voice": "ja-JP-NanamiNeural",
|
| 30 |
-
"prompt": "You are 'Naomi'
|
| 31 |
},
|
| 32 |
"Elena (Spain)": {
|
| 33 |
"role": "๋ฒ ์ด์ค",
|
| 34 |
"voice": "es-ES-ElviraNeural",
|
| 35 |
-
"prompt": "You are 'Elena'
|
| 36 |
},
|
| 37 |
"Amira (Egypt)": {
|
| 38 |
"role": "ํค๋ณด๋",
|
| 39 |
"voice": "ar-EG-SalmaNeural",
|
| 40 |
-
"prompt": "You are 'Amira'
|
| 41 |
},
|
| 42 |
"Liwei (China)": {
|
| 43 |
"role": "DJ / FX",
|
| 44 |
"voice": "zh-CN-XiaoxiaoNeural",
|
| 45 |
-
"prompt": "You are 'Liwei'
|
| 46 |
},
|
| 47 |
"Sophie (France)": {
|
| 48 |
"role": "๋ฐ์ด์ฌ๋ฆฐ",
|
| 49 |
"voice": "fr-FR-DeniseNeural",
|
| 50 |
-
"prompt": "You are 'Sophie'
|
| 51 |
}
|
| 52 |
}
|
| 53 |
|
|
@@ -58,20 +104,16 @@ async def band_consulting(user_input, member_name):
|
|
| 58 |
|
| 59 |
member = MEMBERS[member_name]
|
| 60 |
|
| 61 |
-
# [Step 1] ๋: ๋ณธ๋ช
(gemini-1.5-flash-latest) ์ฌ์ฉ!
|
| 62 |
chat_prompt = f"{member['prompt']}\n\nUser's Error/Worry: {user_input}\nResponse:"
|
| 63 |
|
| 64 |
try:
|
| 65 |
-
#
|
| 66 |
-
response =
|
| 67 |
-
model='gemini-1.5-flash-latest',
|
| 68 |
-
contents=chat_prompt
|
| 69 |
-
)
|
| 70 |
ai_text = response.text
|
| 71 |
except Exception as e:
|
| 72 |
-
|
|
|
|
| 73 |
|
| 74 |
-
# [Step 2] ์
: Edge-TTS
|
| 75 |
try:
|
| 76 |
output_file = f"/tmp/{member_name}_reply.mp3"
|
| 77 |
communicate = edge_tts.Communicate(ai_text, member['voice'])
|
|
@@ -84,9 +126,9 @@ async def band_consulting(user_input, member_name):
|
|
| 84 |
except Exception as e:
|
| 85 |
return f"System Error: {str(e)}", None
|
| 86 |
|
| 87 |
-
# Blocks UI
|
| 88 |
with gr.Blocks() as demo:
|
| 89 |
-
gr.Markdown("# Error 404 Band API Server")
|
| 90 |
with gr.Row():
|
| 91 |
inp_text = gr.Textbox(label="๊ณ ๋ฏผ ์
๋ ฅ")
|
| 92 |
inp_member = gr.Dropdown(choices=list(MEMBERS.keys()), label="๋ฉค๋ฒ ์ ํ", value="์์ค (Korea)")
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import google.generativeai as genai
|
| 3 |
import edge_tts
|
| 4 |
import os
|
| 5 |
+
import re
|
| 6 |
|
| 7 |
+
# 1. ๊ตฌ๊ธ API ์ค์
|
| 8 |
GENAI_KEY = os.getenv("GEMINI_KEY")
|
| 9 |
+
genai.configure(api_key=GENAI_KEY)
|
| 10 |
|
| 11 |
+
# ==========================================
|
| 12 |
+
# ๐ต๏ธโโ๏ธ [์์กด์ ํ์] ์ฌ์ฉ ๊ฐ๋ฅํ ๋ชจ๋ธ ์๋ ์ฐพ๊ธฐ
|
| 13 |
+
# ==========================================
|
| 14 |
+
def get_alive_model():
|
| 15 |
+
print("๐ก ๊ตฌ๊ธ ์๋ฒ์ ์์กด ๋ชจ๋ธ์ ๋ฌธ์ํฉ๋๋ค...")
|
| 16 |
+
available_models = []
|
| 17 |
+
|
| 18 |
+
try:
|
| 19 |
+
# ๊ตฌ๊ธ์ ์๋ ๋ชจ๋ ๋ชจ๋ธ ๋ฆฌ์คํธ๋ฅผ ๊ฐ์ ธ์ต๋๋ค.
|
| 20 |
+
for m in genai.list_models():
|
| 21 |
+
if 'generateContent' in m.supported_generation_methods:
|
| 22 |
+
available_models.append(m.name)
|
| 23 |
+
|
| 24 |
+
print(f"๐ ๋ฐ๊ฒฌ๋ ๋ชจ๋ธ๋ค: {available_models}")
|
| 25 |
+
|
| 26 |
+
# ์ฐ์ ์์: ์ต์ ํ๋์ -> ํ๋ก -> ๋ ๊ฑฐ์ ์์๋ก ์ฐพ๊ธฐ
|
| 27 |
+
# (๋ฆฌ๋๋์ด ๋ง์ํ์ 2.5, 3.0์ด ์๋ค๋ฉด ์ฌ๊ธฐ์ ๊ฑธ๋ฆฝ๋๋ค!)
|
| 28 |
+
priority_keywords = [
|
| 29 |
+
"gemini-2.0-flash",
|
| 30 |
+
"gemini-1.5-flash",
|
| 31 |
+
"gemini-1.5-pro",
|
| 32 |
+
"gemini-pro"
|
| 33 |
+
]
|
| 34 |
+
|
| 35 |
+
for keyword in priority_keywords:
|
| 36 |
+
for model_name in available_models:
|
| 37 |
+
if keyword in model_name:
|
| 38 |
+
print(f"โ
์์กด ํ์ธ! [{model_name}] ๋ชจ๋ธ์ ์ฌ์ฉํฉ๋๋ค.")
|
| 39 |
+
return genai.GenerativeModel(model_name)
|
| 40 |
+
|
| 41 |
+
# ๋ง์ฝ ์ฐ์ ์์์ ์์ผ๋ฉด, ๋ชฉ๋ก์ ์ฒซ ๋ฒ์งธ gemini ๋ชจ๋ธ์ ์๋๋ค.
|
| 42 |
+
fallback = [m for m in available_models if "gemini" in m][0]
|
| 43 |
+
print(f"โ ๏ธ ์ฐ์ ์์ ๋ชจ๋ธ ์์. ๋์ฒด ๋ชจ๋ธ [{fallback}] ์ฌ์ฉ.")
|
| 44 |
+
return genai.GenerativeModel(fallback)
|
| 45 |
+
|
| 46 |
+
except Exception as e:
|
| 47 |
+
print(f"โ ๋ชจ๋ธ ํ์ ์คํจ: {str(e)}")
|
| 48 |
+
# ์ตํ์ ์๋จ: ๊ทธ๋ฅ ๋ฌธ์์ด๋ก ๋ฐ์๋ฒ๋ฆฌ๊ธฐ (Legacy)
|
| 49 |
+
return genai.GenerativeModel("gemini-pro")
|
| 50 |
+
|
| 51 |
+
# ์๋ฒ ์ผ์ง ๋ ๋ฑ ํ ๋ฒ ์คํํด์ ๋ชจ๋ธ์ ํ์ ํฉ๋๋ค.
|
| 52 |
+
model = get_alive_model()
|
| 53 |
+
|
| 54 |
+
# ==========================================
|
| 55 |
+
# 2. ๋ฉค๋ฒ ๋ฐ์ดํฐ (๋ณ๊ฒฝ ์์)
|
| 56 |
+
# ==========================================
|
| 57 |
MEMBERS = {
|
| 58 |
"์์ค (Korea)": {
|
| 59 |
"role": "๋ฉ์ธ ๋ณด์ปฌ",
|
|
|
|
| 63 |
"Chloe (USA)": {
|
| 64 |
"role": "๋ฆฌ๋ ๊ธฐํ",
|
| 65 |
"voice": "en-US-AriaNeural",
|
| 66 |
+
"prompt": "You are 'Chloe'. Respond in English. Personality: Energetic, cool rockstar. Tone: 'Hey! Pump up the volume!'"
|
| 67 |
},
|
| 68 |
"Beatrice (Brazil)": {
|
| 69 |
"role": "๋๋ผ",
|
| 70 |
"voice": "pt-BR-FranciscaNeural",
|
| 71 |
+
"prompt": "You are 'Beatrice'. Respond in English mixed with Portuguese. Personality: Passionate, loud. Tone: 'Vamos! Feel the beat!'"
|
| 72 |
},
|
| 73 |
"Naomi (Japan)": {
|
| 74 |
"role": "๋ฆฌ๋ฌ ๊ธฐํ",
|
| 75 |
"voice": "ja-JP-NanamiNeural",
|
| 76 |
+
"prompt": "You are 'Naomi'. Respond in Japanese. Personality: Calm, mysterious. Tone: 'ๅคงไธๅคซ (It's okay). Keep it steady.'"
|
| 77 |
},
|
| 78 |
"Elena (Spain)": {
|
| 79 |
"role": "๋ฒ ์ด์ค",
|
| 80 |
"voice": "es-ES-ElviraNeural",
|
| 81 |
+
"prompt": "You are 'Elena'. Respond in Spanish mixed with English. Personality: Sexy, mature. Tone: 'Hola. Feel the bass.'"
|
| 82 |
},
|
| 83 |
"Amira (Egypt)": {
|
| 84 |
"role": "ํค๋ณด๋",
|
| 85 |
"voice": "ar-EG-SalmaNeural",
|
| 86 |
+
"prompt": "You are 'Amira'. Respond in Arabic mixed with English. Personality: Dreamy, mystical. Tone: 'Salam. Like a desert wind...'"
|
| 87 |
},
|
| 88 |
"Liwei (China)": {
|
| 89 |
"role": "DJ / FX",
|
| 90 |
"voice": "zh-CN-XiaoxiaoNeural",
|
| 91 |
+
"prompt": "You are 'Liwei'. Respond in Chinese mixed with English. Personality: Trendy Gen-Z. Tone: 'ๅฌ็ (Listen)! Glitch is art.'"
|
| 92 |
},
|
| 93 |
"Sophie (France)": {
|
| 94 |
"role": "๋ฐ์ด์ฌ๋ฆฐ",
|
| 95 |
"voice": "fr-FR-DeniseNeural",
|
| 96 |
+
"prompt": "You are 'Sophie'. Respond in French mixed with English. Personality: Elegant, artistic. Tone: 'C'est magnifique.'"
|
| 97 |
}
|
| 98 |
}
|
| 99 |
|
|
|
|
| 104 |
|
| 105 |
member = MEMBERS[member_name]
|
| 106 |
|
|
|
|
| 107 |
chat_prompt = f"{member['prompt']}\n\nUser's Error/Worry: {user_input}\nResponse:"
|
| 108 |
|
| 109 |
try:
|
| 110 |
+
# ํ์ ๋ ๋ชจ๋ธ๋ก ์์ฑ ์๋
|
| 111 |
+
response = model.generate_content(chat_prompt)
|
|
|
|
|
|
|
|
|
|
| 112 |
ai_text = response.text
|
| 113 |
except Exception as e:
|
| 114 |
+
# ๋ชจ๋ธ์ด ์์ด๋ Quota(ํ๋) ์๋ฌ๊ฐ ๋ ์ ์์ -> ์๋ฌ ๋ฉ์์ง ๋ฐํ
|
| 115 |
+
return f"Error (Gemini): ๋ชจ๋ธ ์ฐ๊ฒฐ ์คํจ.\n{str(e)}", None
|
| 116 |
|
|
|
|
| 117 |
try:
|
| 118 |
output_file = f"/tmp/{member_name}_reply.mp3"
|
| 119 |
communicate = edge_tts.Communicate(ai_text, member['voice'])
|
|
|
|
| 126 |
except Exception as e:
|
| 127 |
return f"System Error: {str(e)}", None
|
| 128 |
|
| 129 |
+
# Blocks UI
|
| 130 |
with gr.Blocks() as demo:
|
| 131 |
+
gr.Markdown("# Error 404 Band API Server (Auto-Discovery Mode)")
|
| 132 |
with gr.Row():
|
| 133 |
inp_text = gr.Textbox(label="๊ณ ๋ฏผ ์
๋ ฅ")
|
| 134 |
inp_member = gr.Dropdown(choices=list(MEMBERS.keys()), label="๋ฉค๋ฒ ์ ํ", value="์์ค (Korea)")
|