SakshamSna commited on
Commit
e4f2a3f
·
1 Parent(s): e8d9db4

updated the error

Browse files
Files changed (1) hide show
  1. app.py +20 -20
app.py CHANGED
@@ -1,32 +1,32 @@
1
  from transformers import pipeline
2
  import gradio as gr
3
 
4
- # 1. Load the sentiment-analysis pipeline
5
- sentiment = pipeline("sentiment-analysis")
6
 
7
- # 2. Define inference function
8
  def analyze_sentiment(text):
9
- """
10
- Takes input text and returns the
11
- predicted label and confidence.
12
- """
13
- result = sentiment(text)[0]
14
- label = result["label"]
15
- score = result["score"]
16
- return {"label": label, "confidence": f"{score:.2f}"}
 
17
 
18
- # 3. Build Gradio interface
19
  iface = gr.Interface(
20
  fn=analyze_sentiment,
21
- inputs=gr.Textbox(lines=4, placeholder="Enter some text..."),
22
  outputs=[
23
- gr.Label(num_top_classes=2, label="Sentiment"),
24
- gr.Textbox(label="Confidence")
25
  ],
26
- title="🤗 Sentiment Analyzer",
27
- description="Enter text and get back its sentiment (POSITIVE/NEGATIVE) with confidence score."
28
  )
29
 
30
- # 4. Launch app
31
- if __name__ == "__main__":
32
- iface.launch()
 
1
  from transformers import pipeline
2
  import gradio as gr
3
 
4
+ # Load sentiment analysis model
5
+ classifier = pipeline("sentiment-analysis")
6
 
7
+ # Define the function Gradio will call
8
  def analyze_sentiment(text):
9
+ if not text:
10
+ return "No input", 0
11
+ try:
12
+ result = classifier(text)[0] # should return a dict
13
+ label = result.get('label', 'Unknown')
14
+ score = round(result.get('score', 0), 3)
15
+ return label, score
16
+ except Exception as e:
17
+ return f"Error: {str(e)}", 0
18
 
19
+ # Define the Gradio Interface
20
  iface = gr.Interface(
21
  fn=analyze_sentiment,
22
+ inputs=gr.Textbox(lines=2, placeholder="Enter text here..."),
23
  outputs=[
24
+ gr.Textbox(label="Sentiment"),
25
+ gr.Number(label="Confidence Score")
26
  ],
27
+ title="Sentiment Analyzer",
28
+ description="Enter a sentence to get its sentiment (POSITIVE/NEGATIVE) and confidence score."
29
  )
30
 
31
+ # Run the app
32
+ iface.launch()