2seriescs's picture
Rename main.py to app.py
2cb8060 verified
raw
history blame
No virus
1.34 kB
import torch
import gradio as gr
import pandas as pd
import matplotlib.pyplot as plt
from transformers import pipeline
analyis = pipeline(
"text-classification",
model="distilbert/distilbert-base-uncased-finetuned-sst-2-english"
)
def sentiment_analysis(review):
sentiment = analyis(review)
return sentiment[0]['label']
def sentiment_bar_chart(df):
sentiment_counts = df['Sentiment'].value_counts()
fig, ax = plt.subplots()
sentiment_counts.plot(kind='pie', ax=ax, autopct='%1.1f%%', color=['green', 'red'])
ax.set_title('Review Sentiment Counts')
ax.set_xlabel('Sentiment')
ax.set_ylabel('Count')
return fig
def read_reviews_and_analyze_sentiment(file_object):
df = pd.read_excel(file_object)
if 'Reviews' not in df.columns:
raise ValueError("Excel file must contain a 'Review' column")
df['Sentiment'] = df['Reviews'].apply(sentiment_analysis)
chart_object = sentiment_bar_chart(df)
return df, chart_object
demo = gr.Interface(
fn=sentiment_analysis,
inputs=[gr.File(file_types=["xlsx"], label="Input users reviews")],
outputs=[gr.DataFrame(label="User sentiment"), gr.Plot(label="Users' Sentiments")],
title="@caesar-2series: Users' Sentiment Analysis",
description="User Sentiment Review Analysis Based on File Uploaded"
)
demo.launch()