Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,21 +1,13 @@
|
|
1 |
-
|
2 |
-
SUBTITLE = """<h2 align="center">Play with Gemini Pro and Gemini Pro Vision</h2>"""
|
3 |
-
|
4 |
-
import os
|
5 |
import time
|
6 |
-
import
|
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 |
from dotenv import load_dotenv
|
12 |
|
13 |
-
|
14 |
# Cargar las variables de entorno desde el archivo .env
|
15 |
load_dotenv()
|
16 |
|
17 |
-
print("google-generativeai:", genai.__version__)
|
18 |
-
|
19 |
# Obtener la clave de la API de las variables de entorno
|
20 |
GOOGLE_API_KEY = os.getenv("GOOGLE_API_KEY")
|
21 |
|
@@ -50,13 +42,21 @@ def transform_history(history):
|
|
50 |
new_history.append({"parts": [{"text": chat_entry[1]}], "role": "model"})
|
51 |
return new_history
|
52 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
53 |
# Función de respuesta que maneja el historial
|
54 |
def bot_response(
|
55 |
model_choice: str,
|
56 |
system_instruction: str,
|
57 |
text_prompt: str,
|
58 |
chatbot: list,
|
59 |
-
) -> Tuple[list, str]:
|
60 |
"""
|
61 |
Envía el mensaje al modelo, obtiene la respuesta y actualiza el historial.
|
62 |
"""
|
@@ -77,7 +77,13 @@ def bot_response(
|
|
77 |
generated_text = response.text
|
78 |
|
79 |
# Actualizar el historial con la pregunta y la respuesta
|
80 |
-
chatbot.append((text_prompt,
|
|
|
|
|
|
|
|
|
|
|
|
|
81 |
|
82 |
return chatbot, ""
|
83 |
|
|
|
1 |
+
from typing import List, Tuple # Asegúrate de importar Tuple aquí
|
|
|
|
|
|
|
2 |
import time
|
3 |
+
import os
|
|
|
4 |
import google.generativeai as genai
|
5 |
import gradio as gr
|
|
|
6 |
from dotenv import load_dotenv
|
7 |
|
|
|
8 |
# Cargar las variables de entorno desde el archivo .env
|
9 |
load_dotenv()
|
10 |
|
|
|
|
|
11 |
# Obtener la clave de la API de las variables de entorno
|
12 |
GOOGLE_API_KEY = os.getenv("GOOGLE_API_KEY")
|
13 |
|
|
|
42 |
new_history.append({"parts": [{"text": chat_entry[1]}], "role": "model"})
|
43 |
return new_history
|
44 |
|
45 |
+
# Función para simular el efecto de escribir carácter por carácter
|
46 |
+
def display_typing_effect(response_text: str, chatbot: list) -> list:
|
47 |
+
chatbot[-1][1] = "" # Borrar la última respuesta en el chatbot
|
48 |
+
for i in range(len(response_text)):
|
49 |
+
chatbot[-1][1] += response_text[i] # Agregar un carácter
|
50 |
+
time.sleep(0.05) # Controlar la velocidad de escritura
|
51 |
+
yield chatbot # Mostrar el progreso en el UI
|
52 |
+
|
53 |
# Función de respuesta que maneja el historial
|
54 |
def bot_response(
|
55 |
model_choice: str,
|
56 |
system_instruction: str,
|
57 |
text_prompt: str,
|
58 |
chatbot: list,
|
59 |
+
) -> Tuple[list, str]: # Aquí se usa Tuple correctamente
|
60 |
"""
|
61 |
Envía el mensaje al modelo, obtiene la respuesta y actualiza el historial.
|
62 |
"""
|
|
|
77 |
generated_text = response.text
|
78 |
|
79 |
# Actualizar el historial con la pregunta y la respuesta
|
80 |
+
chatbot.append((text_prompt, "")) # Inicializar la respuesta vacía
|
81 |
+
|
82 |
+
# Mostrar la respuesta con efecto de escritura
|
83 |
+
yield from display_typing_effect(generated_text, chatbot)
|
84 |
+
|
85 |
+
# Actualizar el historial con la respuesta final
|
86 |
+
chatbot[-1] = (text_prompt, generated_text)
|
87 |
|
88 |
return chatbot, ""
|
89 |
|