Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,33 +1,37 @@
|
|
|
|
1 |
import gradio as gr
|
2 |
-
from gradio import ChatMessage
|
3 |
-
from typing import Iterator
|
4 |
import google.generativeai as genai
|
5 |
-
import
|
6 |
|
7 |
-
|
8 |
-
|
9 |
|
10 |
-
|
|
|
11 |
|
12 |
-
|
13 |
-
""
|
14 |
|
15 |
def chat(message, history):
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
content=example_code.format(random.randint(1, 100)),
|
21 |
-
options=[
|
22 |
-
{"value": "Yes, that's correct.", "label": "Yes"},
|
23 |
-
{"value": "No"}
|
24 |
-
]
|
25 |
-
)
|
26 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
demo = gr.ChatInterface(
|
28 |
-
chat,
|
29 |
-
|
30 |
-
|
|
|
31 |
)
|
32 |
|
33 |
-
|
|
|
|
1 |
+
import os
|
2 |
import gradio as gr
|
|
|
|
|
3 |
import google.generativeai as genai
|
4 |
+
from dotenv import load_dotenv
|
5 |
|
6 |
+
# Cargar variables de entorno
|
7 |
+
load_dotenv()
|
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 |
+
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()
|