Skip to content

Commit 4f885af

Browse files
committed
allow input filetype .gguf for hidream
1 parent 521ad75 commit 4f885af

File tree

1 file changed

+102
-47
lines changed

1 file changed

+102
-47
lines changed

EasyQuantizationGUI.py

Lines changed: 102 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def scroll_entry_to_end(entry):
2626
entry.xview_moveto(1)
2727

2828
def browse_file(entry):
29-
file_path = filedialog.askopenfilename(filetypes=[("Model files", "*.safetensors *.sft")])
29+
file_path = filedialog.askopenfilename(filetypes=[("Model files", "*.safetensors *.gguf *.sft")])
3030
if file_path:
3131
file_path = file_path.replace('\\', '/') # Ensure forward slashes
3232
entry.delete(0, tk.END)
@@ -102,6 +102,11 @@ def run_llama_quantize():
102102
messagebox.showerror("Error", "Please select both input and output files.")
103103
return
104104

105+
# Check if input and output files are the same
106+
if os.path.abspath(input_file) == os.path.abspath(output_file):
107+
messagebox.showerror("Error", "Input and output files cannot be the same.")
108+
return
109+
105110
output_dir = os.path.dirname(output_file)
106111
required_space = 40_000_000_000 # ~40 GB (a bit more than 36.5 GB)
107112
available_space = shutil.disk_usage(output_dir).free
@@ -116,61 +121,95 @@ def run_llama_quantize():
116121

117122
# Clear previous log
118123
process_text.delete('1.0', tk.END)
119-
process_text.insert(tk.END, "Starting conversion process...\n")
120-
process_text.see(tk.END)
121124
root.update()
122125

123-
# Convert the input file to GGUF format
124-
convert_py_path = resource_path("convert.py")
125-
output_dir = os.path.dirname(output_file)
126-
temp_gguf_file = os.path.join(output_dir, "temporary_file_during_quantization")
126+
is_input_gguf = input_file.lower().endswith(".gguf")
127+
temp_gguf_file = None # Initialize temp_gguf_file
128+
129+
if not is_input_gguf:
130+
process_text.insert(tk.END, "Starting conversion process (Safetensors/SFT -> GGUF)...\n")
131+
process_text.see(tk.END)
132+
root.update()
133+
134+
# Convert the input file to GGUF format
135+
convert_py_path = resource_path("convert.py")
136+
output_dir = os.path.dirname(output_file)
137+
# Use a more descriptive temporary file name based on the output file
138+
output_name, _ = os.path.splitext(os.path.basename(output_file))
139+
temp_gguf_file = os.path.join(output_dir, f"{output_name}_temp_conversion.gguf")
140+
141+
142+
# Add cleanup of existing temp file
143+
if os.path.exists(temp_gguf_file):
144+
try:
145+
os.remove(temp_gguf_file)
146+
process_text.insert(tk.END, "Cleaned up existing temporary file.\n")
147+
process_text.see(tk.END)
148+
root.update()
149+
except Exception as e:
150+
process_text.insert(tk.END, f"Error cleaning up temporary file: {e}\n")
151+
process_text.see(tk.END)
152+
root.update()
153+
enable_ui()
154+
return
127155

128-
# Add cleanup of existing temp file
129-
if os.path.exists(temp_gguf_file):
130156
try:
131-
os.remove(temp_gguf_file)
132-
process_text.insert(tk.END, "Cleaned up existing temporary file.\n")
157+
startupinfo = subprocess.STARTUPINFO()
158+
startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
159+
startupinfo.wShowWindow = subprocess.SW_HIDE
160+
161+
# Get the Python executable path from the current environment
162+
pythonpath = sys.executable
163+
164+
process = subprocess.Popen([pythonpath, convert_py_path, "--src", input_file, "--dst", temp_gguf_file],
165+
stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True,
166+
bufsize=1, universal_newlines=True, startupinfo=startupinfo)
167+
168+
for line in process.stdout:
169+
process_text.insert(tk.END, line)
170+
process_text.see(tk.END)
171+
root.update()
172+
173+
process.wait()
174+
if process.returncode != 0:
175+
raise subprocess.CalledProcessError(process.returncode, process.args)
176+
177+
process_text.insert(tk.END, "Conversion completed successfully.\n")
133178
process_text.see(tk.END)
134179
root.update()
135-
except Exception as e:
136-
process_text.insert(tk.END, f"Error cleaning up temporary file: {e}\n")
180+
181+
except subprocess.CalledProcessError as e:
182+
process_text.insert(tk.END, f"Error converting file: {e}\n")
183+
process_text.insert(tk.END, f"Command: {e.cmd}\n")
184+
process_text.insert(tk.END, f"Return code: {e.returncode}\n")
137185
process_text.see(tk.END)
138186
root.update()
187+
# Clean up the temporary file even if conversion fails
188+
if temp_gguf_file and os.path.exists(temp_gguf_file):
189+
os.remove(temp_gguf_file)
139190
enable_ui()
140191
return
141-
142-
try:
143-
startupinfo = subprocess.STARTUPINFO()
144-
startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
145-
startupinfo.wShowWindow = subprocess.SW_HIDE
146-
147-
# Get the Python executable path from the current environment
148-
pythonpath = sys.executable
149-
150-
process = subprocess.Popen([pythonpath, convert_py_path, "--src", input_file, "--dst", temp_gguf_file],
151-
stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True,
152-
bufsize=1, universal_newlines=True, startupinfo=startupinfo)
153-
154-
for line in process.stdout:
155-
process_text.insert(tk.END, line)
192+
except Exception as e: # Catch other potential errors during conversion
193+
process_text.insert(tk.END, f"An unexpected error occurred during conversion: {e}\n")
156194
process_text.see(tk.END)
157195
root.update()
196+
if temp_gguf_file and os.path.exists(temp_gguf_file):
197+
os.remove(temp_gguf_file)
198+
enable_ui()
199+
return
158200

159-
process.wait()
160-
if process.returncode != 0:
161-
raise subprocess.CalledProcessError(process.returncode, process.args)
162-
163-
process_text.insert(tk.END, "Conversion completed successfully.\n")
164-
except subprocess.CalledProcessError as e:
165-
process_text.insert(tk.END, f"Error converting file: {e}\n")
166-
process_text.insert(tk.END, f"Command: {e.cmd}\n")
167-
process_text.insert(tk.END, f"Return code: {e.returncode}\n")
168-
process_text.see(tk.END)
169-
root.update()
170-
enable_ui()
171-
return
201+
# --- End of conversion block ---
202+
else:
203+
process_text.insert(tk.END, "Input is already GGUF. Skipping conversion step.\n")
204+
process_text.see(tk.END)
205+
root.update()
206+
# If input is GGUF, llama-quantize will read directly from it
207+
quantize_input_file = input_file
208+
209+
# Determine the input file for the quantization step
210+
quantize_input_file = temp_gguf_file if temp_gguf_file else input_file
172211

173-
# Quantize the converted file
212+
# Quantize the file (either the temporary one or the original GGUF)
174213
llama_quantize_path = resource_path("llama-quantize.exe")
175214
process_text.insert(tk.END, "Starting quantization process...\n")
176215
process_text.see(tk.END)
@@ -181,7 +220,8 @@ def run_llama_quantize():
181220
startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
182221
startupinfo.wShowWindow = subprocess.SW_HIDE
183222

184-
process = subprocess.Popen([llama_quantize_path, temp_gguf_file, output_file, quantize_level],
223+
# Use quantize_input_file determined above
224+
process = subprocess.Popen([llama_quantize_path, quantize_input_file, output_file, quantize_level],
185225
stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True,
186226
bufsize=1, universal_newlines=True, startupinfo=startupinfo)
187227

@@ -192,6 +232,9 @@ def run_llama_quantize():
192232

193233
process.wait()
194234
if process.returncode != 0:
235+
# If quantization failed and we used a temp file, report the temp file name
236+
if temp_gguf_file:
237+
process_text.insert(tk.END, f"Quantization command failed on temporary file: {temp_gguf_file}\n")
195238
raise subprocess.CalledProcessError(process.returncode, process.args)
196239

197240
process_text.insert(tk.END, "Quantization completed successfully.\n")
@@ -201,12 +244,24 @@ def run_llama_quantize():
201244
process_text.insert(tk.END, f"Return code: {e.returncode}\n")
202245
process_text.see(tk.END)
203246
root.update()
247+
except Exception as e: # Catch other potential errors during quantization
248+
process_text.insert(tk.END, f"An unexpected error occurred during quantization: {e}\n")
249+
process_text.see(tk.END)
250+
root.update()
204251
finally:
205-
# Clean up the temporary file
206-
if os.path.exists(temp_gguf_file):
207-
os.remove(temp_gguf_file)
252+
# Clean up the temporary file if it was created
253+
if temp_gguf_file and os.path.exists(temp_gguf_file):
254+
try:
255+
os.remove(temp_gguf_file)
256+
process_text.insert(tk.END, "Cleaned up temporary conversion file.\n")
257+
process_text.see(tk.END)
258+
root.update()
259+
except Exception as e:
260+
process_text.insert(tk.END, f"Error cleaning up temporary file {temp_gguf_file}: {e}\n")
261+
process_text.see(tk.END)
262+
root.update()
208263

209-
process_text.insert(tk.END, "Quantization process completed.")
264+
process_text.insert(tk.END, "Process finished.\n") # Changed message slightly
210265
process_text.see(tk.END)
211266
root.update()
212267

0 commit comments

Comments
 (0)