Xhaheen commited on
Commit
6f2ea54
1 Parent(s): 886f260

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -56
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
- width, height = img.size
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
- try:
78
- caption, keywords = generate_caption_keywords(design)
79
- prompt = keywords
80
- except:
81
-
82
- prompt = design
83
-
84
-
85
 
86
- if x_prompt == True:
87
-
88
- prompt=alt_prompt
89
-
90
-
91
- answers = stability_api.generate(
92
-
93
- prompt,
94
-
95
- init_image=img,
96
- seed=54321, # if we're passing in an image generated by SD, you may get better results by providing a different seed value than was used to generate the image
97
- start_schedule=strength, # this controls the "strength" of the prompt relative to the init image
98
- )
99
-
100
- # iterating over the generator produces the api response
101
- for resp in answers:
102
- for artifact in resp.artifacts:
103
- if artifact.finish_reason == generation.FILTER:
104
- warnings.warn(
105
- "Your request activated the API's safety filters and could not be processed."
106
- "Please modify the prompt and try again.")
107
- if artifact.type == generation.ARTIFACT_IMAGE:
108
- img2 = Image.open(io.BytesIO(artifact.binary))
109
- #img2 = img2.resize((width1, height1), resample=Image.Resampling.BILINEAR)
110
- ###
111
- img2 = img2.resize((new_width, new_height), resample=Image.Resampling.BILINEAR)
112
- ###
113
- im1 = img2.save("new_image.jpg")
114
-
115
- print(type(img2))
116
- return img2
 
 
 
 
 
 
 
 
 
 
 
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