Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,179 +1,87 @@
|
|
1 |
-
TITLE = """<h1 align="center">Gemini Playground ✨</h1>"""
|
2 |
-
SUBTITLE = """<h2 align="center">Play with Gemini Pro and Gemini Pro Vision</h2>"""
|
3 |
-
|
4 |
import os
|
5 |
import time
|
6 |
-
import uuid
|
7 |
-
from typing import List, Tuple, Optional, Union
|
8 |
-
|
9 |
import google.generativeai as genai
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
print("
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
reader = PdfReader(file_path)
|
49 |
-
text = ""
|
50 |
-
for page in reader.pages:
|
51 |
-
text += page.extract_text()
|
52 |
-
return text
|
53 |
|
54 |
-
def
|
|
|
|
|
55 |
for file in files:
|
56 |
-
file_extension =
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
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 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
model_choice: str,
|
85 |
-
chatbot: CHAT_HISTORY
|
86 |
-
):
|
87 |
-
if not GOOGLE_API_KEY:
|
88 |
-
raise ValueError("GOOGLE_API_KEY is not set.")
|
89 |
-
|
90 |
-
# Configurar la API con la clave
|
91 |
-
genai.configure(api_key=GOOGLE_API_KEY)
|
92 |
-
generation_config = genai.types.GenerationConfig(
|
93 |
-
temperature=0.7, # Valor predeterminado
|
94 |
-
max_output_tokens=8192, # Fijar el límite de tokens a 8,192
|
95 |
-
top_k=10, # Valor predeterminado
|
96 |
-
top_p=0.9 # Valor predeterminado
|
97 |
-
)
|
98 |
-
|
99 |
-
text_prompt = [chatbot[-1][0]] if chatbot and chatbot[-1][0] and isinstance(chatbot[-1][0], str) else []
|
100 |
-
image_prompt = [preprocess_image(Image.open(file).convert('RGB')) for file in files] if files else []
|
101 |
-
model = genai.GenerativeModel(model_choice)
|
102 |
-
response = model.generate_content(text_prompt + image_prompt, stream=True, generation_config=generation_config)
|
103 |
-
|
104 |
-
chatbot[-1][1] = ""
|
105 |
-
for chunk in response:
|
106 |
-
for i in range(0, len(chunk.text), 10):
|
107 |
-
section = chunk.text[i:i + 10]
|
108 |
-
chatbot[-1][1] += section
|
109 |
-
time.sleep(0.01)
|
110 |
-
yield chatbot
|
111 |
-
|
112 |
-
chatbot_component = gr.Chatbot(
|
113 |
-
label='Gemini',
|
114 |
-
bubble_full_width=False,
|
115 |
-
scale=2,
|
116 |
-
height=300
|
117 |
-
)
|
118 |
-
text_prompt_component = gr.Textbox(
|
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(
|
126 |
-
choices=["gemini-1.5-flash", "gemini-2.0-flash-exp", "gemini-1.5-pro"],
|
127 |
-
value="gemini-1.5-flash",
|
128 |
-
label="Select Model",
|
129 |
-
scale=2
|
130 |
-
)
|
131 |
-
|
132 |
-
user_inputs = [
|
133 |
-
text_prompt_component,
|
134 |
-
chatbot_component
|
135 |
]
|
136 |
|
137 |
-
|
138 |
-
|
139 |
-
model_choice_component,
|
140 |
-
chatbot_component
|
141 |
-
]
|
142 |
-
|
143 |
-
with gr.Blocks() as demo:
|
144 |
-
gr.HTML(TITLE)
|
145 |
-
gr.HTML(SUBTITLE)
|
146 |
-
with gr.Column():
|
147 |
-
chatbot_component.render()
|
148 |
-
with gr.Row():
|
149 |
-
text_prompt_component.render()
|
150 |
-
upload_button_component.render()
|
151 |
-
run_button_component.render()
|
152 |
-
model_choice_component.render()
|
153 |
|
154 |
-
|
155 |
-
|
156 |
-
inputs=user_inputs,
|
157 |
-
outputs=[text_prompt_component, chatbot_component],
|
158 |
-
queue=False
|
159 |
-
).then(
|
160 |
-
fn=bot, inputs=bot_inputs, outputs=[chatbot_component],
|
161 |
-
)
|
162 |
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
)
|
171 |
|
172 |
-
|
173 |
-
|
174 |
-
inputs=[upload_button_component, chatbot_component],
|
175 |
-
outputs=[chatbot_component],
|
176 |
-
queue=False
|
177 |
-
)
|
178 |
|
179 |
-
|
|
|
|
|
|
|
|
|
|
1 |
import os
|
2 |
import time
|
|
|
|
|
|
|
3 |
import google.generativeai as genai
|
4 |
+
from typing import List
|
5 |
+
|
6 |
+
# Configurar la API con la clave de entorno
|
7 |
+
genai.configure(api_key=os.environ["GEMINI_API_KEY"])
|
8 |
+
|
9 |
+
def upload_to_gemini(path, mime_type=None):
|
10 |
+
"""Sube el archivo dado a Gemini."""
|
11 |
+
file = genai.upload_file(path, mime_type=mime_type)
|
12 |
+
print(f"Uploaded file '{file.display_name}' as: {file.uri}")
|
13 |
+
return file
|
14 |
+
|
15 |
+
def wait_for_files_active(files: List):
|
16 |
+
"""Espera que los archivos cargados estén activos y procesados."""
|
17 |
+
print("Waiting for file processing...")
|
18 |
+
for name in (file.name for file in files):
|
19 |
+
file = genai.get_file(name)
|
20 |
+
while file.state.name == "PROCESSING":
|
21 |
+
print(".", end="", flush=True)
|
22 |
+
time.sleep(10)
|
23 |
+
file = genai.get_file(name)
|
24 |
+
if file.state.name != "ACTIVE":
|
25 |
+
raise Exception(f"File {file.name} failed to process")
|
26 |
+
print("...all files ready")
|
27 |
+
print()
|
28 |
+
|
29 |
+
# Configuración del modelo y parámetros de generación
|
30 |
+
generation_config = {
|
31 |
+
"temperature": 1,
|
32 |
+
"top_p": 0.95,
|
33 |
+
"top_k": 40,
|
34 |
+
"max_output_tokens": 8192,
|
35 |
+
"response_mime_type": "text/plain",
|
36 |
+
}
|
37 |
+
|
38 |
+
model = genai.GenerativeModel(
|
39 |
+
model_name="gemini-1.5-flash",
|
40 |
+
generation_config=generation_config,
|
41 |
+
)
|
|
|
|
|
|
|
|
|
|
|
42 |
|
43 |
+
def upload_files(files: List[str]) -> List:
|
44 |
+
"""Sube múltiples archivos (imágenes, PDFs, etc.) a Gemini."""
|
45 |
+
uploaded_files = []
|
46 |
for file in files:
|
47 |
+
file_extension = os.path.splitext(file)[1].lower()
|
48 |
+
if file_extension in [".jpg", ".jpeg", ".png", ".gif"]:
|
49 |
+
# Para imágenes, usamos el mime_type correspondiente
|
50 |
+
mime_type = "image/jpeg" if file_extension in [".jpg", ".jpeg"] else "image/png"
|
51 |
+
elif file_extension == ".pdf":
|
52 |
+
mime_type = "application/pdf"
|
53 |
+
elif file_extension in [".txt", ".csv"]:
|
54 |
+
mime_type = "text/plain"
|
55 |
+
else:
|
56 |
+
mime_type = None # Para otros tipos de archivo, determinamos el mime_type si es necesario
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
57 |
|
58 |
+
uploaded_files.append(upload_to_gemini(file, mime_type))
|
59 |
+
|
60 |
+
return uploaded_files
|
61 |
+
|
62 |
+
# Archivos a cargar (Ejemplo: agregar los archivos a subir)
|
63 |
+
files_to_upload = [
|
64 |
+
"Report 2025 - Jan 15th.pdf", # Ejemplo de archivo PDF
|
65 |
+
"image_example.jpg", # Ejemplo de archivo de imagen
|
66 |
+
"text_example.txt", # Ejemplo de archivo de texto
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
67 |
]
|
68 |
|
69 |
+
# Subir los archivos
|
70 |
+
uploaded_files = upload_files(files_to_upload)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
71 |
|
72 |
+
# Esperar a que los archivos sean procesados y estén activos
|
73 |
+
wait_for_files_active(uploaded_files)
|
|
|
|
|
|
|
|
|
|
|
|
|
74 |
|
75 |
+
# Iniciar sesión de chat con el modelo
|
76 |
+
chat_session = model.start_chat(
|
77 |
+
history=[{
|
78 |
+
"role": "user",
|
79 |
+
"parts": uploaded_files, # Incluir archivos subidos en la conversación
|
80 |
+
}],
|
81 |
+
)
|
|
|
82 |
|
83 |
+
# Enviar mensaje al modelo
|
84 |
+
response = chat_session.send_message("INSERT_INPUT_HERE")
|
|
|
|
|
|
|
|
|
85 |
|
86 |
+
# Imprimir la respuesta
|
87 |
+
print(response.text)
|