amankishore commited on
Commit
784095e
·
1 Parent(s): 4877230
Files changed (1) hide show
  1. app.py +45 -13
app.py CHANGED
@@ -6,6 +6,7 @@ from diffusers import StableDiffusionDepth2ImgPipeline
6
  from pathlib import Path
7
 
8
  device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
 
9
  dept2img = StableDiffusionDepth2ImgPipeline.from_pretrained(
10
  "stabilityai/stable-diffusion-2-depth",
11
  torch_dtype=torch.float16,
@@ -38,6 +39,9 @@ def pad_image(input_image):
38
 
39
 
40
  def predict(input_image, prompt, negative_prompt, steps, num_samples, scale, seed, strength, depth_image=None):
 
 
 
41
  depth = None
42
  if depth_image is not None:
43
  depth_image = pad_image(depth_image)
@@ -66,20 +70,48 @@ def predict(input_image, prompt, negative_prompt, steps, num_samples, scale, see
66
  block = gr.Blocks().queue()
67
  with block:
68
  with gr.Box():
69
- top_description = gr.HTML(f'''
70
- <div style="text-align: center; max-width: 650px; margin: 0 auto;">
71
- <div>
72
- <img class="logo" src="file/mirage.png" alt="Mirage Logo"
73
- style="margin: auto; max-width: 7rem;">
 
 
 
 
 
 
 
 
74
  <br />
75
- <h1 style="font-weight: 900; font-size: 2.5rem;">
76
- Depth2Img Web UI
77
- </h1>
78
  <br />
79
- <a class="duplicate-button" style="display:inline-block" target="_blank" href="https://huggingface.co/spaces/MirageML/dreambooth?duplicate=true"><img src="https://img.shields.io/badge/-Duplicate%20Space-blue?labelColor=white&style=flat&logo=data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAAXNSR0IArs4c6QAAAP5JREFUOE+lk7FqAkEURY+ltunEgFXS2sZGIbXfEPdLlnxJyDdYB62sbbUKpLbVNhyYFzbrrA74YJlh9r079973psed0cvUD4A+4HoCjsA85X0Dfn/RBLBgBDxnQPfAEJgBY+A9gALA4tcbamSzS4xq4FOQAJgCDwV2CPKV8tZAJcAjMMkUe1vX+U+SMhfAJEHasQIWmXNN3abzDwHUrgcRGmYcgKe0bxrblHEB4E/pndMazNpSZGcsZdBlYJcEL9Afo75molJyM2FxmPgmgPqlWNLGfwZGG6UiyEvLzHYDmoPkDDiNm9JR9uboiONcBXrpY1qmgs21x1QwyZcpvxt9NS09PlsPAAAAAElFTkSuQmCC&logoWidth=14" alt="Duplicate Space"></a>
80
- </div>
81
- </div>
82
- ''')
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
83
 
84
  with gr.Row():
85
  with gr.Column():
@@ -112,7 +144,7 @@ with block:
112
  with gr.Column():
113
  gallery = gr.Gallery(label="Generated images", show_label=False).style(
114
  grid=[2], height="auto")
115
- if device == "cuda":
116
  gr.Examples(
117
  examples=[
118
  ["./examples/original_iso.png", "hogwarts castle",
 
6
  from pathlib import Path
7
 
8
  device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
9
+ is_gpu_associated = torch.cuda.is_available()
10
  dept2img = StableDiffusionDepth2ImgPipeline.from_pretrained(
11
  "stabilityai/stable-diffusion-2-depth",
12
  torch_dtype=torch.float16,
 
39
 
40
 
41
  def predict(input_image, prompt, negative_prompt, steps, num_samples, scale, seed, strength, depth_image=None):
42
+ if not is_gpu_associated:
43
+ raise gr.Error("Please associate a T4 GPU for this Space")
44
+ torch.cuda.empty_cache()
45
  depth = None
46
  if depth_image is not None:
47
  depth_image = pad_image(depth_image)
 
70
  block = gr.Blocks().queue()
71
  with block:
72
  with gr.Box():
73
+ if is_gpu_associated:
74
+ top_description = gr.HTML(f'''
75
+ <div style="text-align: center; max-width: 650px; margin: 0 auto;">
76
+ <div>
77
+ <img class="logo" src="file/mirage.png" alt="Mirage Logo"
78
+ style="margin: auto; max-width: 7rem;">
79
+ <br />
80
+ <h1 style="font-weight: 900; font-size: 2.5rem;">
81
+ Depth2Img Web UI
82
+ </h1>
83
+ <br />
84
+ <a class="duplicate-button" style="display:inline-block" target="_blank" href="https://huggingface.co/spaces/MirageML/depth2img?duplicate=true"><img src="https://img.shields.io/badge/-Duplicate%20Space-blue?labelColor=white&style=flat&logo=data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAAXNSR0IArs4c6QAAAP5JREFUOE+lk7FqAkEURY+ltunEgFXS2sZGIbXfEPdLlnxJyDdYB62sbbUKpLbVNhyYFzbrrA74YJlh9r079973psed0cvUD4A+4HoCjsA85X0Dfn/RBLBgBDxnQPfAEJgBY+A9gALA4tcbamSzS4xq4FOQAJgCDwV2CPKV8tZAJcAjMMkUe1vX+U+SMhfAJEHasQIWmXNN3abzDwHUrgcRGmYcgKe0bxrblHEB4E/pndMazNpSZGcsZdBlYJcEL9Afo75molJyM2FxmPgmgPqlWNLGfwZGG6UiyEvLzHYDmoPkDDiNm9JR9uboiONcBXrpY1qmgs21x1QwyZcpvxt9NS09PlsPAAAAAElFTkSuQmCC&logoWidth=14" alt="Duplicate Space"></a>
85
+ </div>
86
  <br />
 
 
 
87
  <br />
88
+ <p style="margin-bottom: 10px; font-size: 94%">
89
+ Create variations of an image while preserving shape and depth!
90
+ </p>
91
+ </div>
92
+ ''')
93
+ else:
94
+ top_description = gr.HTML(f'''
95
+ <div style="text-align: center; max-width: 650px; margin: 0 auto;">
96
+ <div>
97
+ <img class="logo" src="file/mirage.png" alt="Mirage Logo"
98
+ style="margin: auto; max-width: 7rem;">
99
+ <br />
100
+ <h1 style="font-weight: 900; font-size: 2.5rem;">
101
+ Depth2Img Web UI
102
+ </h1>
103
+ <br />
104
+ <a class="duplicate-button" style="display:inline-block" target="_blank" href="https://huggingface.co/spaces/MirageML/depth2img?duplicate=true"><img src="https://img.shields.io/badge/-Duplicate%20Space-blue?labelColor=white&style=flat&logo=data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAAXNSR0IArs4c6QAAAP5JREFUOE+lk7FqAkEURY+ltunEgFXS2sZGIbXfEPdLlnxJyDdYB62sbbUKpLbVNhyYFzbrrA74YJlh9r079973psed0cvUD4A+4HoCjsA85X0Dfn/RBLBgBDxnQPfAEJgBY+A9gALA4tcbamSzS4xq4FOQAJgCDwV2CPKV8tZAJcAjMMkUe1vX+U+SMhfAJEHasQIWmXNN3abzDwHUrgcRGmYcgKe0bxrblHEB4E/pndMazNpSZGcsZdBlYJcEL9Afo75molJyM2FxmPgmgPqlWNLGfwZGG6UiyEvLzHYDmoPkDDiNm9JR9uboiONcBXrpY1qmgs21x1QwyZcpvxt9NS09PlsPAAAAAElFTkSuQmCC&logoWidth=14" alt="Duplicate Space"></a>
105
+ </div>
106
+ <br />
107
+ <br />
108
+ <p style="margin-bottom: 10px; font-size: 94%">
109
+ Create variations of an image while preserving shape and depth!
110
+ </p>
111
+ <br />
112
+ <p>There's only one step left before you can run the app: <a href="https://huggingface.co/spaces/{os.environ['SPACE_ID']}/settings" style="text-decoration: underline" target="_blank">attribute a <b>T4 GPU</b> to it (via the Settings tab)</a> and run the training below. You will be billed by the minute from when you activate the GPU until when it is turned it off.</p>
113
+ </div>
114
+ ''')
115
 
116
  with gr.Row():
117
  with gr.Column():
 
144
  with gr.Column():
145
  gallery = gr.Gallery(label="Generated images", show_label=False).style(
146
  grid=[2], height="auto")
147
+ if is_gpu_associated:
148
  gr.Examples(
149
  examples=[
150
  ["./examples/original_iso.png", "hogwarts castle",