Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,7 +1,9 @@
|
|
1 |
import os
|
2 |
import time
|
|
|
3 |
import google.generativeai as genai
|
4 |
import gradio as gr
|
|
|
5 |
from dotenv import load_dotenv
|
6 |
|
7 |
# Cargar las variables de entorno
|
@@ -54,7 +56,21 @@ def response(message, history):
|
|
54 |
time.sleep(0.01)
|
55 |
yield response.text[: i + 1]
|
56 |
|
57 |
-
# Funci贸n para manejar
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
58 |
def count_images(message, history):
|
59 |
num_images = len(message["files"]) # Contar las im谩genes cargadas en el mensaje actual
|
60 |
total_images = 0
|
@@ -76,5 +92,18 @@ demo = gr.ChatInterface(
|
|
76 |
)
|
77 |
)
|
78 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
79 |
# Iniciar la interfaz
|
80 |
demo.launch()
|
|
|
1 |
import os
|
2 |
import time
|
3 |
+
import uuid
|
4 |
import google.generativeai as genai
|
5 |
import gradio as gr
|
6 |
+
from PIL import Image
|
7 |
from dotenv import load_dotenv
|
8 |
|
9 |
# Cargar las variables de entorno
|
|
|
56 |
time.sleep(0.01)
|
57 |
yield response.text[: i + 1]
|
58 |
|
59 |
+
# Funci贸n para manejar las im谩genes cargadas
|
60 |
+
def handle_uploaded_images(files):
|
61 |
+
"""Procesa las im谩genes cargadas, las redimensiona y las guarda"""
|
62 |
+
image_paths = []
|
63 |
+
for file in files:
|
64 |
+
image = Image.open(file).convert('RGB') # Abrir y convertir la imagen a RGB
|
65 |
+
image_height = int(image.height * 512 / image.width) # Mantener la proporci贸n
|
66 |
+
image_resized = image.resize((512, image_height)) # Redimensionar la imagen
|
67 |
+
image_filename = f"{uuid.uuid4()}.jpeg" # Crear un nombre 煤nico para la imagen
|
68 |
+
image_path = f"/tmp/{image_filename}" # Ruta de almacenamiento temporal
|
69 |
+
image_resized.save(image_path, "JPEG") # Guardar la imagen
|
70 |
+
image_paths.append(image_path) # Agregar la ruta al resultado
|
71 |
+
return image_paths
|
72 |
+
|
73 |
+
# Funci贸n para contar las im谩genes cargadas
|
74 |
def count_images(message, history):
|
75 |
num_images = len(message["files"]) # Contar las im谩genes cargadas en el mensaje actual
|
76 |
total_images = 0
|
|
|
92 |
)
|
93 |
)
|
94 |
|
95 |
+
# Funci贸n que se invoca al cargar im谩genes
|
96 |
+
def upload_images_handler(message, history):
|
97 |
+
# Procesar las im谩genes cargadas
|
98 |
+
image_paths = handle_uploaded_images(message["files"])
|
99 |
+
|
100 |
+
# Actualizar el historial con las im谩genes procesadas
|
101 |
+
history.append((None, image_paths))
|
102 |
+
|
103 |
+
return history, count_images(message, history)
|
104 |
+
|
105 |
+
# Conectar la funci贸n de carga de im谩genes con el evento de Gradio
|
106 |
+
demo.upload(upload_images_handler, inputs=[gr.Files()], outputs=[gr.Chatbot()])
|
107 |
+
|
108 |
# Iniciar la interfaz
|
109 |
demo.launch()
|