Upload 2 files
Browse files- model/SimpleMath.pkl +3 -0
- model/main.py +115 -0
model/SimpleMath.pkl
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:f58272ac8cfe63d065dd49572e47fac274096bb85ded06171ba9135d4a22b424
|
3 |
+
size 171953765
|
model/main.py
ADDED
@@ -0,0 +1,115 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
### SimpleMath. answer to simple math questions of + & - from 0 to 2000.
|
2 |
+
|
3 |
+
|
4 |
+
from collections import deque
|
5 |
+
from fastai.text.all import load_learner
|
6 |
+
import sys
|
7 |
+
import re
|
8 |
+
|
9 |
+
# βββ Config βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
10 |
+
MAX_HISTORY_CHARS = 800
|
11 |
+
MAX_HISTORY_MESSAGES = 1 # 1 = no memory. (this model does not support memory!)
|
12 |
+
GENERATE_TOKENS = 70
|
13 |
+
TEMPERATURE = 0.3
|
14 |
+
|
15 |
+
question_keywords = ["what", "how", "when", "where", "why"]
|
16 |
+
|
17 |
+
def evaluate_placeholders(text: str) -> str: # sometimes the model will return the answer like this 1 + 1 = {1 + 1} that can be calculated and returned like this.
|
18 |
+
def repl(match):
|
19 |
+
expr = match.group(1)
|
20 |
+
try:
|
21 |
+
if re.fullmatch(r"[\d\s\+\-\*\/]+", expr):
|
22 |
+
return str(eval(expr))
|
23 |
+
except Exception:
|
24 |
+
pass
|
25 |
+
return match.group(0)
|
26 |
+
|
27 |
+
return re.sub(r"\{([^{}]+)\}", repl, text)
|
28 |
+
|
29 |
+
def remove_before_first_colon(s: str) -> str:
|
30 |
+
return s.split("BOT :", 1)[-1]
|
31 |
+
def remove_before_last_colon(s: str) -> str:
|
32 |
+
return s.rsplit(":", 1)[-1]
|
33 |
+
def remove_after_user(text):
|
34 |
+
keyword = "USER"
|
35 |
+
index = text.find(keyword)
|
36 |
+
if index != -1:
|
37 |
+
return text[:index + len(keyword)]
|
38 |
+
return text
|
39 |
+
def remove_after_bot(text):
|
40 |
+
keyword = "BOT"
|
41 |
+
index = text.find(keyword)
|
42 |
+
if index != -1:
|
43 |
+
return text[:index + len(keyword)]
|
44 |
+
return text
|
45 |
+
|
46 |
+
def truncate(answer):
|
47 |
+
for sep in ["\n", "USER:", "BOT:"]:
|
48 |
+
if sep in answer:
|
49 |
+
answer = answer.split(sep)[0]
|
50 |
+
|
51 |
+
answer = remove_before_first_colon(answer)
|
52 |
+
answer = remove_after_user(answer)
|
53 |
+
answer = remove_after_bot(answer)
|
54 |
+
|
55 |
+
answer = answer.replace(": USER", "").replace(" USER", "").replace("USER", "").replace(" !", "!").replace(" .", ".").replace(" ,", ",").replace(": BOT", "").replace(" BOT", "").replace("BOT", "").replace(" `", "`").replace(' "', '"').replace(" β", "β").replace("do n'", "don'").replace("do nβ", "donβ")
|
56 |
+
answer = answer.replace(" '", "'").replace(" :", ":").replace(" (", "(").replace(" )", ")").replace(" ?", "?").replace("Open Assistant", "Bomba-1") # The bot thinks he is named "Open Assistant", i guess something in the datasets.
|
57 |
+
|
58 |
+
return answer.strip()
|
59 |
+
|
60 |
+
def load_models():
|
61 |
+
print("π€ Loading modelsβ¦")
|
62 |
+
chat_model = load_learner("model/SimpleMath.pkl")
|
63 |
+
chat_model.model.eval()
|
64 |
+
return chat_model
|
65 |
+
|
66 |
+
def main():
|
67 |
+
chat_model = load_models()
|
68 |
+
history = deque()
|
69 |
+
print("π¬ Ready! (empty line to quit)\n")
|
70 |
+
|
71 |
+
while True:
|
72 |
+
try:
|
73 |
+
user = input("USER: ").strip()
|
74 |
+
if not user:
|
75 |
+
break
|
76 |
+
|
77 |
+
history.append(f"USER: {user}")
|
78 |
+
while len(history) > MAX_HISTORY_MESSAGES:
|
79 |
+
history.popleft()
|
80 |
+
|
81 |
+
prompt_lines = list(history)
|
82 |
+
prompt_text = " ".join(history).replace("\n"," ")
|
83 |
+
if len(prompt_text) > MAX_HISTORY_CHARS:
|
84 |
+
prompt_text = prompt_text[-MAX_HISTORY_CHARS:]
|
85 |
+
prompt = f"{prompt_text} BOT: "
|
86 |
+
|
87 |
+
generated = chat_model.predict(
|
88 |
+
prompt,
|
89 |
+
n_words=GENERATE_TOKENS,
|
90 |
+
temperature=TEMPERATURE,
|
91 |
+
min_p=0.01
|
92 |
+
)
|
93 |
+
|
94 |
+
try:
|
95 |
+
_, raw = generated.split(prompt, 1)
|
96 |
+
except ValueError:
|
97 |
+
raw = generated
|
98 |
+
|
99 |
+
raw = raw.strip()
|
100 |
+
if raw.upper().startswith("USER:") and "BOT:" in raw:
|
101 |
+
raw = raw.split("BOT:", 1)[1].strip()
|
102 |
+
|
103 |
+
answer = truncate(raw)
|
104 |
+
answer = evaluate_placeholders(answer)
|
105 |
+
answer = answer.replace("-", "\n-").replace("1)", "\n1)").replace("2)", "\n2)").replace("3)", "\n3)").replace("4)", "\n4)").replace("5)", "\n5)").replace("* ", "\n* ").replace("Final", "\nFinal")
|
106 |
+
if not "Final" in answer:
|
107 |
+
answer = answer.replace("Result", "\nResult")
|
108 |
+
print("BOT:", answer, "\n")
|
109 |
+
history.append(f"BOT: {answer}")
|
110 |
+
|
111 |
+
except KeyboardInterrupt:
|
112 |
+
break
|
113 |
+
|
114 |
+
if __name__ == "__main__":
|
115 |
+
main()
|