tarugaru commited on
Commit
8b7f73f
·
verified ·
1 Parent(s): 6333072

Add HTML percentage bars output

Browse files
Files changed (1) hide show
  1. app.py +19 -8
app.py CHANGED
@@ -41,30 +41,41 @@ transform = transforms.Compose([
41
  transforms.Normalize(mean=[0.485, 0.456, 0.406],
42
  std=[0.229, 0.224, 0.225]),
43
  ])
44
-
45
  def predict(image):
46
  if image is None:
47
- return "No image provided."
48
  pil_image = Image.fromarray(image).convert("RGB")
49
  tensor = transform(pil_image).unsqueeze(0)
50
  with torch.no_grad():
51
  outputs = model(tensor)
52
  probs = torch.softmax(outputs, dim=1)[0]
53
  top_probs, top_idxs = torch.topk(probs, 3)
54
- lines = []
55
  medals = ["🥇", "🥈", "🥉"]
 
 
56
  for i, (p, idx) in enumerate(zip(top_probs, top_idxs)):
57
  style = idx_to_style[idx.item()]
58
- confidence = float(p.item()) * 100
59
- lines.append(f"{medals[i]} {style} — {confidence:.1f}%")
60
- return "\n".join(lines)
 
 
 
 
 
 
 
 
 
 
61
 
62
  demo = gr.Interface(
63
  fn=predict,
64
  inputs=gr.Image(label="Upload your artwork"),
65
- outputs=gr.Text(label="Art Style Predictions"),
66
  title="Art Style Classifier",
67
  description="Upload a photo of your artwork to identify which art style it most resembles.",
68
  )
69
 
70
- demo.launch(server_name="0.0.0.0", server_port=7860)
 
41
  transforms.Normalize(mean=[0.485, 0.456, 0.406],
42
  std=[0.229, 0.224, 0.225]),
43
  ])
 
44
  def predict(image):
45
  if image is None:
46
+ return "<p>No image provided.</p>"
47
  pil_image = Image.fromarray(image).convert("RGB")
48
  tensor = transform(pil_image).unsqueeze(0)
49
  with torch.no_grad():
50
  outputs = model(tensor)
51
  probs = torch.softmax(outputs, dim=1)[0]
52
  top_probs, top_idxs = torch.topk(probs, 3)
53
+
54
  medals = ["🥇", "🥈", "🥉"]
55
+ colors = ["#4f46e5", "#7c3aed", "#a855f7"]
56
+ html = "<div style='font-family:sans-serif;padding:10px'>"
57
  for i, (p, idx) in enumerate(zip(top_probs, top_idxs)):
58
  style = idx_to_style[idx.item()]
59
+ pct = float(p.item()) * 100
60
+ html += f"""
61
+ <div style='margin-bottom:16px'>
62
+ <div style='display:flex;justify-content:space-between;margin-bottom:4px'>
63
+ <span style='font-weight:600'>{medals[i]} {style}</span>
64
+ <span style='color:#666'>{pct:.1f}%</span>
65
+ </div>
66
+ <div style='background:#e5e7eb;border-radius:999px;height:12px'>
67
+ <div style='background:{colors[i]};width:{pct:.1f}%;height:12px;border-radius:999px;transition:width 0.5s'></div>
68
+ </div>
69
+ </div>"""
70
+ html += "</div>"
71
+ return html
72
 
73
  demo = gr.Interface(
74
  fn=predict,
75
  inputs=gr.Image(label="Upload your artwork"),
76
+ outputs=gr.HTML(label="Art Style Predictions"),
77
  title="Art Style Classifier",
78
  description="Upload a photo of your artwork to identify which art style it most resembles.",
79
  )
80
 
81
+ demo.launch(server_name="0.0.0.0", server_port=7860)