tanvir-ishraq commited on
Commit
167829e
1 Parent(s): 594d7aa

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -0
app.py ADDED
@@ -0,0 +1,28 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import onnxruntime as rt
3
+ from transformers import AutoTokenizer
4
+ import torch, json
5
+
6
+ with open("tag_types_encoded.json", "r") as fp:
7
+ encode_tag_types = json.load(fp)
8
+
9
+ tags = list(encode_tag_types.keys())
10
+
11
+ tokenizer = AutoTokenizer.from_pretrained("roberta-base")
12
+
13
+ inf_session = rt.InferenceSession('quote-text-classifier-quantized.onnx')
14
+ input_name = inf_session.get_inputs()[0].name
15
+ output_name = inf_session.get_outputs()[0].name
16
+
17
+ def classify_book_genre(text):
18
+ input_ids = tokenizer(text)['input_ids'][:512]
19
+ logits = inf_session.run([output_name], {input_name: [input_ids]})[0]
20
+ logits = torch.FloatTensor(logits)
21
+ probs = torch.sigmoid(logits)[0]
22
+ return dict(zip(tags, map(float, probs)))
23
+
24
+ label = gr.outputs.Label(num_top_classes=5)
25
+ #interface with i/o
26
+ iface = gr.Interface(fn=classify_book_genre, inputs="text", outputs=label)
27
+ iface.launch(inline=False)
28
+