Update Create_ml_models.py
Browse files- Create_ml_models.py +94 -0
Create_ml_models.py
CHANGED
@@ -0,0 +1,94 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from pages.admin_utils import *
|
3 |
+
from sklearn.svm import SVC
|
4 |
+
from sklearn.pipeline import make_pipeline
|
5 |
+
from sklearn.preprocessing import StandardScaler
|
6 |
+
import joblib
|
7 |
+
from admin_utils import *
|
8 |
+
|
9 |
+
st.title("Let's build our Model...")
|
10 |
+
|
11 |
+
# Create tabs
|
12 |
+
tab_titles = ['Data Preprocessing', 'Model Training', 'Model Evaluation',"Save Model"]
|
13 |
+
tabs = st.tabs(tab_titles)
|
14 |
+
|
15 |
+
# Adding content to each tab
|
16 |
+
|
17 |
+
#Data Preprocessing TAB...
|
18 |
+
with tabs[0]:
|
19 |
+
st.header('Data Preprocessing')
|
20 |
+
st.write('Here we preprocess the data...')
|
21 |
+
|
22 |
+
# Capture the CSV file
|
23 |
+
data = st.file_uploader("Upload CSV file",type="csv")
|
24 |
+
|
25 |
+
button = st.button("Load data",key="data")
|
26 |
+
|
27 |
+
if button:
|
28 |
+
with st.spinner('Wait for it...'):
|
29 |
+
our_data=read_data(data)
|
30 |
+
embeddings=get_embeddings()
|
31 |
+
st.session_state['cleaned_data'] = create_embeddings(our_data,embeddings)
|
32 |
+
st.success('Done!')
|
33 |
+
|
34 |
+
|
35 |
+
#Model Training TAB
|
36 |
+
with tabs[1]:
|
37 |
+
st.header('Model Training')
|
38 |
+
st.write('Here we train the model...')
|
39 |
+
button = st.button("Train model",key="model")
|
40 |
+
|
41 |
+
if button:
|
42 |
+
with st.spinner('Wait for it...'):
|
43 |
+
st.session_state['sentences_train'], st.session_state['sentences_test'], st.session_state['labels_train'], st.session_state['labels_test']=split_train_test__data(st.session_state['cleaned_data'])
|
44 |
+
|
45 |
+
# Initialize a support vector machine, with class_weight='balanced' because
|
46 |
+
# our training set has roughly an equal amount of positive and negative
|
47 |
+
# sentiment sentences
|
48 |
+
st.session_state['svm_classifier'] = make_pipeline(StandardScaler(), SVC(class_weight='balanced'))
|
49 |
+
|
50 |
+
# fit the support vector machine
|
51 |
+
st.session_state['svm_classifier'].fit(st.session_state['sentences_train'], st.session_state['labels_train'])
|
52 |
+
st.success('Done!')
|
53 |
+
|
54 |
+
#Model Evaluation TAB
|
55 |
+
with tabs[2]:
|
56 |
+
st.header('Model Evaluation')
|
57 |
+
st.write('Here we evaluate the model...')
|
58 |
+
button = st.button("Evaluate model",key="Evaluation")
|
59 |
+
|
60 |
+
if button:
|
61 |
+
with st.spinner('Wait for it...'):
|
62 |
+
accuracy_score=get_score(st.session_state['svm_classifier'],st.session_state['sentences_test'],st.session_state['labels_test'])
|
63 |
+
st.success(f"Validation accuracy is {100*accuracy_score}%!")
|
64 |
+
|
65 |
+
|
66 |
+
st.write("A sample run:")
|
67 |
+
|
68 |
+
|
69 |
+
#text="lack of communication regarding policy updates salary, can we please look into it?"
|
70 |
+
text="Rude driver with scary driving"
|
71 |
+
st.write("***Our issue*** : "+text)
|
72 |
+
|
73 |
+
#Converting out TEXT to NUMERICAL representaion
|
74 |
+
embeddings= get_embeddings()
|
75 |
+
query_result = embeddings.embed_query(text)
|
76 |
+
|
77 |
+
#Sample prediction using our trained model
|
78 |
+
result= st.session_state['svm_classifier'].predict([query_result])
|
79 |
+
st.write("***Department it belongs to*** : "+result[0])
|
80 |
+
|
81 |
+
|
82 |
+
st.success('Done!')
|
83 |
+
|
84 |
+
#Save model TAB
|
85 |
+
with tabs[3]:
|
86 |
+
st.header('Save model')
|
87 |
+
st.write('Here we save the model...')
|
88 |
+
|
89 |
+
button = st.button("Save model",key="save")
|
90 |
+
if button:
|
91 |
+
|
92 |
+
with st.spinner('Wait for it...'):
|
93 |
+
joblib.dump(st.session_state['svm_classifier'], 'modelsvm.pk1')
|
94 |
+
st.success('Done!')
|