Download app.py from PaulSouvik/RealTime-Text-Classifier-Gradio: direct link, hf CLI and curl.
- Browser
- Download file 1.63 kB
-
https://huggingface.co/spaces/PaulSouvik/RealTime-Text-Classifier-Gradio/resolve/main/app.py
- Command line
-
hf download hf://spaces/PaulSouvik/RealTime-Text-Classifier-Gradio/app.py
-
curl -L -o app.py https://huggingface.co/spaces/PaulSouvik/RealTime-Text-Classifier-Gradio/resolve/main/app.py
1.63 kB
| import gradio as gr | |
| from transformers import pipeline | |
| # Load the sentiment classification model | |
| classifier = pipeline("text-classification", model="distilbert-base-uncased-finetuned-sst-2-english") | |
| # Define the function for Gradio interface | |
| def classify_sentiment(text): | |
| if not text.strip(): | |
| return "β οΈ Please enter a sentence to analyze.", "" | |
| result = classifier(text)[0] | |
| label = result['label'] | |
| confidence = round(result['score'] * 100, 2) | |
| return f"**Prediction:** {label}", f"**Confidence:** {confidence}%" | |
| # Create Gradio interface | |
| with gr.Blocks() as demo: | |
| gr.Markdown("# π Real-Time Sentiment Classifier") | |
| gr.Markdown(""" | |
| ### π‘ Model Instruction: | |
| This model is trained for binary sentiment classification β **Positive** and **Negative** only. | |
| Neutral or mixed opinions may be interpreted as leaning toward one side. | |
| For best results, input clearly positive or negative sentences. | |
| """) | |
| with gr.Row(): | |
| with gr.Column(): | |
| text_input = gr.Textbox( | |
| label="Enter your sentence below π", | |
| placeholder='Example: "I love the features of this app!" or "The update ruined the experience."', | |
| lines=3 | |
| ) | |
| analyze_button = gr.Button("Analyze Sentiment") | |
| with gr.Column(): | |
| prediction_output = gr.Markdown() | |
| confidence_output = gr.Markdown() | |
| analyze_button.click(fn=classify_sentiment, inputs=text_input, outputs=[prediction_output, confidence_output]) | |
| # Launch the app | |
| demo.launch() | |