Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,25 +1,25 @@
|
|
1 |
import os
|
2 |
import gradio as gr
|
3 |
-
|
4 |
from dotenv import load_dotenv
|
5 |
|
6 |
# Cargar variables de entorno
|
7 |
load_dotenv()
|
8 |
|
9 |
# Configurar la API de Google
|
10 |
-
genai.
|
11 |
|
12 |
-
#
|
13 |
-
|
14 |
|
15 |
-
def
|
16 |
"""Env铆a el mensaje del usuario a Gemini con historial y devuelve la respuesta en streaming."""
|
17 |
try:
|
18 |
-
#
|
19 |
-
|
20 |
|
21 |
# Usar streaming para obtener la respuesta en fragmentos
|
22 |
-
response_stream =
|
23 |
|
24 |
# Devolver los fragmentos como un flujo (streaming) en el chat
|
25 |
for chunk in response_stream:
|
@@ -30,7 +30,7 @@ def chat(message, history):
|
|
30 |
|
31 |
# Crear la interfaz de chat con historial
|
32 |
demo = gr.ChatInterface(
|
33 |
-
fn=
|
34 |
examples=["Write an example Python lambda function."],
|
35 |
title="Gemini Chatbot",
|
36 |
description="Chatbot interactivo con historial de conversaci贸n usando Gemini AI."
|
|
|
1 |
import os
|
2 |
import gradio as gr
|
3 |
+
from google import genai
|
4 |
from dotenv import load_dotenv
|
5 |
|
6 |
# Cargar variables de entorno
|
7 |
load_dotenv()
|
8 |
|
9 |
# Configurar la API de Google
|
10 |
+
client = genai.Client(api_key=os.getenv("GOOGLE_API_KEY"))
|
11 |
|
12 |
+
# Crear la sesi贸n de chat con el modelo de Gemini
|
13 |
+
chat = client.chats.create(model="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 en streaming."""
|
17 |
try:
|
18 |
+
# A帽adir el mensaje al historial
|
19 |
+
chat.send_message(message)
|
20 |
|
21 |
# Usar streaming para obtener la respuesta en fragmentos
|
22 |
+
response_stream = chat.send_message_stream(message)
|
23 |
|
24 |
# Devolver los fragmentos como un flujo (streaming) en el chat
|
25 |
for chunk in response_stream:
|
|
|
30 |
|
31 |
# Crear la interfaz de chat con historial
|
32 |
demo = gr.ChatInterface(
|
33 |
+
fn=chat_stream,
|
34 |
examples=["Write an example Python lambda function."],
|
35 |
title="Gemini Chatbot",
|
36 |
description="Chatbot interactivo con historial de conversaci贸n usando Gemini AI."
|