Update app.py
Browse files
app.py
CHANGED
|
@@ -1,166 +1,66 @@
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
import random
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
| 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 |
-
|
| 19 |
-
|
| 20 |
-
display: inline-block;
|
| 21 |
-
font-size: 1.5em;
|
| 22 |
-
color: lightgray;
|
| 23 |
-
}
|
| 24 |
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 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 |
-
|
| 41 |
-
|
| 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 |
-
|
| 51 |
-
|
| 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 |
-
|
| 62 |
-
|
| 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 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
stars[i].classList.add('filled');
|
| 83 |
-
}
|
| 84 |
-
});
|
| 85 |
-
</script>
|
| 86 |
-
"""
|
| 87 |
-
return stars_html
|
| 88 |
|
| 89 |
-
|
| 90 |
-
def handle_feedback(feedback, name):
|
| 91 |
-
return f"Thank you for your feedback, {name}!"
|
| 92 |
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
else:
|
| 98 |
-
return "Enter your feedback here..."
|
| 99 |
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
|
|
|
|
|
|
|
|
|
| 105 |
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 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 |
-
|
| 138 |
-
placeholder="Enter your feedback here...",
|
| 139 |
-
max_length=5000)
|
| 140 |
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 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 |
-
|
|
|
|
| 1 |
+
# Load model directly
|
| 2 |
+
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, AutoModelForSequenceClassification, TextClassificationPipeline
|
| 3 |
+
import torch
|
| 4 |
import gradio as gr
|
| 5 |
+
from openpyxl import load_workbook
|
| 6 |
+
from numpy import mean
|
|
|
|
| 7 |
|
| 8 |
+
tokenizer = AutoTokenizer.from_pretrained("suriya7/bart-finetuned-text-summarization")
|
| 9 |
+
model = AutoModelForSeq2SeqLM.from_pretrained("suriya7/bart-finetuned-text-summarization")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
+
tokenizer_keywords = AutoTokenizer.from_pretrained("transformer3/H2-keywordextractor")
|
| 12 |
+
model_keywords = AutoModelForSeq2SeqLM.from_pretrained("transformer3/H2-keywordextractor")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 15 |
+
# Load the fine-tuned model and tokenizer
|
| 16 |
+
new_model = AutoModelForSequenceClassification.from_pretrained('roberta-rating')
|
| 17 |
+
new_tokenizer = AutoTokenizer.from_pretrained('roberta-rating')
|
| 18 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
+
# Create a classification pipeline
|
| 21 |
+
classifier = TextClassificationPipeline(model=new_model, tokenizer=new_tokenizer, device=device)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
+
# Add label mapping for sentiment analysis
|
| 24 |
+
label_mapping = {1: '1/5', 2: '2/5', 3: '3/5', 4: '4/5', 5: '5/5'}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
+
def parse_xl(file_path):
|
| 27 |
+
cells = []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
+
workbook = load_workbook(filename=file_path)
|
| 30 |
+
for sheet in workbook.worksheets:
|
| 31 |
+
for row in sheet.iter_rows():
|
| 32 |
+
for cell in row:
|
| 33 |
+
if cell.value != None:
|
| 34 |
+
cells.append(cell.value)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
+
return cells
|
|
|
|
|
|
|
| 37 |
|
| 38 |
+
def evaluate(file):
|
| 39 |
+
reviews = parse_xl(file)
|
| 40 |
+
ratings = []
|
| 41 |
+
text = ""
|
|
|
|
|
|
|
| 42 |
|
| 43 |
+
for review in reviews:
|
| 44 |
+
ratings.append(int(classifier(review)[0]['label'].split('_')[1]))
|
| 45 |
+
text += review
|
| 46 |
+
text += " "
|
| 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 |
+
inputs_keywords = tokenizer_keywords([text], max_length=1024, truncation=True, return_tensors="pt")
|
| 53 |
+
summary_ids_keywords = model_keywords.generate(inputs_keywords["input_ids"], num_beams=2, min_length=0, max_length=100)
|
| 54 |
+
keywords = tokenizer_keywords.batch_decode(summary_ids_keywords, skip_special_tokens=True, clean_up_tokenization_spaces=False)[0]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
|
| 56 |
+
return round(mean(ratings), 2), summary, keywords
|
|
|
|
|
|
|
| 57 |
|
| 58 |
+
iface = gr.Interface(
|
| 59 |
+
fn=evaluate,
|
| 60 |
+
inputs=gr.File(label="Reviews", file_types=[".xlsx", ".xlsm", ".xltx", ".xltm"]),
|
| 61 |
+
outputs=[gr.Textbox(label="Rating"), gr.Textbox(label="Summary"), gr.Textbox(label="Keywords")],
|
| 62 |
+
title='Summarize Reviews',
|
| 63 |
+
description="Evaluate and summarize collection of reviews. Reviews are submitted as an Excel file, where each reviews is in its own cell."
|
| 64 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
|
| 66 |
+
iface.launch(share=True)
|