Spaces:
Restarting
Restarting
Update app.py
Browse files
app.py
CHANGED
@@ -1,22 +1,45 @@
|
|
|
|
1 |
import time
|
2 |
import gradio as gr
|
|
|
|
|
3 |
|
4 |
-
|
5 |
-
|
6 |
-
history = history + [(message, response)] # Agrega el nuevo mensaje al historial
|
7 |
-
|
8 |
-
full_response = ""
|
9 |
-
for i in range(len(response)):
|
10 |
-
time.sleep(0.05)
|
11 |
-
full_response = response[: i + 1]
|
12 |
-
yield history + [(message, full_response)] # Retorna el historial actualizado progresivamente
|
13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
demo = gr.ChatInterface(
|
15 |
-
|
16 |
type="messages",
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
)
|
21 |
|
|
|
22 |
demo.launch()
|
|
|
1 |
+
import os
|
2 |
import time
|
3 |
import gradio as gr
|
4 |
+
import google.generativeai as genai
|
5 |
+
from dotenv import load_dotenv
|
6 |
|
7 |
+
# Cargar variables de entorno
|
8 |
+
load_dotenv()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
+
# Configurar la API de Google
|
11 |
+
genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
|
12 |
+
|
13 |
+
# Modelo de IA de Gemini
|
14 |
+
model = genai.GenerativeModel("gemini-2.0-flash")
|
15 |
+
|
16 |
+
def chat(message, history):
|
17 |
+
"""Env铆a el mensaje del usuario a Gemini con historial y devuelve la respuesta progresiva."""
|
18 |
+
try:
|
19 |
+
# Convertir historial a formato adecuado para Gemini
|
20 |
+
chat_history = [{"role": "user", "parts": [msg[0]]} for msg in history] + [{"role": "user", "parts": [message]}]
|
21 |
+
|
22 |
+
# Enviar el historial y el mensaje actual a Gemini
|
23 |
+
response = model.generate_content(chat_history)
|
24 |
+
|
25 |
+
# Simular respuesta progresiva caracter por caracter
|
26 |
+
full_response = ""
|
27 |
+
for i in range(len(response.text)):
|
28 |
+
time.sleep(0.05)
|
29 |
+
full_response = response.text[: i + 1]
|
30 |
+
yield history + [(message, full_response)] # Retorna el historial actualizado progresivamente
|
31 |
+
|
32 |
+
except Exception as e:
|
33 |
+
yield history + [(message, f"Error: {e}")]
|
34 |
+
|
35 |
+
# Crear la interfaz de chat con historial
|
36 |
demo = gr.ChatInterface(
|
37 |
+
fn=chat,
|
38 |
type="messages",
|
39 |
+
examples=["Write an example Python lambda function."],
|
40 |
+
title="Gemini Chatbot",
|
41 |
+
description="Chatbot interactivo con historial de conversaci贸n usando Gemini AI."
|
42 |
)
|
43 |
|
44 |
+
# Iniciar la app
|
45 |
demo.launch()
|