bentosmau commited on
Commit ·
76fb66f
1
Parent(s): da678b4
Add custom responses to AI chatbot to provide personalized answers
Browse filesImplement custom responses via `respuestas.json` and update `app.py` to prioritize these over AI-generated answers.
Replit-Commit-Author: Agent
Replit-Commit-Session-Id: e3ff2484-bbd8-4aba-bea0-1940769b874a
Replit-Commit-Checkpoint-Type: full_checkpoint
Replit-Commit-Event-Id: 39350bd2-21e2-4a90-9576-16ce1d75ec5c
Replit-Commit-Screenshot-Url: https://storage.googleapis.com/screenshot-production-us-central1/1739408b-93a5-479b-a658-30f2493b0467/e3ff2484-bbd8-4aba-bea0-1940769b874a/17Q9XjF
Replit-Helium-Checkpoint-Created: true
- chat-app/app.py +36 -5
- chat-app/respuestas.json +24 -0
- main.py +5 -0
chat-app/app.py
CHANGED
|
@@ -1,4 +1,6 @@
|
|
| 1 |
import os
|
|
|
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
from openai import OpenAI
|
| 4 |
|
|
@@ -11,7 +13,36 @@ SYSTEM_PROMPT = """Eres un asistente de IA inteligente y útil, similar a ChatGP
|
|
| 11 |
Respondes en el mismo idioma que el usuario.
|
| 12 |
Eres amable, preciso y detallado en tus respuestas."""
|
| 13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
def responder(mensaje, historial):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
mensajes = [{"role": "system", "content": SYSTEM_PROMPT}]
|
| 16 |
for turno in historial:
|
| 17 |
mensajes.append({"role": "user", "content": turno[0]})
|
|
@@ -35,10 +66,10 @@ def responder(mensaje, historial):
|
|
| 35 |
historial[-1][1] = respuesta_parcial
|
| 36 |
yield historial, ""
|
| 37 |
|
| 38 |
-
with gr.Blocks(title="
|
| 39 |
gr.Markdown(
|
| 40 |
"""
|
| 41 |
-
# 🤖
|
| 42 |
### Asistente de inteligencia artificial conversacional
|
| 43 |
"""
|
| 44 |
)
|
|
@@ -46,7 +77,7 @@ with gr.Blocks(title="Mi IA Chat") as demo:
|
|
| 46 |
chatbot = gr.Chatbot(
|
| 47 |
show_label=False,
|
| 48 |
height=500,
|
| 49 |
-
avatar_images=(None, "https://api.dicebear.com/7.x/bottts/svg?seed=
|
| 50 |
)
|
| 51 |
|
| 52 |
with gr.Row():
|
|
@@ -64,10 +95,10 @@ with gr.Blocks(title="Mi IA Chat") as demo:
|
|
| 64 |
|
| 65 |
gr.Examples(
|
| 66 |
examples=[
|
|
|
|
|
|
|
| 67 |
"¿Cuál es la diferencia entre machine learning e inteligencia artificial?",
|
| 68 |
"Explícame cómo funciona una red neuronal de forma sencilla",
|
| 69 |
-
"¿Puedes escribir un poema sobre la tecnología?",
|
| 70 |
-
"¿Cuáles son las mejores prácticas en programación Python?",
|
| 71 |
],
|
| 72 |
inputs=entrada,
|
| 73 |
label="Ejemplos de preguntas",
|
|
|
|
| 1 |
import os
|
| 2 |
+
import json
|
| 3 |
+
import time
|
| 4 |
import gradio as gr
|
| 5 |
from openai import OpenAI
|
| 6 |
|
|
|
|
| 13 |
Respondes en el mismo idioma que el usuario.
|
| 14 |
Eres amable, preciso y detallado en tus respuestas."""
|
| 15 |
|
| 16 |
+
def cargar_respuestas():
|
| 17 |
+
ruta = os.path.join(os.path.dirname(__file__), "respuestas.json")
|
| 18 |
+
try:
|
| 19 |
+
with open(ruta, "r", encoding="utf-8") as f:
|
| 20 |
+
datos = json.load(f)
|
| 21 |
+
return datos.get("respuestas", [])
|
| 22 |
+
except Exception:
|
| 23 |
+
return []
|
| 24 |
+
|
| 25 |
+
RESPUESTAS_PERSONALIZADAS = cargar_respuestas()
|
| 26 |
+
|
| 27 |
+
def buscar_respuesta_personalizada(mensaje):
|
| 28 |
+
texto = mensaje.lower().strip()
|
| 29 |
+
for entrada in RESPUESTAS_PERSONALIZADAS:
|
| 30 |
+
for pregunta in entrada.get("preguntas", []):
|
| 31 |
+
if pregunta.lower() in texto or texto in pregunta.lower():
|
| 32 |
+
return entrada.get("respuesta")
|
| 33 |
+
return None
|
| 34 |
+
|
| 35 |
def responder(mensaje, historial):
|
| 36 |
+
respuesta_personalizada = buscar_respuesta_personalizada(mensaje)
|
| 37 |
+
|
| 38 |
+
if respuesta_personalizada:
|
| 39 |
+
historial = historial + [[mensaje, ""]]
|
| 40 |
+
for caracter in respuesta_personalizada:
|
| 41 |
+
historial[-1][1] += caracter
|
| 42 |
+
time.sleep(0.01)
|
| 43 |
+
yield historial, ""
|
| 44 |
+
return
|
| 45 |
+
|
| 46 |
mensajes = [{"role": "system", "content": SYSTEM_PROMPT}]
|
| 47 |
for turno in historial:
|
| 48 |
mensajes.append({"role": "user", "content": turno[0]})
|
|
|
|
| 66 |
historial[-1][1] = respuesta_parcial
|
| 67 |
yield historial, ""
|
| 68 |
|
| 69 |
+
with gr.Blocks(title="mdfjbots-neo-1") as demo:
|
| 70 |
gr.Markdown(
|
| 71 |
"""
|
| 72 |
+
# 🤖 mdfjbots-neo-1
|
| 73 |
### Asistente de inteligencia artificial conversacional
|
| 74 |
"""
|
| 75 |
)
|
|
|
|
| 77 |
chatbot = gr.Chatbot(
|
| 78 |
show_label=False,
|
| 79 |
height=500,
|
| 80 |
+
avatar_images=(None, "https://api.dicebear.com/7.x/bottts/svg?seed=mdfjbots"),
|
| 81 |
)
|
| 82 |
|
| 83 |
with gr.Row():
|
|
|
|
| 95 |
|
| 96 |
gr.Examples(
|
| 97 |
examples=[
|
| 98 |
+
"Hola, ¿quién eres?",
|
| 99 |
+
"¿Qué puedes hacer?",
|
| 100 |
"¿Cuál es la diferencia entre machine learning e inteligencia artificial?",
|
| 101 |
"Explícame cómo funciona una red neuronal de forma sencilla",
|
|
|
|
|
|
|
| 102 |
],
|
| 103 |
inputs=entrada,
|
| 104 |
label="Ejemplos de preguntas",
|
chat-app/respuestas.json
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"respuestas": [
|
| 3 |
+
{
|
| 4 |
+
"preguntas": ["hola", "buenos días", "buenas tardes", "buenas noches", "hey", "qué tal"],
|
| 5 |
+
"respuesta": "¡Hola! Soy mdfjbots-neo-1, tu asistente de IA. ¿En qué puedo ayudarte hoy?"
|
| 6 |
+
},
|
| 7 |
+
{
|
| 8 |
+
"preguntas": ["quién eres", "qué eres", "cómo te llamas", "cuál es tu nombre", "quién te creó", "quien te hizo"],
|
| 9 |
+
"respuesta": "Soy mdfjbots-neo-1, un modelo de inteligencia artificial creado por mauriminuano125-a11y. Estoy aquí para ayudarte con tus preguntas y conversaciones."
|
| 10 |
+
},
|
| 11 |
+
{
|
| 12 |
+
"preguntas": ["qué puedes hacer", "para qué sirves", "cómo me puedes ayudar", "qué sabes hacer"],
|
| 13 |
+
"respuesta": "Puedo ayudarte con muchas cosas: responder preguntas, explicar conceptos, ayudarte a escribir textos, resolver problemas, mantener conversaciones y mucho más. ¡Solo pregúntame!"
|
| 14 |
+
},
|
| 15 |
+
{
|
| 16 |
+
"preguntas": ["adiós", "hasta luego", "chau", "bye", "nos vemos"],
|
| 17 |
+
"respuesta": "¡Hasta luego! Fue un placer hablar contigo. Vuelve cuando quieras. 😊"
|
| 18 |
+
},
|
| 19 |
+
{
|
| 20 |
+
"preguntas": ["gracias", "muchas gracias", "te lo agradezco"],
|
| 21 |
+
"respuesta": "¡Con mucho gusto! Si necesitas algo más, aquí estaré."
|
| 22 |
+
}
|
| 23 |
+
]
|
| 24 |
+
}
|
main.py
CHANGED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import huggingface_hub
|
| 2 |
+
from huggingface_hub import huggingface_hub
|
| 3 |
+
|
| 4 |
+
# Load the model from Hugging Face Hub
|
| 5 |
+
model_name = "mauriminuano125-a11y"
|