Kiro0o commited on
Commit
0703a02
1 Parent(s): 8d7f097

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -0
app.py CHANGED
@@ -1,4 +1,24 @@
 
 
 
 
1
  import gradio as gr
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
 
3
  demo = gr.Interface(
4
  theme=gr.themes.Base(),
 
1
+ import torch
2
+ from transformers import AutoModelForSequenceClassification, AutoTokenizer, AutoConfig
3
+ import numpy as np
4
+ from scipy.special import softmax
5
  import gradio as gr
6
+ torch.cuda.is_available()
7
+
8
+ model_path = "cardiffnlp/twitter-roberta-base-sentiment-latest"
9
+
10
+ tokenizer = AutoTokenizer.from_pretrained(model_path)
11
+ config = AutoConfig.from_pretrained(model_path)
12
+ model = AutoModelForSequenceClassification.from_pretrained(model_path)
13
+
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
 
23
  demo = gr.Interface(
24
  theme=gr.themes.Base(),