dgotlieb commited on
Commit
742d276
·
verified ·
1 Parent(s): 6ee7a30

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -0
app.py ADDED
@@ -0,0 +1,32 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import pipeline
3
+
4
+ # Load a popular, stable sentiment analysis model
5
+ sentiment_pipe = pipeline("sentiment-analysis", model="distilbert-base-uncased-finetuned-sst-2-english")
6
+
7
+ def analyze_sentiment(text):
8
+ """
9
+ This function takes text and returns its sentiment (Positive/Negative) and a confidence score.
10
+ """
11
+ try:
12
+ # The pipeline returns a list with one dictionary, e.g., [{'label': 'POSITIVE', 'score': 0.99}]
13
+ result = sentiment_pipe(text)[0]
14
+ label = result['label']
15
+ score = result['score']
16
+
17
+ # Format the output into a nice, readable string
18
+ return f"Sentiment: {label} (Confidence Score: {score:.2f})"
19
+ except Exception as e:
20
+ return f"An error occurred: {str(e)}"
21
+
22
+ # Create the user interface for the app
23
+ iface = gr.Interface(
24
+ fn=analyze_sentiment,
25
+ inputs=gr.Textbox(lines=5, label="Enter Your Text Here", placeholder="e.g., I love this product, but the delivery was a bit slow."),
26
+ outputs=gr.Textbox(label="Analysis Result"),
27
+ title="Course Demo: AI Sentiment Analyzer",
28
+ description="A simple tool to analyze the sentiment of a piece of text. Built for the 'Understanding AI' course."
29
+ )
30
+
31
+ # Launch the app
32
+ iface.launch()