Saintadlexx commited on
Commit
43bd98a
·
verified ·
1 Parent(s): 4aef15d

Create app.py

Browse files

# 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()

Files changed (1) hide show
  1. app.py +38 -0
app.py ADDED
@@ -0,0 +1,38 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Step 1: Import the necessary toolkits
2
+ # We need 'gradio' to build the web app and 'transformers' to use the Hugging Face model.
3
+ import gradio as gr
4
+ from transformers import pipeline
5
+
6
+ # Step 2: Load our AI Model
7
+ # We create a 'pipeline' which is a simple way to use a pre-trained model.
8
+ # We tell it the task ("sentiment-analysis") and the specific model we chose.
9
+ # The first time the app runs, it will download the model. This might take a minute.
10
+ print("Loading model...")
11
+ sentiment_pipeline = pipeline("sentiment-analysis", model="distilbert-base-uncased-finetuned-sst-2-english")
12
+ print("Model loaded!")
13
+
14
+ # Step 3: Define the function for the app
15
+ # This function will take some text as input and use the model to predict the sentiment.
16
+ def analyze_sentiment(text):
17
+ result = sentiment_pipeline(text)
18
+ # The model returns a label ('POSITIVE' or 'NEGATIVE') and a confidence score.
19
+ # We'll return the whole result so we can see both.
20
+ return result
21
+
22
+ # Step 4: Create the Gradio Web App Interface
23
+ # This is where we design the look and feel of our app.
24
+ app = gr.Interface(
25
+ fn=analyze_sentiment,
26
+ inputs=gr.Textbox(placeholder="Enter a sentence here..."),
27
+ outputs="json", # We'll use a JSON output to see the label and score clearly
28
+ title="Sentiment Analyzer",
29
+ description="Type in a sentence to see if its sentiment is POSITIVE or NEGATIVE. This app uses a DistilBERT model from Hugging Face.",
30
+ examples=[
31
+ ["The new Star Wars movie was incredible!"],
32
+ ["I am not happy with the customer service."],
33
+ ["The weather today is just okay."]
34
+ ]
35
+ )
36
+
37
+ # Step 5: Launch the app!
38
+ app.launch()