SethRobinson / aitools_client

Seth's AI Tools: A Unity based front end that uses image generators and llm APIs to do fun stuff
Other
154 stars 15 forks source link

Error on generation #1

Closed cooperdk closed 1 year ago

cooperdk commented 1 year ago

Copied from the Web UI discussion board:

When your client does an API call to the API, nothing happens. The image windows fill out with "Generate error".

Testing the API revealed that your UI requests the endpoint /sdapi/v1/options so I curled that.

This is the error I get (the API is enabled, of course):

Q:\StableDiffusion\aitools> curl -I http://127.0.0.1:7860/sdapi/v1/options

HTTP/1.1 405 Method Not Allowed
date: Tue, 13 Dec 2022 19:08:31 GMT
server: uvicorn
allow: GET
content-length: 31
content-type: application/json

But testing the API within the Web UI API documentation area taught me how to actually request, and I did this:

Q:\StableDiffusion\aitools> curl -X "GET" "http://127.0.0.1:7860/sdapi/v1/options" -H "accept: application/json"

Which gave me a correct options JSON:

{"samples_save":true,"samples_format":"png","samples_filename_pattern":"","save_images_add_number":true,"grid_save":true,"grid_format":"png","grid_extended_filename":true,"grid_only_if_multiple":true,"grid_prevent_empty_spots":false,(...)

Unfortunately I cannot see the error it receives when generating because your log clears and it doesn't save anywhere.

I was thinking that maybe I should alter in my command line arguments, because the server ran at 0.0.0.0 and that could sometimes mess up, so I did:

--api --listen --server-name 127.0.0.1 --port 7860

But that did not work either.

My complete commandline_args variable is:

--api --listen --server-name 127.0.0.1 --port 7860 --xformers --enable-insecure-extension-access --gradio-img2img-tool color-sketch --no-half --theme dark --deepdanbooru --deepdanbooru-projects-path "Q:\\stablediffusion\\webui\\models\\deepdanbooru"

I wonder why your client does not receive this but gets a 405 error? It clearly works in my CLI (and in the API documentation as well). Is it because the Unity UI should be run as admin?

So I tried some more. It actually (now at least) DOES communicate with the API and receive the options:

image

Switching models etc. works.

But I had time to do a screen capture, and saw this:

image

So it is a JSON error having to do with double quotes.

The "ctx" and "doc" items are not part of a parameter body for the txt2img endpoint so I have no idea about those.

I have no idea how to pursue this further.

I can only say that generating an image using the api works:

curl -X 'POST' \
  'http://127.0.0.1:7860/sdapi/v1/txt2img' \
  -H 'accept: application/json' \
  -H 'Content-Type: application/json' \
  -d '{
  "enable_hr": false,
  "denoising_strength": 0,
  "firstphase_width": 0,
  "firstphase_height": 0,
  "prompt": "",
  "styles": [
    "string"
  ],
  "seed": -1,
  "subseed": -1,
  "subseed_strength": 0,
  "seed_resize_from_h": -1,
  "seed_resize_from_w": -1,
  "sampler_name": "Euler a",
  "batch_size": 1,
  "n_iter": 1,
  "steps": 50,
  "cfg_scale": 7,
  "width": 512,
  "height": 512,
  "restore_faces": false,
  "tiling": false,
  "negative_prompt": "string",
  "eta": 0,
  "s_churn": 0,
  "s_tmax": 0,
  "s_tmin": 0,
  "s_noise": 1,
  "override_settings": {},
  "sampler_index": "Euler"
}'
Response: 200
{
  "images": [
    "iVBORw0K(...)3wIwlXAAAAABJRU5ErkJggg=="
  ],
  "parameters": {
    "enable_hr": false,
    "denoising_strength": 0,
    "firstphase_width": 0,
    "firstphase_height": 0,
    "prompt": "",
    "styles": [
      "string"
    ],
    "seed": -1,
    "subseed": -1,
    "subseed_strength": 0,
    "seed_resize_from_h": -1,
    "seed_resize_from_w": -1,
    "sampler_name": "Euler a",
    "batch_size": 1,
    "n_iter": 1,
    "steps": 50,
    "cfg_scale": 7,
    "width": 512,
    "height": 512,
    "restore_faces": false,
    "tiling": false,
    "negative_prompt": "string",
    "eta": 0,
    "s_churn": 0,
    "s_tmax": 0,
    "s_tmin": 0,
    "s_noise": 1,
    "override_settings": {},
    "sampler_index": "Euler"
  },
  "info": "{\"prompt\": \"\", \"all_prompts\": [\"\"], \"negative_prompt\": \"string\", \"all_negative_prompts\": [\"string\"], \"seed\": 727843296, \"all_seeds\": [727843296], \"subseed\": 158766109, \"all_subseeds\": [158766109], \"subseed_strength\": 0.0, \"width\": 512, \"height\": 512, \"sampler_name\": \"Euler a\", \"cfg_scale\": 7.0, \"steps\": 50, \"batch_size\": 1, \"restore_faces\": false, \"face_restoration_model\": null, \"sd_model_hash\": \"ddc6edf2\", \"seed_resize_from_w\": -1, \"seed_resize_from_h\": -1, \"denoising_strength\": 0.0, \"extra_generation_params\": {}, \"index_of_first_image\": 0, \"infotexts\": [\"Negative prompt: string\\nSteps: 50, Sampler: Euler a, CFG scale: 7.0, Seed: 727843296, Size: 512x512, Model hash: ddc6edf2, Model: mdjrny-v4, Hypernet: real-hucow, Hypernet strength: 0.901, Seed resize from: -1x-1, Denoising strength: 0.0, ENSD: 31337\"], \"styles\": [\"string\"], \"job_timestamp\": \"20221213204910\", \"clip_skip\": 1, \"is_using_inpainting_conditioning\": false}"
}
Response headers
 content-encoding: gzip 

So it must have to do with your JSON request body is my guess...

But since both your app and the web ui log nothing it is impossible to determine the cause of the error in an easy way... the only thing I know is that my configuration is free of JSON errors. I checked again with the earlier build since this pertains to your beta build. But it's the same.

cooperdk commented 1 year ago

Debugging further with ngrok resulted in this error 422.

{
    "detail": [
        {
            "loc": [
                "body",
                283
            ],
            "msg": "Expecting property name enclosed in double quotes: line 8 column 27 (char 283)",
            "type": "value_error.jsondecode",
            "ctx": {
                "msg": "Expecting property name enclosed in double quotes",
                "doc": "{\r\n            \r\n            \"prompt\": \"an angry teddy bear dressed like a samurai at a kyoto shrine. masterpiece. sharp focus\",\r\n            \"negative_prompt\": \"\",\r\n            \"steps\": 20,\r\n            \"restore_faces\":false,\r\n            \"tiling\":false,\r\n            \"cfg_scale\":7,5,\r\n            \"seed\": -2963563465233642818,\r\n            \"width\": 512,\r\n            \"height\": 512,\r\n            \"sampler_name\": \"Euler a\",\r\n            \"alpha_mask_subject\":false\r\n\r\n        \r\n        }",
                "pos": 283,
                "lineno": 8,
                "colno": 27
            }
        }
    ]
}

In the ngrok inspector, the error returned is: invalid character '5' looking for beginning of object key string But it is not the 5 in width, because it stays as 5 even if I change the size to 640x640.

I then count in Notepad++ and it seems that character #283 in the JSON is the 5 in the cfg scale 7,5.

I compare then to other lines in the JSON and the only difference is that some of your JSON spaced between the item and the value, some are not.

Changing the text strength to 7,0 (converted to the integer 7 by your UI) actually DOES work.

So the issue is that your UI does not consider how to send a decimal point. It requires a dot, I am pretty sure, which is inconsistent with my OS language, but it sends a comma, which is consistent. WHY this happens? No idea. I never experienced it before.

I tested with the API documentation has a way of testing with parameters. This is how I know a decimal dot works.

A cfg_scale of 7,5 did not work. A scale of 7.5 did. I don't know why my Python wants it to be a decimal dot because I am European and we do use commas as decimals. But there you have it. Your app uses the wrong decimal point.

So either you must let people define it (or find out why this happens) or you can only use integers. OR only use decimal dots and wait and see if people report that too :-)

{
    "prompt": "an angry teddy bear dressed like a samurai at a kyoto shrine. masterpiece. sharp focus",
    "negative_prompt": "",
    "steps": 20,
    "restore_faces": false,
    "tiling": false,
    "cfg_scale": 7,
    "seed": -3148459718809389600,
    "width": 640,
    "height": 640,
    "sampler_name": "Euler a",
    "alpha_mask_subject": false
}

NOTE another similar issue: The JSON decoder returns an invalid escape if you try to load a checkpoint that is in a Windows subdirectory, for example (like I sort mine) - example: real\sfw\some-model.ckpt. It doesn't correctly escape the path.

I see that you use a JSONSerializeModule in data\managed. I wonder how it handles what it sends, because it is clearly incorrect.

Note the cfg scale is set to 8, and then it works

image

SethRobinson commented 1 year ago

Thanks so much for digging into that!

I always forget c# can do weird things with even very basic conversions like string to float, I need to force us-style region (or 'cultureinfo as they call it in c#). I would have never figured this out without your detective work.

I will fix this (and hopefully the subdir issue if I can recreate it) in the next release.

cooperdk commented 1 year ago

Thanks so much for digging into that!

I always forget c# can do weird things with even very basic conversions like string to float, I need to force us-style region (or 'cultureinfo as they call it in c#). I would have never figured this out without your detective work.

I will fix this (and hopefully the subdir issue if I can recreate it) in the next release.

You're welcome. I'm happy to have spent some time doing a bit of detective work :-) In the end, this is how independent software gets better, afterall.

SethRobinson commented 1 year ago

Ok, I'm hoping V0.53 fixes this issues!

Unfortunately despite setting my region/etc to UK I was not able to recreate the issue on my Win 11 pro install, so I was forced to do a "blind fix" for the "," vs "." issue. Hope I got it though.

I did recreate the model subdir-switching bug though and was able to verify the fix worked for me.

Thanks again

SethRobinson commented 1 year ago

Fixed (for real this time?) in V0.54