-
Notifications
You must be signed in to change notification settings - Fork 0
/
ui.py
378 lines (309 loc) · 12.9 KB
/
ui.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
import ast
import os
import difflib
import re
import openai
import streamlit as st
from faster_whisper import WhisperModel
from dotenv import load_dotenv
from transformers import pipeline
from gliner import GLiNER
load_dotenv()
ie_model_url = os.environ.get("IE_MODEL", "http://localhost:8706")
math_model_url = os.environ.get("MATH_MODEL", "http://localhost:8708")
stt_model = WhisperModel("small.en", device="cpu", compute_type="int8")
math_model = openai.OpenAI(base_url=math_model_url, api_key="sk-no-key-required")
ie_model = openai.OpenAI(base_url=ie_model_url, api_key="sk-no-key-required")
ner_model = GLiNER.from_pretrained("numind/NuNerZero")
classifier_model = pipeline(
"zero-shot-classification", model="tasksource/deberta-base-long-nli"
)
query_labels = ["order transaction"]
st.set_page_config(page_title="Saia | The Solopreneur's AI Assistant")
st.markdown(
"""
<style>
.reportview-container {
margin-top: -2em;
}
#MainMenu {visibility: show;}
.stAppDeployButton {display:none;}
footer {visibility: hidden;}
#stDecoration {display:none;}
</style>
""",
unsafe_allow_html=True,
)
def transcribe_transaction(audio_value):
transcript = ""
with st.spinner("Transcribing..."):
segments, _ = stt_model.transcribe(audio_value)
for s in segments:
transcript = f"{transcript} {s.text}"
return transcript
def call_llm(messages, client, temperature=0.7):
res = client.chat.completions.create(
model="local-llm",
messages=messages,
max_tokens=4096,
temperature=temperature,
)
return res.choices[0].message.content
def get_last_assigned_variable_name_and_value(code):
# Parse the code into an AST
tree = ast.parse(code)
# Traverse the AST to find the last assignment statement
last_assignment = None
for node in ast.walk(tree):
if isinstance(node, ast.Assign):
last_assignment = node
# Extract the name of the last assigned variable
if last_assignment:
last_var_name = last_assignment.targets[0].id
else:
return None, None
# Execute the code to create the variables in the local namespace
local_vars = {}
exec(code, {}, local_vars)
# Retrieve the value of the last assigned variable
last_var_value = local_vars.get(last_var_name)
return last_var_name, last_var_value
def extract_python_code(response_text: str):
"""
Extract and remove text within ````output ... ``` markers, while keeping the text within ````python ... ``` markers.
:param response_text: The string containing the code and output blocks.
:return: A tuple containing the extracted Python code and the modified string with the output block removed.
"""
extracted_python = []
modified_response_text = []
in_python_block = False
in_output_block = False
last_boxed_sentence = None
for line in response_text.split("\n"):
if line.strip() == "```python":
in_python_block = True
extracted_python = [] # Reset the list to only keep the last Python block
elif line.strip() == "```output":
in_output_block = True
elif line.strip() == "```":
in_python_block = False
in_output_block = False
elif in_python_block:
extracted_python.append(line)
elif not in_output_block:
if "\\boxed{" in line:
last_boxed_sentence = line
else:
modified_response_text.append(line)
extracted_python_text = "\n".join(extracted_python)
modified_response_text = "\n".join(modified_response_text)
return extracted_python_text, modified_response_text, last_boxed_sentence
def compute_by_llm_tir(transcript):
messages = [
{
"role": "system",
"content": "You will assist in computing sales transactions in pesos as the currency or monetary unit. Please integrate natural language reasoning with programs to solve the problems, and put your final answer within \\boxed{}.",
},
{
"role": "user",
"content": f"{transcript}\n\nImportant: When writing the python code, ALWAYS store the final answer in the 'answer' variable.",
},
]
res = math_model.chat.completions.create(
model="local-llm",
messages=messages,
max_tokens=4096,
temperature=0.7,
)
return res.choices[0].message.content
def solution_pipeline(transcript, max_retries=3):
for attempt in range(max_retries):
try:
response = compute_by_llm_tir(transcript)
python_code, modified_response, last_boxed_sentence = extract_python_code(
response
)
_, last_var_value = get_last_assigned_variable_name_and_value(python_code)
return python_code, modified_response, last_boxed_sentence, last_var_value
except SyntaxError:
if attempt < max_retries - 1:
continue
else:
raise
def write_invoice(transcript, new_text):
schema = {
"items": [{"name": "", "price": "", "quantity": "", "discount": ""}],
"total": "",
}
invoice_ie_prompt = [
{
"role": "system",
"content": "You will write an invoice from an order transcript. You will output in JSON format strictly according to the schema specified.",
},
{
"role": "user",
"content": "Order transcript: {transcript}\n{new_text}\n\nTask: Extract the items ordered, their prices, quantities, and if any discounts, as well as the total amount to pay. Strictly follow this JSON schema: {schema}".format(
transcript=transcript, new_text=new_text, schema=schema
),
},
]
res = ie_model.chat.completions.create(
model="local-llm",
messages=invoice_ie_prompt,
max_tokens=4096,
temperature=0.7,
response_format={
"type": "json_object",
"schema": {
"type": "object",
"properties": {
"items": {
"type": "array",
"items": {
"type": "object",
"properties": {
"name": {"type": "string"},
"price": {"type": "number"},
"quantity": {"type": "number"},
"discount": {"type": "number"},
},
"required": ["name", "price", "quantity", "discount"],
},
},
"total": {"type": "number"},
},
"required": ["items", "total"],
},
},
)
return res.choices[0].message.content
def merge_entities(entities):
if not entities:
return []
merged = []
current = entities[0]
for next_entity in entities[1:]:
if next_entity["entity"] == current["entity"] and (
next_entity["start"] == current["end"] + 1
or next_entity["start"] == current["end"]
):
current["word"] += " " + next_entity["word"]
current["end"] = next_entity["end"]
else:
merged.append(current)
current = next_entity
merged.append(current)
return merged
def extract_ordered_items(transcript):
labels = ["ordered_item"]
r = {
"text": transcript,
"entities": [
{
"entity": entity["label"],
"word": entity["text"],
"start": entity["start"],
"end": entity["end"],
"score": 0,
}
for entity in ner_model.predict_entities(transcript, labels, threshold=0.3)
],
}
r["entities"] = merge_entities(r["entities"])
items = ""
for item in r["entities"]:
items = f"{item['word']}, {items}"
return items
def get_variants(ordered_items: str):
ordered_items = ordered_items.lower()
variant_pattern = re.compile(r"\b(large|medium|small)\b")
items_witn_variants = []
for item in ordered_items.split(",")[:-1]:
item = item.strip()
match = variant_pattern.search(item)
if match:
size = match.group(0)
name = item.replace(size, "").strip()
items_witn_variants.append((name, size))
return items_witn_variants
def find_closest_match(input_str, choices):
matches = difflib.get_close_matches(input_str, choices, n=1, cutoff=0.6)
return matches[0] if matches else None
def get_price(product_name, variant, products, variant_class="size"):
names = {product["name"] for product in products}
variants = {product[variant_class] for product in products}
# Find the closest matches
closest_product_name = find_closest_match(product_name, names)
closest_variant = find_closest_match(variant, variants) if variant else None
if closest_product_name and closest_variant:
# Filter the products to get the price
for product in products:
if (
product["name"] == closest_product_name
and product[variant_class] == closest_variant
):
return product["price"]
elif closest_product_name:
# If variant is not provided, find the first match with the closest product_name
for product in products:
if product["name"] == closest_product_name:
return product["price"]
return None
products = [
{"name": "Americano", "size": "large", "price": 90},
{"name": "Americano", "size": "medium", "price": 80},
{"name": "Americano", "size": "small", "price": 50},
{"name": "Vanilla Latte", "size": "large", "price": 110},
{"name": "Vanilla Latte", "size": "medium", "price": 100},
{"name": "Vanilla Latte", "size": "small", "price": 50},
{"name": "Strawberry Matcha", "size": "large", "price": 130},
{"name": "Strawberry Matcha", "size": "medium", "price": 120},
{"name": "Strawberry Matcha", "size": "small", "price": 50},
]
def entry():
st.subheader("Saia - The Solopreneur's AI Assistant")
with st.expander("Read the instructions"):
st.markdown(
"""
***What is this for?*** This AI Assistant will compute how much the customer will pay based on their order.
***How to use this?*** Simply talk to the Assistant by saying the customer's order. You can say: "The customer ordered a Spanish Latte at 110 pesos with a 5% discount, an Americano at 100 pesos without a discount, and a Strawberry Matcha at 105 pesos with a 20% discount. How much will the customer pay?"
***Now what?*** To get started, just click the Microphone icon below to start recording, say the orders, then click the stop button when you're finished saying them. Don't forget to ask "How much will the customer pay?" at the end.
"""
)
audio_value = st.experimental_audio_input("Record audio")
if audio_value:
transcript = transcribe_transaction(audio_value)
st.markdown(f"##### Transaction transcript:\n\n{transcript}")
output = classifier_model(transcript, query_labels)
if output["scores"][0] > 0.8:
with st.spinner(
"Understanding the problem, then outlining and executing a step-by-step plan to solve it..."
):
items = extract_ordered_items(transcript)
items_with_variants = get_variants(items)
price_ref = ""
for name, variant in items_with_variants:
price = get_price(name, variant, products)
price_ref = f"{name} {variant}: {price} pesos\n{price_ref}"
transcript = f"{transcript}\n\n{price_ref}"
python_code, modified_response, last_boxed_sentence, last_var_value = (
solution_pipeline(transcript=transcript)
)
st.markdown(f"##### Transaction query solution:")
pattern = r"\\boxed\{([^\}]+)\}"
new_text = re.sub(
pattern, f"{last_var_value}", last_boxed_sentence, count=1
)
st.markdown(f"> {new_text}")
with st.expander("Read the step-by-step plan"):
st.markdown(modified_response)
with st.expander("View the code implementation"):
st.markdown(f"""```python\n{python_code}""")
with st.spinner("Writing invoice..."):
res = write_invoice(transcript=transcript, new_text=new_text)
with st.expander("View invoice"):
st.markdown(f"```json\n\n{res}")
else:
st.markdown("> Your query is not an order transaction.")
if __name__ == "__main__":
entry()