Spaces:
Runtime error
Runtime error
Improve confidence score formatting
Browse files
app.py
CHANGED
|
@@ -8,16 +8,24 @@ MODEL_PATH = "models/best_model.pkl"
|
|
| 8 |
model = joblib.load(MODEL_PATH)
|
| 9 |
|
| 10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
def classify_document(text):
|
| 12 |
if not text or len(text.strip()) < 5:
|
| 13 |
return "Please enter at least 5 characters.", 0.0
|
| 14 |
|
| 15 |
prediction = model.predict([text])[0]
|
| 16 |
|
| 17 |
-
decision_scores = model.decision_function([text])
|
| 18 |
-
|
|
|
|
|
|
|
| 19 |
|
| 20 |
-
return prediction, round(confidence_score,
|
| 21 |
|
| 22 |
|
| 23 |
demo = gr.Interface(
|
|
@@ -29,7 +37,7 @@ demo = gr.Interface(
|
|
| 29 |
),
|
| 30 |
outputs=[
|
| 31 |
gr.Textbox(label="Predicted Category"),
|
| 32 |
-
gr.Number(label="Confidence Score")
|
| 33 |
],
|
| 34 |
title="BBC News Document Classifier",
|
| 35 |
description=(
|
|
@@ -39,7 +47,9 @@ demo = gr.Interface(
|
|
| 39 |
examples=[
|
| 40 |
["The football team won the final match after scoring two goals."],
|
| 41 |
["The company reported strong profits and growth in global markets."],
|
| 42 |
-
["New software updates improve artificial intelligence performance."]
|
|
|
|
|
|
|
| 43 |
]
|
| 44 |
)
|
| 45 |
|
|
|
|
| 8 |
model = joblib.load(MODEL_PATH)
|
| 9 |
|
| 10 |
|
| 11 |
+
def softmax(scores):
|
| 12 |
+
scores = np.array(scores)
|
| 13 |
+
exp_scores = np.exp(scores - np.max(scores))
|
| 14 |
+
return exp_scores / np.sum(exp_scores)
|
| 15 |
+
|
| 16 |
+
|
| 17 |
def classify_document(text):
|
| 18 |
if not text or len(text.strip()) < 5:
|
| 19 |
return "Please enter at least 5 characters.", 0.0
|
| 20 |
|
| 21 |
prediction = model.predict([text])[0]
|
| 22 |
|
| 23 |
+
decision_scores = model.decision_function([text])[0]
|
| 24 |
+
probabilities = softmax(decision_scores)
|
| 25 |
+
|
| 26 |
+
confidence_score = float(np.max(probabilities)) * 100
|
| 27 |
|
| 28 |
+
return prediction, round(confidence_score, 2)
|
| 29 |
|
| 30 |
|
| 31 |
demo = gr.Interface(
|
|
|
|
| 37 |
),
|
| 38 |
outputs=[
|
| 39 |
gr.Textbox(label="Predicted Category"),
|
| 40 |
+
gr.Number(label="Confidence Score (%)")
|
| 41 |
],
|
| 42 |
title="BBC News Document Classifier",
|
| 43 |
description=(
|
|
|
|
| 47 |
examples=[
|
| 48 |
["The football team won the final match after scoring two goals."],
|
| 49 |
["The company reported strong profits and growth in global markets."],
|
| 50 |
+
["New software updates improve artificial intelligence performance."],
|
| 51 |
+
["The government introduced a new policy during the parliamentary session."],
|
| 52 |
+
["The actor received praise for her performance in the award-winning film."]
|
| 53 |
]
|
| 54 |
)
|
| 55 |
|