Spaces:
Sleeping
Sleeping
IZERE HIRWA Roger
commited on
Commit
·
1cc833a
1
Parent(s):
a457084
ol
Browse files- app.py +5 -2
- static/script.js +13 -2
app.py
CHANGED
@@ -284,7 +284,8 @@ def classify_document():
|
|
284 |
D, I = index.search(np.array([embedding]), k=k)
|
285 |
|
286 |
if len(labels) > 0 and I[0][0] < len(labels):
|
287 |
-
|
|
|
288 |
confidence_threshold = 0.35
|
289 |
|
290 |
best_match = labels[I[0][0]]
|
@@ -292,7 +293,8 @@ def classify_document():
|
|
292 |
|
293 |
for i in range(min(k, len(D[0]))):
|
294 |
if I[0][i] < len(labels):
|
295 |
-
|
|
|
296 |
matches.append({"category": labels[I[0][i]], "similarity": round(sim, 3)})
|
297 |
|
298 |
# Save classified document to SQLite
|
@@ -332,6 +334,7 @@ def classify_document():
|
|
332 |
|
333 |
return jsonify({"error": "Document not recognized"}), 400
|
334 |
except Exception as e:
|
|
|
335 |
return jsonify({"error": str(e)}), 500
|
336 |
|
337 |
@app.route("/api/categories", methods=["GET"])
|
|
|
284 |
D, I = index.search(np.array([embedding]), k=k)
|
285 |
|
286 |
if len(labels) > 0 and I[0][0] < len(labels):
|
287 |
+
# Convert numpy float32 to Python float for JSON serialization
|
288 |
+
similarity = float(1 - D[0][0])
|
289 |
confidence_threshold = 0.35
|
290 |
|
291 |
best_match = labels[I[0][0]]
|
|
|
293 |
|
294 |
for i in range(min(k, len(D[0]))):
|
295 |
if I[0][i] < len(labels):
|
296 |
+
# Convert numpy float32 to Python float
|
297 |
+
sim = float(1 - D[0][i])
|
298 |
matches.append({"category": labels[I[0][i]], "similarity": round(sim, 3)})
|
299 |
|
300 |
# Save classified document to SQLite
|
|
|
334 |
|
335 |
return jsonify({"error": "Document not recognized"}), 400
|
336 |
except Exception as e:
|
337 |
+
print(f"Classification error: {e}")
|
338 |
return jsonify({"error": str(e)}), 500
|
339 |
|
340 |
@app.route("/api/categories", methods=["GET"])
|
static/script.js
CHANGED
@@ -592,11 +592,22 @@ document.getElementById('classifyForm').addEventListener('submit', async (e) =>
|
|
592 |
fileInput.value = '';
|
593 |
document.querySelector('#classifyUpload p').textContent = 'Click to select or drag & drop files here';
|
594 |
loadStats();
|
|
|
595 |
} else {
|
596 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
597 |
}
|
598 |
} catch (error) {
|
599 |
-
|
|
|
600 |
}
|
601 |
});
|
602 |
|
|
|
592 |
fileInput.value = '';
|
593 |
document.querySelector('#classifyUpload p').textContent = 'Click to select or drag & drop files here';
|
594 |
loadStats();
|
595 |
+
loadCategories();
|
596 |
} else {
|
597 |
+
// Handle different error types
|
598 |
+
let errorMessage = result.error || result.detail || 'Classification failed';
|
599 |
+
if (errorMessage.includes('No categories')) {
|
600 |
+
errorMessage = '⚠️ Please add some document categories first before classifying documents.';
|
601 |
+
} else if (errorMessage.includes('Failed to process')) {
|
602 |
+
errorMessage = '❌ Could not process the uploaded file. Please ensure it\'s a valid image or PDF.';
|
603 |
+
} else if (errorMessage.includes('JSON serializable')) {
|
604 |
+
errorMessage = '🔧 Processing error occurred. Please try again.';
|
605 |
+
}
|
606 |
+
showResult(resultDiv, errorMessage, 'error');
|
607 |
}
|
608 |
} catch (error) {
|
609 |
+
console.error('Classification error:', error);
|
610 |
+
showResult(resultDiv, '❌ Network error: Please check your connection and try again.', 'error');
|
611 |
}
|
612 |
});
|
613 |
|