aliokaccound commited on
Commit
fbde8a9
·
verified ·
1 Parent(s): 3479d97

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -8
app.py CHANGED
@@ -47,14 +47,14 @@ def predict(text: str, top_k: int = 5):
47
  }
48
  )
49
 
50
- # Assume outputs[0] contains logits
51
- logits = outputs[0][0] # shape: (num_classes,)
52
- scores = np.exp(logits) / np.sum(np.exp(logits)) # softmax to probabilities
53
 
54
- # Top-k results
55
- top_indices = np.argsort(scores)[::-1][:top_k]
56
- result = [{"label": LABELS[i], "score": float(scores[i])} for i in top_indices]
57
 
 
58
  return result
59
 
60
  # -----------------------------
@@ -63,11 +63,11 @@ def predict(text: str, top_k: int = 5):
63
  iface = gr.Interface(
64
  fn=predict,
65
  inputs=[gr.Textbox(lines=2, placeholder="Type your Bengali text here..."),
66
- gr.Slider(1, 12, value=5, step=1, label="Top K")],
67
  outputs="json",
68
  title="Intent Router ONNX Model",
69
  description="Enter Bengali text to get top_k intent predictions"
70
  )
71
 
72
  if __name__ == "__main__":
73
- iface.launch(server_name="0.0.0.0", server_port=7860, share=True)
 
47
  }
48
  )
49
 
50
+ logits = outputs[0][0] # assume outputs[0] contains logits
51
+ scores = np.exp(logits) / np.sum(np.exp(logits)) # softmax
 
52
 
53
+ # Top-k, ensure it doesn't exceed LABELS length
54
+ safe_top_k = min(top_k, len(LABELS))
55
+ top_indices = np.argsort(scores)[::-1][:safe_top_k]
56
 
57
+ result = [{"label": LABELS[i], "score": float(scores[i])} for i in top_indices]
58
  return result
59
 
60
  # -----------------------------
 
63
  iface = gr.Interface(
64
  fn=predict,
65
  inputs=[gr.Textbox(lines=2, placeholder="Type your Bengali text here..."),
66
+ gr.Slider(1, len(LABELS), value=5, step=1, label="Top K")],
67
  outputs="json",
68
  title="Intent Router ONNX Model",
69
  description="Enter Bengali text to get top_k intent predictions"
70
  )
71
 
72
  if __name__ == "__main__":
73
+ iface.launch(server_name="0.0.0.0", server_port=7860)