Update model/main.py
Browse files- model/main.py +112 -114
model/main.py
CHANGED
@@ -1,115 +1,113 @@
|
|
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 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
def
|
30 |
-
return s.
|
31 |
-
def
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
answer =
|
52 |
-
|
53 |
-
answer =
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
chat_model
|
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 |
-
answer =
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
if __name__ == "__main__":
|
115 |
main()
|
|
|
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 |
+
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.
|
16 |
+
def repl(match):
|
17 |
+
expr = match.group(1)
|
18 |
+
try:
|
19 |
+
if re.fullmatch(r"[\d\s\+\-\*\/]+", expr):
|
20 |
+
return str(eval(expr))
|
21 |
+
except Exception:
|
22 |
+
pass
|
23 |
+
return match.group(0)
|
24 |
+
|
25 |
+
return re.sub(r"\{([^{}]+)\}", repl, text)
|
26 |
+
|
27 |
+
def remove_before_first_colon(s: str) -> str:
|
28 |
+
return s.split("BOT :", 1)[-1]
|
29 |
+
def remove_before_last_colon(s: str) -> str:
|
30 |
+
return s.rsplit(":", 1)[-1]
|
31 |
+
def remove_after_user(text):
|
32 |
+
keyword = "USER"
|
33 |
+
index = text.find(keyword)
|
34 |
+
if index != -1:
|
35 |
+
return text[:index + len(keyword)]
|
36 |
+
return text
|
37 |
+
def remove_after_bot(text):
|
38 |
+
keyword = "BOT"
|
39 |
+
index = text.find(keyword)
|
40 |
+
if index != -1:
|
41 |
+
return text[:index + len(keyword)]
|
42 |
+
return text
|
43 |
+
|
44 |
+
def truncate(answer):
|
45 |
+
for sep in ["\n", "USER:", "BOT:"]:
|
46 |
+
if sep in answer:
|
47 |
+
answer = answer.split(sep)[0]
|
48 |
+
|
49 |
+
answer = remove_before_first_colon(answer)
|
50 |
+
answer = remove_after_user(answer)
|
51 |
+
answer = remove_after_bot(answer)
|
52 |
+
|
53 |
+
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β")
|
54 |
+
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.
|
55 |
+
|
56 |
+
return answer.strip()
|
57 |
+
|
58 |
+
def load_models():
|
59 |
+
print("π€ Loading modelsβ¦")
|
60 |
+
chat_model = load_learner("model/SimpleMath.pkl")
|
61 |
+
chat_model.model.eval()
|
62 |
+
return chat_model
|
63 |
+
|
64 |
+
def main():
|
65 |
+
chat_model = load_models()
|
66 |
+
history = deque()
|
67 |
+
print("π¬ Ready! (empty line to quit)\n")
|
68 |
+
|
69 |
+
while True:
|
70 |
+
try:
|
71 |
+
user = input("USER: ").strip()
|
72 |
+
if not user:
|
73 |
+
break
|
74 |
+
|
75 |
+
history.append(f"USER: {user}")
|
76 |
+
while len(history) > MAX_HISTORY_MESSAGES:
|
77 |
+
history.popleft()
|
78 |
+
|
79 |
+
prompt_lines = list(history)
|
80 |
+
prompt_text = " ".join(history).replace("\n"," ")
|
81 |
+
if len(prompt_text) > MAX_HISTORY_CHARS:
|
82 |
+
prompt_text = prompt_text[-MAX_HISTORY_CHARS:]
|
83 |
+
prompt = f"{prompt_text} BOT: "
|
84 |
+
|
85 |
+
generated = chat_model.predict(
|
86 |
+
prompt,
|
87 |
+
n_words=GENERATE_TOKENS,
|
88 |
+
temperature=TEMPERATURE,
|
89 |
+
min_p=0.01
|
90 |
+
)
|
91 |
+
|
92 |
+
try:
|
93 |
+
_, raw = generated.split(prompt, 1)
|
94 |
+
except ValueError:
|
95 |
+
raw = generated
|
96 |
+
|
97 |
+
raw = raw.strip()
|
98 |
+
if raw.upper().startswith("USER:") and "BOT:" in raw:
|
99 |
+
raw = raw.split("BOT:", 1)[1].strip()
|
100 |
+
|
101 |
+
answer = truncate(raw)
|
102 |
+
answer = evaluate_placeholders(answer)
|
103 |
+
answer = answer.replace("-", "\n-").replace("1)", "\n1)").replace("2)", "\n2)").replace("3)", "\n3)").replace("4)", "\n4)").replace("5)", "\n5)").replace("* ", "\n* ").replace("Final", "\nFinal")
|
104 |
+
if not "Final" in answer:
|
105 |
+
answer = answer.replace("Result", "\nResult")
|
106 |
+
print("BOT:", answer, "\n")
|
107 |
+
history.append(f"BOT: {answer}")
|
108 |
+
|
109 |
+
except KeyboardInterrupt:
|
110 |
+
break
|
111 |
+
|
112 |
+
if __name__ == "__main__":
|
|
|
|
|
113 |
main()
|