|
import gradio as gr
|
|
from transformers import pipeline
|
|
|
|
|
|
model_name = "AventIQ-AI/bert-movie-review-sentiment-analysis"
|
|
sentiment_analyzer = pipeline("sentiment-analysis", model=model_name)
|
|
|
|
|
|
label_mapping = {
|
|
"LABEL_0": "Negative",
|
|
"LABEL_1": "Positive"
|
|
}
|
|
|
|
def analyze_sentiment(review_text):
|
|
"""Analyzes the sentiment of a given movie review."""
|
|
if not review_text.strip():
|
|
return "β οΈ Please enter a movie review."
|
|
|
|
result = sentiment_analyzer(review_text)[0]
|
|
label = label_mapping.get(result['label'], result['label'])
|
|
confidence = round(result['score'] * 100, 2)
|
|
|
|
emoji = "π" if label == "Positive" else "π"
|
|
return f"{emoji} Sentiment: **{label}** (Confidence: {confidence}%)"
|
|
|
|
|
|
example_reviews = [
|
|
"This movie was absolutely fantastic! The story was gripping, and the acting was top-notch.",
|
|
"I was really disappointed. The plot was dull, and the characters were not relatable at all.",
|
|
"An entertaining experience with great visuals and a compelling story!",
|
|
"One of the worst movies I've ever seen. Total waste of time."
|
|
]
|
|
|
|
|
|
with gr.Blocks() as demo:
|
|
gr.Markdown("## π¬ Movie Review Sentiment Analysis")
|
|
gr.Markdown("Enter a movie review, and the AI will determine if the sentiment is **positive** or **negative**!")
|
|
|
|
with gr.Row():
|
|
input_text = gr.Textbox(label="βοΈ Enter your movie review:",
|
|
placeholder="Example: 'The movie was thrilling with an amazing plot twist!'")
|
|
|
|
analyze_button = gr.Button("π Analyze Sentiment")
|
|
output_text = gr.Textbox(label="π Sentiment Result:")
|
|
|
|
gr.Markdown("### π₯ Example Reviews")
|
|
example_buttons = [gr.Button(example) for example in example_reviews]
|
|
|
|
for btn in example_buttons:
|
|
btn.click(fn=lambda text=btn.value: text, outputs=input_text)
|
|
|
|
analyze_button.click(analyze_sentiment, inputs=input_text, outputs=output_text)
|
|
|
|
|
|
demo.launch()
|
|
|