JeCabrera commited on
Commit
3b4b4b8
verified
1 Parent(s): 8d51e47

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -22
app.py CHANGED
@@ -61,7 +61,7 @@ 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
@@ -79,33 +79,34 @@ def count_images(message, history):
79
  total_images += 1
80
  return f"You just uploaded {num_images} images, total uploaded: {total_images + num_images}"
81
 
82
- # Funci贸n que se invoca al cargar im谩genes
83
- def upload_images_handler(files, history):
84
- # Procesar las im谩genes cargadas
85
- image_paths = handle_uploaded_images(files)
86
-
87
- # Actualizar el historial con las im谩genes procesadas
88
- history.append((None, image_paths))
89
-
90
- return history, count_images({"files": files}, history)
 
 
91
 
92
  # Crear la interfaz de Gradio
93
- demo = gr.ChatInterface(
94
- fn=response, # Funci贸n de chat para manejar texto y archivos
 
 
 
 
 
 
 
 
95
  examples=[ # Ejemplos iniciales de mensajes
96
  {"text": "No files", "files": []}
97
  ],
98
- cache_examples=True,
99
- multimodal=True, # Activar la modalidad multimodal
100
- textbox=gr.MultimodalTextbox( # Configuraci贸n del cuadro de texto multimodal
101
- file_count="multiple", # Permitir m煤ltiples archivos
102
- file_types=["image"], # Aceptar solo im谩genes
103
- sources=["upload", "microphone"] # Fuentes de entrada: carga de archivos y micr贸fono
104
- )
105
  )
106
 
107
- # Conectar la funci贸n de carga de im谩genes con el evento de Gradio
108
- demo.add_event("file_upload", upload_images_handler)
109
-
110
  # Iniciar la interfaz
111
  demo.launch()
 
61
  """Procesa las im谩genes cargadas, las redimensiona y las guarda"""
62
  image_paths = []
63
  for file in files:
64
+ image = Image.open(file.name).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
 
79
  total_images += 1
80
  return f"You just uploaded {num_images} images, total uploaded: {total_images + num_images}"
81
 
82
+ # Funci贸n que maneja el texto y las im谩genes
83
+ def handle_message(message, history):
84
+ if message.get("files"):
85
+ # Procesar las im谩genes cargadas
86
+ image_paths = handle_uploaded_images(message["files"])
87
+ # A帽adir las im谩genes al historial
88
+ history.append((None, image_paths))
89
+ return history, count_images(message, history)
90
+
91
+ # Si no hay archivos, procesamos el mensaje como texto
92
+ return response(message, history)
93
 
94
  # Crear la interfaz de Gradio
95
+ demo = gr.Interface(
96
+ fn=handle_message, # Funci贸n de manejo de mensaje y archivos
97
+ inputs=[
98
+ gr.Textbox(placeholder="Escribe un mensaje..."),
99
+ gr.File(label="Subir im谩genes", file_count="multiple", file_types=["image"])
100
+ ],
101
+ outputs=[
102
+ gr.Chatbot(type="messages"), # Formato actualizado para mensajes con "role" y "content"
103
+ gr.Textbox(label="Conteo de im谩genes")
104
+ ],
105
  examples=[ # Ejemplos iniciales de mensajes
106
  {"text": "No files", "files": []}
107
  ],
108
+ cache_examples=True
 
 
 
 
 
 
109
  )
110
 
 
 
 
111
  # Iniciar la interfaz
112
  demo.launch()