Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -47,14 +47,14 @@ def predict(text: str, top_k: int = 5):
|
|
| 47 |
}
|
| 48 |
)
|
| 49 |
|
| 50 |
-
#
|
| 51 |
-
|
| 52 |
-
scores = np.exp(logits) / np.sum(np.exp(logits)) # softmax to probabilities
|
| 53 |
|
| 54 |
-
# Top-k
|
| 55 |
-
|
| 56 |
-
|
| 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,
|
| 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
|
|
|
|
| 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)
|