Spaces:
Sleeping
Sleeping
File size: 2,167 Bytes
d890152 bd6e7f4 d890152 bd6e7f4 59e1a86 bd6e7f4 d890152 bd6e7f4 b640b85 d890152 bd6e7f4 d890152 b640b85 59e1a86 b640b85 d890152 59e1a86 d890152 bd6e7f4 d890152 8817d81 bd6e7f4 d890152 bd6e7f4 d890152 bd6e7f4 8817d81 d890152 f7fe367 |
1 2 3 4 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 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
import torch
import gradio as gr
import pandas as pd
import matplotlib.pyplot as plt
# Use a pipeline as a high-level helper
from transformers import pipeline
# model_path = ("../Models/models--distilbert--distilbert-base-uncased-finetuned-sst-2-english"
# "/snapshots/714eb0fa89d2f80546fda750413ed43d93601a13")
# analyzer = pipeline("text-classification", model=model_path)
analyzer = pipeline("text-classification",
model="distilbert/distilbert-base-uncased-finetuned-sst-2-english")
# print(analyzer(["This production is good", "This product was quite expensive"]))
def sentiment_analyzer(review):
sentiment = analyzer(review)
return sentiment[0]['label']
def sentiment_pie_chart(df):
sentiment_counts = df['Sentiment'].value_counts()
# Create a pie chart
fig, ax = plt.subplots()
sentiment_counts.plot(kind='pie', autopct='%1.1f%%', colors=['green', 'red'], ax=ax)
# Remove the y-axis label
ax.set_ylabel('')
ax.set_title('Review Sentiment Chart')
return fig
def read_reviews_and_analyze_sentiment(file_object):
# Load the Excel file into a DataFrame
df = pd.read_excel(file_object)
# Check if 'Review' column is in the DataFrame
if 'Reviews' not in df.columns:
raise ValueError("Excel file must contain a 'Reviews' column.")
# Apply the get_sentiment function to each review in the DataFrame
df['Sentiment'] = df['Reviews'].apply(sentiment_analyzer)
df = df[['Sentiment'] + [col for col in df.columns if col != 'Sentiment']]
chart_object = sentiment_pie_chart(df)
return df, chart_object
# result = read_reviews_and_analyze_sentiment("../Files/Prod-review.xlsx")
# print(result)
demo = gr.Interface(fn=read_reviews_and_analyze_sentiment,
inputs=[gr.File(file_types=["xlsx"], label="Upload your reviews file")],
outputs=[gr.Dataframe(label="Sentiments"), gr.Plot(label="Sentiment Analysis")],
title="Sentiment Analyzer",
description="Analyze the sentiment based on reviews (Excel file .xlsx must contain a 'Reviews' column)")
demo.launch()
|