-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathHuggingFace.gs
92 lines (75 loc) · 3.41 KB
/
HuggingFace.gs
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
function retrieveArtistsData() {
const baseURL = "https://huggingface.co/spaces/mattthew/SDXL-artists-browser/resolve/main/";
const scriptUrl = baseURL + "artists_and_tags.js";
try {
// Fetch the content of the JavaScript file asynchronously
const response = UrlFetchApp.fetch(scriptUrl);
const content = response.getContentText();
// Find the artistsData array within the content
const match = content.match(/var artistsData = \[([\s\S]*?)\];/);
if (!match) {
Logger.log("artistsData not found in the JavaScript file");
return [];
}
// Extract the artistsData string and split it into individual artist entries
const artistsDataString = match[1];
const artistEntries = artistsDataString.split("],");
// Initialize the array to store artist data, with headers as the first row
const resultArray = [
["Last Name", "First Name", "Tags", "Is Unknown", "Artwork Image URL", "Landscape Image URL", "Portrait Image URL"]
];
// Loop through each artist entry and process the information
for (const entry of artistEntries) {
const trimmedEntry = entry.trim();
if (trimmedEntry.endsWith("]")) {
trimmedEntry += "]";
}
// Custom parsing logic to extract artist data
const artist = extractArtistData(trimmedEntry);
if (artist) {
const [last, first, tags, isUnknown] = artist;
// Construct image source URLs based on artist name
const src = baseURL + "images/SDXL_1_0_thumbs/";
const normalizedLast = last.normalize("NFD");
const normalizedFirst = first.normalize("NFD");
const tagsLowerCase = tags.map(tag => tag.toLowerCase()).join(", ");
const sanitizedTags = tagsLowerCase.replace(/, added-(\d|-)*/g, '');
const imgArtwork = src + (first === '' ? normalizedLast.replaceAll(' ', '_') : normalizedFirst.replaceAll(' ', '_') + '_' + normalizedLast.replaceAll(' ', '_')) + '-artwork.webp';
const imgLandscape = imgArtwork.replace('-artwork', '-landscape');
const imgPortrait = imgArtwork.replace('-artwork', '-portrait');
const artistInfo = [
normalizedLast,
normalizedFirst,
sanitizedTags,
isUnknown,
imgArtwork,
imgLandscape,
imgPortrait
];
resultArray.push(artistInfo);
}
}
return resultArray;
} catch (error) {
Logger.log("An error occurred:", error);
return [];
}
}
function extractArtistData(entry) {
var regex = /\[["']([^"']+)["'],["']([^"']*)["'],["']([^"']+)["'](?:,([^,]+))?/;
var match = entry.match(regex);
if (match) {
var last = match[1];
var first = match[2] ? match[2].replace(/\\'/g, "'") : "";
var tags = match[3].split("|");
var isUnknown = match[4] === undefined || match[4] === "false" ? false : true;
// Handle empty artist names
if (!last && !first) {
return null;
}
return [last, first, tags, isUnknown];
} else {
Logger.log("Error extracting artist data from entry: " + entry);
return null;
}
}