Upload app.py with huggingface_hub
Browse files
app.py
CHANGED
|
@@ -4,6 +4,7 @@ import torch.nn.functional as F
|
|
| 4 |
from torchvision import transforms, models
|
| 5 |
from PIL import Image
|
| 6 |
import json
|
|
|
|
| 7 |
|
| 8 |
# Load model metadata
|
| 9 |
CLASS_NAMES = [
|
|
@@ -23,7 +24,7 @@ DESCRIPTIONS = {
|
|
| 23 |
"Crowding": "Insufficient space causing teeth to overlap or twist.",
|
| 24 |
"Deepbite": "Upper front teeth excessively overlap lower front teeth.",
|
| 25 |
"No Treatment Needed": "Teeth appear to be properly aligned.",
|
| 26 |
-
"Open Bite": "Upper and lower teeth don
|
| 27 |
"Overbite": "Upper front teeth protrude significantly over lower teeth.",
|
| 28 |
"Spacing": "Gaps or spaces between teeth.",
|
| 29 |
"Underbite": "Lower teeth protrude beyond upper teeth."
|
|
@@ -47,52 +48,56 @@ transform = transforms.Compose([
|
|
| 47 |
|
| 48 |
def predict(image):
|
| 49 |
"""Predict orthodontic condition from image"""
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
image
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
|
| 97 |
# Create Gradio interface
|
| 98 |
demo = gr.Interface(
|
|
@@ -106,4 +111,4 @@ demo = gr.Interface(
|
|
| 106 |
)
|
| 107 |
|
| 108 |
if __name__ == "__main__":
|
| 109 |
-
demo.launch()
|
|
|
|
| 4 |
from torchvision import transforms, models
|
| 5 |
from PIL import Image
|
| 6 |
import json
|
| 7 |
+
import traceback
|
| 8 |
|
| 9 |
# Load model metadata
|
| 10 |
CLASS_NAMES = [
|
|
|
|
| 24 |
"Crowding": "Insufficient space causing teeth to overlap or twist.",
|
| 25 |
"Deepbite": "Upper front teeth excessively overlap lower front teeth.",
|
| 26 |
"No Treatment Needed": "Teeth appear to be properly aligned.",
|
| 27 |
+
"Open Bite": "Upper and lower teeth don't touch when mouth is closed.",
|
| 28 |
"Overbite": "Upper front teeth protrude significantly over lower teeth.",
|
| 29 |
"Spacing": "Gaps or spaces between teeth.",
|
| 30 |
"Underbite": "Lower teeth protrude beyond upper teeth."
|
|
|
|
| 48 |
|
| 49 |
def predict(image):
|
| 50 |
"""Predict orthodontic condition from image"""
|
| 51 |
+
try:
|
| 52 |
+
if image is None:
|
| 53 |
+
return {"error": "No image provided"}
|
| 54 |
+
|
| 55 |
+
# Convert to PIL if needed
|
| 56 |
+
if not isinstance(image, Image.Image):
|
| 57 |
+
image = Image.fromarray(image)
|
| 58 |
+
|
| 59 |
+
image = image.convert("RGB")
|
| 60 |
+
|
| 61 |
+
# Preprocess
|
| 62 |
+
img_tensor = transform(image).unsqueeze(0)
|
| 63 |
+
|
| 64 |
+
# Predict
|
| 65 |
+
with torch.no_grad():
|
| 66 |
+
outputs = model(img_tensor)
|
| 67 |
+
probabilities = F.softmax(outputs, dim=1)[0]
|
| 68 |
+
confidence, predicted_idx = torch.max(probabilities, 0)
|
| 69 |
+
|
| 70 |
+
predicted_condition = CLASS_NAMES[predicted_idx.item()]
|
| 71 |
+
confidence_pct = confidence.item() * 100
|
| 72 |
+
|
| 73 |
+
# Get all probabilities
|
| 74 |
+
all_probs = {CLASS_NAMES[i]: float(probabilities[i].item() * 100)
|
| 75 |
+
for i in range(len(CLASS_NAMES))}
|
| 76 |
+
|
| 77 |
+
# Determine recommendation
|
| 78 |
+
if predicted_condition == "No Treatment Needed":
|
| 79 |
+
recommendation = "not_candidate"
|
| 80 |
+
recommendation_text = "Based on the AI analysis, you may not need orthodontic treatment at this time."
|
| 81 |
+
elif confidence_pct >= 70:
|
| 82 |
+
recommendation = "candidate"
|
| 83 |
+
recommendation_text = f"You appear to be a good candidate for orthodontic treatment to address {predicted_condition.lower()}."
|
| 84 |
+
else:
|
| 85 |
+
recommendation = "requires_evaluation"
|
| 86 |
+
recommendation_text = "We recommend scheduling a consultation with an orthodontist for a thorough evaluation."
|
| 87 |
+
|
| 88 |
+
return {
|
| 89 |
+
"predicted_condition": predicted_condition,
|
| 90 |
+
"confidence": round(confidence_pct, 2),
|
| 91 |
+
"all_probabilities": {k: round(v, 2) for k, v in all_probs.items()},
|
| 92 |
+
"recommendation": recommendation,
|
| 93 |
+
"recommendation_text": recommendation_text,
|
| 94 |
+
"condition_description": DESCRIPTIONS.get(predicted_condition, ""),
|
| 95 |
+
"model_version": "ResNet18_512x512",
|
| 96 |
+
"training_accuracy": 72.73
|
| 97 |
+
}
|
| 98 |
+
except Exception as e:
|
| 99 |
+
traceback.print_exc()
|
| 100 |
+
return {"error": str(e), "traceback": traceback.format_exc()}
|
| 101 |
|
| 102 |
# Create Gradio interface
|
| 103 |
demo = gr.Interface(
|
|
|
|
| 111 |
)
|
| 112 |
|
| 113 |
if __name__ == "__main__":
|
| 114 |
+
demo.launch(show_error=True)
|