| *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" |