Upload app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
from tavily import TavilyClient
|
| 4 |
+
from diffusers import StableDiffusionPipeline
|
| 5 |
+
from transformers import BlipProcessor, BlipForConditionalGeneration
|
| 6 |
+
from PIL import Image
|
| 7 |
+
import os, json
|
| 8 |
+
|
| 9 |
+
# Detectar hardware y estado GPU
|
| 10 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 11 |
+
|
| 12 |
+
# Tavily API
|
| 13 |
+
TAVILY_API_KEY = os.environ.get("TAVILY_API_KEY", "TU_API_KEY_AQUI")
|
| 14 |
+
tavily_client = TavilyClient(api_key=TAVILY_API_KEY)
|
| 15 |
+
|
| 16 |
+
# Imagen: modelos reales
|
| 17 |
+
pipe_sd = StableDiffusionPipeline.from_pretrained(
|
| 18 |
+
"stabilityai/stable-diffusion-2-1",
|
| 19 |
+
torch_dtype=torch.float16 if device == "cuda" else torch.float32
|
| 20 |
+
).to(device)
|
| 21 |
+
blip_processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-base")
|
| 22 |
+
blip_model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-base").to(device)
|
| 23 |
+
|
| 24 |
+
# Historial de chat real
|
| 25 |
+
def guardar_historial(historial, user_id="default"):
|
| 26 |
+
with open(f'/tmp/history_{user_id}.json', 'w') as f:
|
| 27 |
+
json.dump(historial, f)
|
| 28 |
+
|
| 29 |
+
def cargar_historial(user_id="default"):
|
| 30 |
+
try:
|
| 31 |
+
with open(f'/tmp/history_{user_id}.json', 'r') as f:
|
| 32 |
+
return json.load(f)
|
| 33 |
+
except FileNotFoundError:
|
| 34 |
+
return []
|
| 35 |
+
|
| 36 |
+
# Módulo de chat / integración básica Hugging Face
|
| 37 |
+
def chat_respuesta(input_text, history):
|
| 38 |
+
# Integración con modelo de texto: reemplaza por tu modelo preferido
|
| 39 |
+
respuesta = f"Chatbot real para: {input_text}" # Aquí puedes poner tu modelo Hugging Face
|
| 40 |
+
history.append((input_text, respuesta))
|
| 41 |
+
guardar_historial(history)
|
| 42 |
+
return "", history
|
| 43 |
+
|
| 44 |
+
# Generación real de imágenes
|
| 45 |
+
def generacion_imagenes(prompt):
|
| 46 |
+
image = pipe_sd(prompt).images[0]
|
| 47 |
+
path = "/tmp/generated_img.png"
|
| 48 |
+
image.save(path)
|
| 49 |
+
return Image.open(path)
|
| 50 |
+
|
| 51 |
+
# Análisis real de imágenes (captioning)
|
| 52 |
+
def analizar_imagen(image):
|
| 53 |
+
inputs = blip_processor(images=image, return_tensors="pt").to(device)
|
| 54 |
+
out = blip_model.generate(**inputs)
|
| 55 |
+
caption = blip_processor.decode(out[0], skip_special_tokens=True)
|
| 56 |
+
return caption
|
| 57 |
+
|
| 58 |
+
# Búsqueda técnica Tavily real
|
| 59 |
+
def busqueda_tecnica(query):
|
| 60 |
+
result = tavily_client.search(query, max_results=3)
|
| 61 |
+
resumen = "\n\n".join([f"{r['title']}: {r['content'][:200]}" for r in result.get('results', [])])
|
| 62 |
+
return resumen or "No se encontraron resultados."
|
| 63 |
+
|
| 64 |
+
# Interfaz Gradio
|
| 65 |
+
def pipeline(text, imagen, history):
|
| 66 |
+
response_text = ""
|
| 67 |
+
response_img = None
|
| 68 |
+
response_caption = ""
|
| 69 |
+
|
| 70 |
+
if text:
|
| 71 |
+
if text.lower().startswith("buscar:"):
|
| 72 |
+
response_text = busqueda_tecnica(text.replace("Buscar:", "").strip())
|
| 73 |
+
else:
|
| 74 |
+
_, history = chat_respuesta(text, history)
|
| 75 |
+
response_text = history[-1][1]
|
| 76 |
+
if imagen:
|
| 77 |
+
response_caption = analizar_imagen(imagen)
|
| 78 |
+
# Genera imagen a partir del caption como prompt si lo deseas
|
| 79 |
+
response_img = generacion_imagenes(response_caption)
|
| 80 |
+
return response_text, response_img, response_caption, history
|
| 81 |
+
|
| 82 |
+
iface = gr.Interface(
|
| 83 |
+
fn=pipeline,
|
| 84 |
+
inputs=[gr.Textbox(label="Texto o consulta"), gr.Image(label="Imagen para análisis"), gr.State([])],
|
| 85 |
+
outputs=[
|
| 86 |
+
gr.Textbox(label="Respuesta o búsqueda"),
|
| 87 |
+
gr.Image(label="Imagen generada (si aplica)"),
|
| 88 |
+
gr.Textbox(label="Caption/Prompt auto-generado (si aplica)"),
|
| 89 |
+
gr.State()
|
| 90 |
+
],
|
| 91 |
+
title="BATUTO_INFINITY"
|
| 92 |
+
)
|
| 93 |
+
|
| 94 |
+
if __name__ == "__main__":
|
| 95 |
+
iface.launch()
|
| 96 |
+
|