Spaces:
Sleeping
Sleeping
Create app.py.2
Browse files
app.py.2
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
|
| 4 |
+
# Cargamos el modelo de texto por defecto (auto selecciona uno si no especificas)
|
| 5 |
+
chatbot = pipeline("text-generation")
|
| 6 |
+
|
| 7 |
+
# Historial del chat (simple lista de tuplas)
|
| 8 |
+
def responder(mensaje, historial):
|
| 9 |
+
if historial is None:
|
| 10 |
+
historial = []
|
| 11 |
+
# Preparamos el prompt (concatenamos las últimas interacciones)
|
| 12 |
+
contexto = ""
|
| 13 |
+
for entrada, salida in historial[-3:]:
|
| 14 |
+
contexto += f"Humano: {entrada}\nBot: {salida}\n"
|
| 15 |
+
contexto += f"Humano: {mensaje}\nBot:"
|
| 16 |
+
|
| 17 |
+
# Generamos respuesta
|
| 18 |
+
respuesta = chatbot(
|
| 19 |
+
contexto,
|
| 20 |
+
max_new_tokens=120,
|
| 21 |
+
pad_token_id=50256,
|
| 22 |
+
do_sample=True,
|
| 23 |
+
temperature=0.7,
|
| 24 |
+
top_p=0.9
|
| 25 |
+
)[0]["generated_text"]
|
| 26 |
+
|
| 27 |
+
# Cortamos el texto para quedarnos solo con la parte del bot
|
| 28 |
+
respuesta = respuesta.split("Bot:")[-1].strip()
|
| 29 |
+
historial.append((mensaje, respuesta))
|
| 30 |
+
return historial, historial
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
# Interfaz con Gradio
|
| 34 |
+
with gr.Blocks(theme="gradio/soft") as demo:
|
| 35 |
+
gr.Markdown("## 💬 Chatbot básico con modelo predeterminado de Hugging Face")
|
| 36 |
+
gr.Markdown("Escribe lo que quieras y el bot intentará responderte (sin API, sin lujos).")
|
| 37 |
+
|
| 38 |
+
chatbot_ui = gr.Chatbot(label="Chatbot")
|
| 39 |
+
entrada = gr.Textbox(placeholder="Escribe un mensaje y presiona Enter...")
|
| 40 |
+
estado = gr.State([])
|
| 41 |
+
|
| 42 |
+
entrada.submit(responder, [entrada, estado], [chatbot_ui, estado])
|
| 43 |
+
entrada.submit(lambda: "", None, entrada) # limpia el campo de entrada
|
| 44 |
+
|
| 45 |
+
# Ejecutar app
|
| 46 |
+
if __name__ == "__main__":
|
| 47 |
+
demo.launch()
|