toshas commited on
Commit
019f566
1 Parent(s): 3cb6d15

revert spaces dependency

Browse files
README.md CHANGED
@@ -58,6 +58,8 @@ Check out the template image processing [app.py](app.py); copy it and start modi
58
  - `squeeze_canvas`: When True, the image is fit to the browser viewport. When False, the image is fit to width (Default: `True`).
59
  - `squeeze_viewport_height_pct`: Percentage of the browser viewport height (Default: `75`).
60
  - `key_original_image`: Name of the key under which the input image is shown in the modality selectors (Default: `"Original"`).
 
 
61
  - `slider_position`: Position of the slider between 0 and 1 (Default: `0.5`).
62
  - `slider_line_color`: Color of the slider line (Default: `"#FFF"`).
63
  - `slider_line_width`: Width of the slider line (Default: `"4px"`).
@@ -65,6 +67,9 @@ Check out the template image processing [app.py](app.py); copy it and start modi
65
  - `slider_arrows_width`: Width of the slider arrows (Default: `2px`).
66
  - `**kwargs`: Any other arguments that Gradio Blocks class can take.
67
 
 
 
 
68
  ## Real talk
69
 
70
  **Q: What is the idea behind this template?**<br>
 
58
  - `squeeze_canvas`: When True, the image is fit to the browser viewport. When False, the image is fit to width (Default: `True`).
59
  - `squeeze_viewport_height_pct`: Percentage of the browser viewport height (Default: `75`).
60
  - `key_original_image`: Name of the key under which the input image is shown in the modality selectors (Default: `"Original"`).
61
+ - `spaces_zero_gpu_enabled`: When True, the app wraps the processing function with the ZeroGPU decorator.
62
+ - `spaces_zero_gpu_duration`: Defines an integer duration in seconds passed into the ZeroGPU decorator.
63
  - `slider_position`: Position of the slider between 0 and 1 (Default: `0.5`).
64
  - `slider_line_color`: Color of the slider line (Default: `"#FFF"`).
65
  - `slider_line_width`: Width of the slider line (Default: `"4px"`).
 
67
  - `slider_arrows_width`: Width of the slider arrows (Default: `2px`).
68
  - `**kwargs`: Any other arguments that Gradio Blocks class can take.
69
 
70
+ **NB**: when setting `spaces_zero_gpu_enabled=True`, it may be required to add `import spaces` at the top of the app.py to
71
+ avoid the `RuntimeError` with "CUDA has been initialized before importing the `spaces` package".
72
+
73
  ## Real talk
74
 
75
  **Q: What is the idea behind this template?**<br>
gradio_dualvision/app_template.py CHANGED
@@ -37,6 +37,7 @@ import os
37
  import re
38
 
39
  import gradio as gr
 
40
  from PIL import Image
41
  from gradio.components.base import Component
42
 
@@ -55,6 +56,8 @@ class DualVisionApp(gr.Blocks):
55
  squeeze_canvas=True,
56
  squeeze_viewport_height_pct=75,
57
  key_original_image="Original",
 
 
58
  slider_position=0.5,
59
  slider_line_color="#FFF",
60
  slider_line_width="4px",
@@ -74,6 +77,8 @@ class DualVisionApp(gr.Blocks):
74
  squeeze_canvas: When True, the image is fit to the browser viewport. When False, the image is fit to width (Default: `True`).
75
  squeeze_viewport_height_pct: Percentage of the browser viewport height (Default: `75`).
76
  key_original_image: Name of the key under which the input image is shown in the modality selectors (Default: `"Original"`).
 
 
77
  slider_position: Position of the slider between 0 and 1 (Default: `0.5`).
78
  slider_line_color: Color of the slider line (Default: `"#FFF"`).
79
  slider_line_width: Width of the slider line (Default: `"4px"`).
@@ -98,6 +103,10 @@ class DualVisionApp(gr.Blocks):
98
  self.key_original_image = key_original_image
99
  self.slider_position = slider_position
100
  self.input_keys = None
 
 
 
 
101
  self.head = ""
102
  self.head += """
103
  <script>
@@ -111,7 +120,7 @@ class DualVisionApp(gr.Blocks):
111
 
112
  const parentDiv = oldButtonLeft.parentNode;
113
  if (!parentDiv) return;
114
-
115
  const createButton = (referenceButton, text, href) => {
116
  let newButton = referenceButton.cloneNode(true);
117
  newButton.href = href;
@@ -122,15 +131,15 @@ class DualVisionApp(gr.Blocks):
122
  newButton.style.cursor = "pointer";
123
  return newButton;
124
  };
125
-
126
  const newButton0 = createButton(oldButtonRight, "Built with Gradio DualVision", "https://github.com/toshas/gradio-dualvision");
127
  const newButton1 = createButton(oldButtonRight, "Template by Anton Obukhov", "https://www.obukhov.ai");
128
  const newButton2 = createButton(oldButtonRight, "Licensed under CC BY-SA 4.0", "http://creativecommons.org/licenses/by-sa/4.0/");
129
-
130
  const separatorDiv = document.createElement("div");
131
  separatorDiv.className = "svelte-1rjryqp";
132
  separatorDiv.textContent = "路";
133
-
134
  parentDiv.replaceChild(newButton0, oldButtonLeft);
135
  parentDiv.replaceChild(newButton1, oldButtonRight);
136
  parentDiv.appendChild(separatorDiv);
 
37
  import re
38
 
39
  import gradio as gr
40
+ import spaces
41
  from PIL import Image
42
  from gradio.components.base import Component
43
 
 
56
  squeeze_canvas=True,
57
  squeeze_viewport_height_pct=75,
58
  key_original_image="Original",
59
+ spaces_zero_gpu_enabled=False,
60
+ spaces_zero_gpu_duration=None,
61
  slider_position=0.5,
62
  slider_line_color="#FFF",
63
  slider_line_width="4px",
 
77
  squeeze_canvas: When True, the image is fit to the browser viewport. When False, the image is fit to width (Default: `True`).
78
  squeeze_viewport_height_pct: Percentage of the browser viewport height (Default: `75`).
79
  key_original_image: Name of the key under which the input image is shown in the modality selectors (Default: `"Original"`).
80
+ spaces_zero_gpu_enabled: When True, the app wraps the processing function with the ZeroGPU decorator.
81
+ spaces_zero_gpu_duration: Defines an integer duration in seconds passed into the ZeroGPU decorator.
82
  slider_position: Position of the slider between 0 and 1 (Default: `0.5`).
83
  slider_line_color: Color of the slider line (Default: `"#FFF"`).
84
  slider_line_width: Width of the slider line (Default: `"4px"`).
 
103
  self.key_original_image = key_original_image
104
  self.slider_position = slider_position
105
  self.input_keys = None
106
+ if spaces_zero_gpu_enabled:
107
+ self.process_components = spaces.GPU(
108
+ self.process_components, duration=spaces_zero_gpu_duration
109
+ )
110
  self.head = ""
111
  self.head += """
112
  <script>
 
120
 
121
  const parentDiv = oldButtonLeft.parentNode;
122
  if (!parentDiv) return;
123
+
124
  const createButton = (referenceButton, text, href) => {
125
  let newButton = referenceButton.cloneNode(true);
126
  newButton.href = href;
 
131
  newButton.style.cursor = "pointer";
132
  return newButton;
133
  };
134
+
135
  const newButton0 = createButton(oldButtonRight, "Built with Gradio DualVision", "https://github.com/toshas/gradio-dualvision");
136
  const newButton1 = createButton(oldButtonRight, "Template by Anton Obukhov", "https://www.obukhov.ai");
137
  const newButton2 = createButton(oldButtonRight, "Licensed under CC BY-SA 4.0", "http://creativecommons.org/licenses/by-sa/4.0/");
138
+
139
  const separatorDiv = document.createElement("div");
140
  separatorDiv.className = "svelte-1rjryqp";
141
  separatorDiv.textContent = "路";
142
+
143
  parentDiv.replaceChild(newButton0, oldButtonLeft);
144
  parentDiv.replaceChild(newButton1, oldButtonRight);
145
  parentDiv.appendChild(separatorDiv);
requirements.txt CHANGED
@@ -1,2 +1,3 @@
1
  gradio==4.44.1
2
  gradio_imageslider==0.0.20
 
 
1
  gradio==4.44.1
2
  gradio_imageslider==0.0.20
3
+ spaces