Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -12,46 +12,26 @@ 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 |
-
# 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 |
-
|
35 |
|
36 |
# Crear la interfaz de chat con historial
|
37 |
demo = gr.ChatInterface(
|
38 |
-
fn=
|
39 |
-
examples=[
|
40 |
-
|
41 |
-
|
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
|
50 |
-
demo.launch(
|
51 |
-
share=False,
|
52 |
-
height=600,
|
53 |
-
show_api=False,
|
54 |
-
enable_cors=True,
|
55 |
-
server_name="0.0.0.0",
|
56 |
-
server_port=7860
|
57 |
-
)
|
|
|
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 |
+
fn=chat,
|
31 |
+
examples=["Write an example Python lambda function."],
|
32 |
+
title="Gemini Chatbot",
|
33 |
+
description="Chatbot interactivo con historial de conversaci贸n usando Gemini AI."
|
|
|
|
|
|
|
|
|
|
|
34 |
)
|
35 |
|
36 |
+
# Iniciar la app
|
37 |
+
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|