Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,53 +1,27 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import pipeline
|
| 3 |
-
|
| 4 |
-
# Cargar un modelo de lenguaje
|
| 5 |
-
from transformers import pipeline
|
| 6 |
from datetime import datetime
|
| 7 |
|
| 8 |
-
# Crear el chatbot
|
| 9 |
chatbot = pipeline("text-generation", model="gpt2")
|
| 10 |
|
|
|
|
| 11 |
def get_response(user_input):
|
| 12 |
-
# Respuestas personalizadas
|
| 13 |
if "hola" in user_input.lower():
|
| 14 |
return "¡Hola, soy Jarbot! ¿Cómo puedo ayudarte hoy?"
|
| 15 |
elif "hora" in user_input.lower():
|
| 16 |
return f"Son las {datetime.now().strftime('%H:%M')}. ¿Hay algo más en lo que pueda ayudarte?"
|
| 17 |
elif "adiós" in user_input.lower():
|
| 18 |
-
return "¡Hasta luego! Que tengas un buen día
|
| 19 |
else:
|
| 20 |
-
# Respuesta por defecto usando el modelo
|
| 21 |
response = chatbot(user_input, max_length=50)
|
| 22 |
return response[0]['generated_text']
|
| 23 |
|
| 24 |
-
#
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
if user_input.lower() == "salir":
|
| 28 |
-
break
|
| 29 |
-
response = get_response(user_input)
|
| 30 |
-
print("Asistente:", response)
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
# Función de respuesta
|
| 34 |
-
def responder_entrada(user_input):
|
| 35 |
-
respuesta = chatbot(user_input, max_length=100, num_return_sequences=1)
|
| 36 |
-
return respuesta[0]["generated_text"]
|
| 37 |
-
|
| 38 |
-
# Interfaz Gradio
|
| 39 |
-
with gr.Blocks() as interfaz:
|
| 40 |
-
gr.Markdown("# Asistente Virtual")
|
| 41 |
-
chat = gr.Chatbot()
|
| 42 |
-
entrada_usuario = gr.Textbox(label="Escribe tu mensaje:")
|
| 43 |
-
boton_enviar = gr.Button("Enviar")
|
| 44 |
-
|
| 45 |
-
def enviar_mensaje(user_input, history):
|
| 46 |
-
history = history or []
|
| 47 |
-
respuesta = responder_entrada(user_input)
|
| 48 |
-
history.append((user_input, respuesta))
|
| 49 |
-
return history, ""
|
| 50 |
-
|
| 51 |
-
boton_enviar.click(enviar_mensaje, [entrada_usuario, chat], [chat, entrada_usuario])
|
| 52 |
|
| 53 |
-
interfaz
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import pipeline
|
|
|
|
|
|
|
|
|
|
| 3 |
from datetime import datetime
|
| 4 |
|
| 5 |
+
# Crear el chatbot usando el modelo de transformers
|
| 6 |
chatbot = pipeline("text-generation", model="gpt2")
|
| 7 |
|
| 8 |
+
# Función para obtener la respuesta del bot
|
| 9 |
def get_response(user_input):
|
| 10 |
+
# Respuestas personalizadas según ciertas palabras clave
|
| 11 |
if "hola" in user_input.lower():
|
| 12 |
return "¡Hola, soy Jarbot! ¿Cómo puedo ayudarte hoy?"
|
| 13 |
elif "hora" in user_input.lower():
|
| 14 |
return f"Son las {datetime.now().strftime('%H:%M')}. ¿Hay algo más en lo que pueda ayudarte?"
|
| 15 |
elif "adiós" in user_input.lower():
|
| 16 |
+
return "¡Hasta luego! Que tengas un buen día."
|
| 17 |
else:
|
| 18 |
+
# Respuesta por defecto usando el modelo GPT-2
|
| 19 |
response = chatbot(user_input, max_length=50)
|
| 20 |
return response[0]['generated_text']
|
| 21 |
|
| 22 |
+
# Crear la interfaz con Gradio
|
| 23 |
+
iface = gr.Interface(fn=get_response, inputs="text", outputs="text",
|
| 24 |
+
title="Asistente Virtual", description="¡Escribe algo para interactuar con el asistente!")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
+
# Lanzar la interfaz
|
| 27 |
+
iface.launch()
|