BasilTh
commited on
Commit
Β·
a621f88
1
Parent(s):
7599510
Deploy latest SLM customer-support chatbot
Browse files- app.py +6 -60
- module.py +108 -0
- requirements.txt +10 -1
app.py
CHANGED
@@ -1,64 +1,10 @@
|
|
1 |
import gradio as gr
|
2 |
-
from
|
3 |
-
|
4 |
-
"""
|
5 |
-
For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
|
6 |
-
"""
|
7 |
-
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
8 |
-
|
9 |
-
|
10 |
-
def respond(
|
11 |
-
message,
|
12 |
-
history: list[tuple[str, str]],
|
13 |
-
system_message,
|
14 |
-
max_tokens,
|
15 |
-
temperature,
|
16 |
-
top_p,
|
17 |
-
):
|
18 |
-
messages = [{"role": "system", "content": system_message}]
|
19 |
-
|
20 |
-
for val in history:
|
21 |
-
if val[0]:
|
22 |
-
messages.append({"role": "user", "content": val[0]})
|
23 |
-
if val[1]:
|
24 |
-
messages.append({"role": "assistant", "content": val[1]})
|
25 |
-
|
26 |
-
messages.append({"role": "user", "content": message})
|
27 |
-
|
28 |
-
response = ""
|
29 |
-
|
30 |
-
for message in client.chat_completion(
|
31 |
-
messages,
|
32 |
-
max_tokens=max_tokens,
|
33 |
-
stream=True,
|
34 |
-
temperature=temperature,
|
35 |
-
top_p=top_p,
|
36 |
-
):
|
37 |
-
token = message.choices[0].delta.content
|
38 |
-
|
39 |
-
response += token
|
40 |
-
yield response
|
41 |
-
|
42 |
-
|
43 |
-
"""
|
44 |
-
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
45 |
-
"""
|
46 |
-
demo = gr.ChatInterface(
|
47 |
-
respond,
|
48 |
-
additional_inputs=[
|
49 |
-
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
50 |
-
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
51 |
-
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
52 |
-
gr.Slider(
|
53 |
-
minimum=0.1,
|
54 |
-
maximum=1.0,
|
55 |
-
value=0.95,
|
56 |
-
step=0.05,
|
57 |
-
label="Top-p (nucleus sampling)",
|
58 |
-
),
|
59 |
-
],
|
60 |
-
)
|
61 |
-
|
62 |
|
|
|
|
|
|
|
|
|
|
|
63 |
if __name__ == "__main__":
|
64 |
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
+
from module import chat_with_memory
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
|
4 |
+
with gr.Blocks() as demo:
|
5 |
+
chatbot = gr.Chatbot()
|
6 |
+
txt = gr.Textbox(placeholder="Type your message and press β")
|
7 |
+
txt.submit(lambda msg, hist: (None, hist + [[msg, chat_with_memory(msg)]]),
|
8 |
+
[txt, chatbot], [txt, chatbot])
|
9 |
if __name__ == "__main__":
|
10 |
demo.launch()
|
module.py
ADDED
@@ -0,0 +1,108 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import re
|
2 |
+
from transformers import pipeline, AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig
|
3 |
+
from langchain.memory import ConversationBufferMemory
|
4 |
+
import torch, unsloth, triton
|
5 |
+
|
6 |
+
# βββ LOAD MODEL & TOKENIZER βββββββββββββββββββββββββββββββββββββββββββββ
|
7 |
+
# Adjust paths or HF repo as needed
|
8 |
+
FINETUNED_DIR = "/content/drive/MyDrive/bitext-qlora-tinyllama"
|
9 |
+
bnb_cfg = BitsAndBytesConfig(
|
10 |
+
load_in_4bit=True,
|
11 |
+
bnb_4bit_quant_type="nf4",
|
12 |
+
bnb_4bit_use_double_quant=True,
|
13 |
+
bnb_4bit_compute_dtype=torch.bfloat16
|
14 |
+
)
|
15 |
+
tokenizer = AutoTokenizer.from_pretrained(FINETUNED_DIR, use_fast=False)
|
16 |
+
tokenizer.pad_token_id = tokenizer.eos_token_id
|
17 |
+
tokenizer.padding_side = "left"
|
18 |
+
tokenizer.truncation_side = "right"
|
19 |
+
model = AutoModelForCausalLM.from_pretrained(
|
20 |
+
FINETUNED_DIR,
|
21 |
+
quantization_config=bnb_cfg,
|
22 |
+
device_map="auto",
|
23 |
+
trust_remote_code=True
|
24 |
+
)
|
25 |
+
|
26 |
+
# βββ MEMORY & PIPELINE βββββββββββββββββββββββββββββββββββββββββββββββββ
|
27 |
+
memory = ConversationBufferMemory(memory_key="user_lines",
|
28 |
+
human_prefix="User",
|
29 |
+
ai_prefix="Assistant",
|
30 |
+
return_messages=False)
|
31 |
+
stored_order = None
|
32 |
+
pending_intent = None
|
33 |
+
|
34 |
+
chat_pipe = pipeline(
|
35 |
+
"text-generation",
|
36 |
+
model=model,
|
37 |
+
tokenizer=tokenizer,
|
38 |
+
trust_remote_code=True,
|
39 |
+
return_full_text=False
|
40 |
+
)
|
41 |
+
|
42 |
+
# βββ HELPERS & HANDLERS ββββββββββββββββββββββββββββββββββββββββββββββββ
|
43 |
+
order_re = re.compile(r"#(\\d{1,10})")
|
44 |
+
def extract_order(text):
|
45 |
+
m = order_re.search(text)
|
46 |
+
return m.group(1) if m else None
|
47 |
+
|
48 |
+
def handle_status(o):
|
49 |
+
return f"Order #{o} is in transit and should arrive in 3β5 business days."
|
50 |
+
def handle_eta(o):
|
51 |
+
return (f"Delivery for order #{o} typically takes 3β5 days; "
|
52 |
+
f"you can track it at https://track.example.com/{o}")
|
53 |
+
def handle_track(o):
|
54 |
+
return f"Track order #{o} here: https://track.example.com/{o}"
|
55 |
+
def handle_link(o):
|
56 |
+
return f"Hereβs the latest tracking link for order #{o}: https://track.example.com/{o}"
|
57 |
+
def handle_return_policy(_=None):
|
58 |
+
return ("Our return policy allows returns of unused items in their original packaging "
|
59 |
+
"within 30 days of receipt. Would you like me to connect you with a human agent?")
|
60 |
+
def handle_gratitude(_=None):
|
61 |
+
return "Youβre welcome! Is there anything else I can help with?"
|
62 |
+
def handle_escalation(_=None):
|
63 |
+
return "Iβm sorry, I donβt have that information. Would you like me to connect you with a human agent?"
|
64 |
+
|
65 |
+
# βββ MAIN CHAT FUNCTION ββββββββββββββββββββββββββββββββββββββββββββββββ
|
66 |
+
def chat_with_memory(user_input: str) -> str:
|
67 |
+
global stored_order, pending_intent
|
68 |
+
|
69 |
+
memory.save_context({"input": user_input}, {"output": ""})
|
70 |
+
new_o = extract_order(user_input)
|
71 |
+
if new_o:
|
72 |
+
stored_order = new_o
|
73 |
+
if pending_intent in ("status","eta","track","link"):
|
74 |
+
fn = {"status":handle_status,"eta":handle_eta,
|
75 |
+
"track":handle_track,"link":handle_link}[pending_intent]
|
76 |
+
reply = fn(stored_order)
|
77 |
+
pending_intent = None
|
78 |
+
memory.save_context({"input": user_input}, {"output": reply})
|
79 |
+
return reply
|
80 |
+
|
81 |
+
ui = user_input.lower().strip()
|
82 |
+
if any(tok in ui for tok in ["thank you","thanks","thx"]):
|
83 |
+
reply = handle_gratitude()
|
84 |
+
elif "return" in ui:
|
85 |
+
reply = handle_return_policy()
|
86 |
+
elif any(k in ui for k in ["status","where is my order","check status"]):
|
87 |
+
intent = "status"
|
88 |
+
elif any(k in ui for k in ["how long","eta","delivery time"]):
|
89 |
+
intent = "eta"
|
90 |
+
elif any(k in ui for k in ["how can i track","track my order","where is my package"]):
|
91 |
+
intent = "track"
|
92 |
+
elif "tracking link" in ui or "resend" in ui:
|
93 |
+
intent = "link"
|
94 |
+
else:
|
95 |
+
intent = "fallback"
|
96 |
+
|
97 |
+
if intent in ("status","eta","track","link"):
|
98 |
+
if not stored_order:
|
99 |
+
pending_intent = intent
|
100 |
+
reply = "Sureβwhatβs your order number (e.g., #12345)?"
|
101 |
+
else:
|
102 |
+
reply = {"status":handle_status,"eta":handle_eta,
|
103 |
+
"track":handle_track,"link":handle_link}[intent](stored_order)
|
104 |
+
else:
|
105 |
+
reply = handle_escalation()
|
106 |
+
|
107 |
+
memory.save_context({"input": user_input}, {"output": reply})
|
108 |
+
return reply
|
requirements.txt
CHANGED
@@ -1 +1,10 @@
|
|
1 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
gradio
|
2 |
+
transformers
|
3 |
+
bitsandbytes
|
4 |
+
accelerate
|
5 |
+
xformers
|
6 |
+
sentencepiece
|
7 |
+
langchain
|
8 |
+
unsloth
|
9 |
+
unsloth_zoo
|
10 |
+
huggingface_hub
|