-
Notifications
You must be signed in to change notification settings - Fork 0
/
LoRA_inspector_helper.vbs
338 lines (220 loc) · 9.03 KB
/
LoRA_inspector_helper.vbs
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
Dim filesys
Set filesys = CreateObject("Scripting.FileSystemObject")
If WScript.Arguments.Count = 0 Then
If Not filesys.FileExists("LoRA_average_weights.txt") Then
Set filetxt = filesys.CreateTextFile("LoRA_average_weights.txt", True)
WScript.Echo "NOTE: LoRA_average_weights.txt was just created." & vbCrlf & _
" copy-paste the results of the inspection into it, and then save it." & vbCrlf & _
" after that, run this script file again."
WScript.Quit
'***** This is the 3rd way to use this script, to have it parse the contents of LoRA_average_weights.txt and display it
Else
X = int(0)
Set objFile = filesys.OpenTextFile("LoRA_average_weights.txt", 1, False)
Set filetxt = filesys.CreateTextFile("LoRA_average_weights.bat", True)
Set filetxtCSV = filesys.CreateTextFile("LoRA_average_weights.csv", True)
filetxtCSV.WriteLine("LoRA filename," & _
"UNet attention weight average magnitude," & _
"UNet attention weight average strength," & _
"Text Encoder weight average magnitude," & _
"Text Encoder weight average strength")
filetxt.WriteLine("@ECHO OFF")
filetxt.WriteLine()
filetxt.WriteLine("ECHO[")
csvToPut = "NULL"
Do Until objFile.AtEndOfStream
line = objFile.ReadLine
If filesys.FileExists(line) Then
fileName = filesys.GetFileName(line)
csvToPut = fileName
UNETWAM = "NULL"
UNETWAS = "NULL"
TEWAM = "NULL"
TEWAS = "NULL"
X = X + 1
If X > 1 Then
filetxt.WriteLine("ECHO ----------------------")
End If
filetxt.WriteLine("ECHO " & fileName)
End If
If instr(lCase(line), "unet weight average magnitude") Then
parts = split(line, ": ")
csvToPut = csvToPut & "," & parts(1)
filetxt.WriteLine("ECHO " & parts(0) & vbTab & parts(1))
ElseIf instr(lCase(line), "unet weight average strength") Then
parts = split(line, ": ")
csvToPut = csvToPut & "," & parts(1)
filetxt.WriteLine("ECHO " & parts(0) & vbTab & parts(1))
ElseIf instr(lCase(line), "text encoder") Then
If instr(lCase(line), "weight average magnitude") Then
parts = split(line, ": ")
csvToPut = csvToPut & "," & parts(1)
filetxt.WriteLine("ECHO " & parts(0) & vbTab & parts(1))
ElseIf instr(lCase(line), "weight average strength") Then
parts = split(line, ": ")
csvToPut = csvToPut & "," & parts(1)
filetxt.WriteLine("ECHO " & parts(0) & vbTab & parts(1))
filetxtCSV.WriteLine(csvToPut)
End If
End If
Loop
objFile.Close
filetxtCSV.Close
filetxt.WriteLine("ECHO ----------------------")
filetxt.WriteLine("ECHO[")
filetxt.WriteLine("PAUSE")
filetxt.WriteLine()
filetxt.Close
CreateObject("Wscript.Shell").Run "LoRA_average_weights.bat", 1, False
wscript.quit
End If
End If
Dim method, cd, folderPath, folder, files, file, jsonData, objFile
cd = filesys.GetAbsolutePathName(".")
folderPath = cd & "\meta"
method = WScript.Arguments(0)
' Check if the folder exists
If Not fileSys.FolderExists(folderPath) Then
WScript.Echo "ERROR: The meta folder does not exist" & vbCrlf _
& vbTab & "This script is used by option 2 in the LoRA_Inspector batch file"
WScript.Quit
End If
' method 1 is to use the json formatter, otherwise the folder path to move the JSON files to is provided
' ***** OPTION 2 STUFF IS HERE TO MOVE THE FILES *****
If method <> "1" Then
loraPath = WScript.Arguments(0)
' Get a reference to the folder with the script's outputs of json files
Set folder = filesys.GetFolder(folderPath)
' Get the collection of JSON files in the folder
Set files = folder.Files
'A folder was passed to the script
If filesys.FolderExists(loraPath) Then
' Get a reference to the folder where the LoRA files are
Set loraFolder = filesys.GetFolder(loraPath)
' Get the collection of LoRA safetensors files
Set loraFiles = loraFolder.Files
Y = int(0)
For Each loraFile in loraFiles
If LCase(filesys.GetExtensionName(loraFile.Name)) = "safetensors" Then
Y = Y + 1
End If
Next
X = int(0)
For Each loraFile in loraFiles
If LCase(filesys.GetExtensionName(loraFile.Name)) = "safetensors" Then
X = X + 1
wscript.echo "[" & X & " of " & Y & "] " & loraFile.Name & ".json"
moveLoraFile loraFile.Name, loraPath
End If
Next
'A single file was passed to the script
ElseIf filesys.FileExists(loraPath) Then
Set loraFile = filesys.GetFile(loraPath)
wscript.echo "[1 of 1] " & loraFile.Name & ".json"
loraFolder = loraFile.ParentFolder.Path
moveLoraFile loraFile.Name, loraFolder
End If
wscript.quit
End If
' ***** OPTION 1 STUFF IS BELOW TO FORMAT THE JSON FILES *****
Set jsonObj = CreateObject("Scripting.Dictionary")
Set json = CreateObject("Scripting.Dictionary")
' Get a reference to the folder with the script's outputs of json files
Set folder = filesys.GetFolder(folderPath)
' Get the collection of JSON files in the folder
Set files = folder.Files
Y = int(0)
For Each file in files
If LCase(fileSys.GetExtensionName(file.Name)) = "json" Then
Y = Y + 1
End If
Next
' Check if there are no JSON files in the folder
If Y = 0 Then
WScript.Echo "ERROR: There are no JSON files in the folder" & vbCrlf _
& vbTab & "This script is used by option 2 in the LoRA_Inspector batch file"
WScript.Quit
End If
X = int(0)
For Each file in files
If LCase(filesys.GetExtensionName(file.Name)) = "json" Then
X = X + 1
wscript.echo "[" & X & " of " & Y & "] " & file.Name
' Open the file for reading
Set objFile = filesys.OpenTextFile(file.Path)
' Read the contents of the file
jsonData = objFile.ReadAll
If InStr(jsonData, vbCrLf) Then
wscript.echo vbTab & "Skipped, already contains carriage returns"
Else
formattedJson = FormatJson(jsonData)
' Close the file
objFile.Close
wscript.sleep(1000)
Set filetxt = filesys.CreateTextFile(file.Path, True)
filetxt.WriteLine(formattedJson)
filetxt.Close
wscript.echo vbTab & "JSON has been formatted"
End If
End If
Next
objFile.Close
wscript.quit
' Function to format the JSON data with carriage returns and tabs
Function FormatJson(jsonData)
Dim formattedJson
Dim x
Dim i
x = 0 ' Number of tabs for indentation
formattedJson = "" ' Formatted JSON string
For i = 1 To Len(jsonData)
Dim currentChar
currentChar = Mid(jsonData, i, 1)
If currentChar = "{" Then
formattedJson = formattedJson & vbCrLf & String(x, vbTab) & currentChar & vbCrLf & String(x + 1, vbTab)
x = x + 1
ElseIf currentChar = "}" Then
formattedJson = formattedJson & vbCrLf & String(x - 1, vbTab) & currentChar
x = x - 1
ElseIf currentChar = "," Then
formattedJson = formattedJson & currentChar & vbCrLf & String(x, vbTab)
Else
formattedJson = formattedJson & currentChar
End If
Next
FormatJson = formattedJson
End Function
' Clean up the dictionary objects
Set jsonObj = Nothing
Set json = Nothing
Sub moveLoraFile(loraFileName, destinationFolder)
For Each file in files
If Len(file.name) > Len(loraFileName) Then
If loraFileName = Left(file.name, Len(loraFileName)) Then
message = "JSON file has been moved"
If filesys.FileExists(destinationFolder & "\" & loraFileName & ".json") Then
On Error Resume Next
filesys.DeleteFile(destinationFolder & "\" & loraFileName & ".json")
If Err.Number <> 0 Then
message = "ERROR: NOT ABLE TO DELETE EXISTING FILE. Error #" & Err.Number & vbCrlf & _
vbTab & Err.Description
Err.Clear
On Error GoTo 0
Else
message = message & " " & chr(40) & "existing file was replaced" & chr(41)
End If
wscript.sleep(1000)
End If
On Error Resume Next
filesys.MoveFile file, destinationFolder & "\" & loraFileName & ".json"
If Err.Number <> 0 Then
message = "ERROR: NOT ABLE TO COPY FILE. Error #" & Err.Number & vbCrlf & _
vbTab & Err.Description
Err.Clear
On Error GoTo 0
End If
wscript.Echo vbTab & message
End If
End If
Next
End Sub