Update app.py
Browse files
app.py
CHANGED
@@ -8,89 +8,72 @@ from fuzzywuzzy import fuzz
|
|
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 |
-
# التحقق من وجود نص في المدخل
|
18 |
if not user_review.strip():
|
19 |
return "Please enter a valid review."
|
20 |
|
21 |
-
# البحث عن المراجعة في قاعدة البيانات باستخدام مطابقة غامضة
|
22 |
best_match = None
|
23 |
best_score = 0
|
24 |
|
25 |
-
# تكرار على كل المراجعات الموجودة في قاعدة البيانات
|
26 |
for _, row in reviews_df.iterrows():
|
27 |
-
# التأكد من أن عمود المراجعات يحتوي على نصوص وليس قيم مفقودة
|
28 |
if pd.isna(row['Review']):
|
29 |
continue
|
30 |
|
31 |
-
# حساب درجة المطابقة بين مراجعة المستخدم والمراجعة في مجموعة البيانات
|
32 |
score = fuzz.token_sort_ratio(user_review.lower(), str(row['Review']).lower())
|
33 |
if score > best_score:
|
34 |
best_score = score
|
35 |
best_match = row
|
36 |
|
37 |
-
|
38 |
-
if best_score > 80: # نستخدم عتبة 80% للتأكد من أن المطابقة دقيقة
|
39 |
rating = best_match['Rating']
|
40 |
-
|
41 |
-
if int(rating) >= 4:
|
42 |
-
rating_based_classification = f"Positive review based on rating: {rating}"
|
43 |
-
else:
|
44 |
-
rating_based_classification = f"Negative review based on rating: {rating}"
|
45 |
|
46 |
-
# تحليل المراجعة باستخدام نموذج Hugging Face
|
47 |
sentiment_result = sentiment_model(user_review)[0]
|
48 |
sentiment = sentiment_result['label']
|
49 |
confidence = sentiment_result['score']
|
50 |
sentiment_based_classification = f"Model prediction: {sentiment} with confidence: {confidence:.2f}"
|
51 |
|
52 |
-
# إرجاع النتائج
|
53 |
return f"{rating_based_classification}\n{sentiment_based_classification}\nMatching Score: {best_score}%"
|
54 |
else:
|
55 |
return "Review not found in the dataset."
|
56 |
|
57 |
except Exception as e:
|
58 |
-
# طباعة الخطأ للحصول على معلومات أكثر حوله
|
59 |
return f"An error occurred: {str(e)}"
|
60 |
|
61 |
-
|
62 |
# وظيفة لرسم توزيع التقييمات
|
63 |
-
|
64 |
def plot_rating_distribution():
|
65 |
plt.figure(figsize=(8, 6))
|
66 |
-
sns.countplot(x='Rating', data=reviews_df, order=[1, 2, 3, 4, 5])
|
67 |
plt.title('Distribution of Ratings')
|
68 |
plt.xlabel('Rating')
|
69 |
plt.ylabel('Count')
|
70 |
plt.tight_layout()
|
71 |
return plt.gcf()
|
72 |
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
# إنشاء واجهة Gradio لتصنيف المراجعات
|
77 |
-
interface = gr.Interface(
|
78 |
fn=classify_review,
|
79 |
inputs=gr.Textbox(lines=2, placeholder="Enter your review here", label="Reviews"),
|
80 |
outputs="text",
|
81 |
title="Review Classifier Based on Rating and Hugging Face Model",
|
82 |
-
description="Enter a restaurant review. The system will classify it based on the dataset rating and use a sentiment analysis model
|
83 |
)
|
84 |
|
85 |
-
|
86 |
-
plot_interface_ratings = gr.Interface(
|
87 |
fn=plot_rating_distribution,
|
88 |
inputs=[],
|
89 |
outputs="plot",
|
90 |
title="Rating Distribution",
|
91 |
description="Shows the distribution of ratings in the dataset."
|
92 |
-
|
93 |
)
|
94 |
-
|
95 |
-
|
96 |
-
|
|
|
|
|
|
|
|
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])
|
52 |
plt.title('Distribution of Ratings')
|
53 |
plt.xlabel('Rating')
|
54 |
plt.ylabel('Count')
|
55 |
plt.tight_layout()
|
56 |
return plt.gcf()
|
57 |
|
58 |
+
# إنشاء واجهات Gradio
|
59 |
+
review_interface = gr.Interface(
|
|
|
|
|
|
|
60 |
fn=classify_review,
|
61 |
inputs=gr.Textbox(lines=2, placeholder="Enter your review here", label="Reviews"),
|
62 |
outputs="text",
|
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=[],
|
70 |
outputs="plot",
|
71 |
title="Rating Distribution",
|
72 |
description="Shows the distribution of ratings in the dataset."
|
|
|
73 |
)
|
74 |
+
|
75 |
+
# دمج الواجهتين باستخدام تبويبات
|
76 |
+
tabbed_interface = gr.TabbedInterface([review_interface, plot_interface], ["Review Classifier", "Rating Distribution"])
|
77 |
+
|
78 |
+
# إطلاق الواجهات
|
79 |
+
tabbed_interface.launch()
|