Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,19 +1,81 @@
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
|
|
2 |
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import time
|
3 |
+
import google.generativeai as genai
|
4 |
import gradio as gr
|
5 |
+
from dotenv import load_dotenv
|
6 |
|
7 |
+
# Cargar las variables de entorno
|
8 |
+
load_dotenv()
|
9 |
+
API_KEY = os.getenv("GOOGLE_API_KEY")
|
10 |
+
|
11 |
+
if not API_KEY:
|
12 |
+
raise ValueError("La clave de API 'GOOGLE_API_KEY' no est谩 configurada en el archivo .env")
|
13 |
+
|
14 |
+
# Configuraci贸n del modelo Gemini
|
15 |
+
generation_config = {
|
16 |
+
"temperature": 1,
|
17 |
+
"top_p": 0.95,
|
18 |
+
"top_k": 40,
|
19 |
+
"max_output_tokens": 8192,
|
20 |
+
"response_mime_type": "text/plain",
|
21 |
+
}
|
22 |
+
|
23 |
+
genai.configure(api_key=API_KEY)
|
24 |
+
|
25 |
+
model = genai.GenerativeModel(
|
26 |
+
model_name="gemini-1.5-flash",
|
27 |
+
generation_config=generation_config,
|
28 |
+
)
|
29 |
+
|
30 |
+
# Inicializar la sesi贸n de chat
|
31 |
+
chat = model.start_chat(history=[])
|
32 |
+
|
33 |
+
# Funci贸n para transformar el historial de Gradio al formato de Gemini
|
34 |
+
def transform_history(history):
|
35 |
+
new_history = []
|
36 |
+
for chat in history:
|
37 |
+
new_history.append({"parts": [{"text": chat[0]}], "role": "user"})
|
38 |
+
new_history.append({"parts": [{"text": chat[1]}], "role": "model"})
|
39 |
+
return new_history
|
40 |
+
|
41 |
+
# Funci贸n de respuesta que maneja el texto y los archivos multimodales
|
42 |
+
def response(message, history):
|
43 |
+
global chat
|
44 |
+
|
45 |
+
# Transformar el historial al formato esperado por Gemini
|
46 |
+
chat.history = transform_history(history)
|
47 |
+
|
48 |
+
# Enviar el mensaje al modelo y obtener la respuesta
|
49 |
+
response = chat.send_message(message["text"])
|
50 |
+
response.resolve()
|
51 |
+
|
52 |
+
# Mostrar la respuesta car谩cter por car谩cter
|
53 |
+
for i in range(len(response.text)):
|
54 |
+
time.sleep(0.01)
|
55 |
+
yield response.text[: i + 1]
|
56 |
+
|
57 |
+
# Funci贸n para manejar los archivos cargados y las im谩genes
|
58 |
+
def count_images(message, history):
|
59 |
+
num_images = len(message["files"]) # Contar las im谩genes cargadas en el mensaje actual
|
60 |
+
total_images = 0
|
61 |
+
for msg in history: # Contar todas las im谩genes en el historial
|
62 |
+
if isinstance(msg["content"], tuple): # Si el contenido es una tupla, es un archivo
|
63 |
+
total_images += 1
|
64 |
+
return f"You just uploaded {num_images} images, total uploaded: {total_images + num_images}"
|
65 |
+
|
66 |
+
# Crear la interfaz de Gradio
|
67 |
+
demo = gr.ChatInterface(
|
68 |
+
response, # Funci贸n de chat para manejar texto y archivos
|
69 |
+
examples=[ # Ejemplos iniciales de mensajes
|
70 |
+
{"text": "No files", "files": []}
|
71 |
+
],
|
72 |
+
multimodal=True, # Activar la modalidad multimodal
|
73 |
+
textbox=gr.MultimodalTextbox( # Configuraci贸n del cuadro de texto multimodal
|
74 |
+
file_count="multiple", # Permitir m煤ltiples archivos
|
75 |
+
file_types=["image"], # Aceptar solo im谩genes
|
76 |
+
sources=["upload", "microphone"] # Fuentes de entrada: carga de archivos y micr贸fono
|
77 |
+
)
|
78 |
+
)
|
79 |
+
|
80 |
+
# Iniciar la interfaz
|
81 |
+
demo.launch()
|