Add application file
Browse files
app.py
CHANGED
|
@@ -1,7 +1,44 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
-
|
| 4 |
-
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import onnxruntime as rt
|
| 3 |
+
from transformers import AutoTokenizer
|
| 4 |
+
import torch, json
|
| 5 |
|
| 6 |
+
# Load tokenizer
|
| 7 |
+
tokenizer = AutoTokenizer.from_pretrained("allenai/scibert_scivocab_uncased")
|
| 8 |
|
| 9 |
+
# Load encoded subject dictionary
|
| 10 |
+
with open("subject_types_encoded.json", "r") as fp:
|
| 11 |
+
encode_subject_types = json.load(fp)
|
| 12 |
+
|
| 13 |
+
subjects = list(encode_subject_types.keys())
|
| 14 |
+
|
| 15 |
+
# Load ONNX model
|
| 16 |
+
inf_session = rt.InferenceSession("Scientific Articles-classifier-quantized.onnx")
|
| 17 |
+
input_name = inf_session.get_inputs()[0].name
|
| 18 |
+
output_name = inf_session.get_outputs()[0].name
|
| 19 |
+
|
| 20 |
+
# Prediction function
|
| 21 |
+
def classify_subjects(abstract: str):
|
| 22 |
+
inputs = tokenizer(
|
| 23 |
+
abstract,
|
| 24 |
+
padding="max_length",
|
| 25 |
+
truncation=True,
|
| 26 |
+
max_length=512,
|
| 27 |
+
return_tensors="np"
|
| 28 |
+
)
|
| 29 |
+
logits = inf_session.run([output_name], {input_name: inputs["input_ids"]})[0]
|
| 30 |
+
logits = torch.FloatTensor(logits)
|
| 31 |
+
probs = torch.sigmoid(logits)[0]
|
| 32 |
+
return dict(zip(subjects, map(float, probs)))
|
| 33 |
+
|
| 34 |
+
# Gradio Interface
|
| 35 |
+
label = gr.Label(num_top_classes=10)
|
| 36 |
+
iface = gr.Interface(
|
| 37 |
+
fn=classify_subjects,
|
| 38 |
+
inputs=gr.Textbox(lines=5, placeholder="Enter research paper abstract here..."),
|
| 39 |
+
outputs=label,
|
| 40 |
+
title="Multi-Label Classification of Computer Science Research Articles",
|
| 41 |
+
description="Predict multiple subject categories from computer science research paper abstracts."
|
| 42 |
+
)
|
| 43 |
+
|
| 44 |
+
iface.launch()
|