Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pandas as pd
|
| 2 |
+
import matplotlib.pyplot as plt
|
| 3 |
+
from transformers import pipeline
|
| 4 |
+
import gradio as gr
|
| 5 |
+
|
| 6 |
+
# Initialize the sentiment analyzer pipeline
|
| 7 |
+
sentiment_Analyzer = pipeline("text-classification", model="distilbert/distilbert-base-uncased-finetuned-sst-2-english")
|
| 8 |
+
|
| 9 |
+
# Function to read reviews from a text file and convert to a pandas DataFrame
|
| 10 |
+
def read_reviews_to_dataframe(file_path):
|
| 11 |
+
reviews = []
|
| 12 |
+
# Open and read the file line by line
|
| 13 |
+
with open(file_path, 'r') as file:
|
| 14 |
+
for line in file:
|
| 15 |
+
reviews.append(line.strip())
|
| 16 |
+
# Convert the list of reviews into a DataFrame
|
| 17 |
+
df = pd.DataFrame(reviews, columns=['Review'])
|
| 18 |
+
return df
|
| 19 |
+
|
| 20 |
+
# Analyzer function to apply sentiment analysis on each review
|
| 21 |
+
def analyzer(text):
|
| 22 |
+
# Use the sentiment analyzer pipeline to get the sentiment and score
|
| 23 |
+
output = (sentiment_Analyzer(text))[0]
|
| 24 |
+
label = output['label']
|
| 25 |
+
score = output['score']
|
| 26 |
+
return label, score
|
| 27 |
+
|
| 28 |
+
# Function to add an 'Evaluation' column to the DataFrame based on sentiment analysis
|
| 29 |
+
def evaluate_reviews(df):
|
| 30 |
+
# Apply the analyzer function to each review in the DataFrame
|
| 31 |
+
df['Evaluation'] = df['Review'].apply(lambda x: analyzer(x))
|
| 32 |
+
# Split the evaluation into 'Sentiment' and 'Score' columns
|
| 33 |
+
df[['Sentiment', 'Score']] = pd.DataFrame(df['Evaluation'].tolist(), index=df.index)
|
| 34 |
+
# Drop the original 'Evaluation' column
|
| 35 |
+
df.drop(columns=['Evaluation'], inplace=True)
|
| 36 |
+
return df
|
| 37 |
+
|
| 38 |
+
# Function to create a pie chart showing the percentage of positive and negative reviews
|
| 39 |
+
def create_pie_chart(df):
|
| 40 |
+
# Count the occurrences of each sentiment
|
| 41 |
+
sentiment_counts = df['Sentiment'].value_counts()
|
| 42 |
+
labels = sentiment_counts.index
|
| 43 |
+
sizes = sentiment_counts.values
|
| 44 |
+
colors = ['#ff9999','#66b3ff','#99ff99','#ffcc99']
|
| 45 |
+
fig1, ax1 = plt.subplots()
|
| 46 |
+
# Create a pie chart
|
| 47 |
+
ax1.pie(sizes, labels=labels, colors=colors, autopct='%1.1f%%', startangle=90)
|
| 48 |
+
ax1.axis('equal') # Equal aspect ratio ensures that pie is drawn as a circle.
|
| 49 |
+
|
| 50 |
+
# Set the title and save the plot as an image
|
| 51 |
+
plt.title('Sentiment Analysis of Reviews')
|
| 52 |
+
chart_path = 'sentiment_pie_chart.png'
|
| 53 |
+
plt.savefig(chart_path)
|
| 54 |
+
plt.show()
|
| 55 |
+
return chart_path
|
| 56 |
+
|
| 57 |
+
# Function to process the uploaded file and generate the output
|
| 58 |
+
def process_reviews(file):
|
| 59 |
+
# Read reviews from the uploaded file
|
| 60 |
+
df = read_reviews_to_dataframe(file.name)
|
| 61 |
+
# Evaluate the reviews
|
| 62 |
+
df = evaluate_reviews(df)
|
| 63 |
+
# Create a pie chart
|
| 64 |
+
chart_path = create_pie_chart(df)
|
| 65 |
+
return df, chart_path
|
| 66 |
+
|
| 67 |
+
# Gradio interface function
|
| 68 |
+
def gradio_interface(file):
|
| 69 |
+
# Process the uploaded file and return the DataFrame and pie chart path
|
| 70 |
+
df, chart_path = process_reviews(file)
|
| 71 |
+
return df, chart_path
|
| 72 |
+
|
| 73 |
+
# Create the Gradio interface
|
| 74 |
+
interface = gr.Interface(
|
| 75 |
+
fn=gradio_interface,
|
| 76 |
+
inputs=gr.File(label="Upload a text file with reviews"), # Input: File upload
|
| 77 |
+
outputs=[
|
| 78 |
+
gr.Dataframe(label="Reviews with Evaluation"), # Output: DataFrame
|
| 79 |
+
gr.Image(label="Sentiment Pie Chart") # Output: Pie chart image
|
| 80 |
+
],
|
| 81 |
+
title="Sentiment Analyzer", # Title of the interface
|
| 82 |
+
description="Upload a text file with reviews to analyze the sentiment and visualize the results." # Description
|
| 83 |
+
)
|
| 84 |
+
|
| 85 |
+
# Launch the Gradio interface
|
| 86 |
+
interface.launch()
|