dominguezdaniel commited on
Commit
68805b4
1 Parent(s): da17c39

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -8
app.py CHANGED
@@ -1,22 +1,29 @@
1
- from transformers import pipeline
2
  import gradio as gr
 
 
 
 
 
3
 
4
- # Load the sentiment analysis model
5
- sentiment_analysis = pipeline("sentiment-analysis")
6
 
7
- # Define the prediction function
8
  def predict_sentiment(text):
9
- result = sentiment_analysis(text)[0]
 
 
 
 
10
  label = result['label']
11
  confidence = round(result['score'], 4)
12
  return f"Sentiment: {label}, Confidence: {confidence}"
13
 
14
  # Create a Gradio interface
15
  interface = gr.Interface(fn=predict_sentiment,
16
- inputs=gr.inputs.Textbox(lines=2, placeholder="Type your text here..."),
17
  outputs="text",
18
- title="Text Sentiment Analysis",
19
- description="This tool predicts the sentiment of the entered text. Sentiment can be positive, negative, or neutral.")
20
 
21
  # Launch the application
22
  interface.launch()
 
 
1
  import gradio as gr
2
+ from transformers import pipeline
3
+
4
+ # Specify the model and revision explicitly
5
+ model_name = "distilbert-base-uncased-finetuned-sst-2-english"
6
+ revision = "af0f99b"
7
 
8
+ # Load the sentiment analysis pipeline with the specified model and revision
9
+ sentiment_pipeline = pipeline("sentiment-analysis", model=model_name, revision=revision)
10
 
 
11
  def predict_sentiment(text):
12
+ """
13
+ Predicts the sentiment of the input text.
14
+ Returns the label (POSITIVE/NEGATIVE) and the confidence score.
15
+ """
16
+ result = sentiment_pipeline(text)[0]
17
  label = result['label']
18
  confidence = round(result['score'], 4)
19
  return f"Sentiment: {label}, Confidence: {confidence}"
20
 
21
  # Create a Gradio interface
22
  interface = gr.Interface(fn=predict_sentiment,
23
+ inputs=gr.inputs.Textbox(lines=2, placeholder="Enter Text Here..."),
24
  outputs="text",
25
+ title="Simple Text Sentiment Analysis",
26
+ description="A simple text sentiment analysis tool using Hugging Face's transformers.")
27
 
28
  # Launch the application
29
  interface.launch()