2
2
import subprocess
3
3
import os
4
4
import sys
5
- from kohya_gui import common_gui , custom_logging
5
+ from kohya_gui import common_gui
6
6
7
7
# Define the script directory
8
8
scriptdir = os .path .dirname (os .path .dirname (os .path .realpath (__file__ )))
@@ -30,28 +30,6 @@ def extract_lora_new(
30
30
verbose ,
31
31
no_metadata ,
32
32
):
33
- # This function will be implemented in a subsequent step
34
- print ("Extract LoRA button clicked. Functionality to be implemented." )
35
- print (f"Model Tuned: { model_tuned } " )
36
- print (f"Model Original: { model_org } " )
37
- print (f"Save To: { save_to } " )
38
- print (f"Save Precision: { save_precision } " )
39
- print (f"Load Precision: { load_precision } " )
40
- print (f"Dimension: { dim } " )
41
- print (f"Conv Dimension: { conv_dim } " )
42
- print (f"Device: { device } " )
43
- print (f"SDXL: { sdxl } " )
44
- print (f"v2: { v2 } " )
45
- print (f"v_parameterization: { v_parameterization } " )
46
- print (f"Clamp Quantile: { clamp_quantile } " )
47
- print (f"Min Diff: { min_diff } " )
48
- print (f"Load Original Model To: { load_original_model_to } " )
49
- print (f"Load Tuned Model To: { load_tuned_model_to } " )
50
- print (f"Dynamic Method: { dynamic_method } " )
51
- print (f"Dynamic Param: { dynamic_param } " )
52
- print (f"Verbose: { verbose } " )
53
- print (f"No Metadata: { no_metadata } " )
54
-
55
33
# Construct the command
56
34
command = [
57
35
PYTHON ,
@@ -67,13 +45,9 @@ def extract_lora_new(
67
45
command .extend (["--save_to" , save_to ])
68
46
if save_precision :
69
47
command .extend (["--save_precision" , save_precision ])
70
- if load_precision and load_precision != "None" : # Handle None case
48
+ if load_precision and load_precision != "None" :
71
49
command .extend (["--load_precision" , load_precision ])
72
50
command .extend (["--dim" , str (dim )])
73
- # conv_dim defaults to dim if not provided or 0 in the script,
74
- # so we only add it if it's explicitly set to a non-zero value by the user that is different from dim,
75
- # or if the script requires it even if it's the same as dim (need to check script logic)
76
- # For now, pass it if it's > 0. The script itself handles the default.
77
51
if conv_dim > 0 :
78
52
command .extend (["--conv_dim" , str (conv_dim )])
79
53
if device :
@@ -82,7 +56,7 @@ def extract_lora_new(
82
56
command .append ("--sdxl" )
83
57
if v2 :
84
58
command .append ("--v2" )
85
- if v_parameterization : # Only relevant if v2 is true, but script might handle it
59
+ if v_parameterization :
86
60
command .append ("--v_parameterization" )
87
61
command .extend (["--clamp_quantile" , str (clamp_quantile )])
88
62
command .extend (["--min_diff" , str (min_diff )])
@@ -95,9 +69,6 @@ def extract_lora_new(
95
69
96
70
if dynamic_method and dynamic_method != "None" :
97
71
command .extend (["--dynamic_method" , dynamic_method ])
98
- # dynamic_param is only needed for certain methods, script should handle if it's missing
99
- # but we should only pass it if a method requiring it is selected.
100
- # This requires knowing which methods need params. Assuming for now all non-"None" methods might use it if provided.
101
72
command .extend (["--dynamic_param" , str (dynamic_param )])
102
73
103
74
if verbose :
@@ -106,36 +77,61 @@ def extract_lora_new(
106
77
command .append ("--no_metadata" )
107
78
108
79
# Run the script
109
- print (f"Running command: { ' ' .join (command )} " )
110
-
111
- log_stream = custom_logging .LogStreaming ()
80
+ print (f"Running command: { ' ' .join (command )} " ) # Log to console
112
81
82
+ all_logs = ""
113
83
try :
84
+ # Use Popen to capture stdout and stderr
114
85
process = subprocess .Popen (
115
86
command ,
116
87
stdout = subprocess .PIPE ,
117
- stderr = subprocess .STDOUT ,
88
+ stderr = subprocess .PIPE , # Capture stderr separately
118
89
text = True ,
119
90
bufsize = 1 ,
120
91
universal_newlines = True ,
121
92
)
122
93
123
- for line in iter (process .stdout .readline , '' ):
124
- log_stream .log (line .strip ())
125
- # Optionally print to console during development/debugging
126
- # print(line.strip())
127
- process .wait ()
94
+ # Stream stdout
95
+ if process .stdout :
96
+ for line in iter (process .stdout .readline , '' ):
97
+ line = line .strip ()
98
+ if line :
99
+ print (line ) # Log to console
100
+ all_logs += line + '\n '
101
+ yield all_logs # Yield accumulated logs
128
102
103
+ # Stream stderr
104
+ # After stdout is exhausted, check stderr
105
+ stderr_output = ""
106
+ if process .stderr :
107
+ for line in iter (process .stderr .readline , '' ):
108
+ line = line .strip ()
109
+ if line :
110
+ print (f"Error: { line } " ) # Log to console
111
+ stderr_output += f"ERROR: { line } \n "
112
+
113
+ process .wait () # Wait for the process to complete
114
+
115
+ # Append any stderr output to all_logs after stdout and process completion
116
+ if stderr_output :
117
+ all_logs += "\n --- Errors/Warnings ---\n " + stderr_output
118
+ yield all_logs
119
+
129
120
if process .returncode == 0 :
130
- return "LoRA extraction completed successfully.", log_stream . get_logs ()
121
+ all_logs += " \n LoRA extraction completed successfully."
131
122
else :
132
- return f"Error during LoRA extraction. Return code: { process .returncode } " , log_stream .get_logs ()
123
+ all_logs += f"\n Error during LoRA extraction. Return code: { process .returncode } "
124
+
125
+ yield all_logs
133
126
134
127
except Exception as e :
135
- return f"Failed to run script: { e } " , log_stream .get_logs ()
128
+ all_logs += f"\n Failed to run script: { e } "
129
+ yield all_logs
136
130
finally :
137
- log_stream .close ()
138
-
131
+ if process .stdout :
132
+ process .stdout .close ()
133
+ if process .stderr :
134
+ process .stderr .close ()
139
135
140
136
# Gradio UI function
141
137
def gradio_extract_lora_new_tab (headless = False ):
@@ -364,7 +360,7 @@ def gradio_extract_lora_new_tab(headless=False):
364
360
verbose ,
365
361
no_metadata ,
366
362
],
367
- outputs = [gr . Textbox ( label = "Status" , interactive = False ), output_logs ], # Two outputs: status and logs
363
+ outputs = [output_logs ], # Output to the log textbox
368
364
show_progress = "full"
369
365
)
370
366
0 commit comments