yeager07 commited on
Commit
40ad1c9
·
1 Parent(s): af18dcf

app file updated

Browse files
Files changed (1) hide show
  1. app.py +8 -19
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
- input_names = [inp.name for inp in inf_session.get_inputs()]
18
  output_name = inf_session.get_outputs()[0].name
19
 
20
- # Prediction function returning top N classes
21
- def classify_subjects(abstract: str, top_n: int = 5):
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
- # Handle models with 1 or 2 inputs automatically
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
- # Sort and select top N classes
41
- sorted_indices = torch.argsort(probs, descending=True)[:top_n]
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 with public share link
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
+