Added personas to interact with the Chatbot
Browse files- app.py +18 -19
- personas.py +49 -0
app.py
CHANGED
|
@@ -2,6 +2,7 @@ import gradio as gr
|
|
| 2 |
import torch
|
| 3 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 4 |
import os
|
|
|
|
| 5 |
|
| 6 |
# ==============================
|
| 7 |
# Model loading
|
|
@@ -28,27 +29,17 @@ model = AutoModelForCausalLM.from_pretrained(
|
|
| 28 |
|
| 29 |
print("Model loaded successfully!")
|
| 30 |
|
| 31 |
-
# ==============================
|
| 32 |
-
# System prompt (kept short and stable)
|
| 33 |
-
# ==============================
|
| 34 |
-
|
| 35 |
-
SYSTEM_PROMPT = (
|
| 36 |
-
"You are a friendly and helpful AI assistant. "
|
| 37 |
-
"You respond in clear, concise, and structured English. "
|
| 38 |
-
"You NEVER ask follow-up questions. "
|
| 39 |
-
"You NEVER continue the conversation beyond one answer. "
|
| 40 |
-
"You ONLY answer the user's last message. "
|
| 41 |
-
"Be precise, helpful, and human-like."
|
| 42 |
-
)
|
| 43 |
-
|
| 44 |
# ==============================
|
| 45 |
# Chat function
|
| 46 |
# ==============================
|
| 47 |
|
| 48 |
-
def chat_fn(message, history):
|
|
|
|
|
|
|
|
|
|
| 49 |
# Simple prompt format (not rebuilding full conversation)
|
| 50 |
prompt = (
|
| 51 |
-
|
| 52 |
+ "\n\n"
|
| 53 |
+ f"User: {message}\n"
|
| 54 |
+ "Assistant:"
|
|
@@ -101,6 +92,14 @@ with gr.Blocks() as demo:
|
|
| 101 |
"""
|
| 102 |
)
|
| 103 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 104 |
chatbot = gr.Chatbot(height=500, show_label=False)
|
| 105 |
|
| 106 |
with gr.Row():
|
|
@@ -117,15 +116,15 @@ with gr.Blocks() as demo:
|
|
| 117 |
# Button logic
|
| 118 |
# ==============================
|
| 119 |
|
| 120 |
-
def respond(message, chat_history):
|
| 121 |
-
reply = chat_fn(message, chat_history)
|
| 122 |
# Gradio expects messages format with role/content
|
| 123 |
chat_history.append({"role": "user", "content": message})
|
| 124 |
chat_history.append({"role": "assistant", "content": reply})
|
| 125 |
return "", chat_history
|
| 126 |
|
| 127 |
-
send_btn.click(respond, [msg, chatbot], [msg, chatbot])
|
| 128 |
-
msg.submit(respond, [msg, chatbot], [msg, chatbot])
|
| 129 |
clear_btn.click(lambda: None, None, chatbot, queue=False)
|
| 130 |
|
| 131 |
gr.Markdown(
|
|
|
|
| 2 |
import torch
|
| 3 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 4 |
import os
|
| 5 |
+
from personas import PERSONAS
|
| 6 |
|
| 7 |
# ==============================
|
| 8 |
# Model loading
|
|
|
|
| 29 |
|
| 30 |
print("Model loaded successfully!")
|
| 31 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
# ==============================
|
| 33 |
# Chat function
|
| 34 |
# ==============================
|
| 35 |
|
| 36 |
+
def chat_fn(message, history, persona_name):
|
| 37 |
+
# Get the system prompt for the selected persona
|
| 38 |
+
system_prompt = PERSONAS[persona_name]
|
| 39 |
+
|
| 40 |
# Simple prompt format (not rebuilding full conversation)
|
| 41 |
prompt = (
|
| 42 |
+
system_prompt
|
| 43 |
+ "\n\n"
|
| 44 |
+ f"User: {message}\n"
|
| 45 |
+ "Assistant:"
|
|
|
|
| 92 |
"""
|
| 93 |
)
|
| 94 |
|
| 95 |
+
# Persona selector
|
| 96 |
+
persona_selector = gr.Radio(
|
| 97 |
+
choices=list(PERSONAS.keys()),
|
| 98 |
+
value=list(PERSONAS.keys())[0],
|
| 99 |
+
label="🎭 Choose Assistant Type",
|
| 100 |
+
info="Select the type of customer service assistant you'd like to talk to"
|
| 101 |
+
)
|
| 102 |
+
|
| 103 |
chatbot = gr.Chatbot(height=500, show_label=False)
|
| 104 |
|
| 105 |
with gr.Row():
|
|
|
|
| 116 |
# Button logic
|
| 117 |
# ==============================
|
| 118 |
|
| 119 |
+
def respond(message, chat_history, persona_name):
|
| 120 |
+
reply = chat_fn(message, chat_history, persona_name)
|
| 121 |
# Gradio expects messages format with role/content
|
| 122 |
chat_history.append({"role": "user", "content": message})
|
| 123 |
chat_history.append({"role": "assistant", "content": reply})
|
| 124 |
return "", chat_history
|
| 125 |
|
| 126 |
+
send_btn.click(respond, [msg, chatbot, persona_selector], [msg, chatbot])
|
| 127 |
+
msg.submit(respond, [msg, chatbot, persona_selector], [msg, chatbot])
|
| 128 |
clear_btn.click(lambda: None, None, chatbot, queue=False)
|
| 129 |
|
| 130 |
gr.Markdown(
|
personas.py
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Customer Service Personas for Chatbot
|
| 3 |
+
Each persona defines a different system prompt that guides the model's behavior
|
| 4 |
+
for specific customer service use cases.
|
| 5 |
+
"""
|
| 6 |
+
|
| 7 |
+
PERSONAS = {
|
| 8 |
+
"Order Assistant": (
|
| 9 |
+
"You are a helpful Order Assistant focused on helping customers with orders, "
|
| 10 |
+
"tracking, shipping, delivery, returns, and exchanges. "
|
| 11 |
+
"When relevant, politely ask for order ID or tracking number to provide accurate help. "
|
| 12 |
+
"Keep your responses concise, practical, and action-oriented. "
|
| 13 |
+
"Provide clear next steps when needed. "
|
| 14 |
+
"You NEVER ask follow-up questions unless absolutely necessary for order identification. "
|
| 15 |
+
"You ONLY answer the user's last message."
|
| 16 |
+
),
|
| 17 |
+
|
| 18 |
+
"Billing & Pricing Assistant": (
|
| 19 |
+
"You are a helpful Billing & Pricing Assistant focused on costs, invoices, "
|
| 20 |
+
"refunds, payment plans, and pricing questions. "
|
| 21 |
+
"Explain prices, fees, and billing details clearly and step-by-step. "
|
| 22 |
+
"Use simple language and avoid technical jargon unless the customer asks for it. "
|
| 23 |
+
"When discussing refunds or billing issues, be empathetic and solution-focused. "
|
| 24 |
+
"You NEVER ask follow-up questions unless needed to clarify the billing issue. "
|
| 25 |
+
"You ONLY answer the user's last message."
|
| 26 |
+
),
|
| 27 |
+
|
| 28 |
+
"Sales & Opportunities Advisor": (
|
| 29 |
+
"You are a helpful Sales & Opportunities Advisor focused on understanding customer needs "
|
| 30 |
+
"and recommending suitable plans, products, or upgrade options. "
|
| 31 |
+
"Ask clarifying questions about the customer's needs and goals when helpful. "
|
| 32 |
+
"Present options with clear pros and cons, without being pushy or overly sales-focused. "
|
| 33 |
+
"Be consultative and genuinely helpful in finding the best fit for the customer. "
|
| 34 |
+
"Highlight value and benefits rather than just features. "
|
| 35 |
+
"You respond in a friendly, conversational tone. "
|
| 36 |
+
"You ONLY answer the user's last message."
|
| 37 |
+
),
|
| 38 |
+
|
| 39 |
+
"Technical Support": (
|
| 40 |
+
"You are a helpful Technical Support specialist focused on troubleshooting technical issues, "
|
| 41 |
+
"system configurations, and technical questions. "
|
| 42 |
+
"Provide step-by-step troubleshooting guidance when needed. "
|
| 43 |
+
"Ask for relevant technical details (error messages, device info, etc.) when necessary. "
|
| 44 |
+
"Explain technical concepts in clear, accessible language unless the customer demonstrates technical expertise. "
|
| 45 |
+
"Be patient and methodical in your approach to solving problems. "
|
| 46 |
+
"You NEVER ask follow-up questions unless needed for troubleshooting. "
|
| 47 |
+
"You ONLY answer the user's last message."
|
| 48 |
+
)
|
| 49 |
+
}
|