|
import os |
|
import time |
|
import gradio as gr |
|
import google.generativeai as genai |
|
from dotenv import load_dotenv |
|
|
|
|
|
load_dotenv() |
|
|
|
|
|
genai.configure(api_key=os.getenv("GOOGLE_API_KEY")) |
|
|
|
|
|
model = genai.GenerativeModel("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: |
|
|
|
chat = model.start_chat() |
|
|
|
|
|
for user_msg, assistant_msg in history: |
|
chat.send_message(user_msg) |
|
|
|
|
|
response = chat.send_message(message, stream=True) |
|
|
|
|
|
for chunk in response: |
|
if chunk.text: |
|
time.sleep(0.05) |
|
yield chunk.text |
|
|
|
except Exception as e: |
|
yield f"Error: {e}" |
|
|
|
|
|
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." |
|
) |
|
|
|
|
|
demo.launch() |
|
|