|
from transformers import pipeline |
|
import torch |
|
import gradio as gr |
|
import pandas as pd |
|
import matplotlib.pyplot as plt |
|
|
|
|
|
def sentiment_analysis(text): |
|
analyzer = pipeline("text-classification", model="mrm8488/distilroberta-finetuned-financial-news-sentiment-analysis") |
|
|
|
sentiment = analyzer(text) |
|
return [sentiment[0]['label'], sentiment[0]['score']] |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def create_charts(df): |
|
|
|
if not all(col in df.columns for col in ['Review', 'Sentiment', 'Sentiment Score']): |
|
raise ValueError("The DataFrame must contain 'Review', 'Sentiment', and 'Sentiment Score' columns.") |
|
|
|
sentiment_counts = df['Sentiment'].value_counts() |
|
fig1, ax1 = plt.subplots(figsize=(8, 6)) |
|
ax1.pie(sentiment_counts, labels=sentiment_counts.index, autopct='%1.1f%%', colors=['skyblue', 'lightcoral', 'lightgreen']) |
|
ax1.set_title('Distribution of Positive and Negative Reviews') |
|
|
|
|
|
fig2, ax2 = plt.subplots(figsize=(10, 6)) |
|
for sentiment, color in zip(['positive', 'negative'], ['green', 'red']): |
|
subset = df[df['Sentiment'].str.lower() == sentiment] |
|
ax2.scatter(subset.index, subset['Sentiment Score'], label=sentiment.capitalize(), color=color, alpha=0.6) |
|
|
|
ax2.axhline(0, color='gray', linewidth=0.5) |
|
ax2.set_xlabel('Review Index') |
|
ax2.set_ylabel('Sentiment Score') |
|
ax2.set_title('Scatter Plot of Reviews by Sentiment Score') |
|
ax2.legend() |
|
|
|
return fig1, fig2 |
|
|
|
|
|
|
|
def analyze_reviews(file_path): |
|
|
|
df = pd.read_excel(file_path) |
|
|
|
|
|
if 'Review' not in df.columns: |
|
for col in df.columns: |
|
if df[col].dtype == 'object': |
|
df.rename(columns={col: 'Review'}, inplace=True) |
|
break |
|
|
|
|
|
if 'Review' not in df.columns: |
|
raise ValueError("The input file must contain a column with review text.") |
|
|
|
|
|
df = df[[col for col in df.columns if not pd.api.types.is_numeric_dtype(df[col]) or col == 'Review']] |
|
|
|
|
|
|
|
results = df['Review'].apply(sentiment_analysis) |
|
|
|
|
|
[df['Sentiment'], df['Sentiment Score']] = zip(*results) |
|
|
|
|
|
df.loc[df['Sentiment'].str.lower() == 'negative' , 'Sentiment Score'] *= -1 |
|
|
|
pie_chart, scatter_plot = create_charts(df) |
|
|
|
return [df, pie_chart, scatter_plot] |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
gr.close_all() |
|
|
|
|
|
demo = gr.Interface( |
|
fn=analyze_reviews, |
|
inputs=[gr.File(label="Upload your excel file containing user reviews")], |
|
outputs=[ |
|
gr.DataFrame(label="Analysis of the uploaded excel file"), |
|
gr.Plot(label="Sentiment Analysis - Positive & Negative"), |
|
gr.Plot(label="Sentiment Analysis - Sentiment Score Distribution") |
|
], |
|
title="GenAI Sentiment Analyzer", |
|
description="This App does sentiment analysis of User Reviews") |
|
demo.launch() |
|
|
|
|
|
|