Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| from transformers import pipeline | |
| # Cargamos el modelo de texto por defecto (auto selecciona uno si no especificas) | |
| chatbot = pipeline("text-generation") | |
| # Historial del chat (simple lista de tuplas) | |
| def responder(mensaje, historial): | |
| if historial is None: | |
| historial = [] | |
| # Preparamos el prompt (concatenamos las últimas interacciones) | |
| contexto = "" | |
| for entrada, salida in historial[-3:]: | |
| contexto += f"Humano: {entrada}\nBot: {salida}\n" | |
| contexto += f"Humano: {mensaje}\nBot:" | |
| # Generamos respuesta | |
| respuesta = chatbot( | |
| contexto, | |
| max_new_tokens=120, | |
| pad_token_id=50256, | |
| do_sample=True, | |
| temperature=0.7, | |
| top_p=0.9 | |
| )[0]["generated_text"] | |
| # Cortamos el texto para quedarnos solo con la parte del bot | |
| respuesta = respuesta.split("Bot:")[-1].strip() | |
| historial.append((mensaje, respuesta)) | |
| return historial, historial | |
| # Interfaz con Gradio | |
| with gr.Blocks(theme="gradio/soft") as demo: | |
| gr.Markdown("## 💬 Chatbot básico con modelo predeterminado de Hugging Face") | |
| gr.Markdown("Escribe lo que quieras y el bot intentará responderte (sin API, sin lujos).") | |
| chatbot_ui = gr.Chatbot(label="Chatbot") | |
| entrada = gr.Textbox(placeholder="Escribe un mensaje y presiona Enter...") | |
| estado = gr.State([]) | |
| entrada.submit(responder, [entrada, estado], [chatbot_ui, estado]) | |
| entrada.submit(lambda: "", None, entrada) # limpia el campo de entrada | |
| # Ejecutar app | |
| if __name__ == "__main__": | |
| demo.launch() | |