File size: 14,075 Bytes
3d205c2 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 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 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 |
*Note: commited intentionally for educational purposes Given the following code snippets, and the list of image generation models with example API requests. [TASK] <|gradio_app_instructions|> Your task is to complete the code snippets by adding the necessary code to make the API requests. The steps are really simple; user inputs any prompt; for example; "A girl with short pink hair wearing a oversize hoodie.". Then, the prompt will be passed to the enhance_prompt function to enhance the prompt. The enhanced prompt will be passed to the image generation model to generate the image. However, here user will select which image generation model to use. The image will be generated and displayed to the user. [UI] <|gradio_app_ui|> List the image generation models on the left side of the UI. Make image generation model selection as checkbox. Display as much Image Output as user selected image generation models. For example; we have 13 image generation models, and user selected 3 models using checkbox. After user enters the prompt. The image will be generated using 3 models and displayed to the user. [DOCS] Feel free to use Gradio documentation to complete the task. [CODE] <|start_of_code_snippet|> import gradio as gr from openai import OpenAI from dotenv import load_dotenv import os load_dotenv() client = OpenAI(api_key=os.getenv("OPENAI_API_KEY")) # i will update [SYSTEM_PROMPT] myself, ignore for now. SYSTEM_PROMPT = """ <i will update myself, ignore for now.> """ # general function to enhance prompt # user prompt will be passed as an argument # this is very first step after user input # then enhanced prompt will be passed to the image generation model def enhance_prompt(user_prompt) -> str: completion = client.chat.completions.create( model="gpt-4o", messages=[ {"role": "system", "content": SYSTEM_PROMPT}, {"role": "user", "content": user_prompt} ] ) ep = completion.choices[0].message.content print('Enhanced Prompt: ' ,ep) return ep # title should be centered # gradio app title title = "Let's Generate Cutesy AI Sticker!" # align project_website and paper_url center and in one row project_website = "https://ai-sticker-maker.vercel.app/" paper_url = "https://rebrand.ly/aistickermakerpaper" # call to action text should be also centered call_to_action_text = "Please consider starring ⭐️ the [GitHub Repo](https://github.com/abdibrokhim/ai-sticker-maker) if you find this useful!" # to build from scratch, you can follow the tutorial on medium and dev.to tutorial_on_medium_link = "https://medium.com/@abdibrokhim/building-an-ai-sticker-maker-platform-with-ai-ml-api-next-js-8b0767a7e159" tutorial_on_dev_link = "https://dev.to/abdibrokhim/building-an-ai-sticker-maker-platform-with-aiml-api-nextjs-react-and-tailwind-css-using-openai-gpt-4o-and-dalle-3-models-46ip" # general input placeholder placeholder = "A girl with short pink hair wearing a oversize hoodie..." <|list_of_image_generation_models|> # list of image generation models with example API requests # 1. stable-diffusion-v35-large # import requests # import base64 # def main(): # headers = { # "Authorization": "Bearer <YOUR_API_KEY>", # } # payload = { # "prompt": "Hyperrealistic art featuring a cat in costume.", # "model": "stable-diffusion-v35-large", # } # response = requests.post( # "https://api.aimlapi.com/images/generations", headers=headers, json=payload # ) # image_base64 = response.json()["output"]["choices"][0]["image_base64"] # image_data = base64.b64decode(image_base64) # with open("./image.png", "wb") as file: # file.write(image_data) # main() # 2. flux-pro/v1.1 # import requests # import base64 # def main(): # headers = { # "Authorization": "Bearer <YOUR_API_KEY>", # } # payload = { # "prompt": "Hyperrealistic art featuring a cat in costume.", # "model": "flux-pro/v1.1", # } # response = requests.post( # "https://api.aimlapi.com/images/generations", headers=headers, json=payload # ) # image_base64 = response.json()["output"]["choices"][0]["image_base64"] # image_data = base64.b64decode(image_base64) # with open("./image.png", "wb") as file: # file.write(image_data) # main() # 3. dall-e-3 # import requests # import base64 # def main(): # headers = { # "Authorization": "Bearer <YOUR_API_KEY>", # } # payload = { # "prompt": "Hyperrealistic art featuring a cat in costume.", # "model": "dall-e-3", # } # response = requests.post( # "https://api.aimlapi.com/images/generations", headers=headers, json=payload # ) # image_base64 = response.json()["output"]["choices"][0]["image_base64"] # image_data = base64.b64decode(image_base64) # with open("./image.png", "wb") as file: # file.write(image_data) # main() # 4. stable-diffusion-v3-medium # import requests # import base64 # def main(): # headers = { # "Authorization": "Bearer <YOUR_API_KEY>", # } # payload = { # "prompt": "Hyperrealistic art featuring a cat in costume.", # "model": "stable-diffusion-v3-medium", # } # response = requests.post( # "https://api.aimlapi.com/images/generations", headers=headers, json=payload # ) # image_base64 = response.json()["output"]["choices"][0]["image_base64"] # image_data = base64.b64decode(image_base64) # with open("./image.png", "wb") as file: # file.write(image_data) # main() # 5. runwayml/stable-diffusion-v1-5 # import requests # import base64 # def main(): # headers = { # "Authorization": "Bearer <YOUR_API_KEY>", # } # payload = { # "prompt": "Hyperrealistic art featuring a cat in costume.", # "model": "runwayml/stable-diffusion-v1-5", # } # response = requests.post( # "https://api.aimlapi.com/images/generations", headers=headers, json=payload # ) # image_base64 = response.json()["output"]["choices"][0]["image_base64"] # image_data = base64.b64decode(image_base64) # with open("./image.png", "wb") as file: # file.write(image_data) # main() # 6. stabilityai/stable-diffusion-xl-base-1.0 # import requests # import base64 # def main(): # headers = { # "Authorization": "Bearer <YOUR_API_KEY>", # } # payload = { # "prompt": "Hyperrealistic art featuring a cat in costume.", # "model": "stabilityai/stable-diffusion-xl-base-1.0", # } # response = requests.post( # "https://api.aimlapi.com/images/generations", headers=headers, json=payload # ) # image_base64 = response.json()["output"]["choices"][0]["image_base64"] # image_data = base64.b64decode(image_base64) # with open("./image.png", "wb") as file: # file.write(image_data) # main() # 7. stabilityai/stable-diffusion-2-1 # import requests # import base64 # def main(): # headers = { # "Authorization": "Bearer <YOUR_API_KEY>", # } # payload = { # "prompt": "Hyperrealistic art featuring a cat in costume.", # "model": "stabilityai/stable-diffusion-2-1", # } # response = requests.post( # "https://api.aimlapi.com/images/generations", headers=headers, json=payload # ) # image_base64 = response.json()["output"]["choices"][0]["image_base64"] # image_data = base64.b64decode(image_base64) # with open("./image.png", "wb") as file: # file.write(image_data) # main() # 8. SG161222/Realistic_Vision_V3.0_VAE # import requests # import base64 # def main(): # headers = { # "Authorization": "Bearer <YOUR_API_KEY>", # } # payload = { # "prompt": "Hyperrealistic art featuring a cat in costume.", # "model": "SG161222/Realistic_Vision_V3.0_VAE", # } # response = requests.post( # "https://api.aimlapi.com/images/generations", headers=headers, json=payload # ) # image_base64 = response.json()["output"]["choices"][0]["image_base64"] # image_data = base64.b64decode(image_base64) # with open("./image.png", "wb") as file: # file.write(image_data) # main() # 9. prompthero/openjourney # import requests # import base64 # def main(): # headers = { # "Authorization": "Bearer <YOUR_API_KEY>", # } # payload = { # "prompt": "Hyperrealistic art featuring a cat in costume.", # "model": "prompthero/openjourney", # } # response = requests.post( # "https://api.aimlapi.com/images/generations", headers=headers, json=payload # ) # image_base64 = response.json()["output"]["choices"][0]["image_base64"] # image_data = base64.b64decode(image_base64) # with open("./image.png", "wb") as file: # file.write(image_data) # main() # 10. wavymulder/Analog-Diffusion # import requests # import base64 # def main(): # headers = { # "Authorization": "Bearer <YOUR_API_KEY>", # } # payload = { # "prompt": "Hyperrealistic art featuring a cat in costume.", # "model": "wavymulder/Analog-Diffusion", # } # response = requests.post( # "https://api.aimlapi.com/images/generations", headers=headers, json=payload # ) # image_base64 = response.json()["output"]["choices"][0]["image_base64"] # image_data = base64.b64decode(image_base64) # with open("./image.png", "wb") as file: # file.write(image_data) # main() # 11. flux-pro # import requests # import base64 # def main(): # headers = { # "Authorization": "Bearer <YOUR_API_KEY>", # } # payload = { # "prompt": "Hyperrealistic art featuring a cat in costume.", # "model": "flux-pro", # } # response = requests.post( # "https://api.aimlapi.com/images/generations", headers=headers, json=payload # ) # image_base64 = response.json()["output"]["choices"][0]["image_base64"] # image_data = base64.b64decode(image_base64) # with open("./image.png", "wb") as file: # file.write(image_data) # main() # 12. flux-realism # import requests # import base64 # def main(): # headers = { # "Authorization": "Bearer <YOUR_API_KEY>", # } # payload = { # "prompt": "Hyperrealistic art featuring a cat in costume.", # "model": "flux-realism", # } # response = requests.post( # "https://api.aimlapi.com/images/generations", headers=headers, json=payload # ) # image_base64 = response.json()["output"]["choices"][0]["image_base64"] # image_data = base64.b64decode(image_base64) # with open("./image.png", "wb") as file: # file.write(image_data) # main() # 13. dall-e-2 # import requests # import base64 # def main(): # headers = { # "Authorization": "Bearer <YOUR_API_KEY>", # } # payload = { # "prompt": "Hyperrealistic art featuring a cat in costume.", # "model": "dall-e-2", # } # response = requests.post( # "https://api.aimlapi.com/images/generations", headers=headers, json=payload # ) # image_base64 = response.json()["output"]["choices"][0]["image_base64"] # image_data = base64.b64decode(image_base64) # with open("./image.png", "wb") as file: # file.write(image_data) # main() <|end_of_code_snippet|> Refactor examples part. Follow this steps: 1. make 4 columns: 1) user prompt, 2) enhanced prompt, 3) generated image, 4) ai model 2. rewrite column labels also. 3. better make dictionary for each entry. so i can easily add more examples. here is example table info: [entry 1:] user prompt: "An adorable kitten playing with a ball of yarn" enhanced prompt: "An adorable, fluffy kitten with big, sparkling eyes and playful whiskers, tumbling around with a vibrant ball of yarn. The kitten's fur is a soft blend of warm creams and greys, giving it a cuddly, huggable appearance. Its expression is full of joy and mischief, with a tiny pink tongue playfully sticking out. The ball of yarn is a bright and cheerful red, unraveling with dynamic loops and curls. The style is chibi-like and sticker-friendly, with minimalistic lines and gentle shading. The background is a simple, soft pastel color with tiny floating paw prints, enhancing the cute and playful theme." generated image: "./generated-images/cat-and-yarn.jpeg" ai model: "dall-e-3" [entry 2:] user prompt: "A cutesy cat eating ice cream under a rainbow" enhanced prompt: "A playful, cartoonish cat with big, sparkling eyes and soft, rounded features, happily licking a colorful ice cream cone. The cat has fluffy fur, pastel colors—like soft cream, peach, or light gray—and tiny pink blush on its cheeks for added charm. It sits contentedly under a bright, arched rainbow with soft, blended hues. Small, floating sparkles and tiny hearts surround the cat and ice cream to add a touch of magic. The ice cream cone has multiple scoops in fun, bright colors like pink, blue, and mint green, making the whole scene feel adorable and sweet, perfect for a cute sticker!" generated image: "./generated-images/cat-and-icecream.jpeg" ai model: "dall-e-3" [entry 3:] user prompt: "A girl with short pink+black hair wearing a pink shirt." enhanced prompt: "An adorable chibi-style character with a soft, cozy look. She has a short, wavy bob hairstyle in gradient shades of gray with delicate highlights that sparkle. Her large, expressive brown eyes have a gentle shine, and her cheeks are lightly blushed, adding a touch of warmth. She wears an off-shoulder, cream-colored sweater, giving a relaxed and comforting vibe. The background is a soft pastel gradient in warm beige and cream tones, decorated with small, floating sparkles and star shapes for a magical effect. The overall style is cute, minimalist, and sticker-friendly." generated image: "./generated-images/girl-with-white-grey-hair.png" ai model: "dall-e-3" |