Kiro0o commited on
Commit
f0226ee
·
verified ·
1 Parent(s): 41b8314

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -0
app.py CHANGED
@@ -1,4 +1,25 @@
 
 
 
 
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
+
15
+ def sentiment_analysis(text):
16
+ encoded_input = tokenizer(text, return_tensors='pt')
17
+ output = model(**encoded_input)
18
+ scores_ = output[0][0].detach().numpy()
19
+ scores_ = softmax(scores_)
20
+ labels = ['Negative', 'Neutral', 'Positive']
21
+ scores = {l: float(s) for (l, s) in zip(labels, scores_)}
22
+ return scores
23
 
24
  demo = gr.Interface(
25
  theme=gr.themes.Base(),