PubAccount commited on
Commit
04840ff
ยท
verified ยท
1 Parent(s): 12a114a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -14
app.py CHANGED
@@ -273,19 +273,22 @@ def run_inference(image, task, dataset_name, h_type, mode_type):
273
  # ๆˆๅŠŸ โ†’ ๆ‰“ๅฐ็ป“ๆžœ่Œƒๅ›ด๏ผŒๅนถๆŠŠ่พ“ๅ‡บๅ›พๅญ˜ๅˆฐ Demo1_result.png
274
  # ๅคฑ่ดฅ โ†’ ๆ‰“ๅฐๅฎŒๆ•ด traceback๏ผŒๆ–นไพฟๅฎšไฝ้”™่ฏฏ
275
  # โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•
276
- _DEMO_IMG_PATH = "Demo1.png"
277
- print(f"\n{'='*60}")
278
- print(f"๐Ÿงช Startup inference test โ€” {_DEMO_IMG_PATH} (device=cpu)")
279
- print(f"{'='*60}")
280
- try:
281
- if not os.path.exists(_DEMO_IMG_PATH):
282
- print(f"โš ๏ธ {_DEMO_IMG_PATH} not found, skipping test.")
283
- else:
 
 
 
 
284
  _test_img = Image.open(_DEMO_IMG_PATH)
285
  print(f" Image size : {_test_img.size}, mode: {_test_img.mode}")
286
 
287
- # ๆŠŠๆจกๅž‹็ป„ไปถ็งปๅˆฐ CPU๏ผˆๆญคๆ—ถๆœฌๆฅๅฐฑๅœจ CPU๏ผŒไป…ๅšๆ˜พๅผ็กฎ่ฎค๏ผ‰
288
- _model.to("cuda")
289
 
290
  _out_img, _info = _run_inference_core(
291
  _model, "cuda",
@@ -300,11 +303,18 @@ try:
300
  print(f" Info : {_info}")
301
  print(f" Saved to : Demo1_result.png")
302
 
303
- except Exception:
304
- print("โŒ Test FAILED โ€” full traceback below:")
305
- traceback.print_exc()
 
 
 
 
 
 
306
 
307
- print(f"{'='*60}\n")
 
308
 
309
 
310
  # โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•
 
273
  # ๆˆๅŠŸ โ†’ ๆ‰“ๅฐ็ป“ๆžœ่Œƒๅ›ด๏ผŒๅนถๆŠŠ่พ“ๅ‡บๅ›พๅญ˜ๅˆฐ Demo1_result.png
274
  # ๅคฑ่ดฅ โ†’ ๆ‰“ๅฐๅฎŒๆ•ด traceback๏ผŒๆ–นไพฟๅฎšไฝ้”™่ฏฏ
275
  # โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•
276
+ @spaces.GPU(duration=120)
277
+ def _startup_gpu_test():
278
+ """ๅฟ…้กปๅŒ…ๅœจ @spaces.GPU ้‡Œ๏ผŒZeroGPU ๆ‰ไผšๅˆ†้…็œŸๅฎž GPU ็ป™่ฟ™ๆฎตไปฃ็ """
279
+ _DEMO_IMG_PATH = "Demo1.png"
280
+ print(f"\n{'='*60}")
281
+ print(f"๐Ÿงช Startup inference test โ€” {_DEMO_IMG_PATH} (device=cuda)")
282
+ print(f"{'='*60}")
283
+ try:
284
+ if not os.path.exists(_DEMO_IMG_PATH):
285
+ print(f"โš ๏ธ {_DEMO_IMG_PATH} not found, skipping test.")
286
+ return
287
+
288
  _test_img = Image.open(_DEMO_IMG_PATH)
289
  print(f" Image size : {_test_img.size}, mode: {_test_img.mode}")
290
 
291
+ _model.to("cuda") # ๅœจ @spaces.GPU ไธŠไธ‹ๆ–‡ๅ†…็งปๅŠจ๏ผŒZeroGPU ๆ‰่ฎคๅพ—
 
292
 
293
  _out_img, _info = _run_inference_core(
294
  _model, "cuda",
 
303
  print(f" Info : {_info}")
304
  print(f" Saved to : Demo1_result.png")
305
 
306
+ except Exception:
307
+ print("โŒ Test FAILED โ€” full traceback below:")
308
+ traceback.print_exc()
309
+
310
+ finally:
311
+ _model.to("cpu") # ็”จๅฎŒๅฝ’่ฟ˜ GPU ๅ†…ๅญ˜
312
+ torch.cuda.empty_cache()
313
+ print(f"{'='*60}\n")
314
+
315
 
316
+ # ่ฐƒ็”จๅฏๅŠจๆต‹่ฏ•๏ผˆๆญคๆ—ถๅทฒๅœจ @spaces.GPU ไธŠไธ‹ๆ–‡ๅ†…๏ผŒGPU ๆญฃๅธธๅฏ็”จ๏ผ‰
317
+ _startup_gpu_test()
318
 
319
 
320
  # โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•