Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Usage with API #4

Open
AldeRoberge opened this issue Oct 16, 2024 · 1 comment
Open

Usage with API #4

AldeRoberge opened this issue Oct 16, 2024 · 1 comment

Comments

@AldeRoberge
Copy link

AldeRoberge commented Oct 16, 2024

If anyone is having trouble to get this to work with the API, here is the json that worked for me :

{
                                    "prompt": "<PROMPT>", // Your prompt here
                                    "steps": 20,
                                    "model": "dreamshaper_8", // Your model here
                                    "script_name": "ABG Remover",
                                    "script_args": [
                                      true,  
                                      false, 
                                      "",    
                                      "FFFFFF", 
                                      false   
                                    ]
                                  }

Complete example (C#) :

using System.Text;
using System.Text.Json;

namespace ADG_ImageGenerator;

public interface IImageGenerator
{
    public Task<string?> GenerateImage(string prompt);
}

public class ImageGenerator : IImageGenerator
{
    private const string ApiUrl = "http://127.0.0.1:7860";
    private const string Text2ImageEndpoint = "/sdapi/v1/txt2img";

    /// <summary>
    /// Generates an image from the given prompt using the API.
    /// </summary>
    /// <param name="prompt">The prompt to generate the image</param>
    /// <returns>The base64 image string</returns>
    public async Task<string?> GenerateImage(string prompt)
    {
        using (var client = new HttpClient())
        {
            try
            {
                var requestJson = """
                                  {
                                    "prompt": "<PROMPT>",
                                    "steps": 20,
                                    "model": "dreamshaper_8",
                                    "script_name": "ABG Remover",
                                    "script_args": [
                                      true,  
                                      false, 
                                      "",    
                                      "FFFFFF", 
                                      false   
                                    ]
                                  }
                                  """;

                requestJson = requestJson.Replace("<PROMPT>", prompt);

                var jsonContent = new StringContent(requestJson, Encoding.UTF8,
                    "application/json");

                var response = await client.PostAsync($"{ApiUrl}{Text2ImageEndpoint}", jsonContent);

                if (response.IsSuccessStatusCode)
                {
                    var responseBody = await response.Content.ReadAsStringAsync();

                    // Assuming response contains base64 image string under "image" key
                    var jsonResponse = JsonDocument.Parse(responseBody);
                    if (jsonResponse.RootElement.TryGetProperty("images", out var imageProperty))
                    {
                        // Extract the first image from the array of images
                        var base64Image = imageProperty[0].GetString();
                        return base64Image;
                    }

                    Console.WriteLine("Image not found in the response.");
                }
                else
                {
                    Console.WriteLine($"API call failed with status code: {response.StatusCode}");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Exception occurred: {ex.Message}");
            }
        }

        return null;
    }
}

Maybe we could include the json somewhere in the documentation?

Thank you

@GeekyGhost
Copy link
Owner

Thanks, I'll take a look at this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants