Initial commit

This commit is contained in:
Samuele Lorefice
2023-11-09 05:04:51 +01:00
commit f400b78edf
9 changed files with 231 additions and 0 deletions

5
.gitignore vendored Normal file
View File

@@ -0,0 +1,5 @@
bin/
obj/
/packages/
riderModule.iml
/_ReSharper.Caches/

13
.idea/.idea.ComfySharp/.idea/.gitignore generated vendored Normal file
View File

@@ -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

View File

@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Encoding" addBOMForNewFiles="with BOM under Windows, with no BOM otherwise" />
</project>

View File

@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="UserContentModel">
<attachedFolders />
<explicitIncludes />
<explicitExcludes />
</component>
</project>

22
ComfySharp.sln Normal file
View File

@@ -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

138
ComfySharp/ComfyClient.cs Normal file
View File

@@ -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<Node> 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<Node>();
}
public async Task<string[]?> GetEmbeddings() {
string[]? embeddings = null;
var req = await client.GetAsync("/embeddings");
if (req is { IsSuccessStatusCode: true, Content: not null })
embeddings = await req.Content.ReadFromJsonAsync<string[]>();
return embeddings;
}
//???
public async Task<ImageInfo?> UploadImage() {
var req = await client.PostAsync("/upload/image", null );
if (req is { IsSuccessStatusCode: true, Content: not null })
return await req.Content.ReadFromJsonAsync<ImageInfo>();
return null;
}
//public async Task<>GetHistory
public async Task<List<Node>?> GetObjectInfo() {
var req = await client.GetAsync("/object_info");
if (req is { IsSuccessStatusCode: true, Content: not null }) {
var doc = await req.Content.ReadFromJsonAsync<JsonDocument>();
foreach (var node in doc.RootElement.EnumerateObject()) {
Node n;
}
}
return null;
}
public async Task<byte[]?> GetImage(string filename) {
var req = await client.GetAsync($"/image?{filename}");
if (req is { IsSuccessStatusCode: true, Content: not null })
return await req.Content.ReadFromJsonAsync<byte[]>();
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<PrimitiveType> Outputs { get; set; }
[DataMember(Name = "output_is_list")]
public List<bool> OutputIsList { get; set; }
[DataMember(Name = "output_name")]
public List<string> 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<Object> 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
}

View File

@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>

17
TestBed/Program.cs Normal file
View File

@@ -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();

15
TestBed/TestBed.csproj Normal file
View File

@@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\ComfySharp.csproj" />
<ProjectReference Include="..\ComfySharp\ComfySharp.csproj" />
</ItemGroup>
</Project>