Browse Source

a1111 example

main
Anindya Maiti 3 weeks ago
parent
commit
93da5c5544
  1. 16
      api-txt2img/README.md
  2. BIN
      api-txt2img/cat_0.png
  3. BIN
      api-txt2img/cat_1.png
  4. BIN
      api-txt2img/cat_2.png
  5. BIN
      api-txt2img/cat_3.png
  6. BIN
      api-txt2img/cat_4.png
  7. 50
      api-txt2img/generate_cat_images.py

16
api-txt2img/README.md

@ -1,4 +1,4 @@
# Image Generation Example with AUTOMATIC1111 API # Image Generation Example with AUTOMATIC1111 API (`sd3_medium` and `clip`)
This example demonstrates how to use Python to generate images of a cat using the AUTOMATIC1111 Stable Diffusion API on a custom port. In this example, we'll generate **5 images** using a fixed prompt and save them locally as PNG files. This example demonstrates how to use Python to generate images of a cat using the AUTOMATIC1111 Stable Diffusion API on a custom port. In this example, we'll generate **5 images** using a fixed prompt and save them locally as PNG files.
@ -10,6 +10,20 @@ The provided Python script does the following:
- Decodes the returned base64 image data. - Decodes the returned base64 image data.
- Saves each image in the local directory as a PNG file. - Saves each image in the local directory as a PNG file.
In addition to image generation, the example also shows how to use the server's CLIP Interrogator endpoint to generate captions from the created images.
## Server Details
For this example, the AUTOMATIC1111 server is running on the following network details:
- **IP Address:** `172.30.200.3`
- **Custom Port:** `35000`
You can use these details to interact with the server for:
- **Image Generation:** Sending requests to the `/sdapi/v1/txt2img` endpoint.
- **CLIP Interrogation:** Sending requests to the `/sdapi/v1/clip-interrogate` endpoint.
This means if you want to use the server for generating images or obtaining image captions via CLIP interrogation, you should point your API calls to `http://172.30.200.3:35000`.
## Prerequisites ## Prerequisites
Before running the script, ensure you have: Before running the script, ensure you have:

BIN
api-txt2img/cat_0.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 654 KiB

BIN
api-txt2img/cat_1.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 MiB

BIN
api-txt2img/cat_2.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 771 KiB

BIN
api-txt2img/cat_3.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 675 KiB

BIN
api-txt2img/cat_4.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 760 KiB

50
api-txt2img/generate_cat_images.py

@ -4,7 +4,8 @@ import io
import base64 import base64
from PIL import Image from PIL import Image
def generate_cat_images(ip, port, model_params, prompt="a cat", num_images=5): def generate_cat_images(ip, port, model_params, prompt="cat", num_images=5):
filenames = []
for i in range(num_images): for i in range(num_images):
# Generate a random seed for each image # Generate a random seed for each image
seed = random.randint(0, 4294967295) seed = random.randint(0, 4294967295)
@ -13,7 +14,6 @@ def generate_cat_images(ip, port, model_params, prompt="a cat", num_images=5):
payload = { payload = {
"prompt": prompt, "prompt": prompt,
"negative_prompt": "", # Modify if needed "negative_prompt": "", # Modify if needed
"sd_model_checkpoint": model_params["model"],
"steps": model_params["steps"], "steps": model_params["steps"],
"cfg_scale": model_params["cfg_scale"], "cfg_scale": model_params["cfg_scale"],
"width": model_params["width"], "width": model_params["width"],
@ -33,31 +33,59 @@ def generate_cat_images(ip, port, model_params, prompt="a cat", num_images=5):
r = response.json() r = response.json()
# Process returned images (the API returns a list in r['images']) # Process returned images (the API returns a list in r['images'])
for img_data in r.get('images', []): for img_data in r['images']:
# Remove any header if present (e.g., "data:image/png;base64,") # Remove any header if present (e.g., "data:image/png;base64,")
img_base64 = img_data.split(",", 1)[-1] image = Image.open(io.BytesIO(base64.b64decode(img_data.split(",",1)[0])))
image = Image.open(io.BytesIO(base64.b64decode(img_base64)))
# Save the image with a unique filename # Save the image with a unique filename
filename = f"cat_{i}.png" filename = f"cat_{i}.png"
image.save(filename) image.save(filename)
print(f"Saved {filename}") print(f"Saved {filename}")
filenames.append(filename)
except requests.exceptions.Timeout: except requests.exceptions.Timeout:
print(f"Timeout occurred while generating image {i}") print(f"Timeout occurred while generating image {i}")
except Exception as e: except Exception as e:
print(f"An error occurred on iteration {i}: {e}") print(f"An error occurred on iteration {i}: {e}")
return filenames
def interrogate_images(ip, port, image_files):
import requests, base64
for filename in image_files:
try:
# Open the image file and encode it in base64
with open(filename, "rb") as f:
img_bytes = f.read()
img_base64 = base64.b64encode(img_bytes).decode("utf-8")
payload = {"image": f"data:image/png;base64,{img_base64}", "model": "clip"}
# Send a POST request to the CLIP Interrogator API endpoint on the A1111 server
response = requests.post(
url=f"http://{ip}:{port}/sdapi/v1/interrogate",
json=payload,
timeout=30
)
response.raise_for_status()
data = response.json()
caption = data.get("caption", "No caption returned")
print(f"Image {filename} caption: {caption}")
except Exception as e:
print(f"An error occurred during interrogation of {filename}: {e}")
if __name__ == "__main__": if __name__ == "__main__":
ip = "172.30.200.3" ip = "172.30.200.3"
port = 35000 # Custom port for the API port = 35000 # Custom port for the API
# Define your model parameters (update 'model' to your actual checkpoint name) # Define your model parameters (update as needed)
model_params = { model_params = {
"model": "sd3.5_checkpoint.ckpt", # Replace with your actual model checkpoint file
"steps": 20, "steps": 20,
"cfg_scale": 7.0, "cfg_scale": 7,
"width": 512, "width": 1024, # Model specific, don't change
"height": 512, "height": 1024, # Model specific, don't change
"sampler": "Euler" # Adjust based on your setup "sampler": "Euler" # Adjust based on your setup
} }
generate_cat_images(ip, port, model_params, prompt="a cat", num_images=5) # Generate cat images and get the list of filenames
image_files = generate_cat_images(ip, port, model_params, prompt="cat, cute, perfect, anime", num_images=5)
# Call the CLIP Interrogator function from main using the same ip and port
interrogate_images(ip, port, image_files)

Loading…
Cancel
Save