IronJayx commited on
Commit
010ba30
·
1 Parent(s): 6f5eed7

log progress

Browse files
Files changed (1) hide show
  1. app.py +60 -24
app.py CHANGED
@@ -9,6 +9,7 @@ from dotenv import load_dotenv
9
  import base64
10
  from typing import Optional, Tuple, Union
11
  import glob
 
12
 
13
  load_dotenv()
14
 
@@ -21,11 +22,7 @@ global_input_image = None
21
  global_image_slider = None
22
 
23
 
24
- def clear_output():
25
- return None
26
-
27
-
28
- def process_image(
29
  image: Optional[Union[str, Image.Image]],
30
  denoise: float,
31
  steps: int,
@@ -35,6 +32,7 @@ def process_image(
35
  color_match: float,
36
  controlnet_tile_end: float,
37
  controlnet_tile_strength: float,
 
38
  ) -> Tuple[Optional[Image.Image], Optional[Image.Image]]:
39
  # Convert image to base64
40
  if image is not None:
@@ -69,9 +67,23 @@ def process_image(
69
 
70
  if result and result.object:
71
  run_id: str = result.object.run_id
 
72
  # Wait for the result
73
  while True:
74
  run_result = client.run.get(run_id=run_id)
 
 
 
 
 
 
 
 
 
 
 
 
 
75
  if run_result.object.status == "success":
76
  for output in run_result.object.outputs:
77
  if output.data and output.data.images:
@@ -84,13 +96,16 @@ def process_image(
84
  return image, processed_image
85
  return None, None
86
  elif run_result.object.status == "failed":
 
87
  return None, None
 
 
88
  except Exception as e:
89
  print(f"Error: {e}")
90
  return None, None
91
 
92
 
93
- def run(
94
  denoise,
95
  steps,
96
  tile_size,
@@ -99,6 +114,7 @@ def run(
99
  color_match,
100
  controlnet_tile_end,
101
  controlnet_tile_strength,
 
102
  ):
103
  global global_input_image
104
  global global_image_slider
@@ -106,11 +122,9 @@ def run(
106
  if not global_input_image:
107
  return None
108
 
109
- # Set image_slider to None before processing
110
  global_image_slider = None
111
 
112
- # Process the image
113
- original, processed = process_image(
114
  global_input_image,
115
  denoise,
116
  steps,
@@ -120,6 +134,7 @@ def run(
120
  color_match,
121
  controlnet_tile_end,
122
  controlnet_tile_strength,
 
123
  )
124
 
125
  if original and processed:
@@ -128,6 +143,11 @@ def run(
128
  return global_image_slider
129
 
130
 
 
 
 
 
 
131
  # Function to load preset images
132
  def load_preset_images():
133
  image_files = glob.glob("images/inputs/*")
@@ -139,10 +159,22 @@ def load_preset_images():
139
  ]
140
 
141
 
142
- def set_input_image(images, evt: gr.SelectData):
 
 
 
 
 
 
 
 
 
 
143
  global global_input_image
144
- global_input_image = images[evt.index][0]
145
- return global_input_image
 
 
146
 
147
 
148
  # Define Gradio interface
@@ -156,18 +188,22 @@ with gr.Blocks() as demo:
156
  value=lambda: global_input_image,
157
  interactive=True,
158
  )
 
 
 
 
159
 
160
  # Add preset images
161
- gr.Markdown("### Preset Images")
162
- preset_images = load_preset_images()
163
- gallery = gr.Gallery(
164
- [img["image"] for img in preset_images],
165
- label="Preset Images",
166
- columns=5,
167
- height=130,
168
- allow_preview=False,
169
- )
170
- gallery.select(set_input_image, gallery, input_image)
171
 
172
  with gr.Accordion("Advanced Parameters", open=False):
173
  denoise: gr.Slider = gr.Slider(0, 1, value=0.4, label="Denoise")
@@ -195,7 +231,7 @@ with gr.Blocks() as demo:
195
  interactive=True,
196
  )
197
 
198
- process_btn: gr.Button = gr.Button("Run")
199
  process_btn.click(
200
  fn=run,
201
  inputs=[
@@ -248,4 +284,4 @@ with gr.Blocks() as demo:
248
  )
249
 
250
  if __name__ == "__main__":
251
- demo.launch(debug=True, share=True)
 
9
  import base64
10
  from typing import Optional, Tuple, Union
11
  import glob
12
+ import asyncio
13
 
14
  load_dotenv()
15
 
 
22
  global_image_slider = None
23
 
24
 
25
+ async def process_image(
 
 
 
 
26
  image: Optional[Union[str, Image.Image]],
27
  denoise: float,
28
  steps: int,
 
32
  color_match: float,
33
  controlnet_tile_end: float,
34
  controlnet_tile_strength: float,
35
+ progress: gr.Progress = gr.Progress(),
36
  ) -> Tuple[Optional[Image.Image], Optional[Image.Image]]:
37
  # Convert image to base64
38
  if image is not None:
 
67
 
68
  if result and result.object:
69
  run_id: str = result.object.run_id
70
+ progress(0, desc="Starting processing...")
71
  # Wait for the result
72
  while True:
73
  run_result = client.run.get(run_id=run_id)
74
+ progress_value = (
75
+ run_result.object.progress
76
+ if run_result.object.progress is not None
77
+ else 0
78
+ )
79
+ status = (
80
+ run_result.object.live_status
81
+ if run_result.object.live_status is not None
82
+ else "Cold starting..."
83
+ )
84
+ progress(progress_value, desc=f"Status: {status}")
85
+ print(f"Progress: {progress_value:.2f}%, Status: {status}")
86
+
87
  if run_result.object.status == "success":
88
  for output in run_result.object.outputs:
89
  if output.data and output.data.images:
 
96
  return image, processed_image
97
  return None, None
98
  elif run_result.object.status == "failed":
99
+ print("Processing failed")
100
  return None, None
101
+
102
+ await asyncio.sleep(2) # Wait for 2 seconds before checking again
103
  except Exception as e:
104
  print(f"Error: {e}")
105
  return None, None
106
 
107
 
108
+ async def run_async(
109
  denoise,
110
  steps,
111
  tile_size,
 
114
  color_match,
115
  controlnet_tile_end,
116
  controlnet_tile_strength,
117
+ progress: gr.Progress = gr.Progress(),
118
  ):
119
  global global_input_image
120
  global global_image_slider
 
122
  if not global_input_image:
123
  return None
124
 
 
125
  global_image_slider = None
126
 
127
+ original, processed = await process_image(
 
128
  global_input_image,
129
  denoise,
130
  steps,
 
134
  color_match,
135
  controlnet_tile_end,
136
  controlnet_tile_strength,
137
+ progress,
138
  )
139
 
140
  if original and processed:
 
143
  return global_image_slider
144
 
145
 
146
+ # Wrapper function for gradio to use
147
+ def run(*args):
148
+ return asyncio.run(run_async(*args))
149
+
150
+
151
  # Function to load preset images
152
  def load_preset_images():
153
  image_files = glob.glob("images/inputs/*")
 
159
  ]
160
 
161
 
162
+ # def set_input_image(images, evt: gr.SelectData):
163
+ # global global_input_image
164
+ # global global_image_slider
165
+
166
+ # global_input_image = images[evt.index][0]
167
+ # global_image_slider = None
168
+
169
+ # return global_input_image
170
+
171
+
172
+ def update_global_input_image(image):
173
  global global_input_image
174
+ global global_image_slider
175
+ global_input_image = image
176
+ global_image_slider = None
177
+ return None
178
 
179
 
180
  # Define Gradio interface
 
188
  value=lambda: global_input_image,
189
  interactive=True,
190
  )
191
+ input_image.change(
192
+ fn=update_global_input_image,
193
+ inputs=[input_image],
194
+ )
195
 
196
  # Add preset images
197
+ # gr.Markdown("### Preset Images")
198
+ # preset_images = load_preset_images()
199
+ # gallery = gr.Gallery(
200
+ # [img["image"] for img in preset_images],
201
+ # label="Preset Images",
202
+ # columns=5,
203
+ # height=130,
204
+ # allow_preview=False,
205
+ # )
206
+ # gallery.select(set_input_image, gallery, input_image)
207
 
208
  with gr.Accordion("Advanced Parameters", open=False):
209
  denoise: gr.Slider = gr.Slider(0, 1, value=0.4, label="Denoise")
 
231
  interactive=True,
232
  )
233
 
234
+ process_btn: gr.Button = gr.Button("Run", variant="primary", size="lg")
235
  process_btn.click(
236
  fn=run,
237
  inputs=[
 
284
  )
285
 
286
  if __name__ == "__main__":
287
+ demo.queue().launch(debug=True, share=True)