MichaelEz123 commited on
Commit
3a4b21e
Β·
verified Β·
1 Parent(s): ef0e102

Update model/main.py

Browse files
Files changed (1) hide show
  1. 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
- 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()
 
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()