Sasidhar commited on
Commit
f888248
1 Parent(s): 959cfc9

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +137 -0
app.py ADDED
@@ -0,0 +1,137 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+ # -*- coding: utf-8 -*-
3
+ """
4
+ Created on Sat Sep 17 22:46:12 2022
5
+ @author: conny
6
+ """
7
+
8
+ import os
9
+ os.environ['KMP_DUPLICATE_LIB_OK']='True'
10
+
11
+ import plotly.express as px
12
+
13
+ import streamlit as st
14
+ from streamlit_option_menu import option_menu
15
+
16
+ st. set_page_config(layout="wide")
17
+
18
+ from transformers import pipeline
19
+
20
+ import pandas as pd
21
+
22
+ @st.cache(allow_output_mutation = True)
23
+ def init_text_summarization_model():
24
+ MODEL = 'facebook/bart-large-cnn'
25
+ pipe = pipeline("summarization", model=MODEL)
26
+ return pipe
27
+
28
+ @st.cache(allow_output_mutation = True)
29
+ def init_zsl_topic_classification():
30
+ MODEL = 'facebook/bart-large-mnli'
31
+ pipe = pipeline("zero-shot-classification", model=MODEL)
32
+ template = "This text is about {}."
33
+ return pipe, template
34
+
35
+ # Model initialization
36
+ pipeline_summarization = init_text_summarization_model()
37
+ pipeline_zsl, template = init_zsl_topic_classification()
38
+
39
+ st.header('Customer Review Analysis')
40
+
41
+ # Review text box
42
+ default_review = \
43
+ """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.
44
+ 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.
45
+ 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."""
46
+ review = st.text_area("Paste/write a review here..", value=default_review, height=250)
47
+
48
+ tabs = option_menu(menu_title=None,
49
+ options=[
50
+ "Text Summarization",
51
+ "Zero-Shot-Learning",
52
+ ],
53
+ default_index=0,
54
+ orientation='horizontal'
55
+ )
56
+
57
+ ### Text Summarization
58
+ if tabs == 'Text Summarization':
59
+ button = st.button('Summarize review')
60
+
61
+ if button:
62
+ # Text summarization inference
63
+ with st.spinner("Summarizing review..."):
64
+ summary_text = pipeline_summarization(review, max_length=130, min_length=30, do_sample=False)
65
+
66
+ # Show output
67
+ st.write(summary_text[0]['summary_text'])
68
+
69
+ ### Zero-Shot-Learning
70
+ elif tabs == 'Zero-Shot-Learning':
71
+ col_product, col_topic = st.columns(2)
72
+
73
+ # Set product classes
74
+ products = col_product.multiselect(
75
+ label='Available Products and Services:',
76
+ options=[
77
+ 'Bank Account',
78
+ 'Credit Card',
79
+ 'Home Loan',
80
+ 'Insurance',
81
+ ],
82
+ default=[
83
+ 'Bank Account',
84
+ 'Credit Card',
85
+ 'Home Loan',
86
+ 'Insurance',
87
+ ]
88
+ )
89
+ product_is_multi_label = col_product.checkbox("Can have more than one classes", value=True)
90
+
91
+ # Set topic classes
92
+ topics = col_topic.multiselect(
93
+ label="Possible Review Topics:",
94
+ options=[
95
+ "Excellent Customer Service",
96
+ "Great Product Feature",
97
+ "Poor Service",
98
+ "Unclear Procedure",
99
+ "Other"
100
+ ],
101
+ default=[
102
+ "Excellent Customer Service",
103
+ "Great Product Feature",
104
+ "Poor Service",
105
+ "Unclear Procedure",
106
+ ]
107
+ )
108
+ topic_is_multi_label = col_topic.checkbox("Can have more than one classes", value=False)
109
+
110
+ button = st.button('Classify')
111
+
112
+ if button:
113
+ # ZSL inference
114
+ with st.spinner("Identifying product/service and classifying review..."):
115
+ product_classification_output = pipeline_zsl(review, products, hypothesis_template=template, multi_label=product_is_multi_label)
116
+ topic_classification_output = pipeline_zsl(review, topics, hypothesis_template=template, multi_label=topic_is_multi_label)
117
+
118
+ # Show output
119
+ col_output_product, col_output_topic = st.columns(2)
120
+
121
+ data = {
122
+ 'Product': product_classification_output['labels'],
123
+ 'Scores': product_classification_output['scores']
124
+ }
125
+ df = pd.DataFrame(data)
126
+ df = df.sort_values(by='Scores', ascending=True)
127
+ fig = px.bar(df, x='Scores', y='Product', orientation='h')
128
+ col_output_product.plotly_chart(fig, use_container_width=True)
129
+
130
+ data = {
131
+ 'Topic': topic_classification_output['labels'],
132
+ 'Scores': topic_classification_output['scores']
133
+ }
134
+ df = pd.DataFrame(data)
135
+ df = df.sort_values(by='Scores', ascending=True)
136
+ fig = px.bar(df, x='Scores', y='Topic', orientation='h')
137
+ col_output_topic.plotly_chart(fig, use_container_width=True)