Spaces:
Running
Running
Commit
·
5dcf9d6
1
Parent(s):
a755db8
Update
Browse files- backend/app.py +19 -2
- backend/model_histo.py +4 -0
backend/app.py
CHANGED
|
@@ -352,13 +352,17 @@ try:
|
|
| 352 |
else:
|
| 353 |
print("⚠️ Warning: Hugging Face authentication failed, using local model only.")
|
| 354 |
else:
|
| 355 |
-
print("⚠️ HF_TOKEN not found in environment —
|
|
|
|
| 356 |
|
| 357 |
# Load Path Foundation model
|
|
|
|
| 358 |
if classifier.load_path_foundation():
|
| 359 |
print("✅ Loaded Path Foundation base model.")
|
| 360 |
else:
|
| 361 |
-
print("⚠️ Could not load Path Foundation base model
|
|
|
|
|
|
|
| 362 |
|
| 363 |
# Load trained histopathology model
|
| 364 |
model_path = "histopathology_trained_model.keras"
|
|
@@ -367,10 +371,13 @@ try:
|
|
| 367 |
print(f"✅ Loaded local histopathology model: {model_path}")
|
| 368 |
else:
|
| 369 |
print(f"⚠️ Model file not found: {model_path}")
|
|
|
|
| 370 |
|
| 371 |
except Exception as e:
|
| 372 |
classifier = None
|
| 373 |
print(f"❌ Error initializing histopathology model: {e}")
|
|
|
|
|
|
|
| 374 |
|
| 375 |
def predict_histopathology(image):
|
| 376 |
if classifier is None:
|
|
@@ -381,7 +388,17 @@ def predict_histopathology(image):
|
|
| 381 |
image = image.convert("RGB")
|
| 382 |
image = image.resize((224, 224))
|
| 383 |
img_array = np.expand_dims(np.array(image).astype("float32") / 255.0, axis=0)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 384 |
embeddings = classifier.extract_embeddings(img_array)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 385 |
prediction_proba = classifier.model.predict(embeddings, verbose=0)[0]
|
| 386 |
predicted_class = int(np.argmax(prediction_proba))
|
| 387 |
class_names = ["Benign", "Malignant"]
|
|
|
|
| 352 |
else:
|
| 353 |
print("⚠️ Warning: Hugging Face authentication failed, using local model only.")
|
| 354 |
else:
|
| 355 |
+
print("⚠️ HF_TOKEN not found in environment — Path Foundation model cannot be loaded.")
|
| 356 |
+
print(" To use histopathology predictions, set the HF_TOKEN environment variable.")
|
| 357 |
|
| 358 |
# Load Path Foundation model
|
| 359 |
+
print("Attempting to load Path Foundation model...")
|
| 360 |
if classifier.load_path_foundation():
|
| 361 |
print("✅ Loaded Path Foundation base model.")
|
| 362 |
else:
|
| 363 |
+
print("⚠️ WARNING: Could not load Path Foundation base model.")
|
| 364 |
+
print(" Histopathology predictions will not work without this model.")
|
| 365 |
+
print(" Set HF_TOKEN environment variable to enable this feature.")
|
| 366 |
|
| 367 |
# Load trained histopathology model
|
| 368 |
model_path = "histopathology_trained_model.keras"
|
|
|
|
| 371 |
print(f"✅ Loaded local histopathology model: {model_path}")
|
| 372 |
else:
|
| 373 |
print(f"⚠️ Model file not found: {model_path}")
|
| 374 |
+
print(" Histopathology predictions will not work without the trained weights.")
|
| 375 |
|
| 376 |
except Exception as e:
|
| 377 |
classifier = None
|
| 378 |
print(f"❌ Error initializing histopathology model: {e}")
|
| 379 |
+
import traceback
|
| 380 |
+
traceback.print_exc()
|
| 381 |
|
| 382 |
def predict_histopathology(image):
|
| 383 |
if classifier is None:
|
|
|
|
| 388 |
image = image.convert("RGB")
|
| 389 |
image = image.resize((224, 224))
|
| 390 |
img_array = np.expand_dims(np.array(image).astype("float32") / 255.0, axis=0)
|
| 391 |
+
|
| 392 |
+
# Check if path_foundation model is loaded
|
| 393 |
+
if classifier.path_foundation is None:
|
| 394 |
+
return {"error": "Histopathology prediction failed: Path Foundation model not loaded. Ensure HF_TOKEN is set and the model can be downloaded."}
|
| 395 |
+
|
| 396 |
embeddings = classifier.extract_embeddings(img_array)
|
| 397 |
+
|
| 398 |
+
# Check if model is loaded
|
| 399 |
+
if classifier.model is None:
|
| 400 |
+
return {"error": "Histopathology prediction failed: Classification model not loaded."}
|
| 401 |
+
|
| 402 |
prediction_proba = classifier.model.predict(embeddings, verbose=0)[0]
|
| 403 |
predicted_class = int(np.argmax(prediction_proba))
|
| 404 |
class_names = ["Benign", "Malignant"]
|
backend/model_histo.py
CHANGED
|
@@ -392,6 +392,10 @@ class BreastCancerClassifier:
|
|
| 392 |
>>> # Process with smaller batch size for memory-constrained environments
|
| 393 |
>>> embeddings = classifier.extract_embeddings(images, batch_size=8)
|
| 394 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
| 395 |
print(f"Extracting embeddings from {len(images)} images...")
|
| 396 |
|
| 397 |
embeddings = []
|
|
|
|
| 392 |
>>> # Process with smaller batch size for memory-constrained environments
|
| 393 |
>>> embeddings = classifier.extract_embeddings(images, batch_size=8)
|
| 394 |
"""
|
| 395 |
+
# Check if model is loaded
|
| 396 |
+
if self.path_foundation is None:
|
| 397 |
+
raise RuntimeError("Path Foundation model is not loaded. Call load_path_foundation() first.")
|
| 398 |
+
|
| 399 |
print(f"Extracting embeddings from {len(images)} images...")
|
| 400 |
|
| 401 |
embeddings = []
|