Kimmilkbreab commited on
Commit
6959cd4
·
verified ·
1 Parent(s): c35f716

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -1
app.py CHANGED
@@ -21,6 +21,17 @@ transform = transforms.Compose([
21
  std=[0.229, 0.224, 0.225])
22
  ])
23
 
 
 
 
 
 
 
 
 
 
 
 
24
  # Predict function
25
  def predict(img):
26
  img = transform(img).unsqueeze(0)
@@ -28,7 +39,7 @@ def predict(img):
28
  outputs = model(img)
29
  probs = torch.softmax(outputs, dim=1)[0]
30
  top5_prob, top5_catid = torch.topk(probs, 5)
31
- return {f"Class {i.item()}": float(top5_prob[j]) for j, i in enumerate(top5_catid)}
32
 
33
  # Gradio interface
34
  demo = gr.Interface(
 
21
  std=[0.229, 0.224, 0.225])
22
  ])
23
 
24
+ # Load class names (expects one name per line, index 0..199)
25
+ try:
26
+ with open("class_names.txt", "r", encoding="utf-8") as f:
27
+ classes = [ln.strip() for ln in f if ln.strip()]
28
+ assert len(classes) == 200, f"Expected 200 classes, got {len(classes)}"
29
+ except Exception as e:
30
+ # Fallback: numbered labels if the file is missing
31
+ print("Warning:", e, "\nUsing numbered class labels.")
32
+ classes = [f"Class {i}" for i in range(200)]
33
+
34
+
35
  # Predict function
36
  def predict(img):
37
  img = transform(img).unsqueeze(0)
 
39
  outputs = model(img)
40
  probs = torch.softmax(outputs, dim=1)[0]
41
  top5_prob, top5_catid = torch.topk(probs, 5)
42
+ return {classes[i.item()]: float(top5_prob[j]) for j, i in enumerate(top5_catid)}
43
 
44
  # Gradio interface
45
  demo = gr.Interface(