-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
57 lines (47 loc) · 1.25 KB
/
model.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
# standard imports
import math
import pickle
import time
from datetime import datetime
# module imports
import pandas as pd
from pytz import timezone
from sklearn.ensemble import RandomForestClassifier
from sklearn.feature_extraction.text import CountVectorizer, TfidfTransformer
from sklearn.pipeline import Pipeline
# logging
last_time = time.time()
def log(*args):
"""Do logs for model processing."""
global last_time
# log
print(
"\x1b[2m",
datetime.now(timezone("Asia/Kolkata")).strftime("%Y-%m-%d %H:%M:%S"),
"\x1b[1m\x1b[97m",
"[MODEL] ",
"\x1b[0m",
"".join(args),
"\x1b[1m\x1b[97m | \x1b[91m",
math.floor(time.time() - last_time),
end="s\x1b[0m\n",
)
last_time = time.time()
# reading data
log("Processing Data....")
DF = pd.read_csv("./data/dialogs.txt", sep="|")
log("Data Processed....")
# model classifiers
log("Started Model Processing....")
MODEL = Pipeline(
[
("bow", CountVectorizer()),
("tfidf", TfidfTransformer(sublinear_tf=True)),
("classifier", RandomForestClassifier(n_estimators=100)),
]
)
# data fit
MODEL.fit(DF["question"], DF["answer"])
log("Model Processing Done...")
# dump
pickle.dump(MODEL, open("model.pkl", "wb"))