fadiyahalanazi commited on
Commit
7c3b9f2
·
verified ·
1 Parent(s): de8e304

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -7
app.py CHANGED
@@ -1,23 +1,23 @@
1
  import gradio as gr
2
  from transformers import pipeline
3
 
4
-
5
  sentiment_analyzer = pipeline("sentiment-analysis", model="nlptown/bert-base-multilingual-uncased-sentiment")
6
 
7
-
8
  def analyze_sentiment(text):
9
  if len(text.strip()) == 0:
10
  return "Please enter some text for sentiment analysis."
11
 
12
  result = sentiment_analyzer(text)[0]
13
 
14
-
15
- sentiment_label = result['label'].split()[0](
16
- confidence = round(result['score'] * 100, 2)
17
 
18
  return f"⭐ Sentiment: {sentiment_label} Stars (Confidence: {confidence}%)"
19
 
20
- e
21
  iface = gr.Interface(
22
  fn=analyze_sentiment,
23
  inputs=gr.Textbox(lines=3, placeholder="Enter a sentence or paragraph...", label="Input Text"),
@@ -33,5 +33,6 @@ iface = gr.Interface(
33
  allow_flagging="never"
34
  )
35
 
36
-
37
  iface.launch()
 
 
1
  import gradio as gr
2
  from transformers import pipeline
3
 
4
+ # Load the sentiment analysis model
5
  sentiment_analyzer = pipeline("sentiment-analysis", model="nlptown/bert-base-multilingual-uncased-sentiment")
6
 
7
+ # Function to analyze sentiment
8
  def analyze_sentiment(text):
9
  if len(text.strip()) == 0:
10
  return "Please enter some text for sentiment analysis."
11
 
12
  result = sentiment_analyzer(text)[0]
13
 
14
+ # Extract numerical rating from the label (e.g., "5 stars" → "5")
15
+ sentiment_label = result['label'].split()[0] # Extract only the number (1-5)
16
+ confidence = round(result['score'] * 100, 2) # Convert to percentage
17
 
18
  return f"⭐ Sentiment: {sentiment_label} Stars (Confidence: {confidence}%)"
19
 
20
+ # Create the Gradio interface
21
  iface = gr.Interface(
22
  fn=analyze_sentiment,
23
  inputs=gr.Textbox(lines=3, placeholder="Enter a sentence or paragraph...", label="Input Text"),
 
33
  allow_flagging="never"
34
  )
35
 
36
+ # Launch the app
37
  iface.launch()
38
+