Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -5,10 +5,10 @@ import os
|
|
5 |
import time
|
6 |
import uuid
|
7 |
from typing import List, Tuple, Optional, Union
|
|
|
8 |
import google.generativeai as genai
|
9 |
import gradio as gr
|
10 |
from PIL import Image
|
11 |
-
import PyPDF2
|
12 |
from dotenv import load_dotenv
|
13 |
|
14 |
# Cargar las variables de entorno desde el archivo .env
|
@@ -28,65 +28,37 @@ IMAGE_WIDTH = 512
|
|
28 |
CHAT_HISTORY = List[Tuple[Optional[Union[Tuple[str], str]], Optional[str]]]
|
29 |
|
30 |
def preprocess_image(image: Image.Image) -> Optional[Image.Image]:
|
|
|
31 |
if image:
|
32 |
image_height = int(image.height * IMAGE_WIDTH / image.width)
|
33 |
return image.resize((IMAGE_WIDTH, image_height))
|
34 |
|
35 |
def cache_pil_image(image: Image.Image) -> str:
|
|
|
36 |
image_filename = f"{uuid.uuid4()}.jpeg"
|
37 |
os.makedirs(IMAGE_CACHE_DIRECTORY, exist_ok=True)
|
38 |
image_path = os.path.join(IMAGE_CACHE_DIRECTORY, image_filename)
|
39 |
image.save(image_path, "JPEG")
|
40 |
return image_path
|
41 |
|
42 |
-
def cache_file(file) -> str:
|
43 |
-
"""Guarda el archivo tal cual en el sistema temporal."""
|
44 |
-
file_path = f"/tmp/{uuid.uuid4()}_{file.name}"
|
45 |
-
with open(file_path, "wb") as f:
|
46 |
-
f.write(file.read()) # Aqu铆 es donde 'file' puede tener el m茅todo read()
|
47 |
-
return file_path
|
48 |
-
|
49 |
-
def extract_text_from_pdf(pdf_path: str) -> str:
|
50 |
-
"""Extrae el texto de un archivo PDF."""
|
51 |
-
with open(pdf_path, 'rb') as f:
|
52 |
-
pdf_reader = PyPDF2.PdfReader(f)
|
53 |
-
text = ""
|
54 |
-
for page in pdf_reader.pages:
|
55 |
-
text += page.extract_text()
|
56 |
-
return text
|
57 |
-
|
58 |
def upload(files: Optional[List[str]], chatbot: CHAT_HISTORY) -> CHAT_HISTORY:
|
|
|
59 |
for file in files:
|
60 |
-
|
61 |
-
|
62 |
-
# Si es una imagen, la procesamos con PIL
|
63 |
-
if mime_type and mime_type.startswith('image'):
|
64 |
-
image = Image.open(file.name).convert('RGB') # Abrir el archivo con su ruta
|
65 |
image_preview = preprocess_image(image)
|
66 |
if image_preview:
|
67 |
-
#
|
68 |
gr.Image(image_preview).render()
|
69 |
image_path = cache_pil_image(image)
|
70 |
chatbot.append(((image_path,), None))
|
71 |
-
|
72 |
-
# Si es un archivo PDF, lo procesamos y extraemos el texto
|
73 |
-
elif mime_type and mime_type == "application/pdf":
|
74 |
-
pdf_content = extract_text_from_pdf(file.name)
|
75 |
-
chatbot.append((pdf_content, None))
|
76 |
-
|
77 |
-
# Si es un archivo de texto, lo procesamos directamente
|
78 |
-
elif mime_type and mime_type == "text/plain":
|
79 |
-
with open(file.name, 'r', encoding='utf-8') as f:
|
80 |
-
text_content = f.read()
|
81 |
-
chatbot.append((text_content, None))
|
82 |
-
|
83 |
else:
|
84 |
-
# Si es otro tipo de archivo, se guarda
|
85 |
-
|
86 |
-
chatbot.append(((file_path,), None))
|
87 |
return chatbot
|
88 |
|
89 |
def user(text_prompt: str, chatbot: CHAT_HISTORY):
|
|
|
90 |
if text_prompt:
|
91 |
chatbot.append((text_prompt, None))
|
92 |
return "", chatbot
|
@@ -96,22 +68,27 @@ def bot(
|
|
96 |
model_choice: str,
|
97 |
chatbot: CHAT_HISTORY
|
98 |
):
|
|
|
99 |
if not GOOGLE_API_KEY:
|
100 |
raise ValueError("GOOGLE_API_KEY is not set.")
|
101 |
-
|
102 |
# Configurar la API con la clave
|
103 |
genai.configure(api_key=GOOGLE_API_KEY)
|
104 |
generation_config = genai.types.GenerationConfig(
|
105 |
-
temperature=0.7,
|
106 |
-
max_output_tokens=8192,
|
107 |
-
top_k=10,
|
108 |
-
top_p=0.9
|
109 |
)
|
110 |
|
|
|
111 |
text_prompt = [chatbot[-1][0]] if chatbot and chatbot[-1][0] and isinstance(chatbot[-1][0], str) else []
|
112 |
-
image_prompt = [preprocess_image(Image.open(file).convert('RGB')) for file in files if file.
|
|
|
|
|
|
|
113 |
model = genai.GenerativeModel(model_choice)
|
114 |
-
response = model.generate_content(text_prompt + image_prompt, stream=True, generation_config=generation_config)
|
115 |
|
116 |
chatbot[-1][1] = ""
|
117 |
for chunk in response:
|
@@ -121,6 +98,7 @@ def bot(
|
|
121 |
time.sleep(0.01)
|
122 |
yield chatbot
|
123 |
|
|
|
124 |
chatbot_component = gr.Chatbot(
|
125 |
label='Gemini',
|
126 |
bubble_full_width=False,
|
@@ -131,7 +109,7 @@ text_prompt_component = gr.Textbox(
|
|
131 |
placeholder="Message...", show_label=False, autofocus=True, scale=8
|
132 |
)
|
133 |
upload_button_component = gr.UploadButton(
|
134 |
-
label="Upload Files", file_count="multiple", file_types=["image", "pdf"
|
135 |
)
|
136 |
run_button_component = gr.Button(value="Run", variant="primary", scale=1)
|
137 |
model_choice_component = gr.Dropdown(
|
@@ -190,3 +168,4 @@ with gr.Blocks() as demo:
|
|
190 |
|
191 |
demo.queue(max_size=99).launch(debug=False, show_error=True)
|
192 |
|
|
|
|
5 |
import time
|
6 |
import uuid
|
7 |
from typing import List, Tuple, Optional, Union
|
8 |
+
|
9 |
import google.generativeai as genai
|
10 |
import gradio as gr
|
11 |
from PIL import Image
|
|
|
12 |
from dotenv import load_dotenv
|
13 |
|
14 |
# Cargar las variables de entorno desde el archivo .env
|
|
|
28 |
CHAT_HISTORY = List[Tuple[Optional[Union[Tuple[str], str]], Optional[str]]]
|
29 |
|
30 |
def preprocess_image(image: Image.Image) -> Optional[Image.Image]:
|
31 |
+
"""Redimensiona la imagen para que se ajuste a la aplicaci贸n."""
|
32 |
if image:
|
33 |
image_height = int(image.height * IMAGE_WIDTH / image.width)
|
34 |
return image.resize((IMAGE_WIDTH, image_height))
|
35 |
|
36 |
def cache_pil_image(image: Image.Image) -> str:
|
37 |
+
"""Guarda la imagen procesada en el sistema de archivos temporal."""
|
38 |
image_filename = f"{uuid.uuid4()}.jpeg"
|
39 |
os.makedirs(IMAGE_CACHE_DIRECTORY, exist_ok=True)
|
40 |
image_path = os.path.join(IMAGE_CACHE_DIRECTORY, image_filename)
|
41 |
image.save(image_path, "JPEG")
|
42 |
return image_path
|
43 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
44 |
def upload(files: Optional[List[str]], chatbot: CHAT_HISTORY) -> CHAT_HISTORY:
|
45 |
+
"""Sube los archivos y los agrega al historial de chat."""
|
46 |
for file in files:
|
47 |
+
if file.name.endswith(('.jpg', '.jpeg', '.png')):
|
48 |
+
image = Image.open(file).convert('RGB')
|
|
|
|
|
|
|
49 |
image_preview = preprocess_image(image)
|
50 |
if image_preview:
|
51 |
+
# Muestra una vista previa de la imagen subida
|
52 |
gr.Image(image_preview).render()
|
53 |
image_path = cache_pil_image(image)
|
54 |
chatbot.append(((image_path,), None))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
55 |
else:
|
56 |
+
# Si es un PDF u otro tipo de archivo, solo se guarda la ruta
|
57 |
+
chatbot.append(((file.name,), None))
|
|
|
58 |
return chatbot
|
59 |
|
60 |
def user(text_prompt: str, chatbot: CHAT_HISTORY):
|
61 |
+
"""Procesa la entrada del usuario y la agrega al historial."""
|
62 |
if text_prompt:
|
63 |
chatbot.append((text_prompt, None))
|
64 |
return "", chatbot
|
|
|
68 |
model_choice: str,
|
69 |
chatbot: CHAT_HISTORY
|
70 |
):
|
71 |
+
"""Genera una respuesta utilizando la API de Gemini."""
|
72 |
if not GOOGLE_API_KEY:
|
73 |
raise ValueError("GOOGLE_API_KEY is not set.")
|
74 |
+
|
75 |
# Configurar la API con la clave
|
76 |
genai.configure(api_key=GOOGLE_API_KEY)
|
77 |
generation_config = genai.types.GenerationConfig(
|
78 |
+
temperature=0.7,
|
79 |
+
max_output_tokens=8192,
|
80 |
+
top_k=10,
|
81 |
+
top_p=0.9
|
82 |
)
|
83 |
|
84 |
+
# Procesar los archivos
|
85 |
text_prompt = [chatbot[-1][0]] if chatbot and chatbot[-1][0] and isinstance(chatbot[-1][0], str) else []
|
86 |
+
image_prompt = [preprocess_image(Image.open(file).convert('RGB')) for file in files if file.name.endswith(('.jpg', '.jpeg', '.png'))] if files else []
|
87 |
+
pdf_prompt = [file.name for file in files if file.name.endswith('.pdf')] if files else []
|
88 |
+
|
89 |
+
# Crear el modelo
|
90 |
model = genai.GenerativeModel(model_choice)
|
91 |
+
response = model.generate_content(text_prompt + image_prompt + pdf_prompt, stream=True, generation_config=generation_config)
|
92 |
|
93 |
chatbot[-1][1] = ""
|
94 |
for chunk in response:
|
|
|
98 |
time.sleep(0.01)
|
99 |
yield chatbot
|
100 |
|
101 |
+
# Componentes de la interfaz de usuario con Gradio
|
102 |
chatbot_component = gr.Chatbot(
|
103 |
label='Gemini',
|
104 |
bubble_full_width=False,
|
|
|
109 |
placeholder="Message...", show_label=False, autofocus=True, scale=8
|
110 |
)
|
111 |
upload_button_component = gr.UploadButton(
|
112 |
+
label="Upload Files", file_count="multiple", file_types=["image", "pdf"], scale=1
|
113 |
)
|
114 |
run_button_component = gr.Button(value="Run", variant="primary", scale=1)
|
115 |
model_choice_component = gr.Dropdown(
|
|
|
168 |
|
169 |
demo.queue(max_size=99).launch(debug=False, show_error=True)
|
170 |
|
171 |
+
|