Update app.py
Browse files
app.py
CHANGED
|
@@ -11,7 +11,7 @@ from mistralai import Mistral
|
|
| 11 |
import google.generativeai as genai
|
| 12 |
from huggingface_hub import snapshot_download
|
| 13 |
|
| 14 |
-
# --- 1. SEGURANÇA (ANTI-SPAM
|
| 15 |
MAX_REQUESTS_PER_MINUTE = 15
|
| 16 |
BLOCK_TIME_SECONDS = 60
|
| 17 |
ip_tracker = {}
|
|
@@ -36,7 +36,6 @@ LOCAL_MODEL_ID = "Qwen/Qwen2.5-Coder-32B-Instruct"
|
|
| 36 |
local_model = None
|
| 37 |
local_tokenizer = None
|
| 38 |
|
| 39 |
-
# Clientes de API (A Berta verifica se as chaves existem para não dar erro feio)
|
| 40 |
groq_client = Groq(api_key=os.environ.get("GROQ_API_KEY")) if os.environ.get("GROQ_API_KEY") else None
|
| 41 |
mistral_client = Mistral(api_key=os.environ.get("MISTRAL_API_KEY")) if os.environ.get("MISTRAL_API_KEY") else None
|
| 42 |
|
|
@@ -50,17 +49,16 @@ def encode_image(image_path):
|
|
| 50 |
return base64.b64encode(image_file.read()).decode('utf-8')
|
| 51 |
except: return None
|
| 52 |
|
| 53 |
-
# --- 4. EXECUTORES
|
| 54 |
|
| 55 |
@spaces.GPU(duration=120)
|
| 56 |
def run_local_h200(messages):
|
| 57 |
global local_model, local_tokenizer
|
| 58 |
-
# Verificação crítica: Modelos locais de texto puro não leem imagens diretamente aqui
|
| 59 |
for m in messages:
|
| 60 |
if isinstance(m['content'], list): return "⚠️ Berta avisa: Modelo Local não suporta imagens. Use Gemini ou Pixtral."
|
| 61 |
|
| 62 |
if local_model is None:
|
| 63 |
-
print(f"🐢
|
| 64 |
local_tokenizer = AutoTokenizer.from_pretrained(LOCAL_MODEL_ID)
|
| 65 |
local_model = AutoModelForCausalLM.from_pretrained(
|
| 66 |
LOCAL_MODEL_ID, torch_dtype=torch.bfloat16, device_map="cuda"
|
|
@@ -73,8 +71,8 @@ def run_local_h200(messages):
|
|
| 73 |
|
| 74 |
def run_groq(messages, model_id):
|
| 75 |
for m in messages:
|
| 76 |
-
if isinstance(m['content'], list): return "⚠️
|
| 77 |
-
if not groq_client: return "❌ Erro:
|
| 78 |
clean_msgs = [{"role": m['role'], "content": m['content']} for m in messages]
|
| 79 |
try:
|
| 80 |
completion = groq_client.chat.completions.create(
|
|
@@ -84,7 +82,7 @@ def run_groq(messages, model_id):
|
|
| 84 |
except Exception as e: return f"❌ Groq Error: {e}"
|
| 85 |
|
| 86 |
def run_mistral(messages, model_id):
|
| 87 |
-
if not mistral_client: return "❌ Erro:
|
| 88 |
formatted_msgs = []
|
| 89 |
for m in messages:
|
| 90 |
content = m['content']
|
|
@@ -106,13 +104,10 @@ def run_mistral(messages, model_id):
|
|
| 106 |
except Exception as e: return f"❌ Mistral Error: {e}"
|
| 107 |
|
| 108 |
def run_gemini(messages, model_id):
|
| 109 |
-
if not os.environ.get("GEMINI_API_KEY"): return "❌ Erro:
|
| 110 |
try:
|
| 111 |
-
# Instancia o modelo com o ID específico solicitado
|
| 112 |
model = genai.GenerativeModel(model_id)
|
| 113 |
chat_history = []
|
| 114 |
-
|
| 115 |
-
# Constrói o histórico (exceto a última mensagem)
|
| 116 |
for m in messages[:-1]:
|
| 117 |
role = "user" if m['role'] == "user" else "model"
|
| 118 |
parts = []
|
|
@@ -126,7 +121,6 @@ def run_gemini(messages, model_id):
|
|
| 126 |
if os.path.exists(path): parts.append(Image.open(path))
|
| 127 |
if parts: chat_history.append({"role": role, "parts": parts})
|
| 128 |
|
| 129 |
-
# Prepara a última mensagem (prompt atual)
|
| 130 |
last_parts = []
|
| 131 |
lc = messages[-1]['content']
|
| 132 |
if isinstance(lc, str): last_parts.append(lc)
|
|
@@ -140,14 +134,13 @@ def run_gemini(messages, model_id):
|
|
| 140 |
chat = model.start_chat(history=chat_history)
|
| 141 |
response = chat.send_message(last_parts)
|
| 142 |
return response.text
|
| 143 |
-
except Exception as e: return f"❌ Gemini Error
|
| 144 |
|
| 145 |
-
# --- 5. ROTEADOR CENTRAL
|
| 146 |
def router(message, history, model_selector, request: gr.Request):
|
| 147 |
-
if not check_spam(request):
|
| 148 |
-
return "⛔ BLOQUEADO: Você está indo rápido demais, querido. Respire um pouco."
|
| 149 |
|
| 150 |
-
# Normaliza histórico
|
| 151 |
messages = []
|
| 152 |
if history:
|
| 153 |
for turn in history:
|
|
@@ -167,31 +160,28 @@ def router(message, history, model_selector, request: gr.Request):
|
|
| 167 |
files = message.get("files", [])
|
| 168 |
if text: current_content.append({"type": "text", "text": text})
|
| 169 |
for f in files: current_content.append({"type": "image_url", "image_url": {"url": f}})
|
| 170 |
-
|
| 171 |
if not files: messages.append({"role": "user", "content": text})
|
| 172 |
else: messages.append({"role": "user", "content": current_content})
|
| 173 |
else:
|
| 174 |
messages.append({"role": "user", "content": str(message)})
|
| 175 |
|
| 176 |
-
#
|
| 177 |
-
|
| 178 |
-
# Rota Google / Gemini / LearnLM / Gemma
|
| 179 |
-
if any(k in model_selector for k in ["Gemini", "LearnLM", "Gemma"]):
|
| 180 |
-
# IDs Padrão
|
| 181 |
tid = "gemini-1.5-flash"
|
| 182 |
|
| 183 |
-
# Mapeamento
|
| 184 |
-
if "
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 185 |
elif "2.5 Pro" in model_selector: tid = "gemini-2.5-pro"
|
| 186 |
-
elif "2.5 Flash
|
| 187 |
-
elif "2.
|
| 188 |
-
elif "2.0" in model_selector and "LearnLM" not in model_selector: tid = "gemini-2.0-flash-exp"
|
| 189 |
-
elif "LearnLM" in model_selector: tid = "learnlm-2.0-flash-experimental" # 🆕 Novo
|
| 190 |
-
elif "Gemma 3" in model_selector: tid = "gemma-3-27b" # 🆕 Novo (Verificar se API aceita este ID exato)
|
| 191 |
|
| 192 |
return run_gemini(messages, tid)
|
| 193 |
|
| 194 |
-
# Rota Mistral
|
| 195 |
elif "Mistral" in model_selector:
|
| 196 |
tid = "mistral-large-latest"
|
| 197 |
if "Pixtral" in model_selector: tid = "pixtral-large-latest"
|
|
@@ -200,31 +190,29 @@ def router(message, history, model_selector, request: gr.Request):
|
|
| 200 |
elif "Codestral" in model_selector: tid = "codestral-2508"
|
| 201 |
return run_mistral(messages, tid)
|
| 202 |
|
| 203 |
-
# Rota Groq
|
| 204 |
elif "Groq" in model_selector:
|
| 205 |
tid = "llama-3.3-70b-versatile"
|
| 206 |
if "120B" in model_selector: tid = "openai/gpt-oss-120b"
|
| 207 |
elif "20B" in model_selector: tid = "openai/gpt-oss-20b"
|
| 208 |
return run_groq(messages, tid)
|
| 209 |
|
| 210 |
-
# Rota Local
|
| 211 |
elif "H200" in model_selector:
|
| 212 |
return run_local_h200(messages)
|
| 213 |
|
| 214 |
-
return "⚠️ Modelo não reconhecido.
|
| 215 |
|
| 216 |
# --- 6. INTERFACE ---
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
gr.Markdown(
|
| 220 |
|
| 221 |
models_list = [
|
| 222 |
-
"✨ Google: LearnLM
|
| 223 |
-
"✨ Google: Gemma 3 27B
|
|
|
|
| 224 |
"✨ Google: Gemini 3.0 Pro (Experimental)",
|
| 225 |
"✨ Google: Gemini 2.5 Pro",
|
| 226 |
"✨ Google: Gemini 2.5 Flash",
|
| 227 |
-
"✨ Google: Gemini 2.5 Flash Lite ⚡", # Flash Lite
|
| 228 |
"✨ Google: Gemini 2.0 Flash",
|
| 229 |
"☁️ Groq: GPT OSS 120B (OpenAI)",
|
| 230 |
"☁️ Groq: GPT OSS 20B (OpenAI)",
|
|
@@ -237,17 +225,14 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 237 |
]
|
| 238 |
|
| 239 |
with gr.Row():
|
| 240 |
-
model_dropdown = gr.Dropdown(choices=models_list, value=models_list[0], label="
|
| 241 |
|
| 242 |
-
# 1. Interface Visual
|
| 243 |
chat = gr.ChatInterface(
|
| 244 |
fn=router,
|
| 245 |
additional_inputs=[model_dropdown],
|
| 246 |
-
multimodal=True
|
| 247 |
-
description="Converse com a Berta e seus amigos AIs."
|
| 248 |
)
|
| 249 |
|
| 250 |
-
# 2. PONTE DE API
|
| 251 |
api_bridge = gr.Interface(
|
| 252 |
fn=router,
|
| 253 |
inputs=[
|
|
|
|
| 11 |
import google.generativeai as genai
|
| 12 |
from huggingface_hub import snapshot_download
|
| 13 |
|
| 14 |
+
# --- 1. SEGURANÇA (ANTI-SPAM) ---
|
| 15 |
MAX_REQUESTS_PER_MINUTE = 15
|
| 16 |
BLOCK_TIME_SECONDS = 60
|
| 17 |
ip_tracker = {}
|
|
|
|
| 36 |
local_model = None
|
| 37 |
local_tokenizer = None
|
| 38 |
|
|
|
|
| 39 |
groq_client = Groq(api_key=os.environ.get("GROQ_API_KEY")) if os.environ.get("GROQ_API_KEY") else None
|
| 40 |
mistral_client = Mistral(api_key=os.environ.get("MISTRAL_API_KEY")) if os.environ.get("MISTRAL_API_KEY") else None
|
| 41 |
|
|
|
|
| 49 |
return base64.b64encode(image_file.read()).decode('utf-8')
|
| 50 |
except: return None
|
| 51 |
|
| 52 |
+
# --- 4. EXECUTORES ---
|
| 53 |
|
| 54 |
@spaces.GPU(duration=120)
|
| 55 |
def run_local_h200(messages):
|
| 56 |
global local_model, local_tokenizer
|
|
|
|
| 57 |
for m in messages:
|
| 58 |
if isinstance(m['content'], list): return "⚠️ Berta avisa: Modelo Local não suporta imagens. Use Gemini ou Pixtral."
|
| 59 |
|
| 60 |
if local_model is None:
|
| 61 |
+
print(f"🐢 Carregando {LOCAL_MODEL_ID}...")
|
| 62 |
local_tokenizer = AutoTokenizer.from_pretrained(LOCAL_MODEL_ID)
|
| 63 |
local_model = AutoModelForCausalLM.from_pretrained(
|
| 64 |
LOCAL_MODEL_ID, torch_dtype=torch.bfloat16, device_map="cuda"
|
|
|
|
| 71 |
|
| 72 |
def run_groq(messages, model_id):
|
| 73 |
for m in messages:
|
| 74 |
+
if isinstance(m['content'], list): return "⚠️ Groq não suporta imagens."
|
| 75 |
+
if not groq_client: return "❌ Erro: GROQ_API_KEY ausente."
|
| 76 |
clean_msgs = [{"role": m['role'], "content": m['content']} for m in messages]
|
| 77 |
try:
|
| 78 |
completion = groq_client.chat.completions.create(
|
|
|
|
| 82 |
except Exception as e: return f"❌ Groq Error: {e}"
|
| 83 |
|
| 84 |
def run_mistral(messages, model_id):
|
| 85 |
+
if not mistral_client: return "❌ Erro: MISTRAL_API_KEY ausente."
|
| 86 |
formatted_msgs = []
|
| 87 |
for m in messages:
|
| 88 |
content = m['content']
|
|
|
|
| 104 |
except Exception as e: return f"❌ Mistral Error: {e}"
|
| 105 |
|
| 106 |
def run_gemini(messages, model_id):
|
| 107 |
+
if not os.environ.get("GEMINI_API_KEY"): return "❌ Erro: GEMINI_API_KEY ausente."
|
| 108 |
try:
|
|
|
|
| 109 |
model = genai.GenerativeModel(model_id)
|
| 110 |
chat_history = []
|
|
|
|
|
|
|
| 111 |
for m in messages[:-1]:
|
| 112 |
role = "user" if m['role'] == "user" else "model"
|
| 113 |
parts = []
|
|
|
|
| 121 |
if os.path.exists(path): parts.append(Image.open(path))
|
| 122 |
if parts: chat_history.append({"role": role, "parts": parts})
|
| 123 |
|
|
|
|
| 124 |
last_parts = []
|
| 125 |
lc = messages[-1]['content']
|
| 126 |
if isinstance(lc, str): last_parts.append(lc)
|
|
|
|
| 134 |
chat = model.start_chat(history=chat_history)
|
| 135 |
response = chat.send_message(last_parts)
|
| 136 |
return response.text
|
| 137 |
+
except Exception as e: return f"❌ Gemini Error: {e}"
|
| 138 |
|
| 139 |
+
# --- 5. ROTEADOR CENTRAL ---
|
| 140 |
def router(message, history, model_selector, request: gr.Request):
|
| 141 |
+
if not check_spam(request): return "⛔ BLOQUEADO: Spam detectado."
|
|
|
|
| 142 |
|
| 143 |
+
# Normaliza histórico
|
| 144 |
messages = []
|
| 145 |
if history:
|
| 146 |
for turn in history:
|
|
|
|
| 160 |
files = message.get("files", [])
|
| 161 |
if text: current_content.append({"type": "text", "text": text})
|
| 162 |
for f in files: current_content.append({"type": "image_url", "image_url": {"url": f}})
|
|
|
|
| 163 |
if not files: messages.append({"role": "user", "content": text})
|
| 164 |
else: messages.append({"role": "user", "content": current_content})
|
| 165 |
else:
|
| 166 |
messages.append({"role": "user", "content": str(message)})
|
| 167 |
|
| 168 |
+
# Seleção de Modelos (Atualizada com seus pedidos)
|
| 169 |
+
if any(x in model_selector for x in ["Gemini", "LearnLM", "Gemma"]):
|
|
|
|
|
|
|
|
|
|
| 170 |
tid = "gemini-1.5-flash"
|
| 171 |
|
| 172 |
+
# Mapeamento estrito dos modelos novos
|
| 173 |
+
if "LearnLM 2.0" in model_selector: tid = "learnlm-2.0-flash-experimental"
|
| 174 |
+
elif "Gemma 3" in model_selector: tid = "gemma-3-27b"
|
| 175 |
+
elif "2.5 Lite" in model_selector: tid = "gemini-2.5-flash-lite"
|
| 176 |
+
|
| 177 |
+
# Mapeamento dos anteriores
|
| 178 |
+
elif "3.0" in model_selector: tid = "gemini-3.0-pro-preview"
|
| 179 |
elif "2.5 Pro" in model_selector: tid = "gemini-2.5-pro"
|
| 180 |
+
elif "2.5 Flash" in model_selector: tid = "gemini-2.5-flash" # Cuidado para não confundir com Lite
|
| 181 |
+
elif "2.0" in model_selector: tid = "gemini-2.0-flash-exp"
|
|
|
|
|
|
|
|
|
|
| 182 |
|
| 183 |
return run_gemini(messages, tid)
|
| 184 |
|
|
|
|
| 185 |
elif "Mistral" in model_selector:
|
| 186 |
tid = "mistral-large-latest"
|
| 187 |
if "Pixtral" in model_selector: tid = "pixtral-large-latest"
|
|
|
|
| 190 |
elif "Codestral" in model_selector: tid = "codestral-2508"
|
| 191 |
return run_mistral(messages, tid)
|
| 192 |
|
|
|
|
| 193 |
elif "Groq" in model_selector:
|
| 194 |
tid = "llama-3.3-70b-versatile"
|
| 195 |
if "120B" in model_selector: tid = "openai/gpt-oss-120b"
|
| 196 |
elif "20B" in model_selector: tid = "openai/gpt-oss-20b"
|
| 197 |
return run_groq(messages, tid)
|
| 198 |
|
|
|
|
| 199 |
elif "H200" in model_selector:
|
| 200 |
return run_local_h200(messages)
|
| 201 |
|
| 202 |
+
return "⚠️ Modelo não reconhecido."
|
| 203 |
|
| 204 |
# --- 6. INTERFACE ---
|
| 205 |
+
# Berta removeu o theme=... aqui para não dar erro
|
| 206 |
+
with gr.Blocks() as demo:
|
| 207 |
+
gr.Markdown("# 🔀 APIDOST v11 - Fixed & Expanded")
|
| 208 |
|
| 209 |
models_list = [
|
| 210 |
+
"✨ Google: LearnLM 2.0 Flash (Exp) 📚", # ID: learnlm-2.0-flash-experimental
|
| 211 |
+
"✨ Google: Gemma 3 27B 💎", # ID: gemma-3-27b
|
| 212 |
+
"✨ Google: Gemini 2.5 Flash Lite ⚡", # ID: gemini-2.5-flash-lite
|
| 213 |
"✨ Google: Gemini 3.0 Pro (Experimental)",
|
| 214 |
"✨ Google: Gemini 2.5 Pro",
|
| 215 |
"✨ Google: Gemini 2.5 Flash",
|
|
|
|
| 216 |
"✨ Google: Gemini 2.0 Flash",
|
| 217 |
"☁️ Groq: GPT OSS 120B (OpenAI)",
|
| 218 |
"☁️ Groq: GPT OSS 20B (OpenAI)",
|
|
|
|
| 225 |
]
|
| 226 |
|
| 227 |
with gr.Row():
|
| 228 |
+
model_dropdown = gr.Dropdown(choices=models_list, value=models_list[0], label="Cérebro", interactive=True)
|
| 229 |
|
|
|
|
| 230 |
chat = gr.ChatInterface(
|
| 231 |
fn=router,
|
| 232 |
additional_inputs=[model_dropdown],
|
| 233 |
+
multimodal=True
|
|
|
|
| 234 |
)
|
| 235 |
|
|
|
|
| 236 |
api_bridge = gr.Interface(
|
| 237 |
fn=router,
|
| 238 |
inputs=[
|