LPX55 commited on
Commit
8f8718d
·
1 Parent(s): e74d8ac

feat: resize b4 inf

Browse files
Files changed (1) hide show
  1. app_v4.py +42 -39
app_v4.py CHANGED
@@ -56,6 +56,13 @@ try:
56
  except Exception as e:
57
  print(f"Failed to dump env info: {e}")
58
 
 
 
 
 
 
 
 
59
  @spaces.GPU(duration=6, progress=gr.Progress(track_tqdm=True))
60
  @torch.no_grad()
61
  def generate_image(prompt, scale, steps, control_image, controlnet_conditioning_scale, guidance_scale, seed, guidance_end):
@@ -94,12 +101,14 @@ def combine_caption_focus(caption, focus):
94
  except Exception as e:
95
  print(f"Error combining caption and focus: {e}")
96
  return "highly detailed photo, raw photography."
97
-
98
  def generate_caption(control_image):
99
  try:
100
  if control_image is None:
101
  return "Waiting for control image..."
102
 
 
 
 
103
  # Generate a detailed caption
104
  mcaption = model.caption(control_image, length="short")
105
  detailed_caption = mcaption["caption"]
@@ -116,56 +125,50 @@ def generate_focus(control_image, focus_list):
116
  return None
117
  if focus_list is None:
118
  return ""
 
 
 
 
119
  # Generate a detailed caption
120
  focus_query = model.query(control_image, "Please provide a concise but illustrative description of the following area(s) of focus: " + focus_list)
121
  focus_description = focus_query["answer"]
122
  print(f"Areas of focus: {focus_description}")
123
-
124
  return focus_description
125
  except Exception as e:
126
  print(f"Error generating focus: {e}")
127
  return "highly detailed photo, raw photography."
128
 
129
- def process_image(control_image, user_prompt, system_prompt, scale, steps,
130
- controlnet_conditioning_scale, guidance_scale, seed,
131
- guidance_end, temperature, top_p, max_new_tokens, log_prompt):
132
- # Initialize with empty caption
133
- final_prompt = user_prompt.strip()
134
- # If no user prompt provided, generate a caption first
135
- if not final_prompt:
136
- # Generate a detailed caption
137
- print("Generating caption...")
138
- mcaption = model.caption(control_image, length="normal")
139
- detailed_caption = mcaption["caption"]
140
- final_prompt = detailed_caption
141
- yield f"Using caption: {final_prompt}", None, final_prompt
142
-
143
- # Show the final prompt being used
144
- yield f"Generating with: {final_prompt}", None, final_prompt
145
-
146
- # Generate the image
147
- try:
148
- image = generate_image(
149
- prompt=final_prompt,
150
- scale=scale,
151
- steps=steps,
152
  control_image=control_image,
153
  controlnet_conditioning_scale=controlnet_conditioning_scale,
 
154
  guidance_scale=guidance_scale,
155
- seed=seed,
156
- guidance_end=guidance_end
157
- )
158
-
159
- try:
160
- debug_img = Image.open(image.save("/tmp/" + str(seed) + "output.png"))
161
- save_image("/tmp/" + str(seed) + "output.png", debug_img)
162
- except Exception as e:
163
- print("Error 160: " + str(e))
164
- log_params(final_prompt, scale, steps, controlnet_conditioning_scale, guidance_scale, seed, guidance_end, control_image, image)
165
- yield f"Completed! Used prompt: {final_prompt}", image, final_prompt
166
- except Exception as e:
167
- print("Error: " + str(e))
168
- yield f"Error: {str(e)}", None, None
169
 
170
  with gr.Blocks(title="FLUX Turbo Upscaler", fill_height=True) as demo:
171
  gr.Markdown("⚠️ WIP SPACE - UNFINISHED & BUGGY")
 
56
  except Exception as e:
57
  print(f"Failed to dump env info: {e}")
58
 
59
+ def resize_image_to_max_side(image: Image, max_side_length=1024) -> Image:
60
+ width, height = image.size
61
+ ratio = min(max_side_length / width, max_side_length / height)
62
+ new_size = (int(width * ratio), int(height * ratio))
63
+ resized_image = image.resize(new_size, Image.BILINEAR)
64
+ return resized_image
65
+
66
  @spaces.GPU(duration=6, progress=gr.Progress(track_tqdm=True))
67
  @torch.no_grad()
68
  def generate_image(prompt, scale, steps, control_image, controlnet_conditioning_scale, guidance_scale, seed, guidance_end):
 
101
  except Exception as e:
102
  print(f"Error combining caption and focus: {e}")
103
  return "highly detailed photo, raw photography."
 
104
  def generate_caption(control_image):
105
  try:
106
  if control_image is None:
107
  return "Waiting for control image..."
108
 
109
+ # Resize the image to a maximum longest side of 1024 pixels
110
+ control_image = resize_image_to_max_side(control_image, max_side_length=1024)
111
+
112
  # Generate a detailed caption
113
  mcaption = model.caption(control_image, length="short")
114
  detailed_caption = mcaption["caption"]
 
125
  return None
126
  if focus_list is None:
127
  return ""
128
+
129
+ # Resize the image to a maximum longest side of 1024 pixels
130
+ control_image = resize_image_to_max_side(control_image, max_side_length=1024)
131
+
132
  # Generate a detailed caption
133
  focus_query = model.query(control_image, "Please provide a concise but illustrative description of the following area(s) of focus: " + focus_list)
134
  focus_description = focus_query["answer"]
135
  print(f"Areas of focus: {focus_description}")
 
136
  return focus_description
137
  except Exception as e:
138
  print(f"Error generating focus: {e}")
139
  return "highly detailed photo, raw photography."
140
 
141
+ @spaces.GPU(duration=6, progress=gr.Progress(track_tqdm=True))
142
+ @torch.no_grad()
143
+ def generate_image(prompt, scale, steps, control_image, controlnet_conditioning_scale, guidance_scale, seed, guidance_end):
144
+ generator = torch.Generator().manual_seed(seed)
145
+ # Load control image
146
+ control_image = load_image(control_image)
147
+
148
+ # Resize the image to a maximum longest side of 1024 pixels
149
+ control_image = resize_image_to_max_side(control_image, max_side_length=1024)
150
+
151
+ w, h = control_image.size
152
+ w = w - w % 32
153
+ h = h - h % 32
154
+ control_image = control_image.resize((int(w * scale), int(h * scale)), resample=2) # Resample.BILINEAR
155
+ print("Size to: " + str(control_image.size[0]) + ", " + str(control_image.size[1]))
156
+ print(f"PromptLog: {repr(prompt)}")
157
+ with torch.inference_mode():
158
+ image = pipe(
159
+ generator=generator,
160
+ prompt=prompt,
 
 
 
161
  control_image=control_image,
162
  controlnet_conditioning_scale=controlnet_conditioning_scale,
163
+ num_inference_steps=steps,
164
  guidance_scale=guidance_scale,
165
+ height=control_image.size[1],
166
+ width=control_image.size[0],
167
+ control_guidance_start=0.0,
168
+ control_guidance_end=guidance_end,
169
+ ).images[0]
170
+ # print("Type: " + str(type(image)))
171
+ return image
 
 
 
 
 
 
 
172
 
173
  with gr.Blocks(title="FLUX Turbo Upscaler", fill_height=True) as demo:
174
  gr.Markdown("⚠️ WIP SPACE - UNFINISHED & BUGGY")