Update app.py
Browse files
app.py
CHANGED
@@ -1,9 +1,8 @@
|
|
1 |
TITLE = """<h1 align="center">Gemini Playground ✨</h1>"""
|
2 |
SUBTITLE = """<h2 align="center">Play with Gemini Pro and Gemini Pro Vision</h2>"""
|
3 |
|
4 |
-
from typing import List, Tuple # Asegúrate de importar Tuple aquí
|
5 |
-
import time
|
6 |
import os
|
|
|
7 |
import google.generativeai as genai
|
8 |
import gradio as gr
|
9 |
from dotenv import load_dotenv
|
@@ -11,6 +10,8 @@ from dotenv import load_dotenv
|
|
11 |
# Cargar las variables de entorno desde el archivo .env
|
12 |
load_dotenv()
|
13 |
|
|
|
|
|
14 |
# Obtener la clave de la API de las variables de entorno
|
15 |
GOOGLE_API_KEY = os.getenv("GOOGLE_API_KEY")
|
16 |
|
@@ -45,21 +46,13 @@ def transform_history(history):
|
|
45 |
new_history.append({"parts": [{"text": chat_entry[1]}], "role": "model"})
|
46 |
return new_history
|
47 |
|
48 |
-
# Función para simular el efecto de escribir carácter por carácter
|
49 |
-
def display_typing_effect(response_text: str, chatbot: List[Tuple[str, str]]) -> List[Tuple[str, str]]:
|
50 |
-
chatbot[-1] = (chatbot[-1][0], "") # Borrar la última respuesta en el chatbot (se modifica correctamente)
|
51 |
-
for i in range(len(response_text)):
|
52 |
-
chatbot[-1] = (chatbot[-1][0], chatbot[-1][1] + response_text[i]) # Modificar el texto de la respuesta
|
53 |
-
time.sleep(0.05) # Controlar la velocidad de escritura
|
54 |
-
yield chatbot # Mostrar el progreso en el UI
|
55 |
-
|
56 |
# Función de respuesta que maneja el historial
|
57 |
def bot_response(
|
58 |
model_choice: str,
|
59 |
system_instruction: str,
|
60 |
text_prompt: str,
|
61 |
-
chatbot:
|
62 |
-
) -> Tuple[
|
63 |
"""
|
64 |
Envía el mensaje al modelo, obtiene la respuesta y actualiza el historial.
|
65 |
"""
|
@@ -80,13 +73,7 @@ def bot_response(
|
|
80 |
generated_text = response.text
|
81 |
|
82 |
# Actualizar el historial con la pregunta y la respuesta
|
83 |
-
chatbot.append((text_prompt,
|
84 |
-
|
85 |
-
# Mostrar la respuesta con efecto de escritura
|
86 |
-
yield from display_typing_effect(generated_text, chatbot)
|
87 |
-
|
88 |
-
# Actualizar el historial con la respuesta final
|
89 |
-
chatbot[-1] = (text_prompt, generated_text)
|
90 |
|
91 |
return chatbot, ""
|
92 |
|
@@ -109,8 +96,8 @@ system_instruction_component = gr.Textbox(
|
|
109 |
|
110 |
# Definir la interfaz
|
111 |
with gr.Blocks() as demo:
|
112 |
-
gr.HTML(
|
113 |
-
gr.HTML(
|
114 |
with gr.Column():
|
115 |
model_dropdown_component.render()
|
116 |
chatbot_component.render()
|
@@ -136,4 +123,3 @@ with gr.Blocks() as demo:
|
|
136 |
# Lanzar la aplicación
|
137 |
if __name__ == "__main__":
|
138 |
demo.queue(max_size=99).launch(debug=True, show_error=True)
|
139 |
-
|
|
|
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 google.generativeai as genai
|
7 |
import gradio as gr
|
8 |
from dotenv import load_dotenv
|
|
|
10 |
# Cargar las variables de entorno desde el archivo .env
|
11 |
load_dotenv()
|
12 |
|
13 |
+
print("google-generativeai:", genai.__version__)
|
14 |
+
|
15 |
# Obtener la clave de la API de las variables de entorno
|
16 |
GOOGLE_API_KEY = os.getenv("GOOGLE_API_KEY")
|
17 |
|
|
|
46 |
new_history.append({"parts": [{"text": chat_entry[1]}], "role": "model"})
|
47 |
return new_history
|
48 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
49 |
# Función de respuesta que maneja el historial
|
50 |
def bot_response(
|
51 |
model_choice: str,
|
52 |
system_instruction: str,
|
53 |
text_prompt: str,
|
54 |
+
chatbot: list,
|
55 |
+
) -> Tuple[list, str]:
|
56 |
"""
|
57 |
Envía el mensaje al modelo, obtiene la respuesta y actualiza el historial.
|
58 |
"""
|
|
|
73 |
generated_text = response.text
|
74 |
|
75 |
# Actualizar el historial con la pregunta y la respuesta
|
76 |
+
chatbot.append((text_prompt, generated_text))
|
|
|
|
|
|
|
|
|
|
|
|
|
77 |
|
78 |
return chatbot, ""
|
79 |
|
|
|
96 |
|
97 |
# Definir la interfaz
|
98 |
with gr.Blocks() as demo:
|
99 |
+
gr.HTML(TITLE)
|
100 |
+
gr.HTML(SUBTITLE)
|
101 |
with gr.Column():
|
102 |
model_dropdown_component.render()
|
103 |
chatbot_component.render()
|
|
|
123 |
# Lanzar la aplicación
|
124 |
if __name__ == "__main__":
|
125 |
demo.queue(max_size=99).launch(debug=True, show_error=True)
|
|