Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -29,6 +29,9 @@ model = genai.GenerativeModel(
|
|
29 |
generation_config=generation_config,
|
30 |
)
|
31 |
|
|
|
|
|
|
|
32 |
# Constantes para el manejo de imágenes
|
33 |
IMAGE_CACHE_DIRECTORY = "/tmp"
|
34 |
IMAGE_WIDTH = 512
|
@@ -50,12 +53,29 @@ def cache_pil_image(image: Image.Image) -> str:
|
|
50 |
image.save(image_path, "JPEG")
|
51 |
return image_path
|
52 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
53 |
# Función principal para manejar las respuestas del chat
|
54 |
def response(message, history):
|
55 |
"""Maneja la interacción multimodal y envía texto e imágenes al modelo."""
|
|
|
|
|
|
|
|
|
|
|
|
|
56 |
text_prompt = message["text"]
|
57 |
files = message["files"]
|
58 |
-
|
59 |
# Procesar imágenes cargadas
|
60 |
image_prompts = []
|
61 |
if files:
|
@@ -66,13 +86,15 @@ def response(message, history):
|
|
66 |
cache_pil_image(image)
|
67 |
image_prompts.append(image_preview) # Incluir en la lista de prompts
|
68 |
|
69 |
-
#
|
70 |
prompts = [text_prompt] + image_prompts
|
71 |
-
response =
|
|
|
72 |
|
73 |
-
# Generar respuesta
|
74 |
-
for
|
75 |
-
|
|
|
76 |
|
77 |
# Crear la interfaz de usuario
|
78 |
demo = gr.ChatInterface(
|
|
|
29 |
generation_config=generation_config,
|
30 |
)
|
31 |
|
32 |
+
# Inicializar la sesión de chat
|
33 |
+
chat = model.start_chat(history=[])
|
34 |
+
|
35 |
# Constantes para el manejo de imágenes
|
36 |
IMAGE_CACHE_DIRECTORY = "/tmp"
|
37 |
IMAGE_WIDTH = 512
|
|
|
53 |
image.save(image_path, "JPEG")
|
54 |
return image_path
|
55 |
|
56 |
+
# Función para transformar el historial de Gradio al formato de Gemini
|
57 |
+
def transform_history(history):
|
58 |
+
"""Transforma el historial del formato de Gradio al formato que Gemini espera."""
|
59 |
+
new_history = []
|
60 |
+
for chat in history:
|
61 |
+
if chat[0]: # Mensaje del usuario
|
62 |
+
new_history.append({"parts": [{"text": chat[0]}], "role": "user"})
|
63 |
+
if chat[1]: # Respuesta del modelo
|
64 |
+
new_history.append({"parts": [{"text": chat[1]}], "role": "model"})
|
65 |
+
return new_history
|
66 |
+
|
67 |
# Función principal para manejar las respuestas del chat
|
68 |
def response(message, history):
|
69 |
"""Maneja la interacción multimodal y envía texto e imágenes al modelo."""
|
70 |
+
global chat
|
71 |
+
|
72 |
+
# Transformar el historial al formato esperado por Gemini
|
73 |
+
chat.history = transform_history(history)
|
74 |
+
|
75 |
+
# Obtener el texto del mensaje y las imágenes cargadas
|
76 |
text_prompt = message["text"]
|
77 |
files = message["files"]
|
78 |
+
|
79 |
# Procesar imágenes cargadas
|
80 |
image_prompts = []
|
81 |
if files:
|
|
|
86 |
cache_pil_image(image)
|
87 |
image_prompts.append(image_preview) # Incluir en la lista de prompts
|
88 |
|
89 |
+
# Combinar texto e imágenes para el modelo
|
90 |
prompts = [text_prompt] + image_prompts
|
91 |
+
response = chat.send_message(prompts)
|
92 |
+
response.resolve()
|
93 |
|
94 |
+
# Generar respuesta carácter por carácter para una experiencia más fluida
|
95 |
+
for i in range(len(response.text)):
|
96 |
+
time.sleep(0.01)
|
97 |
+
yield response.text[: i + 1]
|
98 |
|
99 |
# Crear la interfaz de usuario
|
100 |
demo = gr.ChatInterface(
|