Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -248,22 +248,27 @@ def inference(
|
|
248 |
if invert_init_image and init_image is not None:
|
249 |
init_image = invert_image(init_image)
|
250 |
|
251 |
-
|
252 |
-
|
253 |
-
|
254 |
-
|
255 |
-
|
256 |
-
|
257 |
-
|
258 |
-
|
259 |
-
|
260 |
-
|
261 |
-
|
262 |
-
|
263 |
-
|
264 |
-
|
265 |
-
|
266 |
-
|
|
|
|
|
|
|
|
|
|
|
267 |
if invert_final_image:
|
268 |
final_image = invert_image(final_image)
|
269 |
|
@@ -275,6 +280,27 @@ def inference(
|
|
275 |
print(f"Unexpected error in inference: {str(e)}")
|
276 |
return Image.new('RGB', (1024, 1024), color='white'), -1
|
277 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
278 |
def invert_init_image_display(image):
|
279 |
if image is None:
|
280 |
return None
|
|
|
248 |
if invert_init_image and init_image is not None:
|
249 |
init_image = invert_image(init_image)
|
250 |
|
251 |
+
# Split the prompt into chunks to avoid truncation
|
252 |
+
prompt_chunks = split_prompt(prompt)
|
253 |
+
negative_prompt_chunks = split_prompt(negative_prompt)
|
254 |
+
|
255 |
+
final_image = None
|
256 |
+
for prompt_chunk, negative_prompt_chunk in zip(prompt_chunks, negative_prompt_chunks):
|
257 |
+
out = pipe(
|
258 |
+
prompt=prompt_chunk,
|
259 |
+
negative_prompt=negative_prompt_chunk,
|
260 |
+
image=init_image,
|
261 |
+
control_image=control_image,
|
262 |
+
width=1024,
|
263 |
+
height=1024,
|
264 |
+
guidance_scale=float(guidance_scale),
|
265 |
+
controlnet_conditioning_scale=float(controlnet_conditioning_scale),
|
266 |
+
generator=generator,
|
267 |
+
strength=float(strength),
|
268 |
+
num_inference_steps=100,
|
269 |
+
)
|
270 |
+
final_image = out.images[0] if final_image is None else final_image
|
271 |
+
|
272 |
if invert_final_image:
|
273 |
final_image = invert_image(final_image)
|
274 |
|
|
|
280 |
print(f"Unexpected error in inference: {str(e)}")
|
281 |
return Image.new('RGB', (1024, 1024), color='white'), -1
|
282 |
|
283 |
+
def split_prompt(prompt, max_length=77):
|
284 |
+
"""Split the prompt into chunks that do not exceed the max_length."""
|
285 |
+
words = prompt.split()
|
286 |
+
chunks = []
|
287 |
+
current_chunk = []
|
288 |
+
current_length = 0
|
289 |
+
|
290 |
+
for word in words:
|
291 |
+
if current_length + len(word) + 1 > max_length:
|
292 |
+
chunks.append(" ".join(current_chunk))
|
293 |
+
current_chunk = [word]
|
294 |
+
current_length = len(word) + 1
|
295 |
+
else:
|
296 |
+
current_chunk.append(word)
|
297 |
+
current_length += len(word) + 1
|
298 |
+
|
299 |
+
if current_chunk:
|
300 |
+
chunks.append(" ".join(current_chunk))
|
301 |
+
|
302 |
+
return chunks
|
303 |
+
|
304 |
def invert_init_image_display(image):
|
305 |
if image is None:
|
306 |
return None
|