Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -12,26 +12,47 @@ genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
|
|
12 |
# Modelo de IA de Gemini
|
13 |
model = genai.GenerativeModel("gemini-2.0-flash")
|
14 |
|
15 |
-
def
|
16 |
"""Env铆a el mensaje del usuario a Gemini con historial y devuelve la respuesta."""
|
17 |
try:
|
18 |
-
#
|
19 |
-
|
20 |
-
|
21 |
-
#
|
22 |
-
|
23 |
-
|
24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
except Exception as e:
|
26 |
-
|
27 |
|
28 |
# Crear la interfaz de chat con historial
|
29 |
demo = gr.ChatInterface(
|
30 |
-
fn=
|
31 |
-
examples=[
|
32 |
-
|
33 |
-
|
|
|
|
|
|
|
|
|
|
|
34 |
)
|
35 |
|
36 |
-
# Iniciar la app
|
37 |
-
demo.launch(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
# Modelo de IA de Gemini
|
13 |
model = genai.GenerativeModel("gemini-2.0-flash")
|
14 |
|
15 |
+
def chat_stream(message, history):
|
16 |
"""Env铆a el mensaje del usuario a Gemini con historial y devuelve la respuesta."""
|
17 |
try:
|
18 |
+
# Crear una nueva sesi贸n de chat
|
19 |
+
chat = model.start_chat()
|
20 |
+
|
21 |
+
# Procesar el historial previo
|
22 |
+
for user_msg, assistant_msg in history:
|
23 |
+
chat.send_message(user_msg)
|
24 |
+
|
25 |
+
# Enviar el mensaje actual y obtener respuesta en streaming
|
26 |
+
response = chat.send_message(message, stream=True)
|
27 |
+
|
28 |
+
# Devolver la respuesta en fragmentos
|
29 |
+
for chunk in response:
|
30 |
+
if chunk.text:
|
31 |
+
yield chunk.text
|
32 |
+
|
33 |
except Exception as e:
|
34 |
+
yield f"Error: {e}"
|
35 |
|
36 |
# Crear la interfaz de chat con historial
|
37 |
demo = gr.ChatInterface(
|
38 |
+
fn=chat_stream,
|
39 |
+
examples=[
|
40 |
+
"What is Python?",
|
41 |
+
"Write a simple function to calculate factorial",
|
42 |
+
"How can I read a CSV file in Python?",
|
43 |
+
"Explain object-oriented programming"
|
44 |
+
],
|
45 |
+
title="Gemini AI Assistant",
|
46 |
+
description="Chat interactivo con Gemini AI - Pregunta lo que quieras sobre programaci贸n"
|
47 |
)
|
48 |
|
49 |
+
# Iniciar la app con configuraci贸n mejorada
|
50 |
+
demo.launch(
|
51 |
+
share=False,
|
52 |
+
height=600,
|
53 |
+
show_api=False,
|
54 |
+
allow_flagging="never",
|
55 |
+
enable_cors=True,
|
56 |
+
server_name="0.0.0.0",
|
57 |
+
server_port=7860
|
58 |
+
)
|