Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,96 +1,58 @@
|
|
1 |
-
# 0. Install custom transformers and imports
|
2 |
import os
|
3 |
-
|
4 |
-
os.system("pip install python-docx")
|
5 |
-
|
6 |
-
import threading
|
7 |
import torch
|
8 |
-
import
|
9 |
-
torch._dynamo.config.suppress_errors = True
|
10 |
-
|
11 |
-
from transformers import (
|
12 |
-
AutoModelForCausalLM,
|
13 |
-
AutoTokenizer,
|
14 |
-
TextIteratorStreamer,
|
15 |
-
)
|
16 |
import gradio as gr
|
17 |
-
import spaces
|
18 |
-
from docx import Document
|
19 |
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
1. Greet the customer warmly.
|
24 |
-
2. Help them order food and drinks from our menu.
|
25 |
-
3. Ask the customer for their desired pickup time.
|
26 |
-
4. Confirm the pickup time before ending the conversation.
|
27 |
-
5. Answer questions about ingredients, preparation, etc.
|
28 |
-
6. Handle special requests (allergies, modifications) politely.
|
29 |
-
7. Provide calorie information if asked.
|
30 |
-
Always be polite, helpful, and ensure the customer feels welcomed and cared for!
|
31 |
-
"""
|
32 |
|
33 |
-
|
|
|
|
|
|
|
|
|
34 |
|
35 |
-
#
|
36 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
|
37 |
-
model = AutoModelForCausalLM.from_pretrained(
|
38 |
-
MODEL_ID,
|
39 |
-
torch_dtype=torch.bfloat16,
|
40 |
-
device_map="auto"
|
41 |
-
)
|
42 |
|
43 |
-
|
|
|
44 |
|
45 |
-
#
|
46 |
-
def
|
47 |
doc = Document(docx_path)
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
return "\n".join(full_text)
|
53 |
|
54 |
-
|
55 |
-
|
|
|
|
|
56 |
|
57 |
-
#
|
58 |
-
def
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
if not matches:
|
63 |
-
return "Sorry, I couldn't find relevant menu information."
|
64 |
-
return "\n\n".join(matches[:top_k])
|
65 |
|
66 |
-
#
|
67 |
-
|
68 |
-
|
69 |
-
message: str,
|
70 |
-
history: list[tuple[str, str]],
|
71 |
-
system_message: str,
|
72 |
-
max_tokens: int,
|
73 |
-
temperature: float,
|
74 |
-
top_p: float,
|
75 |
-
):
|
76 |
-
context = retrieve_context(message)
|
77 |
-
|
78 |
messages = [{"role": "system", "content": system_message}]
|
79 |
-
for
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
messages.append({"role": "assistant", "content": bot_msg})
|
84 |
-
messages.append({"role": "user", "content": f"{message}\n\nRelevant menu info:\n{context}"})
|
85 |
|
86 |
-
prompt = tokenizer.apply_chat_template(
|
87 |
-
|
88 |
-
)
|
89 |
-
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
90 |
|
91 |
-
streamer = TextIteratorStreamer(
|
92 |
-
tokenizer, skip_prompt=True, skip_special_tokens=True
|
93 |
-
)
|
94 |
generate_kwargs = dict(
|
95 |
**inputs,
|
96 |
streamer=streamer,
|
@@ -99,64 +61,31 @@ def respond(
|
|
99 |
top_p=top_p,
|
100 |
do_sample=True,
|
101 |
)
|
|
|
102 |
thread = threading.Thread(target=model.generate, kwargs=generate_kwargs)
|
103 |
thread.start()
|
104 |
|
105 |
-
|
106 |
-
for
|
107 |
-
|
108 |
-
yield
|
109 |
|
110 |
-
#
|
111 |
demo = gr.ChatInterface(
|
112 |
-
fn=
|
113 |
-
title="Café Eleven Assistant",
|
114 |
-
description="
|
115 |
examples=[
|
116 |
-
[
|
117 |
-
|
118 |
-
SYSTEM_PROMPT.strip(),
|
119 |
-
512,
|
120 |
-
0.7,
|
121 |
-
0.95,
|
122 |
-
],
|
123 |
-
[
|
124 |
-
"Do you have gluten-free pastries?",
|
125 |
-
SYSTEM_PROMPT.strip(),
|
126 |
-
512,
|
127 |
-
0.7,
|
128 |
-
0.95,
|
129 |
-
],
|
130 |
],
|
131 |
additional_inputs=[
|
132 |
-
gr.Textbox(
|
133 |
-
|
134 |
-
|
135 |
-
),
|
136 |
-
|
137 |
-
minimum=1,
|
138 |
-
maximum=2048,
|
139 |
-
value=512,
|
140 |
-
step=1,
|
141 |
-
label="Max new tokens"
|
142 |
-
),
|
143 |
-
gr.Slider(
|
144 |
-
minimum=0.1,
|
145 |
-
maximum=4.0,
|
146 |
-
value=0.7,
|
147 |
-
step=0.1,
|
148 |
-
label="Temperature"
|
149 |
-
),
|
150 |
-
gr.Slider(
|
151 |
-
minimum=0.1,
|
152 |
-
maximum=1.0,
|
153 |
-
value=0.95,
|
154 |
-
step=0.05,
|
155 |
-
label="Top-p (nucleus sampling)"
|
156 |
-
),
|
157 |
-
],
|
158 |
)
|
159 |
|
160 |
-
# 7. Launch
|
161 |
if __name__ == "__main__":
|
162 |
demo.launch(share=True)
|
|
|
|
|
1 |
import os
|
2 |
+
import faiss
|
|
|
|
|
|
|
3 |
import torch
|
4 |
+
import threading
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
import gradio as gr
|
|
|
|
|
6 |
|
7 |
+
from docx import Document
|
8 |
+
from sentence_transformers import SentenceTransformer
|
9 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
|
11 |
+
# === Configuration ===
|
12 |
+
MODEL_ID = "microsoft/phi-2"
|
13 |
+
EMBED_MODEL = "sentence-transformers/all-MiniLM-L6-v2"
|
14 |
+
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
|
15 |
+
SYSTEM_PROMPT = """You are a friendly café assistant. Help customers place orders, check ingredients, and provide warm service."""
|
16 |
|
17 |
+
# === Load LLM ===
|
18 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
|
19 |
+
model = AutoModelForCausalLM.from_pretrained(MODEL_ID).to(DEVICE)
|
|
|
|
|
|
|
|
|
20 |
|
21 |
+
# === Load Embedder ===
|
22 |
+
embedder = SentenceTransformer(EMBED_MODEL)
|
23 |
|
24 |
+
# === Load Menu Text ===
|
25 |
+
def load_menu(docx_path):
|
26 |
doc = Document(docx_path)
|
27 |
+
return [p.text.strip() for p in doc.paragraphs if p.text.strip()]
|
28 |
+
|
29 |
+
menu_chunks = load_menu("menu.docx")
|
30 |
+
chunk_embeddings = embedder.encode(menu_chunks, convert_to_tensor=True).cpu().numpy()
|
|
|
31 |
|
32 |
+
# === Build FAISS Index ===
|
33 |
+
dimension = chunk_embeddings.shape[1]
|
34 |
+
index = faiss.IndexFlatL2(dimension)
|
35 |
+
index.add(chunk_embeddings)
|
36 |
|
37 |
+
# === Retrieval ===
|
38 |
+
def retrieve_context_faiss(query, top_k=3):
|
39 |
+
query_vec = embedder.encode([query]).astype("float32")
|
40 |
+
distances, indices = index.search(query_vec, top_k)
|
41 |
+
return "\n".join([menu_chunks[i] for i in indices[0]])
|
|
|
|
|
|
|
42 |
|
43 |
+
# === Generate LLM Response ===
|
44 |
+
def generate_response(message, history, system_message, max_tokens, temperature, top_p):
|
45 |
+
context = retrieve_context_faiss(message)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
46 |
messages = [{"role": "system", "content": system_message}]
|
47 |
+
for user, bot in history:
|
48 |
+
messages.append({"role": "user", "content": user})
|
49 |
+
messages.append({"role": "assistant", "content": bot})
|
50 |
+
messages.append({"role": "user", "content": f"{message}\n\nRelevant info:\n{context}"})
|
|
|
|
|
51 |
|
52 |
+
prompt = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
|
53 |
+
inputs = tokenizer(prompt, return_tensors="pt").to(DEVICE)
|
|
|
|
|
54 |
|
55 |
+
streamer = TextIteratorStreamer(tokenizer, skip_prompt=True, skip_special_tokens=True)
|
|
|
|
|
56 |
generate_kwargs = dict(
|
57 |
**inputs,
|
58 |
streamer=streamer,
|
|
|
61 |
top_p=top_p,
|
62 |
do_sample=True,
|
63 |
)
|
64 |
+
|
65 |
thread = threading.Thread(target=model.generate, kwargs=generate_kwargs)
|
66 |
thread.start()
|
67 |
|
68 |
+
output = ""
|
69 |
+
for token in streamer:
|
70 |
+
output += token
|
71 |
+
yield output
|
72 |
|
73 |
+
# === UI ===
|
74 |
demo = gr.ChatInterface(
|
75 |
+
fn=generate_response,
|
76 |
+
title="Café Eleven RAG Assistant",
|
77 |
+
description="LLM + FAISS powered café chatbot with real-time Word document lookup.",
|
78 |
examples=[
|
79 |
+
["Do you have vegetarian options?", SYSTEM_PROMPT, 512, 0.7, 0.9],
|
80 |
+
["What's in the turkey sandwich?", SYSTEM_PROMPT, 512, 0.7, 0.9],
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
81 |
],
|
82 |
additional_inputs=[
|
83 |
+
gr.Textbox(value=SYSTEM_PROMPT, label="System Prompt"),
|
84 |
+
gr.Slider(1, 1024, 512, label="Max Tokens"),
|
85 |
+
gr.Slider(0.1, 2.0, 0.7, label="Temperature"),
|
86 |
+
gr.Slider(0.1, 1.0, 0.9, label="Top-p"),
|
87 |
+
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
88 |
)
|
89 |
|
|
|
90 |
if __name__ == "__main__":
|
91 |
demo.launch(share=True)
|