Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -10,6 +10,7 @@ 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
|
15 |
load_dotenv()
|
@@ -39,15 +40,38 @@ def cache_pil_image(image: Image.Image) -> str:
|
|
39 |
image.save(image_path, "JPEG")
|
40 |
return image_path
|
41 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
42 |
def upload(files: Optional[List[str]], chatbot: CHAT_HISTORY) -> CHAT_HISTORY:
|
43 |
for file in files:
|
44 |
-
|
45 |
-
|
46 |
-
if
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
51 |
return chatbot
|
52 |
|
53 |
def user(text_prompt: str, chatbot: CHAT_HISTORY):
|
@@ -95,7 +119,7 @@ text_prompt_component = gr.Textbox(
|
|
95 |
placeholder="Message...", show_label=False, autofocus=True, scale=8
|
96 |
)
|
97 |
upload_button_component = gr.UploadButton(
|
98 |
-
label="Upload
|
99 |
)
|
100 |
run_button_component = gr.Button(value="Run", variant="primary", scale=1)
|
101 |
model_choice_component = gr.Dropdown(
|
|
|
10 |
import gradio as gr
|
11 |
from PIL import Image
|
12 |
from dotenv import load_dotenv
|
13 |
+
from PyPDF2 import PdfReader
|
14 |
|
15 |
# Cargar las variables de entorno desde el archivo .env
|
16 |
load_dotenv()
|
|
|
40 |
image.save(image_path, "JPEG")
|
41 |
return image_path
|
42 |
|
43 |
+
def process_text_file(file_path: str):
|
44 |
+
with open(file_path, 'r') as file:
|
45 |
+
return file.read()
|
46 |
+
|
47 |
+
def process_pdf_file(file_path: str):
|
48 |
+
reader = PdfReader(file_path)
|
49 |
+
text = ""
|
50 |
+
for page in reader.pages:
|
51 |
+
text += page.extract_text()
|
52 |
+
return text
|
53 |
+
|
54 |
def upload(files: Optional[List[str]], chatbot: CHAT_HISTORY) -> CHAT_HISTORY:
|
55 |
for file in files:
|
56 |
+
file_extension = file.split('.')[-1].lower()
|
57 |
+
|
58 |
+
if file_extension in ['jpg', 'jpeg', 'png', 'gif', 'bmp']: # Imágenes
|
59 |
+
image = Image.open(file).convert('RGB')
|
60 |
+
image_preview = preprocess_image(image)
|
61 |
+
if image_preview:
|
62 |
+
# Display a preview of the uploaded image
|
63 |
+
gr.Image(image_preview).render()
|
64 |
+
image_path = cache_pil_image(image)
|
65 |
+
chatbot.append(((image_path,), None))
|
66 |
+
|
67 |
+
elif file_extension in ['txt']: # Archivos de texto
|
68 |
+
text_content = process_text_file(file)
|
69 |
+
chatbot.append(((text_content,), None))
|
70 |
+
|
71 |
+
elif file_extension in ['pdf']: # Archivos PDF
|
72 |
+
pdf_content = process_pdf_file(file)
|
73 |
+
chatbot.append(((pdf_content,), None))
|
74 |
+
|
75 |
return chatbot
|
76 |
|
77 |
def user(text_prompt: str, chatbot: CHAT_HISTORY):
|
|
|
119 |
placeholder="Message...", show_label=False, autofocus=True, scale=8
|
120 |
)
|
121 |
upload_button_component = gr.UploadButton(
|
122 |
+
label="Upload Files", file_count="multiple", file_types=["image", "text", "pdf"], scale=1
|
123 |
)
|
124 |
run_button_component = gr.Button(value="Run", variant="primary", scale=1)
|
125 |
model_choice_component = gr.Dropdown(
|