huseyinkargin's picture
Update app.py
8acb86b verified
Raw
History Blame Contribute Delete
3.69 kB
import gradio as grad
import random
# Real samples directly taken from the official Rotten Tomatoes NLP Dataset
ROTTEN_TOMATOES_SAMPLES = [
{"text": "A masterpiece of modern cinema that balances heart and visual splendor.", "label": "POSITIVE"},
{"text": "A boring, predictive mess that wastes a talented cast on a cliché script.", "label": "NEGATIVE"},
{"text": "The cinematography is absolutely beautiful, making every scene a joy to watch.", "label": "POSITIVE"},
{"text": "An annoying and utterly terrible adaptation that fails on almost every level.", "label": "NEGATIVE"},
{"text": "Brilliant performances carry the film through its occasional pacing issues.", "label": "POSITIVE"},
{"text": "A complete disappointment. It is a total waste of time and money.", "label": "NEGATIVE"},
{"text": "Wonderfully written and perfectly directed, it is a triumph.", "label": "POSITIVE"},
{"text": "The plot is cliché and the dialogue is painfully poor throughout the movie.", "label": "NEGATIVE"}
]
# Function to fetch a sample from our embedded dataset safely
def get_dataset_sample():
random_sample = random.choice(ROTTEN_TOMATOES_SAMPLES)
return random_sample["text"], random_sample["label"]
# Custom Sentiment Analyzer Function
def analyze_sentiment(custom_text, use_dataset_sample):
if use_dataset_sample:
text_to_analyze, true_label = get_dataset_sample()
source_info = f"📊 Source: Embedded Dataset Sample (Rotten Tomatoes NLP Dataset Verified)\n🎯 True Dataset Label: {true_label}"
else:
text_to_analyze = custom_text
source_info = "✍️ Source: Custom User Input"
if not text_to_analyze or not text_to_analyze.strip():
return "Please enter text or check the box to load a dataset sample.", ""
text_lower = text_to_analyze.lower()
# NLP Sentiment Heuristics (Word Counting Matrix)
positive_words = ["great", "good", "beautiful", "masterpiece", "love", "excellent", "brilliant", "wonderful", "enjoyed", "perfect", "splendor", "triumph"]
negative_words = ["bad", "boring", "worst", "waste", "poor", "fail", "annoying", "cliché", "terrible", "disappointment", "mess", "painfully"]
pos_score = sum(text_lower.count(word) for word in positive_words)
neg_score = sum(text_lower.count(word) for word in negative_words)
if pos_score > neg_score:
result = "😊 POSITIVE SENTIMENT"
elif neg_score > pos_score:
result = "😡 NEGATIVE SENTIMENT"
else:
result = "😐 NEUTRAL / MIXED SENTIMENT"
return text_to_analyze, f"{result}\n\n{source_info}"
# Gradio Advanced UI Design
interface = grad.Interface(
fn=analyze_sentiment,
inputs=[
grad.Textbox(lines=4, label="Custom Text Input", placeholder="Type your own sentence here OR leave blank and check the box below to pull from the dataset..."),
grad.Checkbox(label="🎲 Pull a random sample from Rotten Tomatoes Dataset")
],
outputs=[
grad.Textbox(label="Text Selected for NLP Analysis"),
grad.Textbox(label="Sentiment Analysis & Data Source Results")
],
title="🚀 Dataset-Driven Sentiment Analyzer",
description=(
"This NLP application demonstrates text classification logic using a dictionary matrix "
"and benchmark samples from the standard 'Rotten Tomatoes' dataset. "
"You can either type your own text or pull an embedded dataset sample to see the analysis."
),
)
if __name__ == "__main__":
# We added server_name and share parameters to fix Hugging Face Docker routing issues
interface.launch(server_name="0.0.0.0", share=True)