0

I'm trying to create a wordpress php connction to a stable diffusion model on replicate.com. It seems to work but when i'm adding an init image the processing won't start. Actually it halts on "status: starting" with no output prompts. I can see the image on my api prediction on the replicate Dashboard, but i think the image isn't really uploaded. When i click "Tweek" and replaces the image it works. What am i doing wrong here?

curl_setopt($resource, CURLOPT_URL, 'https://api.replicate.com/v1/predictions');
curl_setopt($resource, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($resource, CURLOPT_POST, 1);
$data = array(
   'version' => '56f26876a159c10b429c382f66ccda648c1d5678d7ce15ed010734b715be5ab9', 
   'input' => array(
        'prompt' => 'circles',
        'init_image' => 'https://example.com/wp-content/uploads/img.jpg'
    )
);
curl_setopt($resource, CURLOPT_POSTFIELDS, json_encode($data));
$headers = array();
$headers[] = 'Authorization: Token njdscknjk3nkjn32nk3jn3j';
$headers[] = 'Content-Type: application/json';
curl_setopt($resource, CURLOPT_HTTPHEADER, $headers);
$output = curl_exec($resource); 
4
  • 3
    Make sure the API can access your image URL. Even when you can access the URL, the API may not. You could change it to a data URL, using Base64. Commented Oct 6, 2022 at 17:34
  • @Phil That was brilliant, closing a question and referring to a closed question. Commented Oct 7, 2022 at 1:58
  • 1
    @Misunderstood I've re-opened. Turns out the API expects URL strings. In any case, even if a post is closed, as long as it has answers it's a valid duplicate target Commented Oct 7, 2022 at 2:06
  • 1
    I was referring to the irony, not if it's proper. I was not dissing you. And the API also requires Content-Type: application/json. Commented Oct 7, 2022 at 2:15

1 Answer 1

2

You curl is doing what you are asking it to do.
This is what the server will see:

Content-Length: 174
Content-Type: application/json
Accept: */*
BODY=
{"version":"56f26876a159c10b429c382f66ccda648c1d5678d7ce15ed010734b715be5ab9","input":{"prompt":"circles","init_image":"https:\/\/example.com\/wp-content\/uploads\/img.jpg"}}

You are transmitting the image url rather than the image. Try this:

$image = base64_encode(file_get_contents('https://example.com/wp-content/uploads/img.jpg'));

'init_image' => $image;

The image may also need to be prefixed by: 'data:[<mediatype>][;base64],<data>'

$image = 'data: image/jpeg;base64,' .  base64_encode(file_get_contents('https://example.com/wp-content/uploads/img.jpg'));

And the base64 data may (or may not) need to be URL encoded with PHP's urlencode() function.

You are doing an HTTPS post so you may (or may not) need these but the request would not be secure and the server would likely reject it.

curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
Sign up to request clarification or add additional context in comments.

4 Comments

The API expects an HTTP or data URL so you should prefix $image with data:image/jpg;base64,
@Phil I swear I did not see your comment until after I saved my edits. I was editing when you posted your comment.
Wish I could upvote twice 😄
Thanks @Misunderstood, it works! You have saved my day :-) The extra curl_setopt wasn't necessary.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.