File size: 1,107 Bytes
c7ba2d7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# -*- coding: utf-8 -*-
"""app_creation.ipynb

Automatically generated by Colaboratory.

Original file is located at
    https://colab.research.google.com/drive/1sguK4GohScbDFj7Toyodw7faqhPrfc1a
"""

import json
import torch
import gradio as gr
import onnxruntime as rt
from transformers import AutoTokenizer

tokenizer = AutoTokenizer.from_pretrained("bert-base-uncased")

with open("encoded_keywords.json", "r") as fp:
  encode_keywords = json.load(fp)

keywords = list(encode_keywords.keys())

inf_session = rt.InferenceSession('bert_quantized.onnx')
input_name = inf_session.get_inputs()[0].name
output_name = inf_session.get_outputs()[0].name

def classify_keywords(abstract):
  input_ids = tokenizer(abstract)['input_ids'][:512]

  logits = inf_session.run([output_name], {
      input_name: [input_ids]
  })[0]

  logits = torch.FloatTensor(logits)

  probs = torch.sigmoid(logits)[0]

  return dict(zip(keywords, map(float, probs)))

label = gr.Label(num_top_classes = 5)

interface = gr.Interface(
    fn = classify_keywords,
    inputs = "text",
    outputs = label
  )

interface.launch()