Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -55,65 +55,56 @@ def generate_caption_keywords(prompt, model='command-xlarge-20221108', max_token
|
|
55 |
|
56 |
|
57 |
def img2img( path ,design,x_prompt,alt_prompt,strength,guidance_scale,steps):
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
img = Image.open(path)
|
62 |
-
width1, height1 = img.size
|
63 |
-
#img = img.resize((512, 704), resample=Image.Resampling.BILINEAR)
|
64 |
-
|
65 |
-
###########
|
66 |
# Read the size of the image
|
67 |
-
|
68 |
-
|
69 |
-
# Calculate the new size of the image, making sure that the width and height are multiples of 64
|
70 |
-
new_width = ((width + 63) // 64) * 64
|
71 |
-
new_height = ((height + 63) // 64) * 64
|
72 |
-
|
73 |
-
# Resize the image
|
74 |
-
img = img.resize((new_width, new_height), resample=Image.Resampling.BILINEAR)
|
75 |
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
prompt = keywords
|
80 |
-
except:
|
81 |
-
|
82 |
-
prompt = design
|
83 |
-
|
84 |
-
|
85 |
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
117 |
|
118 |
|
119 |
|
|
|
55 |
|
56 |
|
57 |
def img2img( path ,design,x_prompt,alt_prompt,strength,guidance_scale,steps):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
58 |
# Read the size of the image
|
59 |
+
img = Image.open(path)
|
60 |
+
width, height = img.size
|
|
|
|
|
|
|
|
|
|
|
|
|
61 |
|
62 |
+
# Calculate the new size of the image, making sure that the width and height are multiples of 64
|
63 |
+
new_width = ((width + 63) // 64) * 64
|
64 |
+
new_height = ((height + 63) // 64) * 64
|
|
|
|
|
|
|
|
|
|
|
|
|
65 |
|
66 |
+
# Resize the image
|
67 |
+
img = img.resize((new_width, new_height), resample=Image.Resampling.BILINEAR)
|
68 |
+
|
69 |
+
max_attempts = 5 # maximum number of attempts before giving up
|
70 |
+
attempts = 0 # current number of attempts
|
71 |
+
while attempts < max_attempts:
|
72 |
+
try:
|
73 |
+
if x_prompt == True:
|
74 |
+
prompt = alt_prompt
|
75 |
+
else:
|
76 |
+
try:
|
77 |
+
caption, keywords = generate_caption_keywords(design)
|
78 |
+
prompt = keywords
|
79 |
+
except:
|
80 |
+
prompt = design
|
81 |
+
|
82 |
+
# call the GRPC service to generate the image
|
83 |
+
answers = stability_api.generate(
|
84 |
+
prompt,
|
85 |
+
init_image=img,
|
86 |
+
seed=54321,
|
87 |
+
start_schedule=strength,
|
88 |
+
)
|
89 |
+
for resp in answers:
|
90 |
+
for artifact in resp.artifacts:
|
91 |
+
if artifact.finish_reason == generation.FILTER:
|
92 |
+
warnings.warn(
|
93 |
+
"Your request activated the API's safety filters and could not be processed."
|
94 |
+
"Please modify the prompt and try again.")
|
95 |
+
if artifact.type == generation.ARTIFACT_IMAGE:
|
96 |
+
img2 = Image.open(io.BytesIO(artifact.binary))
|
97 |
+
img2 = img2.resize((new_width, new_height), resample=Image.Resampling.BILINEAR)
|
98 |
+
img2.save("new_image.jpg")
|
99 |
+
print(type(img2))
|
100 |
+
# if the function reaches this point, it means it succeeded, so we can return the result
|
101 |
+
return img2
|
102 |
+
except Exception as e:
|
103 |
+
# if an exception is thrown, we will increment the attempts counter and try again
|
104 |
+
attempts += 1
|
105 |
+
print("Attempt {} failed: {}".format(attempts, e))
|
106 |
+
# if the function reaches this point, it means the maximum number of attempts has been reached, so we will raise an exception
|
107 |
+
raise Exception("Maximum number of attempts reached, unable to generate image")
|
108 |
|
109 |
|
110 |
|