cgunadi commited on
Commit
c7324e3
·
1 Parent(s): 5bbbde9

update app.py

Browse files
Files changed (1) hide show
  1. app.py +74 -40
app.py CHANGED
@@ -14,53 +14,37 @@ import plotly.express as px
14
  import streamlit as st
15
  from streamlit_option_menu import option_menu
16
 
17
- from transformers import AutoModelForSequenceClassification
18
- from transformers import AutoTokenizer
19
- from transformers import pipeline
20
 
 
21
 
22
- import torch
23
  import pandas as pd
24
 
25
- @st.cache(allow_output_mutation = True)
26
- def init_sentiment_model():
27
- MODEL = 'cardiffnlp/twitter-roberta-base-sentiment'
28
- tokenizer = AutoTokenizer.from_pretrained(MODEL)
29
- model = AutoModelForSequenceClassification.from_pretrained(MODEL)
30
- return tokenizer, model
31
-
32
  @st.cache(allow_output_mutation = True)
33
  def init_text_summarization_model():
34
  MODEL = 'facebook/bart-large-cnn'
35
  pipe = pipeline("summarization", model=MODEL)
36
  return pipe
37
 
38
- @st.cache(allow_output_mutation = True)
39
- def init_topic_classification():
40
- MODEL = 'deepset/roberta-base-squad2'
41
- pipe = pipeline('question-answering', model=MODEL, tokenizer=MODEL)
42
- return pipe
43
-
44
  @st.cache(allow_output_mutation = True)
45
  def init_zsl_topic_classification():
46
- # MODEL = 'typeform/distilbert-base-uncased-mnli'
47
  MODEL = 'facebook/bart-large-mnli'
48
  pipe = pipeline("zero-shot-classification", model=MODEL)
49
  template = "This text is about {}."
50
  return pipe, template
51
 
52
  # Model initialization
53
- #tokenizer_sentiment, model_sentiment = init_sentiment_model()
54
  pipeline_summarization = init_text_summarization_model()
55
- #pipeline_topic = init_topic_classification()
56
- pipeline_topic, template = init_zsl_topic_classification()
57
 
58
  st.header('Customer Review Analysis')
 
 
59
  default_review = \
60
  """I attempted to attend the Bank Alpha Eastbank branch last Friday to open a Kids Savings Account for my 3 year old son. I was informed by the Bank Alpha staffer that I was "too close to closing time" and that "I'd have to come back another time". It was about 20 minutes prior to close and there was no one else in the branch except the staff. I did say that I had my son's birth certificate and a Medicare card to establish his identity. Still, no dice. Come back later. No worries, can do.
61
  I returned today (Monday) to the same branch at 1130, with more than enough time for the account opening to occur. I confirmed with another Bank Alpha staffer that I had my son's birth certificate and Medicare card. However, he went out the back "just to check something". Upon coming back, I was informed that they would not be able to open the account for me today as they required my son, a 3 year old, to be present. The staffer on Friday failed to mention this to me. Equally, the Bank Alpha website for the Kids Savings Account does not list the physical presence of the child as a requirement for opening said account.
62
  I have never come across a bank so committed to not providing services to prospective customers as Bank Alpha. As a result, my son won't be banking with Bank Alpha, and I probably won't be recommending the use of Bank Alpha to any family or friends either."""
63
- review = st.text_area("Paste/write a review here..", value=default_review, height=450)
64
 
65
  tabs = option_menu(menu_title=None,
66
  options=[
@@ -71,36 +55,86 @@ tabs = option_menu(menu_title=None,
71
  orientation='horizontal'
72
  )
73
 
 
74
  if tabs == 'Text Summarization':
75
  button = st.button('Summarize review')
76
 
77
  if button:
78
- summary_text = pipeline_summarization(review, max_length=130, min_length=30, do_sample=False)
 
 
 
 
79
  st.write(summary_text[0]['summary_text'])
80
 
 
81
  elif tabs == 'Zero-Shot-Learning':
82
- topics = st.multiselect("",["Excellent Customer Service", "Great Product Feature", "Poor Service","Unclear Procedure"],
83
- ["Excellent Customer Service", "Great Product Feature", "Poor Service","Unclear Procedure"])
84
- additional_topics = st.text_input("Enter comma separated reasons.")
85
- if additional_topics:
86
- topics = topics + additional_topics.split(",")
87
 
88
- is_multi_label = st.checkbox("Can have more than one classes", value=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
89
 
90
  button = st.button('Classify')
91
 
92
  if button:
93
- classification_output = pipeline_topic(review, topics, hypothesis_template=template, multi_label=is_multi_label)
94
- data = {'Class': classification_output['labels'], 'Scores': classification_output['scores']}
 
 
 
 
 
 
 
 
 
 
95
  df = pd.DataFrame(data)
96
- df = df.sort_values(by='Scores', ascending=False)
97
- fig = px.bar(df, x='Scores', y='Class', orientation='h', width=400, height=300)
98
- fig.update_layout(
99
- yaxis=dict(
100
- autorange='reversed'
101
- )
102
- )
103
- st.write(fig)
104
-
 
 
 
105
 
106
 
 
14
  import streamlit as st
15
  from streamlit_option_menu import option_menu
16
 
17
+ st. set_page_config(layout="wide")
 
 
18
 
19
+ from transformers import pipeline
20
 
 
21
  import pandas as pd
22
 
 
 
 
 
 
 
 
23
  @st.cache(allow_output_mutation = True)
24
  def init_text_summarization_model():
25
  MODEL = 'facebook/bart-large-cnn'
26
  pipe = pipeline("summarization", model=MODEL)
27
  return pipe
28
 
 
 
 
 
 
 
29
  @st.cache(allow_output_mutation = True)
30
  def init_zsl_topic_classification():
 
31
  MODEL = 'facebook/bart-large-mnli'
32
  pipe = pipeline("zero-shot-classification", model=MODEL)
33
  template = "This text is about {}."
34
  return pipe, template
35
 
36
  # Model initialization
 
37
  pipeline_summarization = init_text_summarization_model()
38
+ pipeline_zsl, template = init_zsl_topic_classification()
 
39
 
40
  st.header('Customer Review Analysis')
41
+
42
+ # Review text box
43
  default_review = \
44
  """I attempted to attend the Bank Alpha Eastbank branch last Friday to open a Kids Savings Account for my 3 year old son. I was informed by the Bank Alpha staffer that I was "too close to closing time" and that "I'd have to come back another time". It was about 20 minutes prior to close and there was no one else in the branch except the staff. I did say that I had my son's birth certificate and a Medicare card to establish his identity. Still, no dice. Come back later. No worries, can do.
45
  I returned today (Monday) to the same branch at 1130, with more than enough time for the account opening to occur. I confirmed with another Bank Alpha staffer that I had my son's birth certificate and Medicare card. However, he went out the back "just to check something". Upon coming back, I was informed that they would not be able to open the account for me today as they required my son, a 3 year old, to be present. The staffer on Friday failed to mention this to me. Equally, the Bank Alpha website for the Kids Savings Account does not list the physical presence of the child as a requirement for opening said account.
46
  I have never come across a bank so committed to not providing services to prospective customers as Bank Alpha. As a result, my son won't be banking with Bank Alpha, and I probably won't be recommending the use of Bank Alpha to any family or friends either."""
47
+ review = st.text_area("Paste/write a review here..", value=default_review, height=250)
48
 
49
  tabs = option_menu(menu_title=None,
50
  options=[
 
55
  orientation='horizontal'
56
  )
57
 
58
+ ### Text Summarization
59
  if tabs == 'Text Summarization':
60
  button = st.button('Summarize review')
61
 
62
  if button:
63
+ # Text summarization inference
64
+ with st.spinner("Summarizing review..."):
65
+ summary_text = pipeline_summarization(review, max_length=130, min_length=30, do_sample=False)
66
+
67
+ # Show output
68
  st.write(summary_text[0]['summary_text'])
69
 
70
+ ### Zero-Shot-Learning
71
  elif tabs == 'Zero-Shot-Learning':
72
+ col_product, col_topic = st.columns(2)
 
 
 
 
73
 
74
+ # Set product classes
75
+ products = col_product.multiselect(
76
+ label='Available Products and Services:',
77
+ options=[
78
+ 'Bank Account',
79
+ 'Credit Card',
80
+ 'Home Loan',
81
+ 'Insurance',
82
+ ],
83
+ default=[
84
+ 'Bank Account',
85
+ 'Credit Card',
86
+ 'Home Loan',
87
+ 'Insurance',
88
+ ]
89
+ )
90
+ product_is_multi_label = col_product.checkbox("Can have more than one classes", value=True)
91
+
92
+ # Set topic classes
93
+ topics = col_topic.multiselect(
94
+ label="Possible Review Topics:",
95
+ options=[
96
+ "Excellent Customer Service",
97
+ "Great Product Feature",
98
+ "Poor Service",
99
+ "Unclear Procedure",
100
+ "Other"
101
+ ],
102
+ default=[
103
+ "Excellent Customer Service",
104
+ "Great Product Feature",
105
+ "Poor Service",
106
+ "Unclear Procedure",
107
+ ]
108
+ )
109
+ topic_is_multi_label = col_topic.checkbox("Can have more than one classes", value=False)
110
 
111
  button = st.button('Classify')
112
 
113
  if button:
114
+ # ZSL inference
115
+ with st.spinner("Identifying product/service and classifying review..."):
116
+ product_classification_output = pipeline_zsl(review, products, hypothesis_template=template, multi_label=product_is_multi_label)
117
+ topic_classification_output = pipeline_zsl(review, topics, hypothesis_template=template, multi_label=topic_is_multi_label)
118
+
119
+ # Show output
120
+ col_output_product, col_output_topic = st.columns(2)
121
+
122
+ data = {
123
+ 'Product': product_classification_output['labels'],
124
+ 'Scores': product_classification_output['scores']
125
+ }
126
  df = pd.DataFrame(data)
127
+ df = df.sort_values(by='Scores', ascending=True)
128
+ fig = px.bar(df, x='Scores', y='Product', orientation='h')
129
+ col_output_product.plotly_chart(fig, use_container_width=True)
130
+
131
+ data = {
132
+ 'Topic': topic_classification_output['labels'],
133
+ 'Scores': topic_classification_output['scores']
134
+ }
135
+ df = pd.DataFrame(data)
136
+ df = df.sort_values(by='Scores', ascending=True)
137
+ fig = px.bar(df, x='Scores', y='Topic', orientation='h')
138
+ col_output_topic.plotly_chart(fig, use_container_width=True)
139
 
140