Darazfinal / app.py
Haseeb005's picture
Update app.py
a785b96 verified
Raw History Blame Contribute Delete
2.03 kB
import gradio as gr
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
# Function to classify review based on keywords
def classify_review(review):
review_lower = review.lower()
positive_keywords = ["good", "excellent", "great", "amazing", "wonderful","beautifull","very beautifull", "fantastic"]
negative_keywords = ["bad", "terrible", "horrible", "awful", "not good","poor", "disappointed","disgusting","unacceptable","worst"]
if any(word in review_lower for word in positive_keywords):
return "positive"
elif any(word in review_lower for word in negative_keywords):
return "negative"
else:
return "neutral"
# Function to process reviews from the uploaded CSV file
def process_reviews(file):
# Read the CSV file
df = pd.read_csv(file.name)
# Check if 'Reviews' column exists
if 'Reviews' not in df.columns:
return "Error: The uploaded file must contain a 'Reviews' column."
# Apply the classification function to the 'Reviews' column
df['Predicted'] = df['Reviews'].apply(classify_review)
# Create a bar chart for sentiment counts
sentiment_counts = df['Predicted'].value_counts()
plt.figure(figsize=(8, 5))
sns.barplot(x=sentiment_counts.index, y=sentiment_counts.values, palette='viridis')
plt.title('Sentiment Analysis of Reviews')
plt.xlabel('Sentiment')
plt.ylabel('Number of Reviews')
plt.xticks(rotation=45)
plt.tight_layout()
plt.savefig('sentiment_bar_chart.png')
plt.close()
# Return the DataFrame with the new 'Predicted' column and the chart
return df, 'sentiment_bar_chart.png'
# Create Gradio interface
iface = gr.Interface(
fn=process_reviews,
inputs="file",
outputs=["dataframe", "image"], # Display the DataFrame and the chart
title="Review Sentiment Classifier",
description="Upload a CSV file containing a 'Reviews' column to classify sentiments as positive, negative, or neutral."
)
iface.launch()