Spaces:
Sleeping
Sleeping
| # Step 1: Import the necessary toolkits | |
| # We need 'gradio' to build the web app and 'transformers' to use the Hugging Face model. | |
| import gradio as gr | |
| from transformers import pipeline | |
| # Step 2: Load our AI Model | |
| # We create a 'pipeline' which is a simple way to use a pre-trained model. | |
| # We tell it the task ("sentiment-analysis") and the specific model we chose. | |
| # The first time the app runs, it will download the model. This might take a minute. | |
| print("Loading model...") | |
| sentiment_pipeline = pipeline("sentiment-analysis", model="distilbert-base-uncased-finetuned-sst-2-english") | |
| print("Model loaded!") | |
| # Step 3: Define the function for the app | |
| # This function will take some text as input and use the model to predict the sentiment. | |
| def analyze_sentiment(text): | |
| result = sentiment_pipeline(text) | |
| # The model returns a label ('POSITIVE' or 'NEGATIVE') and a confidence score. | |
| # We'll return the whole result so we can see both. | |
| return result | |
| # Step 4: Create the Gradio Web App Interface | |
| # This is where we design the look and feel of our app. | |
| app = gr.Interface( | |
| fn=analyze_sentiment, | |
| inputs=gr.Textbox(placeholder="Enter a sentence here..."), | |
| outputs="json", # We'll use a JSON output to see the label and score clearly | |
| title="Sentiment Analyzer", | |
| description="Type in a sentence to see if its sentiment is POSITIVE or NEGATIVE. This app uses a DistilBERT model from Hugging Face.", | |
| examples=[ | |
| ["The new Star Wars movie was incredible!"], | |
| ["I am not happy with the customer service."], | |
| ["The weather today is just okay."] | |
| ] | |
| ) | |
| # Step 5: Launch the app! | |
| app.launch() |