hongaik commited on
Commit
8ad74f7
1 Parent(s): 989daab

add charts

Browse files
.ipynb_checkpoints/Untitled-checkpoint.ipynb ADDED
The diff for this file is too large to render. See raw diff
 
.ipynb_checkpoints/app-checkpoint.py CHANGED
@@ -1,4 +1,5 @@
1
  import streamlit as st
 
2
  from utils import *
3
 
4
  ########## Title for the Web App ##########
@@ -10,9 +11,24 @@ feedback = st.text_input('Type your text here', 'The website was user friendly a
10
  if st.button('Click for predictions!'):
11
  with st.spinner('Generating predictions...'):
12
 
13
- result = get_single_prediction(feedback)
14
-
15
- st.success(f'Your text has been predicted to fall under the following topics: {result[:-1]}. The sentiment of this text is {result[-1]}.')
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16
 
17
  st.write("\n")
18
  st.subheader('Or... Upload a csv file if you have a file instead.')
 
1
  import streamlit as st
2
+ import plotly.express as px
3
  from utils import *
4
 
5
  ########## Title for the Web App ##########
 
11
  if st.button('Click for predictions!'):
12
  with st.spinner('Generating predictions...'):
13
 
14
+ topics_prob, sentiment_prob = get_single_prediction(feedback)
15
+
16
+ bar = px.bar(topics_prob, x='probability', y='topic')
17
+ st.plotly_chart(bar, use_container_width=True)
18
+
19
+ pie = px.pie(sentiment_prob,
20
+ values='probability',
21
+ names='sentiment',
22
+ title='Sentiment Probability',
23
+ color_discrete_map={'positive':'rgb(0, 204, 0)',
24
+ 'negative':'rgb(215, 11, 11)'
25
+ },
26
+ color='sentiment'
27
+ )
28
+ st.plotly_chart(pie, use_container_width=True)
29
+
30
+
31
+ #st.success(f'Your text has been predicted to fall under the following topics: {result[:-1]}. The sentiment of this text is {result[-1]}.')
32
 
33
  st.write("\n")
34
  st.subheader('Or... Upload a csv file if you have a file instead.')
.ipynb_checkpoints/requirements-checkpoint.txt CHANGED
@@ -4,4 +4,5 @@ transformers==4.16.1
4
  scikit-learn
5
  pandas==1.2.4
6
  torch==1.10.1
7
- numpy==1.19.5
 
 
4
  scikit-learn
5
  pandas==1.2.4
6
  torch==1.10.1
7
+ numpy==1.19.5
8
+ plotly==5.1.0
.ipynb_checkpoints/utils-checkpoint.py CHANGED
@@ -39,23 +39,16 @@ def get_single_prediction(text):
39
  text_vectors = np.mean([w2v[i] for i in text.split()], axis=0)
40
 
41
  # Make predictions
42
- results = model.predict(text_vectors.reshape(1,300)).squeeze()
43
- print(results)
44
 
45
  # Get sentiment
46
- sentiment = get_sentiment_label_facebook(classifier(text,
47
- candidate_labels=['positive', 'negative'],
48
- hypothesis_template='The sentiment of this is {}'))
 
49
 
50
- # Consolidate results
51
- pred_labels = [labels[idx] for idx, tag in enumerate(results) if tag == 1]
52
-
53
- if len(pred_labels) == 0:
54
- pred_labels.append('others')
55
-
56
- pred_labels.append(sentiment)
57
-
58
- return pred_labels
59
 
60
  def get_multiple_predictions(csv):
61
 
 
39
  text_vectors = np.mean([w2v[i] for i in text.split()], axis=0)
40
 
41
  # Make predictions
42
+ results = model.predict_proba(text_vectors.reshape(1,300)).squeeze().round(2)
43
+ pred_prob = pd.DataFrame({'topic': labels, 'probability': results}).sort_values('probability', ascending=True)
44
 
45
  # Get sentiment
46
+ sentiment_results = classifier(text,
47
+ candidate_labels=['positive', 'negative'],
48
+ hypothesis_template='The sentiment of this is {}')
49
+ sentiment_prob = pd.DataFrame({'sentiment': sentiment_results['labels'], 'probability': sentiment_results['scores']})
50
 
51
+ return (pred_prob, sentiment_prob)
 
 
 
 
 
 
 
 
52
 
53
  def get_multiple_predictions(csv):
54
 
Untitled.ipynb ADDED
The diff for this file is too large to render. See raw diff
 
app.py CHANGED
@@ -1,4 +1,5 @@
1
  import streamlit as st
 
2
  from utils import *
3
 
4
  ########## Title for the Web App ##########
@@ -10,9 +11,24 @@ feedback = st.text_input('Type your text here', 'The website was user friendly a
10
  if st.button('Click for predictions!'):
11
  with st.spinner('Generating predictions...'):
12
 
13
- result = get_single_prediction(feedback)
14
-
15
- st.success(f'Your text has been predicted to fall under the following topics: {result[:-1]}. The sentiment of this text is {result[-1]}.')
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16
 
17
  st.write("\n")
18
  st.subheader('Or... Upload a csv file if you have a file instead.')
 
1
  import streamlit as st
2
+ import plotly.express as px
3
  from utils import *
4
 
5
  ########## Title for the Web App ##########
 
11
  if st.button('Click for predictions!'):
12
  with st.spinner('Generating predictions...'):
13
 
14
+ topics_prob, sentiment_prob = get_single_prediction(feedback)
15
+
16
+ bar = px.bar(topics_prob, x='probability', y='topic')
17
+ st.plotly_chart(bar, use_container_width=True)
18
+
19
+ pie = px.pie(sentiment_prob,
20
+ values='probability',
21
+ names='sentiment',
22
+ title='Sentiment Probability',
23
+ color_discrete_map={'positive':'rgb(0, 204, 0)',
24
+ 'negative':'rgb(215, 11, 11)'
25
+ },
26
+ color='sentiment'
27
+ )
28
+ st.plotly_chart(pie, use_container_width=True)
29
+
30
+
31
+ #st.success(f'Your text has been predicted to fall under the following topics: {result[:-1]}. The sentiment of this text is {result[-1]}.')
32
 
33
  st.write("\n")
34
  st.subheader('Or... Upload a csv file if you have a file instead.')
requirements.txt CHANGED
@@ -4,4 +4,5 @@ transformers==4.16.1
4
  scikit-learn
5
  pandas==1.2.4
6
  torch==1.10.1
7
- numpy==1.19.5
 
 
4
  scikit-learn
5
  pandas==1.2.4
6
  torch==1.10.1
7
+ numpy==1.19.5
8
+ plotly==5.1.0
utils.py CHANGED
@@ -39,23 +39,16 @@ def get_single_prediction(text):
39
  text_vectors = np.mean([w2v[i] for i in text.split()], axis=0)
40
 
41
  # Make predictions
42
- results = model.predict(text_vectors.reshape(1,300)).squeeze()
43
- print(results)
44
 
45
  # Get sentiment
46
- sentiment = get_sentiment_label_facebook(classifier(text,
47
- candidate_labels=['positive', 'negative'],
48
- hypothesis_template='The sentiment of this is {}'))
 
49
 
50
- # Consolidate results
51
- pred_labels = [labels[idx] for idx, tag in enumerate(results) if tag == 1]
52
-
53
- if len(pred_labels) == 0:
54
- pred_labels.append('others')
55
-
56
- pred_labels.append(sentiment)
57
-
58
- return pred_labels
59
 
60
  def get_multiple_predictions(csv):
61
 
 
39
  text_vectors = np.mean([w2v[i] for i in text.split()], axis=0)
40
 
41
  # Make predictions
42
+ results = model.predict_proba(text_vectors.reshape(1,300)).squeeze().round(2)
43
+ pred_prob = pd.DataFrame({'topic': labels, 'probability': results}).sort_values('probability', ascending=True)
44
 
45
  # Get sentiment
46
+ sentiment_results = classifier(text,
47
+ candidate_labels=['positive', 'negative'],
48
+ hypothesis_template='The sentiment of this is {}')
49
+ sentiment_prob = pd.DataFrame({'sentiment': sentiment_results['labels'], 'probability': sentiment_results['scores']})
50
 
51
+ return (pred_prob, sentiment_prob)
 
 
 
 
 
 
 
 
52
 
53
  def get_multiple_predictions(csv):
54