ControlNet Inference
Hi, I’m trying to access a ControlNet model through an inference API, but I’m getting this error. Please help{“error”:“image must be passed and be one of PIL image, numpy array, torch tensor, list of PIL images, list of numpy arrays or list of torch tensors, but is <class ‘NoneType’>”}Here is the code:import requests
from PIL import Image
from io import BytesIO
import numpy as np


# Define your image path
image_path = "/content/drive/MyDrive/input_image_vermeer.png"

import base64
import builtins

# image2 = Image.open(image_path)
# image_np = np.array(image)

def encode_image(image_path):
  with open(image_path, "rb") as i:
    b64 = base64.b64encode(i.read())
  return b64.decode("utf-8")

    
# encoded_image = encode_image(image_path)

# Define the API endpoint and headers
ENDPOINT_URL = "https://api-inference.huggingface.co/models/krea/aesthetic-controlnet"
HF_TOKEN = "###########################"

def predict(prompt, image, negative_prompt=None, controlnet_type = "normal"):
    image = encode_image(image)
    image

    # prepare sample payload
    request = {"inputs": prompt, "image": image, "negative_prompt": negative_prompt, "controlnet_type": controlnet_type}
    # headers
    headers = {
        "Authorization": f"Bearer {HF_TOKEN}",
        "Content-Type": "application/json",
        "Accept": "image/png" # important to get an image back
    }

    response = requests.post(ENDPOINT_URL, headers=headers, json=request)
    if response.status_code != 200:
        print(response.text)
        raise Exception("Prediction failed")
    img = Image.open(BytesIO(response.content))
    return img


prediction = predict(
  prompt = "cloudy sky background lush landscape house and green trees",
  negative_prompt ="lowres, bad anatomy, worst quality, low quality, city, traffic",
  controlnet_type = "hed",
  image = image_path
)

# prediction.save("result.png")