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