Spaces:
Running
Running
Oleg Shulyakov
commited on
Commit
·
fa221c9
1
Parent(s):
4b18966
OOP draft
Browse files
app.py
CHANGED
@@ -56,6 +56,9 @@ class ModelProcessingConfig:
|
|
56 |
quant_config: QuantizationConfig
|
57 |
split_config: SplitConfig
|
58 |
output_config: OutputConfig
|
|
|
|
|
|
|
59 |
|
60 |
class GGUFConverterError(Exception):
|
61 |
"""Custom exception for GGUF conversion errors."""
|
@@ -321,6 +324,19 @@ class HuggingFaceModelProcessor:
|
|
321 |
print(f"Quantized model path: {os.path.abspath(quant_config.quantized_gguf)}")
|
322 |
return quant_config.quantized_gguf
|
323 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
324 |
def _generate_readme(self, outdir: str, token: str, model_id: str,
|
325 |
new_repo_id: str, gguf_name: str) -> str:
|
326 |
"""Generate README.md for the quantized model."""
|
@@ -384,23 +400,16 @@ llama-server --hf-repo "{new_repo_id}" --hf-file "{gguf_name}" -c 4096
|
|
384 |
self._quantize_model(quant_config)
|
385 |
|
386 |
# Create empty repo
|
387 |
-
|
388 |
-
new_repo_url = api.create_repo(
|
389 |
-
repo_id=output_config.repo_name,
|
390 |
-
exist_ok=True,
|
391 |
-
private=output_config.private_repo
|
392 |
-
)
|
393 |
-
new_repo_id = new_repo_url.repo_id
|
394 |
-
print("Repo created successfully!", new_repo_url)
|
395 |
|
396 |
# Upload model
|
397 |
if split_config.enabled:
|
398 |
print(f"Splitting quantized model: {os.path.abspath(quant_config.quantized_gguf)}")
|
399 |
-
self._split_and_upload_model(quant_config.quantized_gguf, processing_config.outdir, new_repo_id, processing_config.token, split_config)
|
400 |
else:
|
401 |
try:
|
402 |
print(f"Uploading quantized model: {os.path.abspath(quant_config.quantized_gguf)}")
|
403 |
-
self._upload_file(processing_config.token, quant_config.quantized_gguf, output_config.filename, new_repo_id)
|
404 |
except Exception as e:
|
405 |
raise GGUFConverterError(f"Error uploading quantized model: {e}")
|
406 |
|
@@ -408,21 +417,16 @@ llama-server --hf-repo "{new_repo_id}" --hf-file "{gguf_name}" -c 4096
|
|
408 |
if quant_config.use_imatrix and os.path.isfile(quant_config.imatrix_file):
|
409 |
try:
|
410 |
print(f"Uploading imatrix.dat: {os.path.abspath(quant_config.imatrix_file)}")
|
411 |
-
self._upload_file(processing_config.token, quant_config.imatrix_file, f"{processing_config.model_name}-imatrix.dat", new_repo_id)
|
412 |
except Exception as e:
|
413 |
raise GGUFConverterError(f"Error uploading imatrix.dat: {e}")
|
414 |
|
415 |
# Upload README.md
|
416 |
-
readme_path = self._generate_readme(processing_config.outdir, processing_config.token, processing_config.model_id, new_repo_id, output_config.filename)
|
417 |
-
self._upload_file(processing_config.token, readme_path, "README.md", new_repo_id)
|
418 |
|
419 |
print(f"Uploaded successfully with {quant_config.imatrix_method if quant_config.use_imatrix else quant_config.method} option!")
|
420 |
|
421 |
-
return (
|
422 |
-
f'<h1>✅ DONE</h1><br/>Find your repo here: <a href="{new_repo_url}" target="_blank" style="text-decoration:underline">{new_repo_id}</a>',
|
423 |
-
"llama.png",
|
424 |
-
)
|
425 |
-
|
426 |
|
427 |
class GGUFConverterUI:
|
428 |
"""Gradio UI for the GGUF Converter."""
|
@@ -793,7 +797,12 @@ class GGUFConverterUI:
|
|
793 |
)
|
794 |
|
795 |
# Call the processor's main method with the config object
|
796 |
-
|
|
|
|
|
|
|
|
|
|
|
797 |
|
798 |
except Exception as e:
|
799 |
print(f"Error processing model: {e}")
|
|
|
56 |
quant_config: QuantizationConfig
|
57 |
split_config: SplitConfig
|
58 |
output_config: OutputConfig
|
59 |
+
# Generated values - These will be set during processing
|
60 |
+
new_repo_url: str = field(default="", init=False)
|
61 |
+
new_repo_id: str = field(default="", init=False)
|
62 |
|
63 |
class GGUFConverterError(Exception):
|
64 |
"""Custom exception for GGUF conversion errors."""
|
|
|
324 |
print(f"Quantized model path: {os.path.abspath(quant_config.quantized_gguf)}")
|
325 |
return quant_config.quantized_gguf
|
326 |
|
327 |
+
def _create_empty_repo(self, processing_config: ModelProcessingConfig):
|
328 |
+
api = HfApi(token=processing_config.token)
|
329 |
+
new_repo_url = api.create_repo(
|
330 |
+
repo_id=processing_config.output_config.repo_name,
|
331 |
+
exist_ok=True,
|
332 |
+
private=processing_config.output_config.private_repo
|
333 |
+
)
|
334 |
+
processing_config.new_repo_url = new_repo_url.url
|
335 |
+
processing_config.new_repo_id = new_repo_url.repo_id
|
336 |
+
print("Repo created successfully!", processing_config.new_repo_url)
|
337 |
+
|
338 |
+
return new_repo_url
|
339 |
+
|
340 |
def _generate_readme(self, outdir: str, token: str, model_id: str,
|
341 |
new_repo_id: str, gguf_name: str) -> str:
|
342 |
"""Generate README.md for the quantized model."""
|
|
|
400 |
self._quantize_model(quant_config)
|
401 |
|
402 |
# Create empty repo
|
403 |
+
self._create_empty_repo(processing_config)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
404 |
|
405 |
# Upload model
|
406 |
if split_config.enabled:
|
407 |
print(f"Splitting quantized model: {os.path.abspath(quant_config.quantized_gguf)}")
|
408 |
+
self._split_and_upload_model(quant_config.quantized_gguf, processing_config.outdir, processing_config.new_repo_id, processing_config.token, split_config)
|
409 |
else:
|
410 |
try:
|
411 |
print(f"Uploading quantized model: {os.path.abspath(quant_config.quantized_gguf)}")
|
412 |
+
self._upload_file(processing_config.token, quant_config.quantized_gguf, output_config.filename, processing_config.new_repo_id)
|
413 |
except Exception as e:
|
414 |
raise GGUFConverterError(f"Error uploading quantized model: {e}")
|
415 |
|
|
|
417 |
if quant_config.use_imatrix and os.path.isfile(quant_config.imatrix_file):
|
418 |
try:
|
419 |
print(f"Uploading imatrix.dat: {os.path.abspath(quant_config.imatrix_file)}")
|
420 |
+
self._upload_file(processing_config.token, quant_config.imatrix_file, f"{processing_config.model_name}-imatrix.dat", processing_config.new_repo_id)
|
421 |
except Exception as e:
|
422 |
raise GGUFConverterError(f"Error uploading imatrix.dat: {e}")
|
423 |
|
424 |
# Upload README.md
|
425 |
+
readme_path = self._generate_readme(processing_config.outdir, processing_config.token, processing_config.model_id, processing_config.new_repo_id, output_config.filename)
|
426 |
+
self._upload_file(processing_config.token, readme_path, "README.md", processing_config.new_repo_id)
|
427 |
|
428 |
print(f"Uploaded successfully with {quant_config.imatrix_method if quant_config.use_imatrix else quant_config.method} option!")
|
429 |
|
|
|
|
|
|
|
|
|
|
|
430 |
|
431 |
class GGUFConverterUI:
|
432 |
"""Gradio UI for the GGUF Converter."""
|
|
|
797 |
)
|
798 |
|
799 |
# Call the processor's main method with the config object
|
800 |
+
self.processor.process_model(processing_config)
|
801 |
+
|
802 |
+
return (
|
803 |
+
f'<h1>✅ DONE</h1><br/>Find your repo here: <a href="{processing_config.new_repo_url}" target="_blank" style="text-decoration:underline">{processing_config.new_repo_id}</a>',
|
804 |
+
"llama.png",
|
805 |
+
)
|
806 |
|
807 |
except Exception as e:
|
808 |
print(f"Error processing model: {e}")
|