hysts HF staff commited on
Commit
b554f48
1 Parent(s): 4d71160
Files changed (2) hide show
  1. app.py +10 -5
  2. model.py +12 -7
app.py CHANGED
@@ -8,6 +8,7 @@ import shlex
8
  import subprocess
9
 
10
  import gradio as gr
 
11
 
12
  if os.getenv('SYSTEM') == 'spaces':
13
  with open('patch') as f:
@@ -43,7 +44,7 @@ from app_scribble_interactive import \
43
  from app_seg import create_demo as create_demo_seg
44
  from model import Model, download_all_controlnet_weights
45
 
46
- DESCRIPTION = '# [ControlNet](https://github.com/lllyasviel/ControlNet) + [Anything-v4.0](https://huggingface.co/andite/anything-v4.0)'
47
 
48
  SPACE_ID = os.getenv('SPACE_ID')
49
  ALLOW_CHANGING_BASE_MODEL = SPACE_ID != 'hysts/ControlNet-with-Anything-v4'
@@ -51,12 +52,16 @@ ALLOW_CHANGING_BASE_MODEL = SPACE_ID != 'hysts/ControlNet-with-Anything-v4'
51
  if SPACE_ID is not None:
52
  DESCRIPTION += f'\n<p>For faster inference without waiting in queue, you may duplicate the space and upgrade to GPU in settings. <a href="https://huggingface.co/spaces/{SPACE_ID}?duplicate=true"><img style="display: inline; margin-top: 0em; margin-bottom: 0em" src="https://bit.ly/3gLdBN6" alt="Duplicate Space" /></a></p>'
53
 
 
 
 
 
 
 
 
54
  MAX_IMAGES = int(os.getenv('MAX_IMAGES', '3'))
55
  DEFAULT_NUM_IMAGES = min(MAX_IMAGES, int(os.getenv('DEFAULT_NUM_IMAGES', '1')))
56
 
57
- if os.getenv('SYSTEM') == 'spaces':
58
- download_all_controlnet_weights()
59
-
60
  DEFAULT_MODEL_ID = os.getenv('DEFAULT_MODEL_ID', 'andite/anything-v4.0')
61
  model = Model(base_model_id=DEFAULT_MODEL_ID, task_name='canny')
62
 
@@ -138,4 +143,4 @@ with gr.Blocks(css='style.css') as demo:
138
  inputs=new_base_model_id,
139
  outputs=current_base_model)
140
 
141
- demo.queue(api_open=False).launch()
8
  import subprocess
9
 
10
  import gradio as gr
11
+ import torch
12
 
13
  if os.getenv('SYSTEM') == 'spaces':
14
  with open('patch') as f:
44
  from app_seg import create_demo as create_demo_seg
45
  from model import Model, download_all_controlnet_weights
46
 
47
+ DESCRIPTION = '# [ControlNet v1.0](https://github.com/lllyasviel/ControlNet) + [Anything-v4.0](https://huggingface.co/andite/anything-v4.0)'
48
 
49
  SPACE_ID = os.getenv('SPACE_ID')
50
  ALLOW_CHANGING_BASE_MODEL = SPACE_ID != 'hysts/ControlNet-with-Anything-v4'
52
  if SPACE_ID is not None:
53
  DESCRIPTION += f'\n<p>For faster inference without waiting in queue, you may duplicate the space and upgrade to GPU in settings. <a href="https://huggingface.co/spaces/{SPACE_ID}?duplicate=true"><img style="display: inline; margin-top: 0em; margin-bottom: 0em" src="https://bit.ly/3gLdBN6" alt="Duplicate Space" /></a></p>'
54
 
55
+ if torch.cuda.is_available():
56
+ DESCRIPTION += '\n<p>Running on GPU 🔥</p>'
57
+ if os.getenv('SYSTEM') == 'spaces':
58
+ download_all_controlnet_weights()
59
+ else:
60
+ DESCRIPTION += '\n<p>Running on CPU 🥶 This demo does not work on CPU.'
61
+
62
  MAX_IMAGES = int(os.getenv('MAX_IMAGES', '3'))
63
  DEFAULT_NUM_IMAGES = min(MAX_IMAGES, int(os.getenv('DEFAULT_NUM_IMAGES', '1')))
64
 
 
 
 
65
  DEFAULT_MODEL_ID = os.getenv('DEFAULT_MODEL_ID', 'andite/anything-v4.0')
66
  model = Model(base_model_id=DEFAULT_MODEL_ID, task_name='canny')
67
 
143
  inputs=new_base_model_id,
144
  outputs=current_base_model)
145
 
146
+ demo.queue(api_open=False, max_size=10).launch()
model.py CHANGED
@@ -18,13 +18,16 @@ repo_dir = pathlib.Path(__file__).parent
18
  submodule_dir = repo_dir / 'ControlNet'
19
  sys.path.append(submodule_dir.as_posix())
20
 
21
- from annotator.canny import apply_canny
22
- from annotator.hed import apply_hed, nms
23
- from annotator.midas import apply_midas
24
- from annotator.mlsd import apply_mlsd
25
- from annotator.openpose import apply_openpose
26
- from annotator.uniformer import apply_uniformer
27
- from annotator.util import HWC3, resize_image
 
 
 
28
 
29
  CONTROLNET_MODEL_IDS = {
30
  'canny': 'lllyasviel/sd-controlnet-canny',
@@ -54,6 +57,8 @@ class Model:
54
  self.pipe = self.load_pipe(base_model_id, task_name)
55
 
56
  def load_pipe(self, base_model_id: str, task_name) -> DiffusionPipeline:
 
 
57
  if base_model_id == self.base_model_id and task_name == self.task_name and hasattr(
58
  self, 'pipe'):
59
  return self.pipe
18
  submodule_dir = repo_dir / 'ControlNet'
19
  sys.path.append(submodule_dir.as_posix())
20
 
21
+ try:
22
+ from annotator.canny import apply_canny
23
+ from annotator.hed import apply_hed, nms
24
+ from annotator.midas import apply_midas
25
+ from annotator.mlsd import apply_mlsd
26
+ from annotator.openpose import apply_openpose
27
+ from annotator.uniformer import apply_uniformer
28
+ from annotator.util import HWC3, resize_image
29
+ except Exception:
30
+ pass
31
 
32
  CONTROLNET_MODEL_IDS = {
33
  'canny': 'lllyasviel/sd-controlnet-canny',
57
  self.pipe = self.load_pipe(base_model_id, task_name)
58
 
59
  def load_pipe(self, base_model_id: str, task_name) -> DiffusionPipeline:
60
+ if self.device.type == 'cpu':
61
+ return None
62
  if base_model_id == self.base_model_id and task_name == self.task_name and hasattr(
63
  self, 'pipe'):
64
  return self.pipe