Spaces:
Sleeping
Sleeping
Muhammad Anas Akhtar
commited on
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import pandas as pd
|
| 4 |
+
import matplotlib.pyplot as plt
|
| 5 |
+
from transformers import pipeline
|
| 6 |
+
|
| 7 |
+
analyzer = pipeline("text-classification",
|
| 8 |
+
model="distilbert/distilbert-base-uncased-finetuned-sst-2-english")
|
| 9 |
+
|
| 10 |
+
def sentiment_analyzer(review):
|
| 11 |
+
# Check if the review is a valid string
|
| 12 |
+
if pd.isna(review) or not isinstance(review, str):
|
| 13 |
+
return "NEUTRAL" # Return neutral for invalid inputs
|
| 14 |
+
try:
|
| 15 |
+
sentiment = analyzer(review)
|
| 16 |
+
return sentiment[0]['label']
|
| 17 |
+
except:
|
| 18 |
+
return "NEUTRAL" # Return neutral for any errors
|
| 19 |
+
|
| 20 |
+
def sentiment_bar_chart(df):
|
| 21 |
+
sentiment_counts = df['Sentiment'].value_counts()
|
| 22 |
+
|
| 23 |
+
# Create a bar chart
|
| 24 |
+
plt.figure(figsize=(10, 6))
|
| 25 |
+
plt.pie(sentiment_counts.values, labels=sentiment_counts.index, autopct='%1.1f%%',
|
| 26 |
+
colors=['lightgreen', 'lightcoral', 'lightgray'])
|
| 27 |
+
plt.title('Review Sentiment Distribution')
|
| 28 |
+
return plt.gcf()
|
| 29 |
+
|
| 30 |
+
def read_reviews_and_analyze_sentiment(file_object):
|
| 31 |
+
try:
|
| 32 |
+
# Load the Excel file into a DataFrame
|
| 33 |
+
df = pd.read_excel(file_object)
|
| 34 |
+
|
| 35 |
+
# Check if 'Review' column is in the DataFrame
|
| 36 |
+
if 'Review' not in df.columns:
|
| 37 |
+
raise ValueError("Excel file must contain a 'Review' column.")
|
| 38 |
+
|
| 39 |
+
# Convert Review column to string type and handle NaN values
|
| 40 |
+
df['Review'] = df['Review'].astype(str)
|
| 41 |
+
|
| 42 |
+
# Apply the sentiment_analyzer function to each review
|
| 43 |
+
df['Sentiment'] = df['Review'].apply(sentiment_analyzer)
|
| 44 |
+
|
| 45 |
+
# Create the chart
|
| 46 |
+
chart_object = sentiment_bar_chart(df)
|
| 47 |
+
|
| 48 |
+
return df, chart_object
|
| 49 |
+
except Exception as e:
|
| 50 |
+
raise gr.Error(f"Error processing file: {str(e)}")
|
| 51 |
+
|
| 52 |
+
# Create the Gradio interface
|
| 53 |
+
demo = gr.Interface(
|
| 54 |
+
fn=read_reviews_and_analyze_sentiment,
|
| 55 |
+
inputs=[gr.File(file_types=["xlsx"], label="Upload your review comment file")],
|
| 56 |
+
outputs=[
|
| 57 |
+
gr.Dataframe(label="Sentiments"),
|
| 58 |
+
gr.Plot(label="Sentiment Analysis")
|
| 59 |
+
],
|
| 60 |
+
title="@GenAILearniverse Project 3: Sentiment Analyzer",
|
| 61 |
+
description="THIS APPLICATION WILL BE USED TO ANALYZE THE SENTIMENT BASED ON FILE UPLOADED."
|
| 62 |
+
)
|
| 63 |
+
|
| 64 |
+
if __name__ == "__main__":
|
| 65 |
+
demo.launch()
|