Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,60 +1,66 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import requests
|
| 3 |
import edge_tts
|
| 4 |
import os
|
| 5 |
import json
|
| 6 |
|
| 7 |
-
# 1. API ํค
|
| 8 |
GENAI_KEY = os.getenv("GEMINI_KEY")
|
| 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 |
headers = {'Content-Type': 'application/json'}
|
| 60 |
data = {
|
|
@@ -68,8 +74,21 @@ def call_gemini_direct(prompt):
|
|
| 68 |
if response.status_code == 200:
|
| 69 |
return response.json()['candidates'][0]['content']['parts'][0]['text']
|
| 70 |
else:
|
| 71 |
-
# ์๋ฌ
|
| 72 |
-
raise Exception(f"Google API Error {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
|
| 74 |
async def band_consulting(user_input, member_name):
|
| 75 |
try:
|
|
@@ -77,16 +96,18 @@ async def band_consulting(user_input, member_name):
|
|
| 77 |
return f"Error: ์ด๋ฆ ๋ถ์ผ์น! ('{member_name}')", None
|
| 78 |
|
| 79 |
member = MEMBERS[member_name]
|
| 80 |
-
|
| 81 |
-
# [Step 1] ๋: ์ง์ ํต์ ํจ์ ํธ์ถ
|
| 82 |
full_prompt = f"{member['prompt']}\n\nUser's Error/Worry: {user_input}\nResponse:"
|
| 83 |
|
| 84 |
try:
|
| 85 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
except Exception as e:
|
| 87 |
-
return f"Error (Gemini
|
| 88 |
|
| 89 |
-
# [Step 2] ์
: Edge-TTS
|
| 90 |
try:
|
| 91 |
output_file = f"/tmp/{member_name}_reply.mp3"
|
| 92 |
communicate = edge_tts.Communicate(ai_text, member['voice'])
|
|
@@ -99,17 +120,15 @@ async def band_consulting(user_input, member_name):
|
|
| 99 |
except Exception as e:
|
| 100 |
return f"System Error: {str(e)}", None
|
| 101 |
|
| 102 |
-
#
|
| 103 |
with gr.Blocks() as demo:
|
| 104 |
-
gr.Markdown("# Error 404 Band API Server (
|
| 105 |
with gr.Row():
|
| 106 |
inp_text = gr.Textbox(label="๊ณ ๋ฏผ ์
๋ ฅ")
|
| 107 |
inp_member = gr.Dropdown(choices=list(MEMBERS.keys()), label="๋ฉค๋ฒ ์ ํ", value="์์ค (Korea)")
|
| 108 |
out_text = gr.Textbox(label="์กฐ์ธ")
|
| 109 |
out_audio = gr.Audio(label="์์ฑ")
|
| 110 |
btn = gr.Button("์๋ด ์์ฒญ")
|
| 111 |
-
|
| 112 |
-
# API ์ด๋ฆ 'predict' ๊ฐ์ ์ง์
|
| 113 |
btn.click(fn=band_consulting, inputs=[inp_text, inp_member], outputs=[out_text, out_audio], api_name="predict")
|
| 114 |
|
| 115 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import requests
|
| 3 |
import edge_tts
|
| 4 |
import os
|
| 5 |
import json
|
| 6 |
|
| 7 |
+
# 1. API ํค ์ค์
|
| 8 |
GENAI_KEY = os.getenv("GEMINI_KEY")
|
| 9 |
|
| 10 |
+
# ======================================================
|
| 11 |
+
# ๐ต๏ธโโ๏ธ [ํต์ฌ] ๊ตฌ๊ธ ์๋ฒ์์ '์ง์ง ๋ชจ๋ธ ์ด๋ฆ' ์์์ค๊ธฐ
|
| 12 |
+
# ======================================================
|
| 13 |
+
def find_working_model():
|
| 14 |
+
# ๋ชจ๋ธ ๋ฆฌ์คํธ ์กฐํ API ํธ์ถ
|
| 15 |
+
url = f"https://generativelanguage.googleapis.com/v1beta/models?key={GENAI_KEY}"
|
| 16 |
+
response = requests.get(url)
|
| 17 |
+
|
| 18 |
+
if response.status_code != 200:
|
| 19 |
+
print(f"โ ๋ชจ๋ธ ๋ฆฌ์คํธ ์กฐํ ์คํจ: {response.text}")
|
| 20 |
+
return "gemini-pro" # ์คํจํ๋ฉด ๊ทธ๋ฅ ๊นกํต ๋ชจ๋ธ๋ก ์ก๊ธฐ
|
| 21 |
+
|
| 22 |
+
data = response.json()
|
| 23 |
+
|
| 24 |
+
# 1. 'generateContent' ๊ธฐ๋ฅ์ด ์๋ ๋ชจ๋ธ๋ง ํํฐ๋ง
|
| 25 |
+
candidates = []
|
| 26 |
+
if 'models' in data:
|
| 27 |
+
for m in data['models']:
|
| 28 |
+
if 'supportedGenerationMethods' in m and 'generateContent' in m['supportedGenerationMethods']:
|
| 29 |
+
# ์ด๋ฆ์์ 'models/' ๋ถ๋ถ ๋ผ๊ณ ์ ์ฅ (์: models/gemini-pro -> gemini-pro)
|
| 30 |
+
clean_name = m['name'].replace("models/", "")
|
| 31 |
+
candidates.append(clean_name)
|
| 32 |
+
|
| 33 |
+
print(f"๐ ์ฌ์ฉ ๊ฐ๋ฅ ๋ชจ๋ธ ๋ชฉ๋ก: {candidates}")
|
| 34 |
+
|
| 35 |
+
# 2. ์ฐ์ ์์๋๋ก ์ ํ (ํ๋์ -> ํ๋ก)
|
| 36 |
+
# ๋ฆฌ๋๋ ํค๋ก 'gemini-1.5-flash'๊ฐ ์๋๋ฉด 'gemini-1.0-pro'๋ผ๋ ์ก์ต๋๋ค.
|
| 37 |
+
preferred_order = [
|
| 38 |
+
"gemini-1.5-flash-latest",
|
| 39 |
+
"gemini-1.5-flash",
|
| 40 |
+
"gemini-1.5-flash-001",
|
| 41 |
+
"gemini-1.5-pro",
|
| 42 |
+
"gemini-1.0-pro",
|
| 43 |
+
"gemini-pro"
|
| 44 |
+
]
|
| 45 |
+
|
| 46 |
+
for pref in preferred_order:
|
| 47 |
+
if pref in candidates:
|
| 48 |
+
print(f"โ
๊ฒฐ์ ๋ ๋ชจ๋ธ: {pref}")
|
| 49 |
+
return pref
|
| 50 |
+
|
| 51 |
+
# ์ ํธํ๋ ๊ฒ ์์ผ๋ฉด ๋ชฉ๋ก์ ์ฒซ ๋ฒ์งธ ๋ ์ก๊ธฐ
|
| 52 |
+
if candidates:
|
| 53 |
+
print(f"โ ๏ธ ์ ํธ ๋ชจ๋ธ ์์. ๋์ฒด ๋ชจ๋ธ ์ฌ์ฉ: {candidates[0]}")
|
| 54 |
+
return candidates[0]
|
| 55 |
+
|
| 56 |
+
return "gemini-pro" # ์ง์ง ์๋ฌด๊ฒ๋ ์์ผ๋ฉด...
|
| 57 |
|
| 58 |
+
# ======================================================
|
| 59 |
+
# 3. ์ง์ ํต์ ํจ์ (๋ชจ๋ธ๋ช
์ ๋์ ์ผ๋ก ๋ฃ์)
|
| 60 |
+
# ======================================================
|
| 61 |
+
def call_gemini_direct(prompt, model_name):
|
| 62 |
+
# ์์์ ์ฐพ์ ๋ชจ๋ธ๋ช
์ URL์ ๋ผ์๋ฃ๊ธฐ
|
| 63 |
+
url = f"https://generativelanguage.googleapis.com/v1beta/models/{model_name}:generateContent?key={GENAI_KEY}"
|
| 64 |
|
| 65 |
headers = {'Content-Type': 'application/json'}
|
| 66 |
data = {
|
|
|
|
| 74 |
if response.status_code == 200:
|
| 75 |
return response.json()['candidates'][0]['content']['parts'][0]['text']
|
| 76 |
else:
|
| 77 |
+
# ์๋ฌ ๋๋ฉด ์๋ฌ ๋ฉ์์ง ๋ฐํ
|
| 78 |
+
raise Exception(f"Google API Error ({model_name}): {response.text}")
|
| 79 |
+
|
| 80 |
+
|
| 81 |
+
# 4. ๋ฉค๋ฒ ๋ฐ์ดํฐ (๊ทธ๋๋ก)
|
| 82 |
+
MEMBERS = {
|
| 83 |
+
"์์ค (Korea)": { "role": "๋ฉ์ธ ๋ณด์ปฌ", "voice": "ko-KR-SunHiNeural", "prompt": "๋น์ ์ ๋ก๋ฐด๋ 'Error 404'์ ๋ฉ์ธ๋ณด์ปฌ '์์ค'์
๋๋ค. ํ๊ตญ์ด(Korean)๋ก ๋๋ตํ์ธ์. ์ฑ๊ฒฉ: ์๋์ปฌ, ์ธค๋ฐ๋ . ๋งํฌ: '์ผ', '์์์' ์ฌ์ฉ." },
|
| 84 |
+
"Chloe (USA)": { "role": "๋ฆฌ๋ ๊ธฐํ", "voice": "en-US-AriaNeural", "prompt": "You are 'Chloe'. Respond in English. Personality: Cool rockstar." },
|
| 85 |
+
"Beatrice (Brazil)": { "role": "๋๋ผ", "voice": "pt-BR-FranciscaNeural", "prompt": "You are 'Beatrice'. Respond in English/Portuguese. Personality: Passionate." },
|
| 86 |
+
"Naomi (Japan)": { "role": "๋ฆฌ๋ฌ ๊ธฐํ", "voice": "ja-JP-NanamiNeural", "prompt": "You are 'Naomi'. Respond in Japanese. Personality: Calm." },
|
| 87 |
+
"Elena (Spain)": { "role": "๋ฒ ์ด์ค", "voice": "es-ES-ElviraNeural", "prompt": "You are 'Elena'. Respond in Spanish/English. Personality: Mature." },
|
| 88 |
+
"Amira (Egypt)": { "role": "ํค๋ณด๋", "voice": "ar-EG-SalmaNeural", "prompt": "You are 'Amira'. Respond in Arabic/English. Personality: Mystical." },
|
| 89 |
+
"Liwei (China)": { "role": "DJ / FX", "voice": "zh-CN-XiaoxiaoNeural", "prompt": "You are 'Liwei'. Respond in Chinese/English. Personality: Trendy." },
|
| 90 |
+
"Sophie (France)": { "role": "๋ฐ์ด์ฌ๋ฆฐ", "voice": "fr-FR-DeniseNeural", "prompt": "You are 'Sophie'. Respond in French/English. Personality: Elegant." }
|
| 91 |
+
}
|
| 92 |
|
| 93 |
async def band_consulting(user_input, member_name):
|
| 94 |
try:
|
|
|
|
| 96 |
return f"Error: ์ด๋ฆ ๋ถ์ผ์น! ('{member_name}')", None
|
| 97 |
|
| 98 |
member = MEMBERS[member_name]
|
|
|
|
|
|
|
| 99 |
full_prompt = f"{member['prompt']}\n\nUser's Error/Worry: {user_input}\nResponse:"
|
| 100 |
|
| 101 |
try:
|
| 102 |
+
# 1. ์ด์์๋ ๋ชจ๋ธ ์ฐพ๊ธฐ (๋งค๋ฒ ์ฐพ์ผ๋ฉด ๋๋ฆฌ๋๊น ์บ์ฑํ๋ฉด ์ข์ง๋ง, ํ์คํจ์ ์ํด ๋งค๋ฒ ํ์ธ)
|
| 103 |
+
active_model = find_working_model()
|
| 104 |
+
|
| 105 |
+
# 2. ๊ทธ ๋ชจ๋ธ๋ก ์์ฒญ ๋ณด๋ด๊ธฐ
|
| 106 |
+
ai_text = call_gemini_direct(full_prompt, active_model)
|
| 107 |
+
|
| 108 |
except Exception as e:
|
| 109 |
+
return f"Error (Gemini): {str(e)}", None
|
| 110 |
|
|
|
|
| 111 |
try:
|
| 112 |
output_file = f"/tmp/{member_name}_reply.mp3"
|
| 113 |
communicate = edge_tts.Communicate(ai_text, member['voice'])
|
|
|
|
| 120 |
except Exception as e:
|
| 121 |
return f"System Error: {str(e)}", None
|
| 122 |
|
| 123 |
+
# UI
|
| 124 |
with gr.Blocks() as demo:
|
| 125 |
+
gr.Markdown("# Error 404 Band API Server (Auto-Discovery Mode)")
|
| 126 |
with gr.Row():
|
| 127 |
inp_text = gr.Textbox(label="๊ณ ๋ฏผ ์
๋ ฅ")
|
| 128 |
inp_member = gr.Dropdown(choices=list(MEMBERS.keys()), label="๋ฉค๋ฒ ์ ํ", value="์์ค (Korea)")
|
| 129 |
out_text = gr.Textbox(label="์กฐ์ธ")
|
| 130 |
out_audio = gr.Audio(label="์์ฑ")
|
| 131 |
btn = gr.Button("์๋ด ์์ฒญ")
|
|
|
|
|
|
|
| 132 |
btn.click(fn=band_consulting, inputs=[inp_text, inp_member], outputs=[out_text, out_audio], api_name="predict")
|
| 133 |
|
| 134 |
demo.launch()
|