[Admin maintenance] Support new ZeroGPU hardware

#16
by multimodalart HF Staff - opened
Files changed (3) hide show
  1. app.py +8 -0
  2. requirements.txt +15 -0
  3. src/util.py +21 -1
app.py CHANGED
@@ -2,6 +2,13 @@
2
  # -*- coding: utf-8 -*-
3
  # @Time : 2023-06-01
4
  # @Author : ashui(Binghui Chen)
 
 
 
 
 
 
 
5
  from sympy import im
6
  from versions import RELEASE_NOTE, VERSION
7
 
@@ -208,6 +215,7 @@ with block:
208
  )
209
 
210
  # user click the image to get points, and show the points on the image
 
211
  def segmentation(img, sel_pix):
212
  # online show seg mask
213
  points = []
 
2
  # -*- coding: utf-8 -*-
3
  # @Time : 2023-06-01
4
  # @Author : ashui(Binghui Chen)
5
+ # `spaces` MUST be imported before torch/cv2/anything that touches CUDA --
6
+ # it monkeypatches torch.cuda so module-scope `.to("cuda")` calls below are
7
+ # safely deferred instead of doing a real (and here, absent) CUDA init on the
8
+ # CPU process. This app previously had no `import spaces` and no
9
+ # `@spaces.GPU` decorator at all, so `mobile_sam = ...to("cuda")` a few lines
10
+ # down triggered `RuntimeError: No CUDA GPUs are available`.
11
+ import spaces
12
  from sympy import im
13
  from versions import RELEASE_NOTE, VERSION
14
 
 
215
  )
216
 
217
  # user click the image to get points, and show the points on the image
218
+ @spaces.GPU
219
  def segmentation(img, sel_pix):
220
  # online show seg mask
221
  points = []
requirements.txt CHANGED
@@ -2,6 +2,21 @@ dashscope
2
  sympy
3
  Pillow==9.5.0
4
  gradio==3.50.2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
  opencv-python
6
  omegaconf
7
  sentencepiece
 
2
  sympy
3
  Pillow==9.5.0
4
  gradio==3.50.2
5
+ # gradio 3.50.2's routes.py calls Starlette's old
6
+ # TemplateResponse(name, context) positional signature. Modern Starlette
7
+ # (bundled with the current ZeroGPU base image) swapped to
8
+ # TemplateResponse(request, name, context) -- under the old call convention
9
+ # the dict `context` lands in the `name` slot, and Jinja2's template cache
10
+ # then tries to hash that dict -> `TypeError: unhashable type: 'dict'` on
11
+ # every request to "/". Pin an older, gradio-3.50.2-compatible Starlette/
12
+ # FastAPI pair to match what this SDK version actually expects.
13
+ starlette==0.27.0
14
+ fastapi==0.104.1
15
+ # fastapi 0.104.1 uses pydantic's old FieldInfo API (field_info.in_); the base
16
+ # image otherwise installs pydantic 2.13.x which removed it -> at Blocks()
17
+ # construction: AttributeError: 'FieldInfo' object has no attribute 'in_'.
18
+ # Pin pydantic to the release fastapi 0.104.1 / gradio 3.50.2 were built against.
19
+ pydantic==2.4.2
20
  opencv-python
21
  omegaconf
22
  sentencepiece
src/util.py CHANGED
@@ -23,7 +23,22 @@ access_key_secret = os.getenv("ACCESS_KEY_SECRET")
23
  bucket_name = os.getenv("BUCKET_NAME")
24
  endpoint = os.getenv("ENDPOINT")
25
 
26
- bucket = oss2.Bucket(oss2.Auth(access_key_id, access_key_secret), endpoint, bucket_name)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27
  oss_path = "ashui"
28
  oss_path_img_gallery = "ashui_img_gallery"
29
 
@@ -55,6 +70,11 @@ def download_images(img_urls, batch_size):
55
  return imgs_pil
56
 
57
  def upload_np_2_oss(input_image, name="cache.png", gallery=False):
 
 
 
 
 
58
  imgByteArr = io.BytesIO()
59
  Image.fromarray(input_image).save(imgByteArr, format="PNG")
60
  imgByteArr = imgByteArr.getvalue()
 
23
  bucket_name = os.getenv("BUCKET_NAME")
24
  endpoint = os.getenv("ENDPOINT")
25
 
26
+ # The original Space depends on a private Alibaba Cloud OSS bucket
27
+ # (ACCESS_KEY_ID / ACCESS_KEY_SECRET / BUCKET_NAME / ENDPOINT secrets) that
28
+ # isn't available here. Without this guard, `oss2.Bucket(...)` raises
29
+ # AttributeError: 'NoneType' object has no attribute 'strip' at import time,
30
+ # crashing the whole app before it ever reaches the (unrelated) CUDA bug this
31
+ # duplicate is meant to repro. Degrade to `bucket = None` instead so import
32
+ # succeeds; only the OSS-upload path (unreachable without those secrets)
33
+ # fails at call time with a clear error.
34
+ if access_key_id and access_key_secret and bucket_name and endpoint:
35
+ bucket = oss2.Bucket(oss2.Auth(access_key_id, access_key_secret), endpoint, bucket_name)
36
+ else:
37
+ logger.error(
38
+ "OSS credentials (ACCESS_KEY_ID/ACCESS_KEY_SECRET/BUCKET_NAME/ENDPOINT) "
39
+ "are not set; image-upload features will be disabled."
40
+ )
41
+ bucket = None
42
  oss_path = "ashui"
43
  oss_path_img_gallery = "ashui_img_gallery"
44
 
 
70
  return imgs_pil
71
 
72
  def upload_np_2_oss(input_image, name="cache.png", gallery=False):
73
+ if bucket is None:
74
+ raise RuntimeError(
75
+ "OSS bucket is not configured (missing ACCESS_KEY_ID/ACCESS_KEY_SECRET/"
76
+ "BUCKET_NAME/ENDPOINT secrets); cannot upload images."
77
+ )
78
  imgByteArr = io.BytesIO()
79
  Image.fromarray(input_image).save(imgByteArr, format="PNG")
80
  imgByteArr = imgByteArr.getvalue()