Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,118 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, AutoModelForSequenceClassification, TextClassificationPipeline
|
| 2 |
+
import torch
|
| 3 |
+
import gradio as gr
|
| 4 |
+
from openpyxl import load_workbook
|
| 5 |
+
from numpy import mean
|
| 6 |
+
|
| 7 |
+
# Load tokenizers and models
|
| 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 |
+
new_model = AutoModelForSequenceClassification.from_pretrained('roberta-rating')
|
| 16 |
+
new_tokenizer = AutoTokenizer.from_pretrained('roberta-rating')
|
| 17 |
+
|
| 18 |
+
classifier = TextClassificationPipeline(model=new_model, tokenizer=new_tokenizer, device=device)
|
| 19 |
+
|
| 20 |
+
label_mapping = {1: '1/5', 2: '2/5', 3: '3/5', 4: '4/5', 5: '5/5'}
|
| 21 |
+
|
| 22 |
+
# Function to parse Excel file
|
| 23 |
+
def parse_xl(file_path):
|
| 24 |
+
cells = []
|
| 25 |
+
|
| 26 |
+
workbook = load_workbook(filename=file_path)
|
| 27 |
+
for sheet in workbook.worksheets:
|
| 28 |
+
for row in sheet.iter_rows():
|
| 29 |
+
for cell in row:
|
| 30 |
+
if cell.value != None:
|
| 31 |
+
cells.append(cell.value)
|
| 32 |
+
|
| 33 |
+
return cells
|
| 34 |
+
|
| 35 |
+
# Function to evaluate reviews from Excel file
|
| 36 |
+
def evaluate(file):
|
| 37 |
+
reviews = parse_xl(file)
|
| 38 |
+
ratings = []
|
| 39 |
+
text = ""
|
| 40 |
+
sentiments = []
|
| 41 |
+
|
| 42 |
+
for review in reviews:
|
| 43 |
+
rating = int(classifier(review)[0]['label'].split('_')[1])
|
| 44 |
+
ratings.append(rating)
|
| 45 |
+
text += review
|
| 46 |
+
text += " "
|
| 47 |
+
|
| 48 |
+
sentiment = classifier(review)[0]['label']
|
| 49 |
+
sentiment_label = "Positive" if sentiment == "LABEL_4" or sentiment == "LABEL_5" else "Negative" if sentiment == "LABEL_1" or sentiment == "LABEL_2" else "Neutral"
|
| 50 |
+
sentiments.append(sentiment_label)
|
| 51 |
+
|
| 52 |
+
overall_sentiment = "Positive" if sentiments.count("Positive") > sentiments.count("Negative") else "Negative" if sentiments.count("Negative") > sentiments.count("Positive") else "Neutral"
|
| 53 |
+
|
| 54 |
+
inputs = tokenizer([text], max_length=1024, truncation=True, return_tensors="pt")
|
| 55 |
+
summary_ids = model.generate(inputs["input_ids"], num_beams=2, min_length=10, max_length=50)
|
| 56 |
+
summary = tokenizer.batch_decode(summary_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False)[0]
|
| 57 |
+
|
| 58 |
+
# Modify the summary to third person
|
| 59 |
+
summary = summary.replace("I", "He/She").replace("my", "his/her").replace("me", "him/her")
|
| 60 |
+
|
| 61 |
+
inputs_keywords = tokenizer_keywords([text], max_length=1024, truncation=True, return_tensors="pt")
|
| 62 |
+
summary_ids_keywords = model_keywords.generate(inputs_keywords["input_ids"], num_beams=2, min_length=0, max_length=100)
|
| 63 |
+
keywords = tokenizer_keywords.batch_decode(summary_ids_keywords, skip_special_tokens=True, clean_up_tokenization_spaces=False)[0]
|
| 64 |
+
|
| 65 |
+
return round(mean(ratings), 2), summary, keywords, overall_sentiment
|
| 66 |
+
|
| 67 |
+
# Function to test a single text input
|
| 68 |
+
def test_area(text):
|
| 69 |
+
inputs = tokenizer([text], max_length=1024, truncation=True, return_tensors="pt")
|
| 70 |
+
summary_ids = model.generate(inputs["input_ids"], num_beams=2, min_length=10, max_length=50)
|
| 71 |
+
summary = tokenizer.batch_decode(summary_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False)[0]
|
| 72 |
+
|
| 73 |
+
# Modify the summary to third person
|
| 74 |
+
summary = summary.replace("I", "He/She").replace("my", "his/her").replace("me", "him/her")
|
| 75 |
+
|
| 76 |
+
inputs_keywords = tokenizer_keywords([text], max_length=1024, truncation=True, return_tensors="pt")
|
| 77 |
+
summary_ids_keywords = model_keywords.generate(inputs_keywords["input_ids"], num_beams=2, min_length=0, max_length=100)
|
| 78 |
+
keywords = tokenizer_keywords.batch_decode(summary_ids_keywords, skip_special_tokens=True, clean_up_tokenization_spaces=False)[0]
|
| 79 |
+
|
| 80 |
+
sentiment = classifier(text)[0]['label']
|
| 81 |
+
sentiment_label = "Positive" if sentiment == "LABEL_4" or sentiment == "LABEL_5" else "Negative" if sentiment == "LABEL_1" or sentiment == "LABEL_2" else "Neutral"
|
| 82 |
+
|
| 83 |
+
rating = int(classifier(text)[0]['label'].split('_')[1])
|
| 84 |
+
|
| 85 |
+
return rating, summary, keywords, sentiment_label
|
| 86 |
+
|
| 87 |
+
# Main interface
|
| 88 |
+
main_interface = gr.Interface(
|
| 89 |
+
fn=evaluate,
|
| 90 |
+
inputs=gr.File(label="Reviews"),
|
| 91 |
+
outputs=[gr.Textbox(label="Rating"), gr.Textbox(label="Summary"), gr.Textbox(label="Keywords"), gr.Textbox(label="Overall Sentiment")],
|
| 92 |
+
title='Summarize Reviews',
|
| 93 |
+
description="Evaluate and summarize collection of reviews. Reviews are submitted as an Excel file, where each review is in its own cell."
|
| 94 |
+
)
|
| 95 |
+
|
| 96 |
+
# Testing area interface
|
| 97 |
+
testing_interface = gr.Interface(
|
| 98 |
+
fn=test_area,
|
| 99 |
+
inputs=gr.Textbox(label="Input Text"),
|
| 100 |
+
outputs=[gr.Textbox(label="Rating"), gr.Textbox(label="Summary"), gr.Textbox(label="Keywords"), gr.Textbox(label="Sentiment")],
|
| 101 |
+
title='Testing Area',
|
| 102 |
+
description="Test the summarization, keyword extraction, sentiment analysis, and rating on custom text input."
|
| 103 |
+
)
|
| 104 |
+
|
| 105 |
+
# Combine interfaces into a tabbed interface with a sidebar
|
| 106 |
+
with gr.Blocks() as demo:
|
| 107 |
+
with gr.Row():
|
| 108 |
+
with gr.Column(scale=1):
|
| 109 |
+
gr.Markdown("## Sidebar")
|
| 110 |
+
gr.Button("Button 1")
|
| 111 |
+
gr.Button("Button 2")
|
| 112 |
+
with gr.Column(scale=4):
|
| 113 |
+
iface = gr.TabbedInterface(
|
| 114 |
+
[main_interface, testing_interface],
|
| 115 |
+
["Summarize Reviews", "Testing Area"]
|
| 116 |
+
)
|
| 117 |
+
|
| 118 |
+
demo.launch(share=True)
|