Stability-AI / rest-api-support

Stability REST API examples, issues, and discussions | https://api.stability.ai
107 stars 21 forks source link

Difference in the Images generated by UI and RestAPI #27

Closed HasnainKhanNiazi closed 1 year ago

HasnainKhanNiazi commented 1 year ago

hey guys, I am using the stable diffusion within C# and I have replicated the curl command within C# and it's working good but I am not satisfied with the generated image,

string new_text = "A fox in the jungle, low poly";
var request = new HttpRequestMessage
        {
            Method = HttpMethod.Post,
            RequestUri = new Uri("https://api.stability.ai/v1/generation/stable-diffusion-v1-5/text-to-image"),
            Headers =
            {
                { "Accept", "image/png" },
                { "Authorization", "API_KEY" },
                { "Cookie", "__cf_bm=FMRRcWzshoDLeugLIOuR49M55SurpuyHsp1trp5ooPk-1681039461-0-AXhrLXucsQywuiiyfV1/YKGADKSRzWJ6g5vbIozpe0xTpWYaYqM0eG2Nv778H6EuxdZqbHj0fPVUOdsrxxRIHqY=" }
            },
            Content = new StringContent($@"{{
            ""text_prompts"": [
                {{
                    ""text"": ""{new_text}""
                }}
            ],
            ""cfg_scale"": 8,
            ""height"": 512,
            ""width"": 512,
            ""samples"": 1,
            ""generation.sampler"": ""SAMPLER_K_DPMPP_2M"",
            ""steps"": 50,
            ""engine"": ""stable-diffusion-xl-beta-v2-2-2""
        }}", Encoding.UTF8, "application/json")
        };

And the generated image looks like this;

image

Now, I have also used the DreamStudio UI to generate the image with the same prompt and the parameters and the result looks much better, you can see the result below, what could be wrong in the above code.

4045498837_There is a fox in the jungle_xl-beta-v2-2-2

johnsabath commented 1 year ago

Hello!

I'm assuming that you were using the SDXL model on DreamStudio, and are also attempting to use SDXL here based on ""engine"": ""stable-diffusion-xl-beta-v2-2-2"" in the POST body.

If that's the case, then instead of specifying the engine in the POST body, you'll want to specify the engine in the RequestUri, like this:

RequestUri = new Uri("https://api.stability.ai/v1/generation/stable-diffusion-xl-beta-v2-2-2/text-to-image")

HasnainKhanNiazi commented 1 year ago

Hello @johnsabath yes, that was the exact issue. Thanks for pointing out this issue.

HasnainKhanNiazi commented 1 year ago

Hi @johnsabath thanks for the help before, as I mentioned in my previous comment that the problem is solved by changing the URI and it solved the problem but now, without changing anything, we are getting StatusCode: 400, ReasonPhrase: 'Bad Request', if I change the URI to the "https://api.stability.ai/v1/generation/stable-diffusion-v1-5/text-to-image" then it is working fine but if I change the URI to the "https://api.stability.ai/v1/generation/stable-diffusion-xl-beta-v2-2-2/text-to-image" then I am getting the 400 bad request error.

It was working completely fine and we are having this problem without changing anything.