app file updated
Browse files
app.py
CHANGED
|
@@ -14,11 +14,11 @@ subjects = list(encode_subject_types.keys())
|
|
| 14 |
|
| 15 |
# Load ONNX model
|
| 16 |
inf_session = rt.InferenceSession("Scientific Articles-classifier-quantized.onnx")
|
| 17 |
-
|
| 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",
|
|
@@ -27,24 +27,12 @@ def classify_subjects(abstract: str, top_n: int = 5):
|
|
| 27 |
return_tensors="np"
|
| 28 |
)
|
| 29 |
|
| 30 |
-
|
| 31 |
-
onnx_inputs = {"input_ids": inputs["input_ids"]}
|
| 32 |
-
if "attention_mask" in input_names:
|
| 33 |
-
onnx_inputs["attention_mask"] = inputs["attention_mask"]
|
| 34 |
-
|
| 35 |
-
# Run inference
|
| 36 |
-
logits = inf_session.run([output_name], onnx_inputs)[0]
|
| 37 |
logits = torch.FloatTensor(logits)
|
| 38 |
probs = torch.sigmoid(logits)[0]
|
| 39 |
|
| 40 |
-
#
|
| 41 |
-
|
| 42 |
-
top_preds = {
|
| 43 |
-
subjects[i]: float(probs[i])
|
| 44 |
-
for i in sorted_indices
|
| 45 |
-
}
|
| 46 |
-
|
| 47 |
-
return top_preds
|
| 48 |
|
| 49 |
# Gradio Interface
|
| 50 |
iface = gr.Interface(
|
|
@@ -64,5 +52,6 @@ iface = gr.Interface(
|
|
| 64 |
allow_flagging="never"
|
| 65 |
)
|
| 66 |
|
| 67 |
-
# Launch
|
| 68 |
iface.launch(share=False)
|
|
|
|
|
|
| 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",
|
|
|
|
| 27 |
return_tensors="np"
|
| 28 |
)
|
| 29 |
|
| 30 |
+
logits = inf_session.run([output_name], {input_name: inputs["input_ids"]})[0]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
logits = torch.FloatTensor(logits)
|
| 32 |
probs = torch.sigmoid(logits)[0]
|
| 33 |
|
| 34 |
+
# Return dictionary of all subjects with probabilities
|
| 35 |
+
return dict(zip(subjects, map(float, probs)))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
# Gradio Interface
|
| 38 |
iface = gr.Interface(
|
|
|
|
| 52 |
allow_flagging="never"
|
| 53 |
)
|
| 54 |
|
| 55 |
+
# Launch app
|
| 56 |
iface.launch(share=False)
|
| 57 |
+
|