Spaces:
Sleeping
Sleeping
added touchpoint
Browse files- .ipynb_checkpoints/app-checkpoint.py +58 -0
- .ipynb_checkpoints/utils-checkpoint.py +17 -11
- app.py +4 -2
- models/tfidf_touchpoint.sav +0 -0
- models/touchpoint_model.sav +0 -0
- utils.py +17 -11
.ipynb_checkpoints/app-checkpoint.py
ADDED
@@ -0,0 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import plotly.express as px
|
3 |
+
from plotly.subplots import make_subplots
|
4 |
+
from utils import *
|
5 |
+
|
6 |
+
########## Title for the Web App ##########
|
7 |
+
st.title("Text Classification for HC")
|
8 |
+
|
9 |
+
########## Create Input field ##########
|
10 |
+
feedback = st.text_input('Type your text here', 'Customer suggested that the customer service needs to be improved and the response time needs to be improved.')
|
11 |
+
|
12 |
+
if st.button('Click for predictions!'):
|
13 |
+
with st.spinner('Generating predictions...'):
|
14 |
+
|
15 |
+
topics_prob, sentiment_prob, touchpoint_prob = get_single_prediction(feedback)
|
16 |
+
|
17 |
+
bar_topic = px.bar(topics_prob, x='probability', y='topic')
|
18 |
+
|
19 |
+
bar_touchpoint = px.bar(touchpoint_prob, x='probability', y='touchpoint')
|
20 |
+
|
21 |
+
pie = px.pie(sentiment_prob,
|
22 |
+
values='probability',
|
23 |
+
names='sentiment',
|
24 |
+
color_discrete_map={'positive':'rgb(0, 204, 0)',
|
25 |
+
'negative':'rgb(215, 11, 11)'
|
26 |
+
},
|
27 |
+
color='sentiment'
|
28 |
+
)
|
29 |
+
|
30 |
+
st.plotly_chart(bar, use_container_width=True)
|
31 |
+
st.plotly_chart(pie, use_container_width=True)
|
32 |
+
|
33 |
+
st.write("\n")
|
34 |
+
st.subheader('Or... Upload a csv file if you have a file instead.')
|
35 |
+
st.write("\n")
|
36 |
+
|
37 |
+
st.download_button(
|
38 |
+
label="Download sample file here",
|
39 |
+
data=sample_file,
|
40 |
+
file_name='sample_data.csv',
|
41 |
+
mime='text/csv',
|
42 |
+
)
|
43 |
+
|
44 |
+
uploaded_file = st.file_uploader("Please upload a csv file with only 1 column of texts.")
|
45 |
+
|
46 |
+
if uploaded_file is not None:
|
47 |
+
|
48 |
+
with st.spinner('Generating predictions...'):
|
49 |
+
results = get_multiple_predictions(uploaded_file)
|
50 |
+
|
51 |
+
st.download_button(
|
52 |
+
label="Download results as CSV",
|
53 |
+
data=results,
|
54 |
+
file_name='results.csv',
|
55 |
+
mime='text/csv',
|
56 |
+
)
|
57 |
+
|
58 |
+
|
.ipynb_checkpoints/utils-checkpoint.py
CHANGED
@@ -3,11 +3,12 @@ import pickle
|
|
3 |
import numpy as np
|
4 |
import pandas as pd
|
5 |
|
6 |
-
|
7 |
tfidf = pickle.load(open('models/tfidf.sav', 'rb'))
|
8 |
svc_sentiment = pickle.load(open('models/sentiment_model.sav', 'rb'))
|
9 |
tfidf_sentiment = pickle.load(open('models/tfidf_sentiment.sav', 'rb'))
|
10 |
-
|
|
|
11 |
|
12 |
labels = [
|
13 |
'Product quality', 'Knowledge',
|
@@ -23,23 +24,24 @@ def get_single_prediction(text):
|
|
23 |
|
24 |
# manipulate data into a format that we pass to our model
|
25 |
text = text.lower().strip() #lower case
|
26 |
-
|
27 |
-
# Vectorise text
|
28 |
-
text_vectors = tfidf.transform([text])
|
29 |
-
|
30 |
# Make topic predictions
|
|
|
31 |
results = svc.predict_proba(text_vectors).squeeze().round(2)
|
32 |
-
|
33 |
pred_prob = pd.DataFrame({'topic': labels, 'probability': results}).sort_values('probability', ascending=True)
|
34 |
|
35 |
# Make sentiment predictions
|
36 |
text_vectors_sentiment = tfidf_sentiment.transform([text])
|
37 |
|
38 |
-
results_sentiment = svc_sentiment.predict_proba(
|
39 |
pred_prob_sentiment = pd.DataFrame({'sentiment': ['Negative', 'Positive'], 'probability': results_sentiment}).sort_values('probability', ascending=True)
|
40 |
|
41 |
-
|
42 |
-
|
|
|
|
|
|
|
|
|
43 |
|
44 |
def get_multiple_predictions(csv):
|
45 |
|
@@ -65,9 +67,13 @@ def get_multiple_predictions(csv):
|
|
65 |
# Vectorise text and get sentiment predictions
|
66 |
text_vectors_sentiment = tfidf_sentiment.transform(df['sequence_clean'])
|
67 |
pred_results_sentiment = pd.DataFrame(svc_sentiment.predict(text_vectors_sentiment), columns = ['sentiment'])
|
|
|
|
|
|
|
|
|
68 |
|
69 |
# Join back to original sequence
|
70 |
-
final_results = df.join(pred_results).join(pred_results_sentiment)
|
71 |
|
72 |
final_results.drop(columns=['sequence_clean'], inplace=True)
|
73 |
|
|
|
3 |
import numpy as np
|
4 |
import pandas as pd
|
5 |
|
6 |
+
svc = pickle.load(open('models/svc_model.sav', 'rb'))
|
7 |
tfidf = pickle.load(open('models/tfidf.sav', 'rb'))
|
8 |
svc_sentiment = pickle.load(open('models/sentiment_model.sav', 'rb'))
|
9 |
tfidf_sentiment = pickle.load(open('models/tfidf_sentiment.sav', 'rb'))
|
10 |
+
svc_touchpoint = pickle.load(open('models/touchpoint_model.sav', 'rb'))
|
11 |
+
tfidf_touchpoint = pickle.load(open('models/tfidf_touchpoint.sav', 'rb'))
|
12 |
|
13 |
labels = [
|
14 |
'Product quality', 'Knowledge',
|
|
|
24 |
|
25 |
# manipulate data into a format that we pass to our model
|
26 |
text = text.lower().strip() #lower case
|
27 |
+
|
|
|
|
|
|
|
28 |
# Make topic predictions
|
29 |
+
text_vectors = tfidf.transform([text])
|
30 |
results = svc.predict_proba(text_vectors).squeeze().round(2)
|
|
|
31 |
pred_prob = pd.DataFrame({'topic': labels, 'probability': results}).sort_values('probability', ascending=True)
|
32 |
|
33 |
# Make sentiment predictions
|
34 |
text_vectors_sentiment = tfidf_sentiment.transform([text])
|
35 |
|
36 |
+
results_sentiment = svc_sentiment.predict_proba(text_vectors_sentiment).squeeze().round(2)
|
37 |
pred_prob_sentiment = pd.DataFrame({'sentiment': ['Negative', 'Positive'], 'probability': results_sentiment}).sort_values('probability', ascending=True)
|
38 |
|
39 |
+
# Make touchpoint predictions
|
40 |
+
text_vectors_touchpoint = tfidf_touchpoint.transform([text])
|
41 |
+
results_touchpoint = svc_touchpoint.predict_proba(text_vectors_touchpoint).squeeze().round(2)
|
42 |
+
pred_prob_touchpoint = pd.DataFrame({'touchpoint': ['ASC', 'CC', 'Technician'], 'probability': results_touchpoint}).sort_values('probability', ascending=True)
|
43 |
+
|
44 |
+
return (pred_prob, pred_prob_sentiment, pred_prob_touchpoint)
|
45 |
|
46 |
def get_multiple_predictions(csv):
|
47 |
|
|
|
67 |
# Vectorise text and get sentiment predictions
|
68 |
text_vectors_sentiment = tfidf_sentiment.transform(df['sequence_clean'])
|
69 |
pred_results_sentiment = pd.DataFrame(svc_sentiment.predict(text_vectors_sentiment), columns = ['sentiment'])
|
70 |
+
|
71 |
+
# Vectorise text and get touchpoint predictions
|
72 |
+
text_vectors_touchpoint = tfidf_touchpoint.transform(df['sequence_clean'])
|
73 |
+
pred_results_touchpoint = pd.DataFrame(svc_touchpoint.predict(text_vectors_touchpoint), columns = ['touchpoint'])
|
74 |
|
75 |
# Join back to original sequence
|
76 |
+
final_results = df.join(pred_results).join(pred_results_sentiment).join(pred_results_touchpoint)
|
77 |
|
78 |
final_results.drop(columns=['sequence_clean'], inplace=True)
|
79 |
|
app.py
CHANGED
@@ -12,9 +12,11 @@ feedback = st.text_input('Type your text here', 'Customer suggested that the cus
|
|
12 |
if st.button('Click for predictions!'):
|
13 |
with st.spinner('Generating predictions...'):
|
14 |
|
15 |
-
topics_prob, sentiment_prob = get_single_prediction(feedback)
|
16 |
|
17 |
-
|
|
|
|
|
18 |
|
19 |
pie = px.pie(sentiment_prob,
|
20 |
values='probability',
|
|
|
12 |
if st.button('Click for predictions!'):
|
13 |
with st.spinner('Generating predictions...'):
|
14 |
|
15 |
+
topics_prob, sentiment_prob, touchpoint_prob = get_single_prediction(feedback)
|
16 |
|
17 |
+
bar_topic = px.bar(topics_prob, x='probability', y='topic')
|
18 |
+
|
19 |
+
bar_touchpoint = px.bar(touchpoint_prob, x='probability', y='touchpoint')
|
20 |
|
21 |
pie = px.pie(sentiment_prob,
|
22 |
values='probability',
|
models/tfidf_touchpoint.sav
ADDED
Binary file (103 kB). View file
|
|
models/touchpoint_model.sav
ADDED
Binary file (136 kB). View file
|
|
utils.py
CHANGED
@@ -3,11 +3,12 @@ import pickle
|
|
3 |
import numpy as np
|
4 |
import pandas as pd
|
5 |
|
6 |
-
|
7 |
tfidf = pickle.load(open('models/tfidf.sav', 'rb'))
|
8 |
svc_sentiment = pickle.load(open('models/sentiment_model.sav', 'rb'))
|
9 |
tfidf_sentiment = pickle.load(open('models/tfidf_sentiment.sav', 'rb'))
|
10 |
-
|
|
|
11 |
|
12 |
labels = [
|
13 |
'Product quality', 'Knowledge',
|
@@ -23,23 +24,24 @@ def get_single_prediction(text):
|
|
23 |
|
24 |
# manipulate data into a format that we pass to our model
|
25 |
text = text.lower().strip() #lower case
|
26 |
-
|
27 |
-
# Vectorise text
|
28 |
-
text_vectors = tfidf.transform([text])
|
29 |
-
|
30 |
# Make topic predictions
|
|
|
31 |
results = svc.predict_proba(text_vectors).squeeze().round(2)
|
32 |
-
|
33 |
pred_prob = pd.DataFrame({'topic': labels, 'probability': results}).sort_values('probability', ascending=True)
|
34 |
|
35 |
# Make sentiment predictions
|
36 |
text_vectors_sentiment = tfidf_sentiment.transform([text])
|
37 |
|
38 |
-
results_sentiment = svc_sentiment.predict_proba(
|
39 |
pred_prob_sentiment = pd.DataFrame({'sentiment': ['Negative', 'Positive'], 'probability': results_sentiment}).sort_values('probability', ascending=True)
|
40 |
|
41 |
-
|
42 |
-
|
|
|
|
|
|
|
|
|
43 |
|
44 |
def get_multiple_predictions(csv):
|
45 |
|
@@ -65,9 +67,13 @@ def get_multiple_predictions(csv):
|
|
65 |
# Vectorise text and get sentiment predictions
|
66 |
text_vectors_sentiment = tfidf_sentiment.transform(df['sequence_clean'])
|
67 |
pred_results_sentiment = pd.DataFrame(svc_sentiment.predict(text_vectors_sentiment), columns = ['sentiment'])
|
|
|
|
|
|
|
|
|
68 |
|
69 |
# Join back to original sequence
|
70 |
-
final_results = df.join(pred_results).join(pred_results_sentiment)
|
71 |
|
72 |
final_results.drop(columns=['sequence_clean'], inplace=True)
|
73 |
|
|
|
3 |
import numpy as np
|
4 |
import pandas as pd
|
5 |
|
6 |
+
svc = pickle.load(open('models/svc_model.sav', 'rb'))
|
7 |
tfidf = pickle.load(open('models/tfidf.sav', 'rb'))
|
8 |
svc_sentiment = pickle.load(open('models/sentiment_model.sav', 'rb'))
|
9 |
tfidf_sentiment = pickle.load(open('models/tfidf_sentiment.sav', 'rb'))
|
10 |
+
svc_touchpoint = pickle.load(open('models/touchpoint_model.sav', 'rb'))
|
11 |
+
tfidf_touchpoint = pickle.load(open('models/tfidf_touchpoint.sav', 'rb'))
|
12 |
|
13 |
labels = [
|
14 |
'Product quality', 'Knowledge',
|
|
|
24 |
|
25 |
# manipulate data into a format that we pass to our model
|
26 |
text = text.lower().strip() #lower case
|
27 |
+
|
|
|
|
|
|
|
28 |
# Make topic predictions
|
29 |
+
text_vectors = tfidf.transform([text])
|
30 |
results = svc.predict_proba(text_vectors).squeeze().round(2)
|
|
|
31 |
pred_prob = pd.DataFrame({'topic': labels, 'probability': results}).sort_values('probability', ascending=True)
|
32 |
|
33 |
# Make sentiment predictions
|
34 |
text_vectors_sentiment = tfidf_sentiment.transform([text])
|
35 |
|
36 |
+
results_sentiment = svc_sentiment.predict_proba(text_vectors_sentiment).squeeze().round(2)
|
37 |
pred_prob_sentiment = pd.DataFrame({'sentiment': ['Negative', 'Positive'], 'probability': results_sentiment}).sort_values('probability', ascending=True)
|
38 |
|
39 |
+
# Make touchpoint predictions
|
40 |
+
text_vectors_touchpoint = tfidf_touchpoint.transform([text])
|
41 |
+
results_touchpoint = svc_touchpoint.predict_proba(text_vectors_touchpoint).squeeze().round(2)
|
42 |
+
pred_prob_touchpoint = pd.DataFrame({'touchpoint': ['ASC', 'CC', 'Technician'], 'probability': results_touchpoint}).sort_values('probability', ascending=True)
|
43 |
+
|
44 |
+
return (pred_prob, pred_prob_sentiment, pred_prob_touchpoint)
|
45 |
|
46 |
def get_multiple_predictions(csv):
|
47 |
|
|
|
67 |
# Vectorise text and get sentiment predictions
|
68 |
text_vectors_sentiment = tfidf_sentiment.transform(df['sequence_clean'])
|
69 |
pred_results_sentiment = pd.DataFrame(svc_sentiment.predict(text_vectors_sentiment), columns = ['sentiment'])
|
70 |
+
|
71 |
+
# Vectorise text and get touchpoint predictions
|
72 |
+
text_vectors_touchpoint = tfidf_touchpoint.transform(df['sequence_clean'])
|
73 |
+
pred_results_touchpoint = pd.DataFrame(svc_touchpoint.predict(text_vectors_touchpoint), columns = ['touchpoint'])
|
74 |
|
75 |
# Join back to original sequence
|
76 |
+
final_results = df.join(pred_results).join(pred_results_sentiment).join(pred_results_touchpoint)
|
77 |
|
78 |
final_results.drop(columns=['sequence_clean'], inplace=True)
|
79 |
|