Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -6,6 +6,7 @@ from PIL import Image
|
|
6 |
import google.generativeai as genai
|
7 |
import gradio as gr
|
8 |
from dotenv import load_dotenv
|
|
|
9 |
|
10 |
# Cargar las variables de entorno desde el archivo .env
|
11 |
load_dotenv()
|
@@ -32,26 +33,16 @@ model = genai.GenerativeModel(
|
|
32 |
# Inicializar la sesi贸n de chat
|
33 |
chat = model.start_chat(history=[])
|
34 |
|
35 |
-
#
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
return image.resize((IMAGE_WIDTH, image_height))
|
46 |
-
|
47 |
-
# Funci贸n para almacenar una imagen en cach茅
|
48 |
-
def cache_pil_image(image: Image.Image) -> str:
|
49 |
-
"""Guarda la imagen como archivo JPEG en un directorio temporal."""
|
50 |
-
image_filename = f"{uuid.uuid4()}.jpeg"
|
51 |
-
os.makedirs(IMAGE_CACHE_DIRECTORY, exist_ok=True)
|
52 |
-
image_path = os.path.join(IMAGE_CACHE_DIRECTORY, image_filename)
|
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):
|
@@ -81,16 +72,16 @@ def response(message, history):
|
|
81 |
if files:
|
82 |
for file in files:
|
83 |
image = Image.open(file).convert('RGB')
|
84 |
-
|
85 |
-
|
86 |
-
image_path = cache_pil_image(image) # Convertir imagen a ruta
|
87 |
-
image_prompts.append(image_path) # Usar la ruta en lugar del objeto PIL.Image
|
88 |
|
89 |
-
#
|
90 |
-
|
|
|
|
|
91 |
|
92 |
-
#
|
93 |
-
response = chat.send_message({"
|
94 |
response.resolve()
|
95 |
|
96 |
# Generar respuesta car谩cter por car谩cter para una experiencia m谩s fluida
|
|
|
6 |
import google.generativeai as genai
|
7 |
import gradio as gr
|
8 |
from dotenv import load_dotenv
|
9 |
+
import base64
|
10 |
|
11 |
# Cargar las variables de entorno desde el archivo .env
|
12 |
load_dotenv()
|
|
|
33 |
# Inicializar la sesi贸n de chat
|
34 |
chat = model.start_chat(history=[])
|
35 |
|
36 |
+
# Funci贸n para transformar una imagen en un blob compatible con Gemini
|
37 |
+
def image_to_blob(image: Image.Image) -> dict:
|
38 |
+
"""Convierte una imagen a un blob compatible con Gemini."""
|
39 |
+
buffered = io.BytesIO()
|
40 |
+
image.save(buffered, format="JPEG")
|
41 |
+
image_data = base64.b64encode(buffered.getvalue()).decode()
|
42 |
+
return {
|
43 |
+
"mime_type": "image/jpeg",
|
44 |
+
"data": image_data
|
45 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
46 |
|
47 |
# Funci贸n para transformar el historial de Gradio al formato de Gemini
|
48 |
def transform_history(history):
|
|
|
72 |
if files:
|
73 |
for file in files:
|
74 |
image = Image.open(file).convert('RGB')
|
75 |
+
image_blob = image_to_blob(image)
|
76 |
+
image_prompts.append(image_blob)
|
|
|
|
|
77 |
|
78 |
+
# Crear las partes para el modelo
|
79 |
+
parts = [{"text": text_prompt}]
|
80 |
+
for img_blob in image_prompts:
|
81 |
+
parts.append({"inline_data": img_blob})
|
82 |
|
83 |
+
# Enviar el mensaje al modelo
|
84 |
+
response = chat.send_message({"parts": parts})
|
85 |
response.resolve()
|
86 |
|
87 |
# Generar respuesta car谩cter por car谩cter para una experiencia m谩s fluida
|