Update app.py
Browse files
app.py
CHANGED
@@ -4,48 +4,49 @@ import gradio as gr
|
|
4 |
import seaborn as sns
|
5 |
import matplotlib.pyplot as plt
|
6 |
from fuzzywuzzy import fuzz
|
7 |
-
|
8 |
-
#
|
9 |
reviews_df = pd.read_csv('Restaurant_reviews.csv')
|
10 |
-
|
11 |
-
#
|
12 |
sentiment_model = pipeline("sentiment-analysis", model="distilbert-base-uncased-finetuned-sst-2-english")
|
13 |
-
|
14 |
-
#
|
15 |
def classify_review(user_review):
|
16 |
try:
|
17 |
if not user_review.strip():
|
18 |
return "Please enter a valid review."
|
19 |
-
|
20 |
best_match = None
|
21 |
best_score = 0
|
22 |
-
|
23 |
for _, row in reviews_df.iterrows():
|
24 |
if pd.isna(row['Review']):
|
25 |
continue
|
26 |
-
|
|
|
27 |
score = fuzz.token_sort_ratio(user_review.lower(), str(row['Review']).lower())
|
28 |
if score > best_score:
|
29 |
best_score = score
|
30 |
best_match = row
|
31 |
-
|
32 |
-
if best_score > 80:
|
33 |
rating = best_match['Rating']
|
34 |
rating_based_classification = f"Positive review based on rating: {rating}" if int(rating) >= 4 else f"Negative review based on rating: {rating}"
|
35 |
-
|
36 |
sentiment_result = sentiment_model(user_review)[0]
|
37 |
sentiment = sentiment_result['label']
|
38 |
confidence = sentiment_result['score']
|
39 |
sentiment_based_classification = f"Model prediction: {sentiment} with confidence: {confidence:.2f}"
|
40 |
-
|
41 |
return f"{rating_based_classification}\n{sentiment_based_classification}\nMatching Score: {best_score}%"
|
42 |
else:
|
43 |
return "Review not found in the dataset."
|
44 |
-
|
45 |
except Exception as e:
|
46 |
return f"An error occurred: {str(e)}"
|
47 |
-
|
48 |
-
#
|
49 |
def plot_rating_distribution():
|
50 |
plt.figure(figsize=(8, 6))
|
51 |
sns.countplot(x='Rating', data=reviews_df, order=[1, 2, 3, 4, 5])
|
@@ -54,8 +55,16 @@ def plot_rating_distribution():
|
|
54 |
plt.ylabel('Count')
|
55 |
plt.tight_layout()
|
56 |
return plt.gcf()
|
57 |
-
|
58 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
59 |
review_interface = gr.Interface(
|
60 |
fn=classify_review,
|
61 |
inputs=gr.Textbox(lines=2, placeholder="Enter your review here", label="Reviews"),
|
@@ -63,7 +72,8 @@ review_interface = gr.Interface(
|
|
63 |
title="Review Classifier Based on Rating and Hugging Face Model",
|
64 |
description="Enter a restaurant review. The system will classify it based on the dataset rating and use a sentiment analysis model."
|
65 |
)
|
66 |
-
|
|
|
67 |
plot_interface = gr.Interface(
|
68 |
fn=plot_rating_distribution,
|
69 |
inputs=[],
|
@@ -71,9 +81,30 @@ plot_interface = gr.Interface(
|
|
71 |
title="Rating Distribution",
|
72 |
description="Shows the distribution of ratings in the dataset."
|
73 |
)
|
74 |
-
|
75 |
-
#
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
import seaborn as sns
|
5 |
import matplotlib.pyplot as plt
|
6 |
from fuzzywuzzy import fuzz
|
7 |
+
|
8 |
+
# Load the restaurant reviews dataset
|
9 |
reviews_df = pd.read_csv('Restaurant_reviews.csv')
|
10 |
+
|
11 |
+
# Load the Hugging Face sentiment analysis model
|
12 |
sentiment_model = pipeline("sentiment-analysis", model="distilbert-base-uncased-finetuned-sst-2-english")
|
13 |
+
|
14 |
+
# Function to classify a user's review based on dataset matching and sentiment analysis
|
15 |
def classify_review(user_review):
|
16 |
try:
|
17 |
if not user_review.strip():
|
18 |
return "Please enter a valid review."
|
19 |
+
|
20 |
best_match = None
|
21 |
best_score = 0
|
22 |
+
|
23 |
for _, row in reviews_df.iterrows():
|
24 |
if pd.isna(row['Review']):
|
25 |
continue
|
26 |
+
|
27 |
+
# Use fuzzy matching to find the most similar review in the dataset
|
28 |
score = fuzz.token_sort_ratio(user_review.lower(), str(row['Review']).lower())
|
29 |
if score > best_score:
|
30 |
best_score = score
|
31 |
best_match = row
|
32 |
+
|
33 |
+
if best_score > 80: # If the fuzzy match score is greater than 80
|
34 |
rating = best_match['Rating']
|
35 |
rating_based_classification = f"Positive review based on rating: {rating}" if int(rating) >= 4 else f"Negative review based on rating: {rating}"
|
36 |
+
|
37 |
sentiment_result = sentiment_model(user_review)[0]
|
38 |
sentiment = sentiment_result['label']
|
39 |
confidence = sentiment_result['score']
|
40 |
sentiment_based_classification = f"Model prediction: {sentiment} with confidence: {confidence:.2f}"
|
41 |
+
|
42 |
return f"{rating_based_classification}\n{sentiment_based_classification}\nMatching Score: {best_score}%"
|
43 |
else:
|
44 |
return "Review not found in the dataset."
|
45 |
+
|
46 |
except Exception as e:
|
47 |
return f"An error occurred: {str(e)}"
|
48 |
+
|
49 |
+
# Function to plot the distribution of ratings from the dataset
|
50 |
def plot_rating_distribution():
|
51 |
plt.figure(figsize=(8, 6))
|
52 |
sns.countplot(x='Rating', data=reviews_df, order=[1, 2, 3, 4, 5])
|
|
|
55 |
plt.ylabel('Count')
|
56 |
plt.tight_layout()
|
57 |
return plt.gcf()
|
58 |
+
|
59 |
+
# Function to allow users to preview the dataset (first 10 rows)
|
60 |
+
def preview_dataset():
|
61 |
+
return reviews_df.head(10) # Return the first 10 rows of the dataset
|
62 |
+
|
63 |
+
# Function to download the dataset
|
64 |
+
def download_dataset():
|
65 |
+
return 'Restaurant_reviews.csv' # Provide the path to the dataset for download
|
66 |
+
|
67 |
+
# Create the Gradio interface for classifying reviews
|
68 |
review_interface = gr.Interface(
|
69 |
fn=classify_review,
|
70 |
inputs=gr.Textbox(lines=2, placeholder="Enter your review here", label="Reviews"),
|
|
|
72 |
title="Review Classifier Based on Rating and Hugging Face Model",
|
73 |
description="Enter a restaurant review. The system will classify it based on the dataset rating and use a sentiment analysis model."
|
74 |
)
|
75 |
+
|
76 |
+
# Create the Gradio interface for plotting the rating distribution
|
77 |
plot_interface = gr.Interface(
|
78 |
fn=plot_rating_distribution,
|
79 |
inputs=[],
|
|
|
81 |
title="Rating Distribution",
|
82 |
description="Shows the distribution of ratings in the dataset."
|
83 |
)
|
84 |
+
|
85 |
+
# Create the Gradio interface for previewing the dataset
|
86 |
+
preview_interface = gr.Interface(
|
87 |
+
fn=preview_dataset,
|
88 |
+
inputs=[],
|
89 |
+
outputs="dataframe",
|
90 |
+
title="Preview Restaurant Reviews Dataset",
|
91 |
+
description="Displays the first 10 rows of the dataset for preview."
|
92 |
+
)
|
93 |
+
|
94 |
+
# Create the Gradio interface for downloading the dataset
|
95 |
+
download_interface = gr.Interface(
|
96 |
+
fn=download_dataset,
|
97 |
+
inputs=[],
|
98 |
+
outputs=gr.File(),
|
99 |
+
title="Download Restaurant Reviews Dataset",
|
100 |
+
description="Download the full restaurant reviews dataset in CSV format."
|
101 |
+
)
|
102 |
+
|
103 |
+
# Combine all interfaces (Review Classifier, Rating Distribution, Dataset Preview, Dataset Download) into tabs
|
104 |
+
tabbed_interface = gr.TabbedInterface(
|
105 |
+
[review_interface, plot_interface, preview_interface, download_interface],
|
106 |
+
["Review Classifier", "Rating Distribution", "Dataset Preview", "Download Dataset"]
|
107 |
+
)
|
108 |
+
|
109 |
+
# Launch the Gradio interface
|
110 |
+
tabbed_interface.launch()
|