nzs234 commited on
Commit
f63e025
·
verified ·
1 Parent(s): 73f654d

UX: add real-time status updates and queue for predict action

Browse files
Files changed (1) hide show
  1. app.py +18 -10
app.py CHANGED
@@ -120,29 +120,37 @@ def _ensure_loaded():
120
 
121
  def predict(img: Image.Image):
122
  if img is None:
123
- return "error: no image"
 
 
124
  try:
 
 
125
  _ensure_loaded()
126
  except Exception:
127
- return f"error: model load failed: {_MODEL_ERR}"
 
 
128
  if img.mode != "RGB":
129
  img = img.convert("RGB")
130
  proc = processor(images=img, return_tensors="pt")
131
- with torch.inference_mode():
132
- pred_01 = model(proc["pixel_values"]).item()
133
- pred_01 = max(0.0, min(1.0, float(pred_01)))
134
- pred_score = pred_01 * (score_max - score_min) + score_min
135
- score_int = int(round(pred_score))
136
- score_int = max(int(score_min), min(int(score_max), score_int))
137
- return f"score_{score_int} (raw={pred_score:.4f})"
138
 
139
 
140
  with gr.Blocks() as demo:
141
  gr.Markdown("# SigLIP2 Aesthetic Scorer Demo")
142
  inp = gr.Image(type="pil", label="Image")
143
  out = gr.Textbox(label="Result")
 
144
  btn = gr.Button("Predict")
145
- btn.click(fn=predict, inputs=[inp], outputs=[out])
146
 
147
 
 
148
  demo.launch(server_name="0.0.0.0", server_port=7860)
 
120
 
121
  def predict(img: Image.Image):
122
  if img is None:
123
+ yield "error: no image", "status: please upload image first"
124
+ return
125
+ yield "", "status: starting"
126
  try:
127
+ if not _MODEL_READY:
128
+ yield "", "status: loading model (first run takes longer)"
129
  _ensure_loaded()
130
  except Exception:
131
+ yield f"error: model load failed: {_MODEL_ERR}", "status: failed"
132
+ return
133
+ yield "", "status: model ready, running inference"
134
  if img.mode != "RGB":
135
  img = img.convert("RGB")
136
  proc = processor(images=img, return_tensors="pt")
137
+ with torch.inference_mode():
138
+ pred_01 = model(proc["pixel_values"]).item()
139
+ pred_01 = max(0.0, min(1.0, float(pred_01)))
140
+ pred_score = pred_01 * (score_max - score_min) + score_min
141
+ score_int = int(round(pred_score))
142
+ score_int = max(int(score_min), min(int(score_max), score_int))
143
+ yield f"score_{score_int} (raw={pred_score:.4f})", "status: done"
144
 
145
 
146
  with gr.Blocks() as demo:
147
  gr.Markdown("# SigLIP2 Aesthetic Scorer Demo")
148
  inp = gr.Image(type="pil", label="Image")
149
  out = gr.Textbox(label="Result")
150
+ status = gr.Textbox(label="Status", value="status: idle")
151
  btn = gr.Button("Predict")
152
+ btn.click(fn=predict, inputs=[inp], outputs=[out, status])
153
 
154
 
155
+ demo.queue(default_concurrency_limit=1)
156
  demo.launch(server_name="0.0.0.0", server_port=7860)