-
Notifications
You must be signed in to change notification settings - Fork 0
/
CivitaiSyncExtension.cs
69 lines (60 loc) · 2.63 KB
/
CivitaiSyncExtension.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
using System.Net.Http;
using Newtonsoft.Json;
namespace CivitaiSyncExtension
{
public class CivitaiSyncExtension : SwarmUI.Extension
{
private static readonly HttpClient client = new HttpClient();
public override void Initialize()
{
base.Initialize();
// Register the extension
SwarmUI.Logger.Info("Initializing CivitaiSyncExtension...");
// Register JavaScript and other assets
ScriptFiles.Add("Assets/civitaiSync.js");
StyleSheetFiles.Add("Assets/civitaiSync.css"); // Optional
OtherAssets.Add("Images/example_icon.png"); // Optional
// Populate metadata
ExtensionName = "CivitaiSyncExtension";
Description = "Automatically syncs model descriptions from Civitai.";
ExtensionAuthor = "Your Name";
Version = "1.0.0";
License = "MIT";
// Hook into the model load event
SwarmUI.Events.OnModelLoad += OnModelLoad;
}
private async void OnModelLoad(Model model)
{
if (string.IsNullOrEmpty(model.Description))
{
string hash = model.Hash;
if (!string.IsNullOrEmpty(hash))
{
string url = $"https://civitai.com/api/v1/model-versions/by-hash/{hash}";
try
{
HttpResponseMessage response = await client.GetAsync(url);
if (response.IsSuccessStatusCode)
{
string json = await response.Content.ReadAsStringAsync();
dynamic data = JsonConvert.DeserializeObject(json);
// Update model data
model.Description = data.description;
model.Author = data.model.creator.username;
model.Tags = string.Join(", ", data.model.tags);
SwarmUI.Logger.Info($"Model {model.Name} updated with data from Civitai.");
}
else
{
SwarmUI.Logger.Warning($"Failed to fetch data for model {model.Name}: {response.StatusCode}");
}
}
catch (Exception ex)
{
SwarmUI.Logger.Error($"Error fetching data for model {model.Name}: {ex.Message}");
}
}
}
}
}
}