afkdark commited on
Commit
da3b2ee
·
verified ·
1 Parent(s): 5f2d0aa

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -16
app.py CHANGED
@@ -1,26 +1,22 @@
1
- import gradio as gr
2
  from transformers import pipeline
3
- # Load model directly
4
- from transformers import AutoTokenizer, AutoModel
5
-
6
- tokenizer = AutoTokenizer.from_pretrained("sentence-transformers/all-MiniLM-L6-v2")
7
- model = AutoModel.from_pretrained("sentence-transformers/all-MiniLM-L6-v2")
8
 
9
- # Load Sentiment Analysis model
10
  sentiment_pipeline = pipeline("sentiment-analysis")
11
 
 
12
  def analyze_sentiment(text):
13
- result = sentiment_pipeline(text)[0]
14
- return f"Sentiment: {result['label']} (Confidence: {result['score']:.2f})"
15
 
16
- # Create Gradio Interface
17
  iface = gr.Interface(
18
  fn=analyze_sentiment,
19
- inputs=gr.Textbox(lines=2, placeholder="Enter text here..."),
20
- outputs="text",
21
- title="Sentiment Analysis API",
22
- description="Enter a sentence, and the model will predict if it's POSITIVE or NEGATIVE."
23
  )
24
 
25
- # Launch the Gradio app
26
- iface.launch()
 
 
1
  from transformers import pipeline
2
+ import gradio as gr
 
 
 
 
3
 
4
+ # Load pre-trained sentiment analysis pipeline
5
  sentiment_pipeline = pipeline("sentiment-analysis")
6
 
7
+ # Function to analyze sentiment
8
  def analyze_sentiment(text):
9
+ results = sentiment_pipeline(text)
10
+ return {result['label']: round(result['score'], 2) for result in results}
11
 
12
+ # Gradio Interface
13
  iface = gr.Interface(
14
  fn=analyze_sentiment,
15
+ inputs=gr.Textbox(lines=2, placeholder="Enter a sentence here..."),
16
+ outputs=gr.Label(num_top_classes=3),
17
+ title="Sentiment Analysis",
18
+ description="Enter a sentence, and the model will classify it as Positive, Negative, or Neutral."
19
  )
20
 
21
+ # Launch the app
22
+ iface.launch()