Update app.py
Browse files
app.py
CHANGED
|
@@ -31,7 +31,6 @@ def encode_image(image_path):
|
|
| 31 |
|
| 32 |
@spaces.GPU(duration=60)
|
| 33 |
def run_local_h200(messages):
|
| 34 |
-
# Qwen não suporta imagens
|
| 35 |
for m in messages:
|
| 36 |
if isinstance(m['content'], list):
|
| 37 |
return "⚠️ Qwen H200 não suporta imagens. Use Gemini ou Pixtral."
|
|
@@ -52,7 +51,7 @@ def run_local_h200(messages):
|
|
| 52 |
def run_groq(messages, model_id):
|
| 53 |
for m in messages:
|
| 54 |
if isinstance(m['content'], list):
|
| 55 |
-
return "⚠️ Groq não suporta imagens. Use
|
| 56 |
|
| 57 |
if not groq_client: return "❌ Erro: GROQ_API_KEY ausente."
|
| 58 |
|
|
@@ -83,7 +82,6 @@ def run_mistral(messages, model_id):
|
|
| 83 |
new_content.append({"type": "text", "text": item['text']})
|
| 84 |
elif item.get('type') == 'image_url':
|
| 85 |
url = item['image_url']['url']
|
| 86 |
-
# Gradio 4 local path handling
|
| 87 |
if not url.startswith("data:") and os.path.exists(url):
|
| 88 |
b64 = encode_image(url)
|
| 89 |
new_content.append({"type": "image_url", "image_url": f"data:image/jpeg;base64,{b64}"})
|
|
@@ -132,25 +130,19 @@ def run_gemini(messages, model_id):
|
|
| 132 |
return response.text
|
| 133 |
except Exception as e: return f"❌ Gemini Error ({model_id}): {e}"
|
| 134 |
|
| 135 |
-
# --- ROTEADOR
|
| 136 |
def router(message, history, model_selector):
|
| 137 |
-
# Em Gradio 4, history é [[user, bot], [user, bot]]
|
| 138 |
-
# message é {'text': '...', 'files': ['path']}
|
| 139 |
-
|
| 140 |
formatted_history = []
|
| 141 |
|
| 142 |
-
#
|
| 143 |
for user_turn, bot_turn in history:
|
| 144 |
-
# User turn (pode ser string ou tupla/lista com imagem no Gradio 4 antigo, mas vamos tratar como string safe)
|
| 145 |
u_text = str(user_turn)
|
| 146 |
-
# Tenta limpar se vier com sujeira de arquivo
|
| 147 |
if isinstance(user_turn, tuple): u_text = user_turn[0]
|
| 148 |
-
|
| 149 |
formatted_history.append({"role": "user", "content": u_text})
|
| 150 |
if bot_turn:
|
| 151 |
formatted_history.append({"role": "assistant", "content": str(bot_turn)})
|
| 152 |
|
| 153 |
-
#
|
| 154 |
current_content = []
|
| 155 |
text_input = message.get("text", "")
|
| 156 |
files = message.get("files", [])
|
|
@@ -161,13 +153,12 @@ def router(message, history, model_selector):
|
|
| 161 |
for file_path in files:
|
| 162 |
current_content.append({"type": "image_url", "image_url": {"url": file_path}})
|
| 163 |
|
| 164 |
-
# Adiciona ao histórico final
|
| 165 |
if not files:
|
| 166 |
formatted_history.append({"role": "user", "content": text_input})
|
| 167 |
else:
|
| 168 |
formatted_history.append({"role": "user", "content": current_content})
|
| 169 |
|
| 170 |
-
#
|
| 171 |
if "Gemini" in model_selector:
|
| 172 |
tid = "gemini-1.5-flash"
|
| 173 |
if "3.0" in model_selector: tid = "gemini-3.0-pro-preview"
|
|
@@ -192,9 +183,9 @@ def router(message, history, model_selector):
|
|
| 192 |
|
| 193 |
return "Modelo desconhecido."
|
| 194 |
|
| 195 |
-
# ---
|
| 196 |
-
with gr.Blocks(
|
| 197 |
-
gr.Markdown("# 🔀 APIDOST
|
| 198 |
|
| 199 |
with gr.Row():
|
| 200 |
model_dropdown = gr.Dropdown(
|
|
@@ -211,7 +202,6 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 211 |
interactive=True
|
| 212 |
)
|
| 213 |
|
| 214 |
-
# REMOVIDO type="messages"
|
| 215 |
chat = gr.ChatInterface(
|
| 216 |
fn=router,
|
| 217 |
additional_inputs=[model_dropdown],
|
|
|
|
| 31 |
|
| 32 |
@spaces.GPU(duration=60)
|
| 33 |
def run_local_h200(messages):
|
|
|
|
| 34 |
for m in messages:
|
| 35 |
if isinstance(m['content'], list):
|
| 36 |
return "⚠️ Qwen H200 não suporta imagens. Use Gemini ou Pixtral."
|
|
|
|
| 51 |
def run_groq(messages, model_id):
|
| 52 |
for m in messages:
|
| 53 |
if isinstance(m['content'], list):
|
| 54 |
+
return "⚠️ Groq não suporta imagens. Use Pixtral ou Gemini."
|
| 55 |
|
| 56 |
if not groq_client: return "❌ Erro: GROQ_API_KEY ausente."
|
| 57 |
|
|
|
|
| 82 |
new_content.append({"type": "text", "text": item['text']})
|
| 83 |
elif item.get('type') == 'image_url':
|
| 84 |
url = item['image_url']['url']
|
|
|
|
| 85 |
if not url.startswith("data:") and os.path.exists(url):
|
| 86 |
b64 = encode_image(url)
|
| 87 |
new_content.append({"type": "image_url", "image_url": f"data:image/jpeg;base64,{b64}"})
|
|
|
|
| 130 |
return response.text
|
| 131 |
except Exception as e: return f"❌ Gemini Error ({model_id}): {e}"
|
| 132 |
|
| 133 |
+
# --- ROTEADOR ---
|
| 134 |
def router(message, history, model_selector):
|
|
|
|
|
|
|
|
|
|
| 135 |
formatted_history = []
|
| 136 |
|
| 137 |
+
# Processa histórico
|
| 138 |
for user_turn, bot_turn in history:
|
|
|
|
| 139 |
u_text = str(user_turn)
|
|
|
|
| 140 |
if isinstance(user_turn, tuple): u_text = user_turn[0]
|
|
|
|
| 141 |
formatted_history.append({"role": "user", "content": u_text})
|
| 142 |
if bot_turn:
|
| 143 |
formatted_history.append({"role": "assistant", "content": str(bot_turn)})
|
| 144 |
|
| 145 |
+
# Processa mensagem ATUAL
|
| 146 |
current_content = []
|
| 147 |
text_input = message.get("text", "")
|
| 148 |
files = message.get("files", [])
|
|
|
|
| 153 |
for file_path in files:
|
| 154 |
current_content.append({"type": "image_url", "image_url": {"url": file_path}})
|
| 155 |
|
|
|
|
| 156 |
if not files:
|
| 157 |
formatted_history.append({"role": "user", "content": text_input})
|
| 158 |
else:
|
| 159 |
formatted_history.append({"role": "user", "content": current_content})
|
| 160 |
|
| 161 |
+
# Roteamento
|
| 162 |
if "Gemini" in model_selector:
|
| 163 |
tid = "gemini-1.5-flash"
|
| 164 |
if "3.0" in model_selector: tid = "gemini-3.0-pro-preview"
|
|
|
|
| 183 |
|
| 184 |
return "Modelo desconhecido."
|
| 185 |
|
| 186 |
+
# --- INTERFACE (SEM TEMA - CORREÇÃO DE VERDADE) ---
|
| 187 |
+
with gr.Blocks() as demo: # <--- TEMA REMOVIDO AQUI
|
| 188 |
+
gr.Markdown("# 🔀 APIDOST (Safe Mode)")
|
| 189 |
|
| 190 |
with gr.Row():
|
| 191 |
model_dropdown = gr.Dropdown(
|
|
|
|
| 202 |
interactive=True
|
| 203 |
)
|
| 204 |
|
|
|
|
| 205 |
chat = gr.ChatInterface(
|
| 206 |
fn=router,
|
| 207 |
additional_inputs=[model_dropdown],
|