Spaces:
Running
Running
File size: 1,219 Bytes
ff48396 446d2fd d135972 ff48396 a6341df ff48396 a6341df ff48396 d135972 ff48396 d135972 ff48396 d135972 403eda5 ff48396 d135972 ff48396 403eda5 d135972 403eda5 ff48396 403eda5 ff48396 a6341df d135972 ff48396 a6341df 5906ce7 ff48396 29f787a |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
import os
import gradio as gr
from google import genai
from dotenv import load_dotenv
# Cargar variables de entorno
load_dotenv()
# Configurar la API de Google
client = genai.Client(api_key=os.getenv("GOOGLE_API_KEY"))
# Crear la sesión de chat con el modelo de Gemini
chat = client.chats.create(model="gemini-2.0-flash")
def chat_stream(message, history):
"""Envía el mensaje del usuario a Gemini con historial y devuelve la respuesta en streaming."""
try:
# Añadir el mensaje al historial
chat.send_message(message)
# Usar streaming para obtener la respuesta en fragmentos
response_stream = chat.send_message_stream(message)
# Devolver los fragmentos como un flujo (streaming) en el chat
for chunk in response_stream:
yield chunk.text # Genera cada fragmento uno por uno
except Exception as e:
yield f"Error: {e}"
# Crear la interfaz de chat con historial
demo = gr.ChatInterface(
fn=chat_stream,
examples=["Write an example Python lambda function."],
title="Gemini Chatbot",
description="Chatbot interactivo con historial de conversación usando Gemini AI."
)
# Iniciar la app
demo.launch()
|