File size: 15,688 Bytes
4ff0ec9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7bb28c8
4ff0ec9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
#!/usr/bin/env python3
"""
Gradio wrapper app to convert safetensors / Hugging Face diffusers into GGUF and/or ONNX,
with environment & version collection for reproducibility.

Usage:
  pip install -r requirements.txt
  python app.py
"""

import gradio as gr
import os
import shutil
import subprocess
import tempfile
import time
import zipfile
import shlex
import sys
import platform
from pathlib import Path
from huggingface_hub import snapshot_download, HfApi
from importlib import metadata

# -------------------------
# DEFAULT COMMAND TEMPLATES
DEFAULT_GGUF_CMD_LLM = (
    "python /path/to/llama.cpp/tools/convert.py --model {INPUT_PATH} --outfile {OUTPUT_PATH} --format gguf"
)
DEFAULT_ONNX_CMD_SDXL = (
    "python /path/to/convert_diffusers_to_onnx.py --repo {INPUT_DIR} --outdir {OUTPUT_DIR} --opset {OPSET}"
)
DEFAULT_GGUF_CMD_GENERIC = "echo 'Please edit GGUF command template in the UI to a real converter' && false"
DEFAULT_ONNX_CMD_GENERIC = "echo 'Please edit ONNX command template in the UI to a real converter' && false"

# Packages to probe by default (you can change this list in code)
DEFAULT_PACKAGES = [
    "gradio",
    "huggingface_hub",
    "transformers",
    "diffusers",
    "onnx",
    "onnxruntime",
    "optimum",
    "bitsandbytes",
    "accelerate",
]

# -------------------------
# Utilities
def run_and_stream(cmd, cwd=None):
    """
    Run command (string) and yield stdout/stderr lines for Gradio streaming.
    Returns exit code at the end of the generator.
    """
    if isinstance(cmd, str):
        process = subprocess.Popen(
            cmd,
            stdout=subprocess.PIPE,
            stderr=subprocess.STDOUT,
            cwd=cwd,
            shell=True,
            universal_newlines=True,
            bufsize=1,
            executable="/bin/bash",
        )
    else:
        process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, cwd=cwd, universal_newlines=True, bufsize=1)

    output_accum = []
    for line in iter(process.stdout.readline, ""):
        if line is None:
            break
        output_accum.append(line)
        yield "".join(output_accum)
    process.stdout.close()
    return_code = process.wait()
    yield "".join(output_accum) + f"\n[PROCESS EXIT CODE {return_code}]\n"
    return return_code

def zip_dir(dir_path, zip_path):
    with zipfile.ZipFile(zip_path, "w", zipfile.ZIP_DEFLATED) as zf:
        for root, _, files in os.walk(dir_path):
            for f in files:
                absf = os.path.join(root, f)
                arcname = os.path.relpath(absf, dir_path)
                zf.write(absf, arcname)
    return zip_path

def get_package_version(pkg_name):
    try:
        return metadata.version(pkg_name)
    except metadata.PackageNotFoundError:
        return None
    except Exception:
        return None

def run_version_command(cmd, cwd=None, timeout=30):
    """
    Run a single version-check command and return (success, output).
    """
    try:
        out = subprocess.check_output(cmd, shell=True, stderr=subprocess.STDOUT, cwd=cwd, timeout=timeout, executable="/bin/bash")
        return True, out.decode("utf-8", errors="replace")
    except subprocess.CalledProcessError as e:
        return False, e.output.decode("utf-8", errors="replace") if e.output else str(e)
    except Exception as e:
        return False, str(e)

def collect_environment_info(outputs_dir, hf_model_id=None, extra_version_cmds=None, packages=None):
    """
    Collect environment and version information and write to environment.txt in outputs_dir.
    Returns a string with the summary.
    """
    lines = []
    lines.append(f"Timestamp: {time.strftime('%Y-%m-%d %H:%M:%S')}")
    lines.append(f"Platform: {platform.platform()}")
    lines.append(f"Python: {sys.version.replace(os.linesep, ' ')}")
    try:
        lines.append(f"CPU count: {os.cpu_count()}")
    except Exception:
        pass

    # pip / git versions
    ok, out = run_version_command("python -V")
    if ok:
        lines.append(f"python -V: {out.strip()}")
    ok, out = run_version_command("pip --version")
    if ok:
        lines.append(f"pip --version: {out.strip()}")
    ok, out = run_version_command("git --version")
    if ok:
        lines.append(f"git --version: {out.strip()}")

    # Selected package versions
    pkgs = packages or DEFAULT_PACKAGES
    lines.append("\nPython package versions:")
    for p in pkgs:
        v = get_package_version(p)
        lines.append(f"  {p}: {v if v is not None else '<not installed>'}")

    # Extra version-check commands (one per line)
    lines.append("\nExtra version-check commands output:")
    if extra_version_cmds:
        for i, cmd in enumerate([c.strip() for c in extra_version_cmds.splitlines() if c.strip()]):
            lines.append(f"--- CMD #{i+1}: {cmd}")
            ok, out = run_version_command(cmd)
            if ok:
                lines.append(out.strip())
            else:
                lines.append(f"[FAILED] {out.strip()}")
    else:
        lines.append("  (none)")

    # HF model/sha info (if provided)
    if hf_model_id:
        try:
            api = HfApi()
            info = api.model_info(hf_model_id)
            # model_info contains 'sha' or 'sha' in revision property on some versions
            sha = getattr(info, "sha", None) or getattr(info, "sha256", None) or getattr(info, "pipeline_tag", None)
            lines.append(f"\nHugging Face model: {hf_model_id}")
            lines.append(f"  model_info (repr): {info}")
            if sha:
                lines.append(f"  reported sha/identifier: {sha}")
        except Exception as e:
            lines.append(f"\nFailed to retrieve HF model info for {hf_model_id}: {e}")

    # Write to file
    env_path = Path(outputs_dir) / "environment.txt"
    try:
        env_path.write_text("\n".join(lines), encoding="utf-8")
    except Exception:
        pass

    return "\n".join(lines), str(env_path)

# -------------------------
# Main Gradio function
def convert_pipeline(
    input_mode,
    uploaded_file,
    hf_repo_id,
    model_type,
    want_gguf,
    want_onnx,
    gguf_template,
    onnx_template,
    opset,
    quant_option,
    collect_env,
    extra_version_cmds,
):
    start_ts = time.strftime("%Y%m%d-%H%M%S")
    work_root = Path(tempfile.mkdtemp(prefix=f"convert-{start_ts}-"))
    outputs_dir = work_root / "outputs"
    outputs_dir.mkdir(parents=True, exist_ok=True)
    log_accum = []

    def append_log(s):
        log_accum.append(s)
        return "".join(log_accum)

    yield "Preparing workspace...\n"

    input_path = None
    input_dir = None
    model_id_used = None
    try:
        # Collect environment info early if requested
        if collect_env:
            yield "Collecting environment and version information...\n"
            env_summary, env_file = collect_environment_info(outputs_dir, hf_model_id=(hf_repo_id.strip() if hf_repo_id else None), extra_version_cmds=extra_version_cmds)
            yield env_summary + "\n"
            yield f"Environment info written to: {env_file}\n"

        # Prepare input
        if input_mode == "upload":
            if uploaded_file is None:
                yield "No file uploaded. Aborting.\n"
                return
            # Normalize gradio uploaded file shape
            saved = None
            # gr.File commonly gives a dict with 'name'/'tmp_path' or a tuple; handle common cases
            try:
                if isinstance(uploaded_file, (list, tuple)) and len(uploaded_file) >= 1:
                    # usually (filename, filepath)
                    possible = uploaded_file[-1]
                    if os.path.exists(possible):
                        saved = Path(possible)
                elif hasattr(uploaded_file, "name") and os.path.exists(uploaded_file.name):
                    saved = Path(uploaded_file.name)
                elif hasattr(uploaded_file, "tmp_path") and os.path.exists(uploaded_file.tmp_path):
                    saved = Path(uploaded_file.tmp_path)
            except Exception:
                pass

            if saved is None:
                # fallback: try to write bytes to a file
                try:
                    tmpf = work_root / "uploaded.bin"
                    with open(tmpf, "wb") as f:
                        f.write(uploaded_file.read())
                    saved = tmpf
                except Exception as e:
                    yield f"Failed to save uploaded file: {e}\n"
                    return

            if saved.suffix in [".zip"]:
                extract_dir = work_root / "uploaded_extracted"
                extract_dir.mkdir()
                try:
                    shutil.unpack_archive(str(saved), str(extract_dir))
                    input_dir = str(extract_dir)
                    input_path = str(saved)
                    yield f"Uploaded archive extracted to {input_dir}\n"
                except Exception as e:
                    yield f"Failed to extract archive: {e}\n"
                    return
            else:
                input_path = str(saved)
                input_dir = None
                yield f"Uploaded file saved at {input_path}\n"

        else:  # huggingface_repo
            if not hf_repo_id or hf_repo_id.strip() == "":
                yield "No Hugging Face repo id provided. Aborting.\n"
                return
            model_id_used = hf_repo_id.strip()
            yield f"Downloading {model_id_used} from Hugging Face (this may take a while)...\n"
            try:
                repo_dir = snapshot_download(repo_id=model_id_used, local_dir=work_root / "hf_repo", repo_type="model")
                input_dir = str(repo_dir)
                input_path = None
                yield f"Downloaded repo to {input_dir}\n"
                # After download, if collect_env was not requested earlier, add model info now
                if not collect_env:
                    try:
                        api = HfApi()
                        info = api.model_info(model_id_used)
                        yield f"Hugging Face model info: {info}\n"
                    except Exception as e:
                        yield f"Failed to fetch model info: {e}\n"
            except Exception as e:
                yield f"Failed to download {model_id_used}: {e}\n"
                return

        # Now run conversions based on selections
        final_zip = outputs_dir / f"converted_{start_ts}.zip"
        did_any = False

        if want_gguf:
            did_any = True
            yield append_log("\n=== Starting GGUF conversion ===\n")
            gguf_out = outputs_dir / "model.gguf"
            tpl = gguf_template.strip() or DEFAULT_GGUF_CMD_GENERIC
            cmd = tpl.format(
                INPUT_PATH=input_path or input_dir or "",
                INPUT_DIR=input_dir or input_path or "",
                OUTPUT_DIR=str(outputs_dir),
                OUTPUT_PATH=str(gguf_out),
                OPSET=opset,
                QUANT=quant_option or "",
                MODEL_ID=model_id_used or "",
            )
            yield append_log(f"Running: {cmd}\n")
            for out in run_and_stream(cmd):
                yield out

        if want_onnx:
            did_any = True
            yield append_log("\n=== Starting ONNX conversion ===\n")
            onnx_outdir = outputs_dir / "onnx"
            onnx_outdir.mkdir(exist_ok=True)
            tpl = onnx_template.strip() or DEFAULT_ONNX_CMD_GENERIC
            cmd = tpl.format(
                INPUT_PATH=input_path or input_dir or "",
                INPUT_DIR=input_dir or input_path or "",
                OUTPUT_DIR=str(onnx_outdir),
                OUTPUT_PATH=str(onnx_outdir),
                OPSET=opset,
                QUANT=quant_option or "",
                MODEL_ID=model_id_used or "",
            )
            yield append_log(f"Running: {cmd}\n")
            for out in run_and_stream(cmd):
                yield out

        if not did_any:
            yield "No conversion target selected. Aborting.\n"
            return

        # Ensure environment.txt exists (collect if not already done)
        if not (outputs_dir / "environment.txt").exists():
            env_summary, env_file = collect_environment_info(outputs_dir, hf_model_id=(model_id_used or hf_repo_id or None), extra_version_cmds=extra_version_cmds)
            yield f"\nWrote environment summary to {env_file}\n"

        # Zip outputs for download
        yield "\nPackaging outputs...\n"
        zip_dir(str(outputs_dir), str(final_zip))
        yield f"Packaging complete. Download zip created: {final_zip}\n"
        yield f"[DONE]\n"
        return "".join(log_accum), str(final_zip)

    finally:
        # Keep workspace for inspection by default. Uncomment to auto-clean:
        # shutil.rmtree(work_root, ignore_errors=True)
        pass

# -------------------------
# Gradio UI
with gr.Blocks(title="Safetensors / Diffusers -> GGUF/ONNX converter (with version capture)") as demo:
    gr.Markdown(
        """
        # Model converter wrapper (with environment/version capture)
        This app wraps external converter tools. It can record environment/package/command versions for reproducibility.
        Edit the command templates to match your installed converters.
        """
    )

    with gr.Row():
        with gr.Column(scale=1):
            input_mode = gr.Radio(choices=["upload", "huggingface_repo"], value="upload", label="Input mode")
            uploaded = gr.File(label="Upload safetensors or archive (zip)")
            hf_repo = gr.Textbox(label="Hugging Face repo id (e.g. runwayml/stable-diffusion-xl)", placeholder="owner/model-name")
            model_type = gr.Dropdown(choices=["LLM", "SDXL"], value="LLM", label="Model type / target domain")
            want_gguf = gr.Checkbox(value=True, label="Produce GGUF")
            want_onnx = gr.Checkbox(value=False, label="Produce ONNX")
            opset = gr.Number(value=16, label="ONNX opset (if producing ONNX)")
            quant_option = gr.Textbox(value="", label="Quantization option string (passed to templates)", placeholder="e.g. int8 or --quantize")
            collect_env = gr.Checkbox(value=True, label="Collect environment & version info")
            extra_version_cmds = gr.Textbox(label="Extra version-check commands (one per line)", placeholder="e.g. python /path/to/convert.py --version\n/path/to/gguf_quant --version", lines=4)
            convert_button = gr.Button("Convert")

        with gr.Column(scale=1):
            gr.Markdown("### Command templates (edit to match your converters)")
            gguf_template = gr.Textbox(label="GGUF command template", value=DEFAULT_GGUF_CMD_LLM, lines=3)
            onnx_template = gr.Textbox(label="ONNX command template", value=DEFAULT_ONNX_CMD_SDXL, lines=3)
            gr.Markdown(
                """
                Placeholders: {INPUT_PATH}, {INPUT_DIR}, {OUTPUT_DIR}, {OUTPUT_PATH}, {OPSET}, {QUANT}, {MODEL_ID}.
                Example GGUF (llama.cpp convert script) : 
                python /path/to/llama.cpp/tools/convert.py --safetensors {INPUT_PATH} --outfile {OUTPUT_PATH} --gguf
                """
            )

    log_output = gr.Textbox(label="Conversion log (streamed)", interactive=False, lines=20)
    download_file = gr.File(label="Download: zipped outputs")

    convert_button.click(
        convert_pipeline,
        inputs=[input_mode, uploaded, hf_repo, model_type, want_gguf, want_onnx, gguf_template, onnx_template, opset, quant_option, collect_env, extra_version_cmds],
        outputs=[log_output, download_file],
    )

if __name__ == "__main__":
    demo.launch()