Update app.py
Browse files
app.py
CHANGED
@@ -1,66 +1,166 @@
|
|
1 |
-
# Load model directly
|
2 |
-
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, AutoModelForSequenceClassification, TextClassificationPipeline
|
3 |
-
import torch
|
4 |
import gradio as gr
|
5 |
-
|
6 |
-
|
|
|
7 |
|
8 |
-
|
9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
|
11 |
-
|
12 |
-
|
|
|
|
|
|
|
|
|
13 |
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
|
20 |
-
|
21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
|
23 |
-
|
24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
|
26 |
-
|
27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
28 |
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
35 |
|
36 |
-
|
|
|
|
|
37 |
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
|
|
|
|
42 |
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
inputs = tokenizer([text], max_length=1024, truncation=True, return_tensors="pt")
|
49 |
-
summary_ids = model.generate(inputs["input_ids"], num_beams=2, min_length=50, max_length=1000)
|
50 |
-
summary = tokenizer.batch_decode(summary_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False)[0]
|
51 |
|
52 |
-
|
53 |
-
|
54 |
-
|
|
|
|
|
|
|
|
|
55 |
|
56 |
-
|
57 |
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
65 |
|
66 |
-
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
import pandas as pd
|
3 |
+
import numpy as np
|
4 |
+
import random
|
5 |
|
6 |
+
def show_stars():
|
7 |
+
""" Function that shows the HTML script for stars -Janika"""
|
8 |
+
stars_html = """
|
9 |
+
<style>
|
10 |
+
/* Title */
|
11 |
+
.title-text {
|
12 |
+
color: white;
|
13 |
+
font-family: sans-serif;
|
14 |
+
font-size: 14px;
|
15 |
+
font-weight: bold;
|
16 |
+
}
|
17 |
|
18 |
+
/* Empty stars */
|
19 |
+
.star-rating {
|
20 |
+
display: inline-block;
|
21 |
+
font-size: 1.5em;
|
22 |
+
color: lightgray;
|
23 |
+
}
|
24 |
|
25 |
+
/* Filled stars */
|
26 |
+
.star-rating .star.filled {
|
27 |
+
color: gold;
|
28 |
+
}
|
29 |
|
30 |
+
/* Box for the whole star rating component */
|
31 |
+
.rating-box {
|
32 |
+
border: 1px transparent;
|
33 |
+
background-color: #1f2937;
|
34 |
+
padding: 10px;
|
35 |
+
border-radius: 5px;
|
36 |
+
width: 100%;
|
37 |
+
margin-left: 0;
|
38 |
+
}
|
39 |
|
40 |
+
/* Box for the title */
|
41 |
+
.title-box {
|
42 |
+
border: 1px transparent;
|
43 |
+
background-color: #ca8a04;
|
44 |
+
padding: 3px;
|
45 |
+
border-radius: 8px;
|
46 |
+
display: inline-block;
|
47 |
+
margin-bottom: 6px;
|
48 |
+
}
|
49 |
|
50 |
+
/* Box for stars */
|
51 |
+
.star-box {
|
52 |
+
border: 1px transparent;
|
53 |
+
background-color: #374151;
|
54 |
+
padding: 10px;
|
55 |
+
border-radius: 5px;
|
56 |
+
width: fit-content;
|
57 |
+
margin: 0;
|
58 |
+
}
|
59 |
+
</style>
|
60 |
|
61 |
+
<div class="rating-box">
|
62 |
+
<div class="title-box">
|
63 |
+
<p class="title-text">Star rating</p>
|
64 |
+
</div>
|
65 |
+
<div class="star-box">
|
66 |
+
<div class="star-rating" data-rating="4">
|
67 |
+
<span class="star">★</span>
|
68 |
+
<span class="star">★</span>
|
69 |
+
<span class="star">★</span>
|
70 |
+
<span class="star">★</span>
|
71 |
+
<span class="star">★</span>
|
72 |
+
</div>
|
73 |
+
</div>
|
74 |
+
</div>
|
75 |
|
76 |
+
<script>
|
77 |
+
document.addEventListener('DOMContentLoaded', function() {
|
78 |
+
const ratingElement = document.querySelector('.star-rating');
|
79 |
+
const rating = parseInt(ratingElement.getAttribute('data-rating'));
|
80 |
+
const stars = ratingElement.querySelectorAll('.star');
|
81 |
+
for (let i = 0; i < rating; i++) {
|
82 |
+
stars[i].classList.add('filled');
|
83 |
+
}
|
84 |
+
});
|
85 |
+
</script>
|
86 |
+
"""
|
87 |
+
return stars_html
|
88 |
|
89 |
+
# Function to handle feedback submission -Piitu
|
90 |
+
def handle_feedback(feedback, name):
|
91 |
+
return f"Thank you for your feedback, {name}!"
|
92 |
|
93 |
+
# Function to update placeholder text -Piitu
|
94 |
+
def update_placeholder(name):
|
95 |
+
if name:
|
96 |
+
return f"Enter your feedback here, {name}..."
|
97 |
+
else:
|
98 |
+
return "Enter your feedback here..."
|
99 |
|
100 |
+
# Random data for testing, actual data added later
|
101 |
+
df = pd.DataFrame({
|
102 |
+
'Year': np.random.randint(2000, 2024, 25),
|
103 |
+
'Reviews': np.random.randint(120, 320, 25),
|
104 |
+
})
|
|
|
|
|
|
|
105 |
|
106 |
+
# Theme
|
107 |
+
theme = gr.themes.Soft(
|
108 |
+
primary_hue="yellow",
|
109 |
+
secondary_hue="amber",
|
110 |
+
spacing_size="sm",
|
111 |
+
radius_size="lg",
|
112 |
+
)
|
113 |
|
114 |
+
with gr.Blocks(theme=theme) as demo:
|
115 |
|
116 |
+
# Basic user interface for company's view -Janika
|
117 |
+
with gr.Tab("User Interface"):
|
118 |
+
with gr.Row():
|
119 |
+
with gr.Column(scale=2, min_width=300):
|
120 |
+
# Overall summary from all feedback
|
121 |
+
com_summary_output = gr.Textbox(label="Summary")
|
122 |
+
|
123 |
+
with gr.Column(scale=1, min_width=300):
|
124 |
+
# Average star rating across all feedback
|
125 |
+
com_star_rating = gr.HTML(value=show_stars())
|
126 |
+
|
127 |
+
# The most common keywords across all feedback
|
128 |
+
com_keywords_output = gr.Textbox(label="Keywords")
|
129 |
+
|
130 |
+
|
131 |
+
# Testing Area -Piitu
|
132 |
+
with gr.Tab("Testing Area"):
|
133 |
+
with gr.Row():
|
134 |
+
with gr.Column(scale=1, min_width=300):
|
135 |
+
name_input = gr.Textbox(label="Enter your name", placeholder="Enter your name here...")
|
136 |
+
|
137 |
+
text_input = gr.Textbox(label="Please give us feedback!",
|
138 |
+
placeholder="Enter your feedback here...",
|
139 |
+
max_length=5000)
|
140 |
+
|
141 |
+
# Update placeholder based on name input -Piitu
|
142 |
+
name_input.change(fn=update_placeholder, inputs=name_input, outputs=text_input)
|
143 |
+
|
144 |
+
# Send button -Piitu
|
145 |
+
send_button = gr.Button("Send")
|
146 |
+
|
147 |
+
# Output for feedback submission confirmation -Piitu
|
148 |
+
feedback_output = gr.Textbox(label="Submission Result", interactive=False)
|
149 |
+
|
150 |
+
# Link button to function that handles feedback submission -Piitu
|
151 |
+
send_button.click(handle_feedback, inputs=[text_input, name_input], outputs=feedback_output)
|
152 |
+
|
153 |
+
with gr.Column(scale=2, min_width=300):
|
154 |
+
# Estimated star rating from customer's feedback -Janika
|
155 |
+
cus_star_rating = gr.HTML(value=show_stars())
|
156 |
+
|
157 |
+
# Estimated sentiment from customer's feedback -Janika
|
158 |
+
cus_sentiment = gr.Textbox(label="Sentiment")
|
159 |
+
|
160 |
+
# Summary from customer's feedback -Janika
|
161 |
+
cus_summary_output = gr.Textbox(label="Summary")
|
162 |
+
|
163 |
+
# The most common keywords from customer's feedback -Janika
|
164 |
+
cus_keywords_output = gr.Textbox(label="Keywords")
|
165 |
|
166 |
+
demo.launch()
|