Leeps commited on
Commit
0affb4b
·
verified ·
1 Parent(s): 10a713a

Upload folder using huggingface_hub

Browse files
Files changed (2) hide show
  1. .DS_Store +0 -0
  2. api/index.py +38 -20
.DS_Store CHANGED
Binary files a/.DS_Store and b/.DS_Store differ
 
api/index.py CHANGED
@@ -72,7 +72,7 @@ def call_openai(pil_image):
72
  # Could even do this 4 different times to get more diversity of renders
73
  # Add "simple" to prompt before word
74
 
75
- def image_classifier(moodboard, prompt):
76
 
77
  if moodboard is not None:
78
  pil_image = Image.fromarray(moodboard.astype('uint8'))
@@ -81,29 +81,43 @@ def image_classifier(moodboard, prompt):
81
 
82
  else:
83
  raise gr.Error(f"Please upload a moodboard to control image generation style")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
84
 
85
  input = {
86
  "prompt": "high quality render of a " + prompt + " which " + openai_response + ", minimalist and simple mockup on a white background",
87
  "output_format": "jpg"
88
  }
89
 
90
- try:
91
- output = replicate.run(
92
- "stability-ai/stable-diffusion-3",
93
- input=input
94
- )
95
- except Exception as e:
96
- raise gr.Error(f"Error: {e}")
97
 
98
- try:
99
- image_url = output[0]
100
- response = requests.get(image_url)
101
- img1 = Image.open(io.BytesIO(response.content))
102
- except Exception as e:
103
- raise gr.Error(f"Image download failed: {e}")
104
-
105
- input["aspect_ratio"] = "3:2"
106
- input["cfg"] = 6
107
 
108
  try:
109
  output = replicate.run(
@@ -128,13 +142,17 @@ def image_classifier(moodboard, prompt):
128
  "num_outputs": 2,
129
  "guidance_scale": 8.5
130
  }
 
 
 
 
131
 
132
  output = replicate.run(
133
  "stability-ai/sdxl:7762fd07cf82c948538e41f63f77d685e02b063e37e496e96eefd46c929f9bdc",
134
  input=input
135
  )
136
 
137
- images = [img1, img2]
138
 
139
  for i in range(min(len(output), 2)):
140
  image_url = output[i]
@@ -142,13 +160,13 @@ def image_classifier(moodboard, prompt):
142
  images.append(Image.open(io.BytesIO(response.content)))
143
 
144
  # Add empty images if fewer than 3 were returned
145
- while len(images) < 4:
146
  images.append(Image.new('RGB', (768, 768), 'gray'))
147
 
148
  images.reverse()
149
  return images
150
 
151
- demo = gr.Interface(fn=image_classifier, inputs=["image", "text"], outputs=["image", "image", "image", "image"])
152
  demo.launch(share=True)
153
 
154
 
 
72
  # Could even do this 4 different times to get more diversity of renders
73
  # Add "simple" to prompt before word
74
 
75
+ def image_classifier(moodboard, starter_image, image_strength, prompt):
76
 
77
  if moodboard is not None:
78
  pil_image = Image.fromarray(moodboard.astype('uint8'))
 
81
 
82
  else:
83
  raise gr.Error(f"Please upload a moodboard to control image generation style")
84
+
85
+ if starter_image is not None:
86
+ starter_image_pil = Image.fromarray(starter_image.astype('uint8'))
87
+
88
+ # Resize the starter image if either dimension is larger than 768 pixels
89
+ if starter_image_pil.size[0] > 768 or starter_image_pil.size[1] > 768:
90
+ # Calculate the new size while maintaining the aspect ratio
91
+ if starter_image_pil.size[0] > starter_image_pil.size[1]:
92
+ # Width is larger than height
93
+ new_width = 768
94
+ new_height = int((768 / starter_image_pil.size[0]) * starter_image_pil.size[1])
95
+ else:
96
+ # Height is larger than width
97
+ new_height = 768
98
+ new_width = int((768 / starter_image_pil.size[1]) * starter_image_pil.size[0])
99
+
100
+ # Resize the image
101
+ starter_image_pil = starter_image_pil.resize((new_width, new_height), Image.LANCZOS)
102
+
103
+ # Save the starter image to a bytes buffer
104
+ buffered = io.BytesIO()
105
+ starter_image_pil.save(buffered, format="JPEG")
106
+
107
+ # Encode the starter image to base64
108
+ starter_image_base64 = base64.b64encode(buffered.getvalue()).decode('utf-8')
109
+
110
 
111
  input = {
112
  "prompt": "high quality render of a " + prompt + " which " + openai_response + ", minimalist and simple mockup on a white background",
113
  "output_format": "jpg"
114
  }
115
 
 
 
 
 
 
 
 
116
 
117
+ if starter_image is not None:
118
+ input["image"] = "data:image/jpeg;base64," + starter_image_base64
119
+ input["prompt_strength"] = 1-image_strength
120
+
 
 
 
 
 
121
 
122
  try:
123
  output = replicate.run(
 
142
  "num_outputs": 2,
143
  "guidance_scale": 8.5
144
  }
145
+
146
+ if starter_image is not None:
147
+ input["image"] = "data:image/jpeg;base64," + starter_image_base64
148
+ input["prompt_strength"] = 1-image_strength
149
 
150
  output = replicate.run(
151
  "stability-ai/sdxl:7762fd07cf82c948538e41f63f77d685e02b063e37e496e96eefd46c929f9bdc",
152
  input=input
153
  )
154
 
155
+ images = [img2]
156
 
157
  for i in range(min(len(output), 2)):
158
  image_url = output[i]
 
160
  images.append(Image.open(io.BytesIO(response.content)))
161
 
162
  # Add empty images if fewer than 3 were returned
163
+ while len(images) < 3:
164
  images.append(Image.new('RGB', (768, 768), 'gray'))
165
 
166
  images.reverse()
167
  return images
168
 
169
+ demo = gr.Interface(fn=image_classifier, inputs=["image", "image", gr.Slider(0, 1, step=0.05, value=0.2), "text"], outputs=["image", "image", "image"])
170
  demo.launch(share=True)
171
 
172