Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| from transformers import pipeline | |
| # Load the Sentiment Analysis pipeline... | |
| classifier = pipeline( | |
| "sentiment-analysis", | |
| model="distilbert-base-uncased-finetuned-sst-2-english" | |
| ) | |
| # Define the prediction function... | |
| def sentiment_predictor(text): | |
| if not text: | |
| return "Please enter some text.", 0.0 | |
| result = classifier(text)[0] | |
| label = result['label'] | |
| score = result['score'] | |
| output_text = f"Predicted Sentiment: **{label}**" | |
| return output_text, score | |
| # Create the Gradio Interface | |
| iface = gr.Interface( | |
| fn=sentiment_predictor, | |
| inputs=gr.Textbox(lines=5, placeholder="Type a sentence here...", label="Enter Text for Sentiment Analysis"), | |
| outputs=[ | |
| gr.Markdown(label="Analysis Result"), | |
| gr.Number(label="Confidence Score") | |
| ], | |
| title="🤗 Simple Sentiment Analyzer on Hugging Face Spaces", | |
| description="A demonstration of deploying a DistilBERT-based model for sentiment classification using Gradio and Hugging Face Spaces. Type in any sentence and see the prediction!", | |
| # The allow_flagging argument is now obsolete and removed. | |
| ) | |
| # Launch the interface | |
| iface.launch() |