How to Use ModelsLab API for Ultra-Realistic Image Generation

Mar 11, 2024 | Educational

If you’re looking to generate stunning ultra-realistic images using the ModelsLab API, you’re in the right place! This guide will walk you through the process step by step, making it easy to dive into the world of text-to-image generation.

Step 1: Get Your API Key

First things first, you’ll need to obtain your API key from the ModelsLab API. The best part? No payment is required!

Step 2: Prepare Your Code

Once you have your API key, the next step is to replace the placeholder key in the example code below. You may also want to change the model_id to realistic-vision-v51 to leverage the model’s capabilities.


import requests
import json

url = "https://modelslab.com/api/v6/images/text2img"
payload = json.dumps({
    "key": "your_api_key",
    "model_id": "realistic-vision-v51",
    "prompt": "ultra realistic close up portrait ((beautiful pale cyberpunk female with heavy black eyeliner)), blue eyes, shaved side haircut, hyper detail, cinematic lighting, magic neon, dark red city, Canon EOS R3, nikon, f1.4, ISO 200, 1160s, 8K, RAW, unedited, symmetrical balance, in-frame, 8K",
    "negative_prompt": "painting, extra fingers, mutated hands, poorly drawn hands, poorly drawn face, deformed, ugly, blurry, bad anatomy, bad proportions, extra limbs, cloned face, skinny, glitchy, double torso, extra arms, extra hands, mangled fingers, missing lips, ugly face, distorted face, extra legs, anime",
    "width": 512,
    "height": 512,
    "samples": 1,
    "num_inference_steps": 30,
    "safety_checker": "no",
    "enhance_prompt": "yes",
    "seed": "None",
    "guidance_scale": 7.5,
    "multi_lingual": "no",
    "panorama": "no",
    "self_attention": "no",
    "upscale": "no",
    "embeddings": "embeddings_model_id",
    "lora": "lora_model_id",
    "webhook": "None",
    "track_id": "None"
})

headers = {
    "Content-Type": "application/json"
}

response = requests.request("POST", url, headers=headers, data=payload)
print(response.text)

Step 3: Running Your Code

Now that your code is ready, simply run it in your Python environment and watch the magic happen with your unexpected image results!

A Fun Analogy for the Code

Think of generating images through the ModelsLab API like sending a postcard to an artist. The postcard (the payload) contains your heartfelt message (the prompt), specifying exactly what kind of artwork you want. You also include specific details on what to avoid (the negative_prompt). Once the artist receives your postcard with your request (after you’ve set the proper headers and posted it to the right address), they create your masterpiece (the generated image) and send it back to you (the response). The beauty of this setup allows you to create rich and complex images just by jotting down your thoughts!

Troubleshooting

  • Error: API Key Invalid – Double-check that you’ve copied your API key correctly from the ModelsLab API.
  • Error: Model Not Found – Ensure you’re using the correct model_id in your code.
  • Error: Connection Issues – Verify your internet connection or check if the ModelsLab service is up and running.
  • Unexpected Image Results – Adjust the prompt or negative_prompt to better fit your vision. Sometimes a little tweaking is needed until you get it just right!

For more insights, updates, or to collaborate on AI development projects, stay connected with fxis.ai.

Discount Code

Lastly, as a cherry on top, when you’re ready to dive in, don’t forget to use this coupon code for 25% off: DMGG0RBN.

At fxis.ai, we believe that such advancements are crucial for the future of AI, as they enable more comprehensive and effective solutions. Our team is continually exploring new methodologies to push the envelope in artificial intelligence, ensuring that our clients benefit from the latest technological innovations.

Stay Informed with the Newest F(x) Insights and Blogs

Tech News and Blog Highlights, Straight to Your Inbox