import gradio as gr from transformers import pipeline # Load model classifier = pipeline("sentiment-analysis", model="textattack/bert-base-uncased-imdb") def predict(text): if not text.strip(): return "⚠️ Please enter a review.", "", 0 result = classifier(text)[0] label = result["label"].upper() score = round(result["score"], 4) # Map labels properly if label in ["POSITIVE", "LABEL_1"]: sentiment = "Positive" emoji = "😊" else: sentiment = "Negative" emoji = "😡" return f"{emoji} {sentiment}", f"Confidence: {score}", score with gr.Blocks(theme=gr.themes.Soft(), title="IMDb Sentiment Analyzer") as demo: gr.Markdown( """ # 🎬 IMDb Sentiment Analysis (BERT) Analyze movie reviews using a fine-tuned BERT model. """ ) with gr.Row(): with gr.Column(): text_input = gr.Textbox( label="Enter Movie Review", placeholder="Type your review here...", lines=6 ) with gr.Row(): submit_btn = gr.Button("Analyze", variant="primary") clear_btn = gr.Button("Clear") with gr.Column(): output_label = gr.Textbox(label="Prediction") output_conf = gr.Textbox(label="Confidence") confidence_bar = gr.Slider( minimum=0, maximum=1, label="Confidence Score", interactive=False ) gr.Examples( examples=[ "This movie was absolutely amazing, I loved every moment!", "Worst film I have ever seen. Totally waste of time.", "The acting was decent but the story was boring.", "Brilliant direction and outstanding performances!" ], inputs=text_input ) submit_btn.click( fn=predict, inputs=text_input, outputs=[output_label, output_conf, confidence_bar] ) clear_btn.click( fn=lambda: ("", "", 0), inputs=[], outputs=[output_label, output_conf, confidence_bar] ) demo.launch()