Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,37 +1,22 @@
|
|
1 |
-
import
|
2 |
import gradio as gr
|
3 |
-
import google.generativeai as genai
|
4 |
-
from dotenv import load_dotenv
|
5 |
|
6 |
-
|
7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
|
9 |
-
# Configurar la API de Google
|
10 |
-
genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
|
11 |
-
|
12 |
-
# Modelo de IA de Gemini
|
13 |
-
model = genai.GenerativeModel("gemini-2.0-flash")
|
14 |
-
|
15 |
-
def chat(message, history):
|
16 |
-
"""Env铆a el mensaje del usuario a Gemini con historial y devuelve la respuesta."""
|
17 |
-
try:
|
18 |
-
# Convertir historial a formato adecuado para Gemini
|
19 |
-
chat_history = [{"role": "user", "parts": [msg[0]]} for msg in history] + [{"role": "user", "parts": [message]}]
|
20 |
-
|
21 |
-
# Enviar el historial y el mensaje actual a Gemini
|
22 |
-
response = model.generate_content(chat_history)
|
23 |
-
|
24 |
-
return response.text # Devuelve solo el texto de la respuesta
|
25 |
-
except Exception as e:
|
26 |
-
return f"Error: {e}"
|
27 |
-
|
28 |
-
# Crear la interfaz de chat con historial
|
29 |
demo = gr.ChatInterface(
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
|
|
34 |
)
|
35 |
|
36 |
-
# Iniciar la app
|
37 |
demo.launch()
|
|
|
1 |
+
import time
|
2 |
import gradio as gr
|
|
|
|
|
3 |
|
4 |
+
def slow_echo(message, history):
|
5 |
+
response = "You typed: " + message
|
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 |
+
slow_echo,
|
16 |
+
type="messages",
|
17 |
+
flagging_mode="manual",
|
18 |
+
flagging_options=["Like", "Spam", "Inappropriate", "Other"],
|
19 |
+
save_history=True,
|
20 |
)
|
21 |
|
|
|
22 |
demo.launch()
|