Upload folder using huggingface_hub
Browse files- .gradio/certificate.pem +31 -0
- README.md +51 -12
- __pycache__/app.cpython-311.pyc +0 -0
- app.py +172 -0
- app_spaces.py +171 -0
- requirements.txt +6 -0
- requirements_flexible.txt +5 -0
- requirements_minimal.txt +4 -0
- run.bat +38 -0
- run.py +73 -0
- run.sh +45 -0
.gradio/certificate.pem
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
-----BEGIN CERTIFICATE-----
|
| 2 |
+
MIIFazCCA1OgAwIBAgIRAIIQz7DSQONZRGPgu2OCiwAwDQYJKoZIhvcNAQELBQAw
|
| 3 |
+
TzELMAkGA1UEBhMCVVMxKTAnBgNVBAoTIEludGVybmV0IFNlY3VyaXR5IFJlc2Vh
|
| 4 |
+
cmNoIEdyb3VwMRUwEwYDVQQDEwxJU1JHIFJvb3QgWDEwHhcNMTUwNjA0MTEwNDM4
|
| 5 |
+
WhcNMzUwNjA0MTEwNDM4WjBPMQswCQYDVQQGEwJVUzEpMCcGA1UEChMgSW50ZXJu
|
| 6 |
+
ZXQgU2VjdXJpdHkgUmVzZWFyY2ggR3JvdXAxFTATBgNVBAMTDElTUkcgUm9vdCBY
|
| 7 |
+
MTCCAiIwDQYJKoZIhvcNAQEBBQADggIPADCCAgoCggIBAK3oJHP0FDfzm54rVygc
|
| 8 |
+
h77ct984kIxuPOZXoHj3dcKi/vVqbvYATyjb3miGbESTtrFj/RQSa78f0uoxmyF+
|
| 9 |
+
0TM8ukj13Xnfs7j/EvEhmkvBioZxaUpmZmyPfjxwv60pIgbz5MDmgK7iS4+3mX6U
|
| 10 |
+
A5/TR5d8mU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8 |
+
# 🍽️ Restaurant Review Analyzer
|
| 9 |
+
|
| 10 |
+
A Gradio-powered web interface for analyzing restaurant reviews using **Aspect-Based Sentiment Analysis (ABSA)**. This application identifies specific aspects (like food, service, atmosphere) mentioned in reviews and determines the sentiment associated with each aspect.
|
| 11 |
+
|
| 12 |
+
## 🎯 How It Works
|
| 13 |
+
|
| 14 |
+
The application uses two fine-tuned DistilBERT models:
|
| 15 |
+
|
| 16 |
+
1. **Aspect Extraction**: Identifies aspects mentioned in reviews (e.g., "food", "service", "atmosphere")
|
| 17 |
+
2. **Sentiment Classification**: Determines sentiment (positive/negative) for each aspect
|
| 18 |
+
|
| 19 |
+
## 🚀 Try It Out!
|
| 20 |
+
|
| 21 |
+
Simply enter a restaurant review in the text box and click "Analyze Sentiment" to see:
|
| 22 |
+
- **Identified Aspects**: What specific elements are mentioned
|
| 23 |
+
- **Sentiment Analysis**: Whether each aspect is viewed positively or negatively
|
| 24 |
+
- **Confidence Scores**: How certain the model is about each prediction
|
| 25 |
+
|
| 26 |
+
## 📊 Example
|
| 27 |
+
|
| 28 |
+
**Input**: "The services here is wonderful, but I hate the food. However, I still love the atmosphere here."
|
| 29 |
+
|
| 30 |
+
**Output**:
|
| 31 |
+
- **service** → POSITIVE (0.952)
|
| 32 |
+
- **food** → NEGATIVE (0.887)
|
| 33 |
+
- **atmosphere** → POSITIVE (0.934)
|
| 34 |
+
|
| 35 |
+
## 🔧 Models
|
| 36 |
+
|
| 37 |
+
- **Aspect Extraction**: [sdf299/abte-restaurants-distilbert-base-uncased](https://huggingface.co/sdf299/abte-restaurants-distilbert-base-uncased)
|
| 38 |
+
- **Sentiment Classification**: [sdf299/absa-restaurants-distilbert-base-uncased](https://huggingface.co/sdf299/absa-restaurants-distilbert-base-uncased)
|
| 39 |
+
|
| 40 |
+
## 💡 Use Cases
|
| 41 |
+
|
| 42 |
+
Perfect for:
|
| 43 |
+
- Restaurant owners analyzing customer feedback
|
| 44 |
+
- Review aggregation platforms
|
| 45 |
+
- Market research on dining experiences
|
| 46 |
+
- Academic research in sentiment analysis
|
| 47 |
+
- Understanding customer opinions at scale
|
| 48 |
+
|
| 49 |
+
---
|
| 50 |
+
|
| 51 |
+
*Built with 🤗 Transformers and Gradio*
|
__pycache__/app.cpython-311.pyc
ADDED
|
Binary file (8.12 kB). View file
|
|
|
app.py
ADDED
|
@@ -0,0 +1,172 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import pandas as pd
|
| 3 |
+
from transformers import pipeline
|
| 4 |
+
import warnings
|
| 5 |
+
warnings.filterwarnings("ignore")
|
| 6 |
+
|
| 7 |
+
# Initialize the models
|
| 8 |
+
print("Loading models...")
|
| 9 |
+
token_classifier = pipeline(
|
| 10 |
+
model="sdf299/abte-restaurants-distilbert-base-uncased",
|
| 11 |
+
aggregation_strategy="simple"
|
| 12 |
+
)
|
| 13 |
+
|
| 14 |
+
classifier = pipeline(
|
| 15 |
+
model="sdf299/absa-restaurants-distilbert-base-uncased"
|
| 16 |
+
)
|
| 17 |
+
print("Models loaded successfully!")
|
| 18 |
+
|
| 19 |
+
def analyze_sentiment(sentence):
|
| 20 |
+
"""
|
| 21 |
+
Perform aspect-based sentiment analysis on the input sentence.
|
| 22 |
+
|
| 23 |
+
Args:
|
| 24 |
+
sentence (str): Input sentence to analyze
|
| 25 |
+
|
| 26 |
+
Returns:
|
| 27 |
+
tuple: (formatted_results, aspects_summary, detailed_dataframe)
|
| 28 |
+
"""
|
| 29 |
+
if not sentence.strip():
|
| 30 |
+
return "Please enter a sentence to analyze.", "", pd.DataFrame()
|
| 31 |
+
|
| 32 |
+
try:
|
| 33 |
+
# Extract aspects using token classifier
|
| 34 |
+
results = token_classifier(sentence)
|
| 35 |
+
|
| 36 |
+
if not results:
|
| 37 |
+
return "No aspects found in the sentence.", "", pd.DataFrame()
|
| 38 |
+
|
| 39 |
+
# Get unique aspects
|
| 40 |
+
aspects = list(set([result['word'] for result in results]))
|
| 41 |
+
|
| 42 |
+
# Analyze sentiment for each aspect
|
| 43 |
+
detailed_results = []
|
| 44 |
+
formatted_output = f"**Input Sentence:** {sentence}\n\n**Analysis Results:**\n\n"
|
| 45 |
+
|
| 46 |
+
for aspect in aspects:
|
| 47 |
+
# Classify sentiment for this aspect
|
| 48 |
+
sentiment_result = classifier(f'{sentence} [SEP] {aspect}')
|
| 49 |
+
|
| 50 |
+
# Extract sentiment label and confidence
|
| 51 |
+
sentiment_label = sentiment_result[0]['label']
|
| 52 |
+
confidence = sentiment_result[0]['score']
|
| 53 |
+
|
| 54 |
+
# Format the result
|
| 55 |
+
formatted_output += f"🎯 **Aspect:** {aspect}\n"
|
| 56 |
+
formatted_output += f" **Sentiment:** {sentiment_label} (Confidence: {confidence:.3f})\n\n"
|
| 57 |
+
|
| 58 |
+
# Store for dataframe
|
| 59 |
+
detailed_results.append({
|
| 60 |
+
'Aspect': aspect,
|
| 61 |
+
'Sentiment': sentiment_label,
|
| 62 |
+
'Confidence': f"{confidence:.3f}"
|
| 63 |
+
})
|
| 64 |
+
|
| 65 |
+
# Create summary
|
| 66 |
+
aspects_summary = f"**Identified Aspects:** {', '.join(aspects)}"
|
| 67 |
+
|
| 68 |
+
# Create dataframe for tabular view
|
| 69 |
+
df = pd.DataFrame(detailed_results)
|
| 70 |
+
|
| 71 |
+
return formatted_output, aspects_summary, df
|
| 72 |
+
|
| 73 |
+
except Exception as e:
|
| 74 |
+
error_msg = f"Error during analysis: {str(e)}"
|
| 75 |
+
return error_msg, "", pd.DataFrame()
|
| 76 |
+
|
| 77 |
+
def create_interface():
|
| 78 |
+
"""Create and configure the Gradio interface."""
|
| 79 |
+
|
| 80 |
+
with gr.Blocks(
|
| 81 |
+
title="Aspect-Based Sentiment Analysis",
|
| 82 |
+
theme=gr.themes.Soft(),
|
| 83 |
+
css="""
|
| 84 |
+
.gradio-container {
|
| 85 |
+
font-family: 'Arial', sans-serif;
|
| 86 |
+
}
|
| 87 |
+
.main-header {
|
| 88 |
+
text-align: center;
|
| 89 |
+
margin-bottom: 30px;
|
| 90 |
+
}
|
| 91 |
+
"""
|
| 92 |
+
) as demo:
|
| 93 |
+
|
| 94 |
+
gr.HTML("""
|
| 95 |
+
<div class="main-header">
|
| 96 |
+
<h1>🍽️ Restaurant Review Analyzer</h1>
|
| 97 |
+
<h3>Aspect-Based Sentiment Analysis</h3>
|
| 98 |
+
<p>Analyze restaurant reviews to identify specific aspects (food, service, atmosphere, etc.) and their associated sentiments.</p>
|
| 99 |
+
</div>
|
| 100 |
+
""")
|
| 101 |
+
|
| 102 |
+
with gr.Row():
|
| 103 |
+
with gr.Column(scale=2):
|
| 104 |
+
# Input section
|
| 105 |
+
sentence_input = gr.Textbox(
|
| 106 |
+
label="Enter Restaurant Review",
|
| 107 |
+
placeholder="e.g., The services here is wonderful, but I hate the food. However, I still love the atmosphere here.",
|
| 108 |
+
lines=3,
|
| 109 |
+
max_lines=5
|
| 110 |
+
)
|
| 111 |
+
|
| 112 |
+
analyze_btn = gr.Button("🔍 Analyze Sentiment", variant="primary", size="lg")
|
| 113 |
+
|
| 114 |
+
# Example sentences
|
| 115 |
+
gr.Examples(
|
| 116 |
+
examples=[
|
| 117 |
+
["The services here is wonderful, but I hate the food. However, I still love the atmosphere here."],
|
| 118 |
+
["The food was amazing and the staff was very friendly, but the restaurant was too noisy."],
|
| 119 |
+
["Great location and delicious pizza, but the service was slow and the prices are too high."],
|
| 120 |
+
["The ambiance is perfect for a romantic dinner, excellent wine selection, but the dessert was disappointing."],
|
| 121 |
+
["Fast service and good value for money, but the food quality could be better."]
|
| 122 |
+
],
|
| 123 |
+
inputs=sentence_input
|
| 124 |
+
)
|
| 125 |
+
|
| 126 |
+
with gr.Column(scale=3):
|
| 127 |
+
# Output section
|
| 128 |
+
with gr.Tab("📊 Detailed Results"):
|
| 129 |
+
results_output = gr.Markdown(label="Analysis Results")
|
| 130 |
+
|
| 131 |
+
with gr.Tab("📋 Quick Summary"):
|
| 132 |
+
aspects_output = gr.Markdown(label="Aspects Summary")
|
| 133 |
+
|
| 134 |
+
with gr.Tab("📈 Data Table"):
|
| 135 |
+
table_output = gr.Dataframe(
|
| 136 |
+
label="Results Table",
|
| 137 |
+
headers=["Aspect", "Sentiment", "Confidence"]
|
| 138 |
+
)
|
| 139 |
+
|
| 140 |
+
# Event handlers
|
| 141 |
+
analyze_btn.click(
|
| 142 |
+
fn=analyze_sentiment,
|
| 143 |
+
inputs=[sentence_input],
|
| 144 |
+
outputs=[results_output, aspects_output, table_output]
|
| 145 |
+
)
|
| 146 |
+
|
| 147 |
+
sentence_input.submit(
|
| 148 |
+
fn=analyze_sentiment,
|
| 149 |
+
inputs=[sentence_input],
|
| 150 |
+
outputs=[results_output, aspects_output, table_output]
|
| 151 |
+
)
|
| 152 |
+
|
| 153 |
+
# Footer
|
| 154 |
+
gr.HTML("""
|
| 155 |
+
<div style="text-align: center; margin-top: 30px; padding: 20px; border-top: 1px solid #eee;">
|
| 156 |
+
<p><strong>Models Used:</strong></p>
|
| 157 |
+
<p>🔤 Aspect Extraction: <code>sdf299/abte-restaurants-distilbert-base-uncased</code></p>
|
| 158 |
+
<p>😊 Sentiment Classification: <code>sdf299/absa-restaurants-distilbert-base-uncased</code></p>
|
| 159 |
+
</div>
|
| 160 |
+
""")
|
| 161 |
+
|
| 162 |
+
return demo
|
| 163 |
+
|
| 164 |
+
if __name__ == "__main__":
|
| 165 |
+
# Create and launch the interface
|
| 166 |
+
demo = create_interface()
|
| 167 |
+
demo.launch(
|
| 168 |
+
share=True, # Creates a public link
|
| 169 |
+
server_name="0.0.0.0", # Makes it accessible from other devices on the network
|
| 170 |
+
server_port=7860,
|
| 171 |
+
show_error=True
|
| 172 |
+
)
|
app_spaces.py
ADDED
|
@@ -0,0 +1,171 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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+
|
| 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 analyze_sentiment(sentence):
|
| 21 |
+
"""
|
| 22 |
+
Perform aspect-based sentiment analysis on the input sentence.
|
| 23 |
+
|
| 24 |
+
Args:
|
| 25 |
+
sentence (str): Input sentence to analyze
|
| 26 |
+
|
| 27 |
+
Returns:
|
| 28 |
+
tuple: (formatted_results, aspects_summary, detailed_dataframe)
|
| 29 |
+
"""
|
| 30 |
+
if not sentence.strip():
|
| 31 |
+
return "Please enter a sentence to analyze.", "", pd.DataFrame()
|
| 32 |
+
|
| 33 |
+
try:
|
| 34 |
+
# Extract aspects using token classifier
|
| 35 |
+
results = token_classifier(sentence)
|
| 36 |
+
|
| 37 |
+
if not results:
|
| 38 |
+
return "No aspects found in the sentence.", "", pd.DataFrame()
|
| 39 |
+
|
| 40 |
+
# Get unique aspects
|
| 41 |
+
aspects = list(set([result['word'] for result in results]))
|
| 42 |
+
|
| 43 |
+
# Analyze sentiment for each aspect
|
| 44 |
+
detailed_results = []
|
| 45 |
+
formatted_output = f"**Input Sentence:** {sentence}\n\n**Analysis Results:**\n\n"
|
| 46 |
+
|
| 47 |
+
for aspect in aspects:
|
| 48 |
+
# Classify sentiment for this aspect
|
| 49 |
+
sentiment_result = classifier(f'{sentence} [SEP] {aspect}')
|
| 50 |
+
|
| 51 |
+
# Extract sentiment label and confidence
|
| 52 |
+
sentiment_label = sentiment_result[0]['label']
|
| 53 |
+
confidence = sentiment_result[0]['score']
|
| 54 |
+
|
| 55 |
+
# Format the result
|
| 56 |
+
formatted_output += f"🎯 **Aspect:** {aspect}\n"
|
| 57 |
+
formatted_output += f" **Sentiment:** {sentiment_label} (Confidence: {confidence:.3f})\n\n"
|
| 58 |
+
|
| 59 |
+
# Store for dataframe
|
| 60 |
+
detailed_results.append({
|
| 61 |
+
'Aspect': aspect,
|
| 62 |
+
'Sentiment': sentiment_label,
|
| 63 |
+
'Confidence': f"{confidence:.3f}"
|
| 64 |
+
})
|
| 65 |
+
|
| 66 |
+
# Create summary
|
| 67 |
+
aspects_summary = f"**Identified Aspects:** {', '.join(aspects)}"
|
| 68 |
+
|
| 69 |
+
# Create dataframe for tabular view
|
| 70 |
+
df = pd.DataFrame(detailed_results)
|
| 71 |
+
|
| 72 |
+
return formatted_output, aspects_summary, df
|
| 73 |
+
|
| 74 |
+
except Exception as e:
|
| 75 |
+
error_msg = f"Error during analysis: {str(e)}"
|
| 76 |
+
return error_msg, "", pd.DataFrame()
|
| 77 |
+
|
| 78 |
+
# Create the Gradio interface
|
| 79 |
+
with gr.Blocks(
|
| 80 |
+
title="🍽️ Restaurant Review Analyzer - ABSA",
|
| 81 |
+
theme=gr.themes.Soft(),
|
| 82 |
+
css="""
|
| 83 |
+
.gradio-container {
|
| 84 |
+
font-family: 'Arial', sans-serif;
|
| 85 |
+
max-width: 1200px;
|
| 86 |
+
}
|
| 87 |
+
.main-header {
|
| 88 |
+
text-align: center;
|
| 89 |
+
margin-bottom: 30px;
|
| 90 |
+
}
|
| 91 |
+
"""
|
| 92 |
+
) as demo:
|
| 93 |
+
|
| 94 |
+
gr.HTML("""
|
| 95 |
+
<div class="main-header">
|
| 96 |
+
<h1>🍽️ Restaurant Review Analyzer</h1>
|
| 97 |
+
<h3>Aspect-Based Sentiment Analysis</h3>
|
| 98 |
+
<p>Analyze restaurant reviews to identify specific aspects (food, service, atmosphere, etc.) and their associated sentiments.</p>
|
| 99 |
+
<p><em>Powered by DistilBERT models fine-tuned on restaurant reviews</em></p>
|
| 100 |
+
</div>
|
| 101 |
+
""")
|
| 102 |
+
|
| 103 |
+
with gr.Row():
|
| 104 |
+
with gr.Column(scale=2):
|
| 105 |
+
# Input section
|
| 106 |
+
sentence_input = gr.Textbox(
|
| 107 |
+
label="Enter Restaurant Review",
|
| 108 |
+
placeholder="e.g., The services here is wonderful, but I hate the food. However, I still love the atmosphere here.",
|
| 109 |
+
lines=3,
|
| 110 |
+
max_lines=5
|
| 111 |
+
)
|
| 112 |
+
|
| 113 |
+
analyze_btn = gr.Button("🔍 Analyze Sentiment", variant="primary", size="lg")
|
| 114 |
+
|
| 115 |
+
# Example sentences
|
| 116 |
+
gr.Examples(
|
| 117 |
+
examples=[
|
| 118 |
+
["The services here is wonderful, but I hate the food. However, I still love the atmosphere here."],
|
| 119 |
+
["The food was amazing and the staff was very friendly, but the restaurant was too noisy."],
|
| 120 |
+
["Great location and delicious pizza, but the service was slow and the prices are too high."],
|
| 121 |
+
["The ambiance is perfect for a romantic dinner, excellent wine selection, but the dessert was disappointing."],
|
| 122 |
+
["Fast service and good value for money, but the food quality could be better."],
|
| 123 |
+
["Excellent sushi and attentive waiters, though the wait time was quite long."],
|
| 124 |
+
["Beautiful decor and reasonable prices, but the pasta was overcooked."]
|
| 125 |
+
],
|
| 126 |
+
inputs=sentence_input,
|
| 127 |
+
label="Try these examples:"
|
| 128 |
+
)
|
| 129 |
+
|
| 130 |
+
with gr.Column(scale=3):
|
| 131 |
+
# Output section
|
| 132 |
+
with gr.Tab("📊 Detailed Results"):
|
| 133 |
+
results_output = gr.Markdown(label="Analysis Results")
|
| 134 |
+
|
| 135 |
+
with gr.Tab("📋 Quick Summary"):
|
| 136 |
+
aspects_output = gr.Markdown(label="Aspects Summary")
|
| 137 |
+
|
| 138 |
+
with gr.Tab("📈 Data Table"):
|
| 139 |
+
table_output = gr.Dataframe(
|
| 140 |
+
label="Results Table",
|
| 141 |
+
headers=["Aspect", "Sentiment", "Confidence"]
|
| 142 |
+
)
|
| 143 |
+
|
| 144 |
+
# Event handlers
|
| 145 |
+
analyze_btn.click(
|
| 146 |
+
fn=analyze_sentiment,
|
| 147 |
+
inputs=[sentence_input],
|
| 148 |
+
outputs=[results_output, aspects_output, table_output]
|
| 149 |
+
)
|
| 150 |
+
|
| 151 |
+
sentence_input.submit(
|
| 152 |
+
fn=analyze_sentiment,
|
| 153 |
+
inputs=[sentence_input],
|
| 154 |
+
outputs=[results_output, aspects_output, table_output]
|
| 155 |
+
)
|
| 156 |
+
|
| 157 |
+
# Footer with model information
|
| 158 |
+
gr.HTML("""
|
| 159 |
+
<div style="text-align: center; margin-top: 30px; padding: 20px; border-top: 1px solid #eee;">
|
| 160 |
+
<p><strong>Models Used:</strong></p>
|
| 161 |
+
<p>🔤 Aspect Extraction: <a href="https://huggingface.co/sdf299/abte-restaurants-distilbert-base-uncased" target="_blank">sdf299/abte-restaurants-distilbert-base-uncased</a></p>
|
| 162 |
+
<p>😊 Sentiment Classification: <a href="https://huggingface.co/sdf299/absa-restaurants-distilbert-base-uncased" target="_blank">sdf299/absa-restaurants-distilbert-base-uncased</a></p>
|
| 163 |
+
<p style="margin-top: 15px; font-size: 0.9em; color: #666;">
|
| 164 |
+
This app demonstrates aspect-based sentiment analysis for restaurant reviews using fine-tuned DistilBERT models.
|
| 165 |
+
</p>
|
| 166 |
+
</div>
|
| 167 |
+
""")
|
| 168 |
+
|
| 169 |
+
# Launch the app
|
| 170 |
+
if __name__ == "__main__":
|
| 171 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio==4.8.0
|
| 2 |
+
transformers==4.35.0
|
| 3 |
+
torch==2.1.0
|
| 4 |
+
pandas==2.1.0
|
| 5 |
+
numpy==1.24.0
|
| 6 |
+
tokenizers==0.14.1
|
requirements_flexible.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio>=4.0.0
|
| 2 |
+
transformers>=4.30.0
|
| 3 |
+
torch>=1.12.0
|
| 4 |
+
pandas>=1.5.0
|
| 5 |
+
numpy>=1.21.0
|
requirements_minimal.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
transformers
|
| 3 |
+
torch
|
| 4 |
+
pandas
|
run.bat
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
@echo off
|
| 2 |
+
echo.
|
| 3 |
+
echo ==========================================
|
| 4 |
+
echo Restaurant Review Analyzer (ABSA)
|
| 5 |
+
echo ==========================================
|
| 6 |
+
echo.
|
| 7 |
+
|
| 8 |
+
REM Check if Python is installed
|
| 9 |
+
python --version >nul 2>&1
|
| 10 |
+
if %errorlevel% neq 0 (
|
| 11 |
+
echo Error: Python is not installed or not in PATH
|
| 12 |
+
echo Please install Python 3.8+ and try again
|
| 13 |
+
pause
|
| 14 |
+
exit /b 1
|
| 15 |
+
)
|
| 16 |
+
|
| 17 |
+
REM Check if requirements are installed
|
| 18 |
+
echo Checking dependencies...
|
| 19 |
+
python -c "import gradio, transformers, pandas" >nul 2>&1
|
| 20 |
+
if %errorlevel% neq 0 (
|
| 21 |
+
echo Installing requirements...
|
| 22 |
+
pip install -r requirements.txt
|
| 23 |
+
if %errorlevel% neq 0 (
|
| 24 |
+
echo Error: Failed to install requirements
|
| 25 |
+
pause
|
| 26 |
+
exit /b 1
|
| 27 |
+
)
|
| 28 |
+
)
|
| 29 |
+
|
| 30 |
+
echo.
|
| 31 |
+
echo Starting the application...
|
| 32 |
+
echo This may take a few minutes on first run (downloading models)
|
| 33 |
+
echo.
|
| 34 |
+
|
| 35 |
+
REM Launch the application
|
| 36 |
+
python app.py
|
| 37 |
+
|
| 38 |
+
pause
|
run.py
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""
|
| 3 |
+
Simple launcher script for the ABSA Gradio application.
|
| 4 |
+
Provides different launch options for various use cases.
|
| 5 |
+
"""
|
| 6 |
+
|
| 7 |
+
import argparse
|
| 8 |
+
import sys
|
| 9 |
+
import os
|
| 10 |
+
|
| 11 |
+
def main():
|
| 12 |
+
parser = argparse.ArgumentParser(description="Launch the ABSA Gradio Application")
|
| 13 |
+
parser.add_argument(
|
| 14 |
+
'--mode',
|
| 15 |
+
choices=['dev', 'prod', 'share'],
|
| 16 |
+
default='dev',
|
| 17 |
+
help='Launch mode: dev (development), prod (production), share (public link)'
|
| 18 |
+
)
|
| 19 |
+
parser.add_argument('--port', type=int, default=7860, help='Port to run the server on')
|
| 20 |
+
parser.add_argument('--host', default='127.0.0.1', help='Host to bind to')
|
| 21 |
+
|
| 22 |
+
args = parser.parse_args()
|
| 23 |
+
|
| 24 |
+
# Import here to avoid loading models during argument parsing
|
| 25 |
+
try:
|
| 26 |
+
from app import create_interface
|
| 27 |
+
print("Loading ABSA models... This may take a few minutes on first run.")
|
| 28 |
+
demo = create_interface()
|
| 29 |
+
|
| 30 |
+
# Configure launch parameters based on mode
|
| 31 |
+
launch_kwargs = {
|
| 32 |
+
'server_port': args.port,
|
| 33 |
+
'show_error': True
|
| 34 |
+
}
|
| 35 |
+
|
| 36 |
+
if args.mode == 'dev':
|
| 37 |
+
launch_kwargs.update({
|
| 38 |
+
'server_name': '127.0.0.1',
|
| 39 |
+
'share': False,
|
| 40 |
+
'debug': True
|
| 41 |
+
})
|
| 42 |
+
print(f"🚀 Starting in DEVELOPMENT mode on http://127.0.0.1:{args.port}")
|
| 43 |
+
|
| 44 |
+
elif args.mode == 'prod':
|
| 45 |
+
launch_kwargs.update({
|
| 46 |
+
'server_name': '0.0.0.0',
|
| 47 |
+
'share': False,
|
| 48 |
+
'debug': False
|
| 49 |
+
})
|
| 50 |
+
print(f"🚀 Starting in PRODUCTION mode on http://0.0.0.0:{args.port}")
|
| 51 |
+
|
| 52 |
+
elif args.mode == 'share':
|
| 53 |
+
launch_kwargs.update({
|
| 54 |
+
'server_name': '0.0.0.0',
|
| 55 |
+
'share': True,
|
| 56 |
+
'debug': False
|
| 57 |
+
})
|
| 58 |
+
print("🚀 Starting with PUBLIC LINK (share=True)")
|
| 59 |
+
print("⚠️ The public link will be accessible from anywhere on the internet!")
|
| 60 |
+
|
| 61 |
+
# Launch the application
|
| 62 |
+
demo.launch(**launch_kwargs)
|
| 63 |
+
|
| 64 |
+
except ImportError as e:
|
| 65 |
+
print(f"❌ Error importing required modules: {e}")
|
| 66 |
+
print("💡 Make sure you've installed the requirements: pip install -r requirements.txt")
|
| 67 |
+
sys.exit(1)
|
| 68 |
+
except Exception as e:
|
| 69 |
+
print(f"❌ Error starting the application: {e}")
|
| 70 |
+
sys.exit(1)
|
| 71 |
+
|
| 72 |
+
if __name__ == "__main__":
|
| 73 |
+
main()
|
run.sh
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/bin/bash
|
| 2 |
+
|
| 3 |
+
echo ""
|
| 4 |
+
echo "=========================================="
|
| 5 |
+
echo " Restaurant Review Analyzer (ABSA)"
|
| 6 |
+
echo "=========================================="
|
| 7 |
+
echo ""
|
| 8 |
+
|
| 9 |
+
# Check if Python is installed
|
| 10 |
+
if ! command -v python3 &> /dev/null && ! command -v python &> /dev/null; then
|
| 11 |
+
echo "Error: Python is not installed"
|
| 12 |
+
echo "Please install Python 3.8+ and try again"
|
| 13 |
+
exit 1
|
| 14 |
+
fi
|
| 15 |
+
|
| 16 |
+
# Use python3 if available, otherwise python
|
| 17 |
+
PYTHON_CMD="python3"
|
| 18 |
+
if ! command -v python3 &> /dev/null; then
|
| 19 |
+
PYTHON_CMD="python"
|
| 20 |
+
fi
|
| 21 |
+
|
| 22 |
+
# Check Python version
|
| 23 |
+
PYTHON_VERSION=$($PYTHON_CMD --version 2>&1 | awk '{print $2}')
|
| 24 |
+
echo "Using Python $PYTHON_VERSION"
|
| 25 |
+
|
| 26 |
+
# Check if requirements are installed
|
| 27 |
+
echo "Checking dependencies..."
|
| 28 |
+
$PYTHON_CMD -c "import gradio, transformers, pandas" 2>/dev/null
|
| 29 |
+
if [ $? -ne 0 ]; then
|
| 30 |
+
echo "Installing requirements..."
|
| 31 |
+
pip install -r requirements.txt
|
| 32 |
+
if [ $? -ne 0 ]; then
|
| 33 |
+
echo "Error: Failed to install requirements"
|
| 34 |
+
echo "You may need to use pip3 instead of pip"
|
| 35 |
+
exit 1
|
| 36 |
+
fi
|
| 37 |
+
fi
|
| 38 |
+
|
| 39 |
+
echo ""
|
| 40 |
+
echo "Starting the application..."
|
| 41 |
+
echo "This may take a few minutes on first run (downloading models)"
|
| 42 |
+
echo ""
|
| 43 |
+
|
| 44 |
+
# Launch the application
|
| 45 |
+
$PYTHON_CMD app.py
|