Update app_spaces.py
Browse files- app_spaces.py +241 -170
app_spaces.py
CHANGED
|
@@ -1,171 +1,242 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
import pandas as pd
|
| 3 |
-
from transformers import pipeline
|
| 4 |
-
import warnings
|
| 5 |
-
import os
|
| 6 |
-
warnings.filterwarnings("ignore")
|
| 7 |
-
|
| 8 |
-
# Initialize the models
|
| 9 |
-
print("Loading ABSA models for Hugging Face Spaces...")
|
| 10 |
-
token_classifier = pipeline(
|
| 11 |
-
model="sdf299/abte-restaurants-distilbert-base-uncased",
|
| 12 |
-
aggregation_strategy="simple"
|
| 13 |
-
)
|
| 14 |
-
|
| 15 |
-
classifier = pipeline(
|
| 16 |
-
model="sdf299/absa-restaurants-distilbert-base-uncased"
|
| 17 |
-
)
|
| 18 |
-
print("Models loaded successfully!")
|
| 19 |
-
|
| 20 |
-
def
|
| 21 |
-
"""
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
<
|
| 161 |
-
<p
|
| 162 |
-
<
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 171 |
demo.launch()
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import pandas as pd
|
| 3 |
+
from transformers import pipeline
|
| 4 |
+
import warnings
|
| 5 |
+
import os
|
| 6 |
+
warnings.filterwarnings("ignore")
|
| 7 |
+
|
| 8 |
+
# Initialize the models
|
| 9 |
+
print("Loading ABSA models for Hugging Face Spaces...")
|
| 10 |
+
token_classifier = pipeline(
|
| 11 |
+
model="sdf299/abte-restaurants-distilbert-base-uncased",
|
| 12 |
+
aggregation_strategy="simple"
|
| 13 |
+
)
|
| 14 |
+
|
| 15 |
+
classifier = pipeline(
|
| 16 |
+
model="sdf299/absa-restaurants-distilbert-base-uncased"
|
| 17 |
+
)
|
| 18 |
+
print("Models loaded successfully!")
|
| 19 |
+
|
| 20 |
+
def get_sentiment_color(sentiment_label):
|
| 21 |
+
"""Return color based on sentiment label."""
|
| 22 |
+
sentiment_lower = sentiment_label.lower()
|
| 23 |
+
if 'positive' in sentiment_lower:
|
| 24 |
+
return "#28a745", "π’" # Green
|
| 25 |
+
elif 'negative' in sentiment_lower:
|
| 26 |
+
return "#dc3545", "π΄" # Red
|
| 27 |
+
else:
|
| 28 |
+
return "#6c757d", "βͺ" # Gray for neutral
|
| 29 |
+
|
| 30 |
+
def analyze_sentiment(sentence):
|
| 31 |
+
"""
|
| 32 |
+
Perform aspect-based sentiment analysis on the input sentence.
|
| 33 |
+
|
| 34 |
+
Args:
|
| 35 |
+
sentence (str): Input sentence to analyze
|
| 36 |
+
|
| 37 |
+
Returns:
|
| 38 |
+
tuple: (formatted_results, aspects_summary, detailed_dataframe)
|
| 39 |
+
"""
|
| 40 |
+
if not sentence.strip():
|
| 41 |
+
return "Please enter a sentence to analyze.", "", pd.DataFrame()
|
| 42 |
+
|
| 43 |
+
try:
|
| 44 |
+
# Extract aspects using token classifier
|
| 45 |
+
results = token_classifier(sentence)
|
| 46 |
+
|
| 47 |
+
if not results:
|
| 48 |
+
return "No aspects found in the sentence.", "", pd.DataFrame()
|
| 49 |
+
|
| 50 |
+
# Get unique aspects
|
| 51 |
+
aspects = list(set([result['word'] for result in results]))
|
| 52 |
+
|
| 53 |
+
# Analyze sentiment for each aspect
|
| 54 |
+
detailed_results = []
|
| 55 |
+
formatted_output = f"**π Input Sentence:** {sentence}\n\n"
|
| 56 |
+
formatted_output += "## π― Analysis Results:\n\n"
|
| 57 |
+
|
| 58 |
+
# Count sentiments for summary
|
| 59 |
+
sentiment_counts = {'positive': 0, 'negative': 0, 'neutral': 0}
|
| 60 |
+
|
| 61 |
+
for aspect in aspects:
|
| 62 |
+
# Classify sentiment for this aspect
|
| 63 |
+
sentiment_result = classifier(f'{sentence} [SEP] {aspect}')
|
| 64 |
+
|
| 65 |
+
# Extract sentiment label and confidence
|
| 66 |
+
sentiment_label = sentiment_result[0]['label']
|
| 67 |
+
confidence = sentiment_result[0]['score']
|
| 68 |
+
|
| 69 |
+
# Get color and emoji for this sentiment
|
| 70 |
+
color, emoji = get_sentiment_color(sentiment_label)
|
| 71 |
+
|
| 72 |
+
# Count sentiments
|
| 73 |
+
if 'positive' in sentiment_label.lower():
|
| 74 |
+
sentiment_counts['positive'] += 1
|
| 75 |
+
elif 'negative' in sentiment_label.lower():
|
| 76 |
+
sentiment_counts['negative'] += 1
|
| 77 |
+
else:
|
| 78 |
+
sentiment_counts['neutral'] += 1
|
| 79 |
+
|
| 80 |
+
# Format the result with colors
|
| 81 |
+
formatted_output += f'<div style="margin: 15px 0; padding: 15px; border-left: 4px solid {color}; background-color: {color}15; border-radius: 5px;">'
|
| 82 |
+
formatted_output += f'<strong style="color: {color};">{emoji} Aspect: {aspect}</strong><br>'
|
| 83 |
+
formatted_output += f'<span style="color: {color}; font-weight: bold;">Sentiment: {sentiment_label}</span> '
|
| 84 |
+
formatted_output += f'<span style="color: #666; font-size: 0.9em;">(Confidence: {confidence:.3f})</span>'
|
| 85 |
+
formatted_output += '</div>\n\n'
|
| 86 |
+
|
| 87 |
+
# Store for dataframe with colored styling
|
| 88 |
+
detailed_results.append({
|
| 89 |
+
'Aspect': aspect,
|
| 90 |
+
'Sentiment': sentiment_label,
|
| 91 |
+
'Confidence': f"{confidence:.3f}",
|
| 92 |
+
'Color': color,
|
| 93 |
+
'Emoji': emoji
|
| 94 |
+
})
|
| 95 |
+
|
| 96 |
+
# Create colorful summary
|
| 97 |
+
aspects_summary = "## π Summary:\n\n"
|
| 98 |
+
aspects_summary += f"**π Total Aspects Found:** {len(aspects)}\n\n"
|
| 99 |
+
|
| 100 |
+
# Add sentiment breakdown
|
| 101 |
+
if sentiment_counts['positive'] > 0:
|
| 102 |
+
aspects_summary += f"π’ **Positive:** {sentiment_counts['positive']} aspects\n\n"
|
| 103 |
+
if sentiment_counts['negative'] > 0:
|
| 104 |
+
aspects_summary += f"π΄ **Negative:** {sentiment_counts['negative']} aspects\n\n"
|
| 105 |
+
if sentiment_counts['neutral'] > 0:
|
| 106 |
+
aspects_summary += f"βͺ **Neutral:** {sentiment_counts['neutral']} aspects\n\n"
|
| 107 |
+
|
| 108 |
+
aspects_summary += f"**π Identified Aspects:** {', '.join(aspects)}"
|
| 109 |
+
|
| 110 |
+
# Create dataframe for tabular view (simplified for table)
|
| 111 |
+
df_data = []
|
| 112 |
+
for result in detailed_results:
|
| 113 |
+
df_data.append({
|
| 114 |
+
'Aspect': result['Aspect'],
|
| 115 |
+
'Sentiment': f"{result['Emoji']} {result['Sentiment']}",
|
| 116 |
+
'Confidence': result['Confidence']
|
| 117 |
+
})
|
| 118 |
+
df = pd.DataFrame(df_data)
|
| 119 |
+
|
| 120 |
+
return formatted_output, aspects_summary, df
|
| 121 |
+
|
| 122 |
+
except Exception as e:
|
| 123 |
+
error_msg = f"β **Error during analysis:** {str(e)}\n\nPlease try again with a different sentence."
|
| 124 |
+
return error_msg, "", pd.DataFrame()
|
| 125 |
+
|
| 126 |
+
# Create the Gradio interface
|
| 127 |
+
with gr.Blocks(
|
| 128 |
+
title="π½οΈ Restaurant Review Analyzer - ABSA",
|
| 129 |
+
theme=gr.themes.Soft(),
|
| 130 |
+
css="""
|
| 131 |
+
.gradio-container {
|
| 132 |
+
font-family: 'Arial', sans-serif;
|
| 133 |
+
max-width: 1200px;
|
| 134 |
+
}
|
| 135 |
+
.main-header {
|
| 136 |
+
text-align: center;
|
| 137 |
+
margin-bottom: 30px;
|
| 138 |
+
}
|
| 139 |
+
.sentiment-positive {
|
| 140 |
+
color: #28a745 !important;
|
| 141 |
+
background-color: #d4edda;
|
| 142 |
+
border-color: #c3e6cb;
|
| 143 |
+
}
|
| 144 |
+
.sentiment-negative {
|
| 145 |
+
color: #dc3545 !important;
|
| 146 |
+
background-color: #f8d7da;
|
| 147 |
+
border-color: #f5c6cb;
|
| 148 |
+
}
|
| 149 |
+
.sentiment-neutral {
|
| 150 |
+
color: #6c757d !important;
|
| 151 |
+
background-color: #f8f9fa;
|
| 152 |
+
border-color: #dee2e6;
|
| 153 |
+
}
|
| 154 |
+
"""
|
| 155 |
+
) as demo:
|
| 156 |
+
|
| 157 |
+
gr.HTML("""
|
| 158 |
+
<div class="main-header">
|
| 159 |
+
<h1>π½οΈ Restaurant Review Analyzer</h1>
|
| 160 |
+
<h3>π¨ Colorful Aspect-Based Sentiment Analysis</h3>
|
| 161 |
+
<p>Analyze restaurant reviews to identify specific aspects and their sentiments with beautiful color coding!</p>
|
| 162 |
+
<div style="margin: 15px 0; padding: 10px; background-color: #f8f9fa; border-radius: 8px; border: 1px solid #dee2e6;">
|
| 163 |
+
<p style="margin: 5px 0;"><strong>π¨ Color Guide:</strong></p>
|
| 164 |
+
<span style="color: #28a745; font-weight: bold;">π’ Positive</span> |
|
| 165 |
+
<span style="color: #dc3545; font-weight: bold;">π΄ Negative</span> |
|
| 166 |
+
<span style="color: #6c757d; font-weight: bold;">βͺ Neutral</span>
|
| 167 |
+
</div>
|
| 168 |
+
<p><em>Powered by DistilBERT models fine-tuned on restaurant reviews</em></p>
|
| 169 |
+
</div>
|
| 170 |
+
""")
|
| 171 |
+
|
| 172 |
+
with gr.Row():
|
| 173 |
+
with gr.Column(scale=2):
|
| 174 |
+
# Input section
|
| 175 |
+
sentence_input = gr.Textbox(
|
| 176 |
+
label="π½οΈ Enter Restaurant Review",
|
| 177 |
+
placeholder="e.g., The services here is wonderful, but I hate the food. However, I still love the atmosphere here.",
|
| 178 |
+
lines=3,
|
| 179 |
+
max_lines=5
|
| 180 |
+
)
|
| 181 |
+
|
| 182 |
+
analyze_btn = gr.Button("π Analyze Sentiment", variant="primary", size="lg")
|
| 183 |
+
|
| 184 |
+
# Example sentences
|
| 185 |
+
gr.Examples(
|
| 186 |
+
examples=[
|
| 187 |
+
["The services here is wonderful, but I hate the food. However, I still love the atmosphere here."],
|
| 188 |
+
["The food was amazing and the staff was very friendly, but the restaurant was too noisy."],
|
| 189 |
+
["Great location and delicious pizza, but the service was slow and the prices are too high."],
|
| 190 |
+
["The ambiance is perfect for a romantic dinner, excellent wine selection, but the dessert was disappointing."],
|
| 191 |
+
["Fast service and good value for money, but the food quality could be better."],
|
| 192 |
+
["Excellent sushi and attentive waiters, though the wait time was quite long."],
|
| 193 |
+
["Beautiful decor and reasonable prices, but the pasta was overcooked."],
|
| 194 |
+
["Outstanding customer service and fresh ingredients, highly recommend this place!"],
|
| 195 |
+
["Terrible experience - rude staff, cold food, and dirty tables. Never coming back."]
|
| 196 |
+
],
|
| 197 |
+
inputs=sentence_input,
|
| 198 |
+
label="π‘ Try these examples:"
|
| 199 |
+
)
|
| 200 |
+
|
| 201 |
+
with gr.Column(scale=3):
|
| 202 |
+
# Output section
|
| 203 |
+
with gr.Tab("π¨ Colorful Results"):
|
| 204 |
+
results_output = gr.HTML(label="Visual Analysis Results")
|
| 205 |
+
|
| 206 |
+
with gr.Tab("π Summary Dashboard"):
|
| 207 |
+
aspects_output = gr.Markdown(label="Quick Summary")
|
| 208 |
+
|
| 209 |
+
with gr.Tab("π Data Table"):
|
| 210 |
+
table_output = gr.Dataframe(
|
| 211 |
+
label="Results Table",
|
| 212 |
+
headers=["Aspect", "Sentiment", "Confidence"]
|
| 213 |
+
)
|
| 214 |
+
|
| 215 |
+
# Event handlers
|
| 216 |
+
analyze_btn.click(
|
| 217 |
+
fn=analyze_sentiment,
|
| 218 |
+
inputs=[sentence_input],
|
| 219 |
+
outputs=[results_output, aspects_output, table_output]
|
| 220 |
+
)
|
| 221 |
+
|
| 222 |
+
sentence_input.submit(
|
| 223 |
+
fn=analyze_sentiment,
|
| 224 |
+
inputs=[sentence_input],
|
| 225 |
+
outputs=[results_output, aspects_output, table_output]
|
| 226 |
+
)
|
| 227 |
+
|
| 228 |
+
# Footer with model information
|
| 229 |
+
gr.HTML("""
|
| 230 |
+
<div style="text-align: center; margin-top: 30px; padding: 20px; border-top: 1px solid #eee;">
|
| 231 |
+
<p><strong>π€ Models Used:</strong></p>
|
| 232 |
+
<p>π€ Aspect Extraction: <a href="https://huggingface.co/sdf299/abte-restaurants-distilbert-base-uncased" target="_blank">sdf299/abte-restaurants-distilbert-base-uncased</a></p>
|
| 233 |
+
<p>π Sentiment Classification: <a href="https://huggingface.co/sdf299/absa-restaurants-distilbert-base-uncased" target="_blank">sdf299/absa-restaurants-distilbert-base-uncased</a></p>
|
| 234 |
+
<p style="margin-top: 15px; font-size: 0.9em; color: #666;">
|
| 235 |
+
β¨ This app demonstrates colorful aspect-based sentiment analysis for restaurant reviews using fine-tuned DistilBERT models.
|
| 236 |
+
</p>
|
| 237 |
+
</div>
|
| 238 |
+
""")
|
| 239 |
+
|
| 240 |
+
# Launch the app
|
| 241 |
+
if __name__ == "__main__":
|
| 242 |
demo.launch()
|