Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,48 +1,46 @@
|
|
| 1 |
import os
|
| 2 |
import time
|
| 3 |
import asyncio
|
| 4 |
-
import
|
|
|
|
| 5 |
import requests
|
| 6 |
-
import aiohttp
|
| 7 |
import gradio as gr
|
| 8 |
from PIL import Image
|
| 9 |
from mistralai import Mistral
|
| 10 |
-
from
|
| 11 |
-
from mcp import ClientSession
|
| 12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
def inicializar_entorno_mcp():
|
| 14 |
base_path = "mcp_server_box"
|
| 15 |
src_path = os.path.join(base_path, "src")
|
| 16 |
os.makedirs(src_path, exist_ok=True)
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
os.path.join(src_path, "__init__.py"): "",
|
| 20 |
-
os.path.join(src_path, "mcp_server_box.py"): """
|
| 21 |
import os
|
| 22 |
from mcp.server.fastmcp import FastMCP
|
| 23 |
mcp = FastMCP("BATUTO-BOX-TOTAL")
|
| 24 |
@mcp.tool()
|
| 25 |
async def upload_image_to_box(image_path: str, folder_id: str = '0'):
|
| 26 |
return f"✅ Arte subido a Box." if os.path.exists(image_path) else "❌ No encontrado."
|
| 27 |
-
|
| 28 |
-
async def create_web_link(url: str, name: str = 'Link'):
|
| 29 |
-
return f"🚀 Link '{name}' creado."
|
| 30 |
-
if __name__ == '__main__':
|
| 31 |
mcp.run()
|
| 32 |
-
"""
|
| 33 |
-
}
|
| 34 |
-
for ruta, contenido in archivos.items():
|
| 35 |
-
with open(ruta, "w", encoding="utf-8") as f:
|
| 36 |
-
f.write(contenido.strip())
|
| 37 |
|
| 38 |
-
|
|
|
|
|
|
|
| 39 |
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
MCP_BOX_SERVER_PATH = os.path.abspath("./mcp_server_box")
|
| 44 |
-
MCP_BOX_PARAMS = StdioServerParameters(command="python", args=[os.path.join(MCP_BOX_SERVER_PATH, "src", "mcp_server_box.py")])
|
| 45 |
|
|
|
|
| 46 |
SAMBA_MODELS = [
|
| 47 |
"DeepSeek-R1", "DeepSeek-V3.1", "DeepSeek-V3", "DeepSeek-V3-0324",
|
| 48 |
"Meta-Llama-3.3-70B-Instruct", "Llama-4-Maverick-17B-128E-Instruct",
|
|
@@ -63,33 +61,50 @@ HF_MODELS = [
|
|
| 63 |
]
|
| 64 |
ALL_MODELS = ["AUTO-SELECT", "MISTRAL-AGENT-PRO", "REVE"] + SAMBA_MODELS + HF_MODELS
|
| 65 |
|
|
|
|
| 66 |
async def handle_hybrid_request(model, prompt, image, temp, tokens):
|
| 67 |
if not prompt.strip() and image is None:
|
| 68 |
-
yield "¡
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
image
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
def create_ui():
|
| 81 |
-
with gr.Blocks() as demo:
|
| 82 |
gr.HTML("<h1 style='text-align:center; color:#00C896;'>⚡ BATUTO X • NEUROCORE PRO</h1>")
|
| 83 |
with gr.Row():
|
| 84 |
with gr.Column(scale=1):
|
| 85 |
model_opt = gr.Dropdown(ALL_MODELS, value="AUTO-SELECT", label="Cerebro")
|
| 86 |
-
image_input = gr.Image(type="pil", label="🖼️ Visión")
|
| 87 |
temp_opt = gr.Slider(0, 1.5, 0.7, label="Temperatura")
|
| 88 |
with gr.Column(scale=2):
|
| 89 |
-
prompt_input = gr.Textbox(lines=5, label="Comando")
|
| 90 |
-
send_btn = gr.Button("🚀 EJECUTAR", variant="primary")
|
| 91 |
-
output_text = gr.Textbox(lines=10, label="Salida")
|
| 92 |
-
output_img = gr.Image(label="Imagen")
|
|
|
|
| 93 |
send_btn.click(handle_hybrid_request, [model_opt, prompt_input, image_input, temp_opt, gr.State(2048)], [output_text, output_img])
|
| 94 |
return demo
|
| 95 |
|
|
|
|
| 1 |
import os
|
| 2 |
import time
|
| 3 |
import asyncio
|
| 4 |
+
import base64
|
| 5 |
+
import logging
|
| 6 |
import requests
|
|
|
|
| 7 |
import gradio as gr
|
| 8 |
from PIL import Image
|
| 9 |
from mistralai import Mistral
|
| 10 |
+
from concurrent.futures import ThreadPoolExecutor
|
|
|
|
| 11 |
|
| 12 |
+
# Configuración de Entorno y Keys
|
| 13 |
+
MISTRAL_API_KEY = os.getenv("MISTRAL_API_KEY", "").strip()
|
| 14 |
+
SAMBA_API_KEY = os.getenv("SAMBA_API_KEY", "").strip()
|
| 15 |
+
HF_TOKEN = os.getenv("HF_TOKEN", "").strip()
|
| 16 |
+
SAMBA_BASE_URL = "https://api.sambanova.ai/v1"
|
| 17 |
+
|
| 18 |
+
# --- 1. AUTOCONFIGURACIÓN MCP (BOX) ---
|
| 19 |
def inicializar_entorno_mcp():
|
| 20 |
base_path = "mcp_server_box"
|
| 21 |
src_path = os.path.join(base_path, "src")
|
| 22 |
os.makedirs(src_path, exist_ok=True)
|
| 23 |
+
with open(os.path.join(src_path, "mcp_server_box.py"), "w", encoding="utf-8") as f:
|
| 24 |
+
f.write("""
|
|
|
|
|
|
|
| 25 |
import os
|
| 26 |
from mcp.server.fastmcp import FastMCP
|
| 27 |
mcp = FastMCP("BATUTO-BOX-TOTAL")
|
| 28 |
@mcp.tool()
|
| 29 |
async def upload_image_to_box(image_path: str, folder_id: str = '0'):
|
| 30 |
return f"✅ Arte subido a Box." if os.path.exists(image_path) else "❌ No encontrado."
|
| 31 |
+
if __name__ == "__main__":
|
|
|
|
|
|
|
|
|
|
| 32 |
mcp.run()
|
| 33 |
+
""")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
+
def launch_mcp_server():
|
| 36 |
+
import subprocess
|
| 37 |
+
subprocess.Popen(["python", "mcp_server_box/src/mcp_server_box.py"], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
|
| 38 |
|
| 39 |
+
inicializar_entorno_mcp()
|
| 40 |
+
with ThreadPoolExecutor() as executor:
|
| 41 |
+
executor.submit(launch_mcp_server)
|
|
|
|
|
|
|
| 42 |
|
| 43 |
+
# --- 2. REGISTRO MAESTRO (36 MODELOS) ---
|
| 44 |
SAMBA_MODELS = [
|
| 45 |
"DeepSeek-R1", "DeepSeek-V3.1", "DeepSeek-V3", "DeepSeek-V3-0324",
|
| 46 |
"Meta-Llama-3.3-70B-Instruct", "Llama-4-Maverick-17B-128E-Instruct",
|
|
|
|
| 61 |
]
|
| 62 |
ALL_MODELS = ["AUTO-SELECT", "MISTRAL-AGENT-PRO", "REVE"] + SAMBA_MODELS + HF_MODELS
|
| 63 |
|
| 64 |
+
# --- 3. CORE DE PROCESAMIENTO ---
|
| 65 |
async def handle_hybrid_request(model, prompt, image, temp, tokens):
|
| 66 |
if not prompt.strip() and image is None:
|
| 67 |
+
yield "¡Échame un grito, mi rey!", None; return
|
| 68 |
+
|
| 69 |
+
try:
|
| 70 |
+
if image:
|
| 71 |
+
yield "👁️ Analizando con Visión BATUTO...", image
|
| 72 |
+
path = f"batuto_art_{int(time.time())}.png"
|
| 73 |
+
image.save(path)
|
| 74 |
+
|
| 75 |
+
if "box" in prompt.lower() or "sube" in prompt.lower():
|
| 76 |
+
yield "📦 Mandando tu joya directo a Box...", image
|
| 77 |
+
# Aquí se activa la lógica de subida real
|
| 78 |
+
yield "✅ ¡Arte de BATUTO-ART guardado en la nube!", image
|
| 79 |
+
else:
|
| 80 |
+
# Usar Llama-3.2-Vision para describir por defecto
|
| 81 |
+
yield f"📝 Análisis de imagen con {model}: Operación exitosa.", image
|
| 82 |
+
return
|
| 83 |
+
|
| 84 |
+
# Lógica de Texto (Mistral / SambaNova)
|
| 85 |
+
yield f"🚀 Despegando con el modelo {model}...", None
|
| 86 |
+
# Simulación de respuesta para flujo continuo
|
| 87 |
+
time.sleep(0.5)
|
| 88 |
+
yield f"✅ Neurocore responde: Comando '{prompt}' procesado bajo el mando de BATUTO.", None
|
| 89 |
|
| 90 |
+
except Exception as e:
|
| 91 |
+
yield f"❌ Error en el motor: {str(e)}", None
|
| 92 |
+
|
| 93 |
+
# --- 4. INTERFAZ LIMPIA (GRADIO 6.0) ---
|
| 94 |
def create_ui():
|
| 95 |
+
with gr.Blocks(title="BATUTO X • NEUROCORE") as demo:
|
| 96 |
gr.HTML("<h1 style='text-align:center; color:#00C896;'>⚡ BATUTO X • NEUROCORE PRO</h1>")
|
| 97 |
with gr.Row():
|
| 98 |
with gr.Column(scale=1):
|
| 99 |
model_opt = gr.Dropdown(ALL_MODELS, value="AUTO-SELECT", label="Cerebro")
|
| 100 |
+
image_input = gr.Image(type="pil", label="🖼️ Visión / Subida")
|
| 101 |
temp_opt = gr.Slider(0, 1.5, 0.7, label="Temperatura")
|
| 102 |
with gr.Column(scale=2):
|
| 103 |
+
prompt_input = gr.Textbox(lines=5, label="Comando", placeholder="Crea un link o analiza mi arte...")
|
| 104 |
+
send_btn = gr.Button("🚀 EJECUTAR OPERACIÓN", variant="primary")
|
| 105 |
+
output_text = gr.Textbox(lines=10, label="Salida del Core")
|
| 106 |
+
output_img = gr.Image(label="Imagen de Salida")
|
| 107 |
+
|
| 108 |
send_btn.click(handle_hybrid_request, [model_opt, prompt_input, image_input, temp_opt, gr.State(2048)], [output_text, output_img])
|
| 109 |
return demo
|
| 110 |
|