From f400b78edf0157b2b6392cbc3e3c50e1efec6910 Mon Sep 17 00:00:00 2001 From: Samuele Lorefice Date: Thu, 9 Nov 2023 05:04:51 +0100 Subject: [PATCH] Initial commit --- .gitignore | 5 + .idea/.idea.ComfySharp/.idea/.gitignore | 13 ++ .idea/.idea.ComfySharp/.idea/encodings.xml | 4 + .idea/.idea.ComfySharp/.idea/indexLayout.xml | 8 ++ ComfySharp.sln | 22 +++ ComfySharp/ComfyClient.cs | 138 +++++++++++++++++++ ComfySharp/ComfySharp.csproj | 9 ++ TestBed/Program.cs | 17 +++ TestBed/TestBed.csproj | 15 ++ 9 files changed, 231 insertions(+) create mode 100644 .gitignore create mode 100644 .idea/.idea.ComfySharp/.idea/.gitignore create mode 100644 .idea/.idea.ComfySharp/.idea/encodings.xml create mode 100644 .idea/.idea.ComfySharp/.idea/indexLayout.xml create mode 100644 ComfySharp.sln create mode 100644 ComfySharp/ComfyClient.cs create mode 100644 ComfySharp/ComfySharp.csproj create mode 100644 TestBed/Program.cs create mode 100644 TestBed/TestBed.csproj diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..add57be --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +bin/ +obj/ +/packages/ +riderModule.iml +/_ReSharper.Caches/ \ No newline at end of file diff --git a/.idea/.idea.ComfySharp/.idea/.gitignore b/.idea/.idea.ComfySharp/.idea/.gitignore new file mode 100644 index 0000000..758be93 --- /dev/null +++ b/.idea/.idea.ComfySharp/.idea/.gitignore @@ -0,0 +1,13 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Rider ignored files +/modules.xml +/contentModel.xml +/projectSettingsUpdater.xml +/.idea.ComfySharp.iml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/.idea.ComfySharp/.idea/encodings.xml b/.idea/.idea.ComfySharp/.idea/encodings.xml new file mode 100644 index 0000000..df87cf9 --- /dev/null +++ b/.idea/.idea.ComfySharp/.idea/encodings.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/.idea/.idea.ComfySharp/.idea/indexLayout.xml b/.idea/.idea.ComfySharp/.idea/indexLayout.xml new file mode 100644 index 0000000..7b08163 --- /dev/null +++ b/.idea/.idea.ComfySharp/.idea/indexLayout.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/ComfySharp.sln b/ComfySharp.sln new file mode 100644 index 0000000..51437b2 --- /dev/null +++ b/ComfySharp.sln @@ -0,0 +1,22 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestBed", "TestBed\TestBed.csproj", "{894EB8E8-CED6-493A-87A3-06007BC2C51C}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ComfySharp", "ComfySharp\ComfySharp.csproj", "{D338D6F0-6A8F-4D4A-85E8-F88034BFC3D4}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {894EB8E8-CED6-493A-87A3-06007BC2C51C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {894EB8E8-CED6-493A-87A3-06007BC2C51C}.Debug|Any CPU.Build.0 = Debug|Any CPU + {894EB8E8-CED6-493A-87A3-06007BC2C51C}.Release|Any CPU.ActiveCfg = Release|Any CPU + {894EB8E8-CED6-493A-87A3-06007BC2C51C}.Release|Any CPU.Build.0 = Release|Any CPU + {D338D6F0-6A8F-4D4A-85E8-F88034BFC3D4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {D338D6F0-6A8F-4D4A-85E8-F88034BFC3D4}.Debug|Any CPU.Build.0 = Debug|Any CPU + {D338D6F0-6A8F-4D4A-85E8-F88034BFC3D4}.Release|Any CPU.ActiveCfg = Release|Any CPU + {D338D6F0-6A8F-4D4A-85E8-F88034BFC3D4}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection +EndGlobal diff --git a/ComfySharp/ComfyClient.cs b/ComfySharp/ComfyClient.cs new file mode 100644 index 0000000..01c6439 --- /dev/null +++ b/ComfySharp/ComfyClient.cs @@ -0,0 +1,138 @@ +using System.ComponentModel.DataAnnotations; +using System.Net; +using System.Net.Http.Json; +using System.Runtime.Serialization; +using System.Text.Json; +using System.Text.Json.Serialization; + +namespace ComfySharp; + +public class ComfyClient { + private HttpClient client; + private List nodes; + + public string BaseUrl { get; set; } + + public ComfyClient(string baseUrl) { + BaseUrl = baseUrl; + client = new HttpClient { + BaseAddress = new Uri(baseUrl), + DefaultRequestHeaders = { { "User-Agent", "ComfySharp" } } + }; + nodes= new List(); + } + + public async Task GetEmbeddings() { + string[]? embeddings = null; + + var req = await client.GetAsync("/embeddings"); + + if (req is { IsSuccessStatusCode: true, Content: not null }) + embeddings = await req.Content.ReadFromJsonAsync(); + + return embeddings; + } + + //??? + public async Task UploadImage() { + var req = await client.PostAsync("/upload/image", null ); + + if (req is { IsSuccessStatusCode: true, Content: not null }) + return await req.Content.ReadFromJsonAsync(); + return null; + } + + //public async Task<>GetHistory + + public async Task?> GetObjectInfo() { + var req = await client.GetAsync("/object_info"); + + if (req is { IsSuccessStatusCode: true, Content: not null }) { + var doc = await req.Content.ReadFromJsonAsync(); + + foreach (var node in doc.RootElement.EnumerateObject()) { + Node n; + + + } + } + + return null; + } + + public async Task GetImage(string filename) { + var req = await client.GetAsync($"/image?{filename}"); + + if (req is { IsSuccessStatusCode: true, Content: not null }) + return await req.Content.ReadFromJsonAsync(); + return null; + } +} + +[DataContract, JsonSerializable(typeof(Node))] +public class Node { + [DataMember(Name = "name")] + public required string Name { get; set; } + [DataMember(Name = "input")] + public required Input Input { get; set; } + [DataMember(Name = "output")] + public required List Outputs { get; set; } + [DataMember(Name = "output_is_list")] + public List OutputIsList { get; set; } + [DataMember(Name = "output_name")] + public List OutputNames { get; set; } + + +} + +[DataContract] +public struct Input { + public required InputField[] Required { get; set; } + public InputField[] Optional { get; set; } +} + +public struct InputField { + [DataMember(Name = "name")] + public required string Name { get; set; } + public required List Type { get; set; } + +} + +public enum PrimitiveType { + ANY, + CLIP, + CLIP_VISION, + CLIP_VISION_OUTPUT, + CONDITIONING, + CONTROL_NET, + FLOAT, + GLIGEN, + IMAGE, + INT, + LATENT, + MASK, + MODEL, + SAMPLER, + SIGMAS, + STRING, + STYLE_MODEL, + UPSCALE_MODEL, + VAE, +} + +[DataContract, JsonSerializable(typeof(ImageInfo))] +public class ImageInfo { + [DataMember(Name = "name")] + public required string Name { get; set; } + [DataMember(Name = "subfolder")] + public required string Subfolder { get; set; } + [DataMember(Name = "type")] + public required DirType Type { get; set; } +} + +[DataContract] +public enum DirType { + [DataMember(Name = "input")] Input, + [DataMember(Name = "temp")] Temp, + [DataMember(Name = "output")] Output +} \ No newline at end of file diff --git a/ComfySharp/ComfySharp.csproj b/ComfySharp/ComfySharp.csproj new file mode 100644 index 0000000..6836c68 --- /dev/null +++ b/ComfySharp/ComfySharp.csproj @@ -0,0 +1,9 @@ + + + + net7.0 + enable + enable + + + diff --git a/TestBed/Program.cs b/TestBed/Program.cs new file mode 100644 index 0000000..bcef539 --- /dev/null +++ b/TestBed/Program.cs @@ -0,0 +1,17 @@ +using System.Threading.Channels; +using ComfySharp; + +Console.WriteLine("Setting Up testing for default comfyUI server running on localhost:8188"); +var client = new ComfyClient("http://localhost:8188"); + +var info = await client.GetObjectInfo(); + +Console.WriteLine("Testing GetEmbeddings"); +var embeddings = await client.GetEmbeddings(); + +for (int i = 0; i < embeddings.Length; i++) { + Console.WriteLine($"Embedding {i}: {embeddings[i]}"); +} + +Console.WriteLine("Testing UploadImage"); +Console.ReadLine(); \ No newline at end of file diff --git a/TestBed/TestBed.csproj b/TestBed/TestBed.csproj new file mode 100644 index 0000000..03118a3 --- /dev/null +++ b/TestBed/TestBed.csproj @@ -0,0 +1,15 @@ + + + + Exe + net7.0 + enable + enable + + + + + + + +