Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -247,21 +247,28 @@ def container_memory():
|
|
| 247 |
|
| 248 |
@app.post("/predict/contracts_clauses", response_model=ClassificationResult)
|
| 249 |
def predict_contracts_clauses(req: TextRequest):
|
|
|
|
| 250 |
# The SetFit model predicts labels directly (no integer conversion needed)
|
| 251 |
-
preds =
|
| 252 |
label = preds[0] # Already a string like 'terms'
|
| 253 |
|
| 254 |
# Try to get a confidence score using predict_proba if available
|
| 255 |
score = 1.0
|
| 256 |
-
if hasattr(
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 265 |
|
| 266 |
return ClassificationResult(label=label, score=round(float(score), 4))
|
| 267 |
|
|
|
|
| 247 |
|
| 248 |
@app.post("/predict/contracts_clauses", response_model=ClassificationResult)
|
| 249 |
def predict_contracts_clauses(req: TextRequest):
|
| 250 |
+
model = models["contracts_clauses"]
|
| 251 |
# The SetFit model predicts labels directly (no integer conversion needed)
|
| 252 |
+
preds = model.predict([req.text])
|
| 253 |
label = preds[0] # Already a string like 'terms'
|
| 254 |
|
| 255 |
# Try to get a confidence score using predict_proba if available
|
| 256 |
score = 1.0
|
| 257 |
+
if hasattr(model, "predict_proba"):
|
| 258 |
+
try:
|
| 259 |
+
probs = model.predict_proba([req.text])[0]
|
| 260 |
+
# model.labels stores the label strings in the order expected by predict_proba
|
| 261 |
+
if hasattr(model, "labels") and model.labels is not None:
|
| 262 |
+
# Find the index of the predicted label
|
| 263 |
+
if label in model.labels:
|
| 264 |
+
idx = model.labels.index(label)
|
| 265 |
+
score = probs[idx]
|
| 266 |
+
else:
|
| 267 |
+
score = max(probs)
|
| 268 |
+
else:
|
| 269 |
+
score = max(probs)
|
| 270 |
+
except Exception:
|
| 271 |
+
score = 1.0
|
| 272 |
|
| 273 |
return ClassificationResult(label=label, score=round(float(score), 4))
|
| 274 |
|