🍽️ Restaurant Review Analyzer
Aspect-Based Sentiment Analysis
Analyze restaurant reviews to identify specific aspects (food, service, atmosphere, etc.) and their associated sentiments.
import gradio as gr import pandas as pd from transformers import pipeline import warnings warnings.filterwarnings("ignore") # Initialize the models print("Loading models...") token_classifier = pipeline( model="sdf299/abte-restaurants-distilbert-base-uncased", aggregation_strategy="simple" ) classifier = pipeline( model="sdf299/absa-restaurants-distilbert-base-uncased" ) print("Models loaded successfully!") def analyze_sentiment(sentence): """ Perform aspect-based sentiment analysis on the input sentence. Args: sentence (str): Input sentence to analyze Returns: tuple: (formatted_results, aspects_summary, detailed_dataframe) """ if not sentence.strip(): return "Please enter a sentence to analyze.", "", pd.DataFrame() try: # Extract aspects using token classifier results = token_classifier(sentence) if not results: return "No aspects found in the sentence.", "", pd.DataFrame() # Get unique aspects aspects = list(set([result['word'] for result in results])) # Analyze sentiment for each aspect detailed_results = [] formatted_output = f"**Input Sentence:** {sentence}\n\n**Analysis Results:**\n\n" for aspect in aspects: # Classify sentiment for this aspect sentiment_result = classifier(f'{sentence} [SEP] {aspect}') # Extract sentiment label and confidence sentiment_label = sentiment_result[0]['label'] confidence = sentiment_result[0]['score'] # Format the result formatted_output += f"🎯 **Aspect:** {aspect}\n" formatted_output += f" **Sentiment:** {sentiment_label} (Confidence: {confidence:.3f})\n\n" # Store for dataframe detailed_results.append({ 'Aspect': aspect, 'Sentiment': sentiment_label, 'Confidence': f"{confidence:.3f}" }) # Create summary aspects_summary = f"**Identified Aspects:** {', '.join(aspects)}" # Create dataframe for tabular view df = pd.DataFrame(detailed_results) return formatted_output, aspects_summary, df except Exception as e: error_msg = f"Error during analysis: {str(e)}" return error_msg, "", pd.DataFrame() def create_interface(): """Create and configure the Gradio interface.""" with gr.Blocks( title="Aspect-Based Sentiment Analysis", theme=gr.themes.Soft(), css=""" .gradio-container { font-family: 'Arial', sans-serif; } .main-header { text-align: center; margin-bottom: 30px; } """ ) as demo: gr.HTML("""
Analyze restaurant reviews to identify specific aspects (food, service, atmosphere, etc.) and their associated sentiments.
Models Used:
🔤 Aspect Extraction: sdf299/abte-restaurants-distilbert-base-uncased
😊 Sentiment Classification: sdf299/absa-restaurants-distilbert-base-uncased