Bhess7 commited on
Commit
14c852d
·
verified ·
1 Parent(s): 19f6c47

Upload app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +53 -48
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\'t touch when mouth is closed.",
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
- if image is None:
51
- return {"error": "No image provided"}
52
-
53
- # Convert to PIL if needed
54
- if not isinstance(image, Image.Image):
55
- image = Image.fromarray(image)
56
-
57
- image = image.convert("RGB")
58
-
59
- # Preprocess
60
- img_tensor = transform(image).unsqueeze(0)
61
-
62
- # Predict
63
- with torch.no_grad():
64
- outputs = model(img_tensor)
65
- probabilities = F.softmax(outputs, dim=1)[0]
66
- confidence, predicted_idx = torch.max(probabilities, 0)
67
-
68
- predicted_condition = CLASS_NAMES[predicted_idx.item()]
69
- confidence_pct = confidence.item() * 100
70
-
71
- # Get all probabilities
72
- all_probs = {CLASS_NAMES[i]: float(probabilities[i].item() * 100)
73
- for i in range(len(CLASS_NAMES))}
74
-
75
- # Determine recommendation
76
- if predicted_condition == "No Treatment Needed":
77
- recommendation = "not_candidate"
78
- recommendation_text = "Based on the AI analysis, you may not need orthodontic treatment at this time."
79
- elif confidence_pct >= 70:
80
- recommendation = "candidate"
81
- recommendation_text = f"You appear to be a good candidate for orthodontic treatment to address {predicted_condition.lower()}."
82
- else:
83
- recommendation = "requires_evaluation"
84
- recommendation_text = "We recommend scheduling a consultation with an orthodontist for a thorough evaluation."
85
-
86
- return {
87
- "predicted_condition": predicted_condition,
88
- "confidence": round(confidence_pct, 2),
89
- "all_probabilities": {k: round(v, 2) for k, v in all_probs.items()},
90
- "recommendation": recommendation,
91
- "recommendation_text": recommendation_text,
92
- "condition_description": DESCRIPTIONS.get(predicted_condition, ""),
93
- "model_version": "ResNet18_512x512",
94
- "training_accuracy": 72.73
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)