sd99 commited on
Commit
f5459a1
·
1 Parent(s): 4de5210

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -0
app.py ADDED
@@ -0,0 +1,26 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import AutoTokenizer, AutoModelForTokenClassification
3
+ import torch
4
+
5
+ description = "Sentiment Analysis :) && :("
6
+ title = "SentBERT"
7
+ examples = [["That ice cream was really bad"], ["Great to meet you!"], ["Hey, there's a snake there"]]
8
+
9
+ class2interpret = {
10
+ 0: 'Positive/Neutral',
11
+ 1: 'Negative'
12
+ }
13
+
14
+ def classify(example):
15
+ tokenizer = DistilBertTokenizer.from_pretrained("distilbert-base-uncased")
16
+ model = DistilBertForSequenceClassification.from_pretrained("distilbert-base-uncased")
17
+ inputs = tokenizer(example, return_tensors="pt")
18
+ with torch.no_grad():
19
+ logits = model(**inputs).logits
20
+
21
+ probs = torch.nn.Softmax(dim=1)(logits).tolist()[0]
22
+
23
+ return {class2interpret[0]: probs[0], class2interpret[1]: probs[1]}, {class2interpret[0]: probs[0], class2interpret[1]: probs[1]}
24
+
25
+ interface = gr.Interface(fn=classify, inputs='text', outputs=['label', 'json'], examples=examples, description=description, title=title)
26
+ interface.launch()