Kiro0o commited on
Commit
5675e6d
·
verified ·
1 Parent(s): 28c59c8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -1
app.py CHANGED
@@ -5,4 +5,34 @@ from transformers import AutoModelForSequenceClassification, AutoTokenizer, Auto
5
  import numpy as np
6
  from scipy.special import softmax
7
  import gradio as gr
8
- torch.cuda.is_available()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
  import numpy as np
6
  from scipy.special import softmax
7
  import gradio as gr
8
+ torch.cuda.is_available()
9
+ model_path = "cardiffnlp/twitter-roberta-base-sentiment-latest"
10
+
11
+ tokenizer = AutoTokenizer.from_pretrained(model_path)
12
+ config = AutoConfig.from_pretrained(model_path)
13
+ model = AutoModelForSequenceClassification.from_pretrained(model_path)
14
+ def sentiment_analysis(text):
15
+ encoded_input = tokenizer(text, return_tensors='pt')
16
+ output = model(**encoded_input)
17
+ scores_ = output[0][0].detach().numpy()
18
+ scores_ = softmax(scores_)
19
+ labels = ['Negative', 'Neutral', 'Positive']
20
+ scores = {l: float(s) for (l, s) in zip(labels, scores_)}
21
+ return scores
22
+ demo = gr.Interface(
23
+ theme=gr.themes.Base(),
24
+ fn=sentiment_analysis,
25
+ inputs=gr.Textbox(placeholder="Write your text here..."),
26
+ outputs="label",
27
+ examples=[
28
+ ["I'm thrilled about the job offer!"],
29
+ ["The weather today is absolutely beautiful."],
30
+ ["I had a fantastic time at the concert last night."],
31
+ ["I'm so frustrated with this software glitch."],
32
+ ["The customer service was terrible at the store."],
33
+ ["I'm really disappointed with the quality of this product."]
34
+ ],
35
+ title='Sentiment Analysis App',
36
+ description='This app classifies a positive, neutral, or negative sentiment.'
37
+ )
38
+ demo.launch()