File size: 2,178 Bytes
ba92ce2 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 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 |
import gradio as gr
from transformers import pipeline
# Load the sentiment analysis model
model_name = "AventIQ-AI/bert-movie-review-sentiment-analysis"
sentiment_analyzer = pipeline("sentiment-analysis", model=model_name)
# Mapping labels (Adjust based on actual model output)
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']) # Convert label
confidence = round(result['score'] * 100, 2)
emoji = "π" if label == "Positive" else "π"
return f"{emoji} Sentiment: **{label}** (Confidence: {confidence}%)"
# Example movie reviews
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."
]
# Create Gradio UI
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)
# Launch the Gradio app
demo.launch()
|