import gradio as gr from groq import Groq import os from dotenv import load_dotenv load_dotenv() cliente = Groq(api_key=os.getenv("GROQ_API_KEY")) def responder(mensaje, historial): if not mensaje or str(mensaje).strip() == "": return historial if historial is not None else [] # Crear mensajes para Groq mensajes = [{"role": "system", "content": "Eres MeridaAI, IA personal de Wilder Merida de Cochabamba. Habla relajado, con humor boliviano y sé útil."}] # Agregar el historial anterior if historial: for user_msg, bot_msg in historial: if user_msg: mensajes.append({"role": "user", "content": str(user_msg)}) if bot_msg: mensajes.append({"role": "assistant", "content": str(bot_msg)}) mensajes.append({"role": "user", "content": str(mensaje)}) # Llamada a Groq respuesta = cliente.chat.completions.create( model="llama-3.3-70b-versatile", messages=mensajes, temperature=0.8, max_tokens=1024 ) respuesta_texto = respuesta.choices[0].message.content # Devolver historial actualizado nuevo_historial = historial + [[mensaje, respuesta_texto]] if historial is not None else [[mensaje, respuesta_texto]] return nuevo_historial # Interfaz with gr.Blocks(title="MeridaAI") as demo: gr.Markdown("# 🧠 MeridaAI\n**Tu IA Personal - Wilder Merida**") chatbot = gr.Chatbot(height=500, label="Conversación") msg = gr.Textbox(placeholder="Escribe algo...", label="Mensaje", lines=2) with gr.Row(): gr.Button("Enviar", variant="primary", size="large").click( responder, inputs=[msg, chatbot], outputs=chatbot ) gr.Button("Limpiar Chat").click(lambda: [], None, chatbot) msg.submit(responder, inputs=[msg, chatbot], outputs=chatbot) if __name__ == "__main__": demo.launch()