import gradio as gr from transformers import pipeline def classifier(sentence): classifier = pipeline( "text-classification", model="AirrStorm/DistilBERT-SST2", tokenizer="AirrStorm/DistilBERT-SST2", device=0 ) label_mapping = {"LABEL_0": "negative", "LABEL_1": "positive"} result = classifier(sentence) predicted_label = label_mapping[result[0]['label']] return predicted_label # Should print "negative" or "positive" examples = [ ["This movie is amazing!"], ["I dislike this product."], ["The food was bland. The texture was fine but the taste was lacking."], ["The book was enjoyable. The story was good but predictable."], ["The movie was boring. The plot was dull and unoriginal."] ] # Define the Gradio Interface demo = gr.Interface( fn=classifier, inputs=gr.Textbox( lines=4, placeholder="Enter a sentence to analyze sentiment (e.g., 'I really liked this product.')", label="Input Text" ), outputs=gr.Textbox( label="Predicted Sentiment" ), title="Sentiment Analysis", description="Classify the sentiment of the input text as positive or negative.", theme="hugging-face", # Optional, you can experiment with other themes like 'huggingface' allow_flagging="never", # Disable flagging if not needed examples=examples # Add examples to make it easier for users ) # Launch the interface demo.launch()