File size: 12,277 Bytes
1c6575b 00598ab 1c6575b bd45861 1c6575b bd45861 1c6575b bd45861 00598ab bd45861 1c6575b bd45861 00598ab bd45861 1c6575b bd45861 1c6575b bd45861 1c6575b bd45861 1c6575b bd45861 00598ab bd45861 1c6575b bd45861 00598ab bd45861 00598ab 1c6575b 00598ab bd45861 00598ab bd45861 00598ab bd45861 00598ab 1c6575b 00598ab 1c6575b bd45861 | 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 | import os
import shutil
import subprocess
import tempfile
from pathlib import Path
import gradio as gr
import numpy as np
import torch
from safetensors.torch import load_file, save_file
try:
import h5py
H5PY_AVAILABLE = True
except ImportError:
H5PY_AVAILABLE = False
PYTORCH_EXTENSIONS = {".pt", ".pth", ".bin", ".ckpt"}
HDF5_EXTENSIONS = {".h5", ".hdf5"}
SUPPORTED_INPUTS = PYTORCH_EXTENSIONS | {".safetensors", ".npz"} | HDF5_EXTENSIONS
ONNX_TASKS = [
"auto",
"text-classification",
"token-classification",
"question-answering",
"feature-extraction",
"text-generation",
"text2text-generation",
"image-classification",
"image-to-text",
"semantic-segmentation",
]
def get_file_path(uploaded_file):
"""Works with Gradio versions that return either a filepath string or file object."""
if isinstance(uploaded_file, str):
return uploaded_file
return uploaded_file.name
def normalize_state_dict(data):
"""Extract a flat tensor dictionary from common checkpoint structures."""
if not isinstance(data, dict):
raise ValueError(
"The file does not contain a state dictionary. "
"Full serialized model objects are not supported."
)
for key in ("state_dict", "model_state_dict", "model"):
if key in data and isinstance(data[key], dict):
data = data[key]
break
result = {}
skipped = []
for key, value in data.items():
if isinstance(value, torch.Tensor):
result[str(key)] = value.detach().cpu().contiguous()
elif isinstance(value, np.ndarray):
result[str(key)] = torch.from_numpy(value).detach().cpu().contiguous()
else:
try:
result[str(key)] = torch.as_tensor(value).detach().cpu().contiguous()
except Exception:
skipped.append(str(key))
if not result:
raise ValueError("No tensor weights were found in this file.")
return result, skipped
def read_hdf5_datasets(group, prefix=""):
"""Read nested HDF5 datasets while preserving their group paths."""
state_dict = {}
for name, item in group.items():
key = f"{prefix}/{name}" if prefix else name
if isinstance(item, h5py.Dataset):
state_dict[key] = torch.from_numpy(np.asarray(item))
elif isinstance(item, h5py.Group):
state_dict.update(read_hdf5_datasets(item, key))
return state_dict
def convert_model(uploaded_file, target_format):
if uploaded_file is None:
return None, "❌ Please upload a model-weight file."
input_path = get_file_path(uploaded_file)
input_ext = Path(input_path).suffix.lower()
if input_ext not in SUPPORTED_INPUTS:
return None, f"❌ Unsupported input format: `{input_ext or 'no extension'}`."
if target_format == ".onnx":
return None, (
"⚠️ ONNX cannot be created from a weights file alone. "
"Use the **Hugging Face → ONNX** tab and enter a Hugging Face model ID. "
"ONNX needs the model architecture and example inputs."
)
if target_format in HDF5_EXTENSIONS and not H5PY_AVAILABLE:
return None, "❌ HDF5 support is unavailable. Add `h5py` to `requirements.txt`."
try:
if input_ext in PYTORCH_EXTENSIONS:
try:
data = torch.load(input_path, map_location="cpu", weights_only=True)
except Exception:
return None, (
"❌ This PyTorch checkpoint cannot be safely loaded with "
"`weights_only=True`. Do not upload untrusted pickle-based "
"files; they can execute code when deserialized."
)
state_dict, skipped = normalize_state_dict(data)
elif input_ext == ".safetensors":
state_dict, skipped = normalize_state_dict(load_file(input_path))
elif input_ext == ".npz":
with np.load(input_path, allow_pickle=False) as archive:
data = {key: archive[key] for key in archive.files}
state_dict, skipped = normalize_state_dict(data)
elif input_ext in HDF5_EXTENSIONS:
if not H5PY_AVAILABLE:
return None, "❌ HDF5 support is unavailable. Add `h5py` to `requirements.txt`."
with h5py.File(input_path, "r") as h5_file:
data = read_hdf5_datasets(h5_file)
state_dict, skipped = normalize_state_dict(data)
else:
return None, f"❌ Unsupported input format: `{input_ext}`."
except Exception as error:
return None, f"❌ Could not load file: `{type(error).__name__}: {error}`"
try:
# Do NOT use TemporaryDirectory here: Gradio needs the returned file to persist.
fd, output_path = tempfile.mkstemp(
prefix="converted_model_",
suffix=target_format,
)
os.close(fd)
if target_format in PYTORCH_EXTENSIONS:
torch.save(state_dict, output_path)
elif target_format == ".safetensors":
save_file(state_dict, output_path)
elif target_format == ".npz":
np.savez(
output_path,
**{key: value.cpu().numpy() for key, value in state_dict.items()},
)
elif target_format in HDF5_EXTENSIONS:
with h5py.File(output_path, "w") as h5_file:
for key, value in state_dict.items():
h5_file.create_dataset(key, data=value.cpu().numpy())
else:
os.unlink(output_path)
return None, f"❌ Unsupported target format: `{target_format}`."
message = f"✅ Converted {len(state_dict)} tensor(s) to `{target_format}`."
if skipped:
message += f" Skipped {len(skipped)} non-tensor value(s)."
return output_path, message
except Exception as error:
if "output_path" in locals() and os.path.exists(output_path):
os.unlink(output_path)
return None, f"❌ Could not save file: `{type(error).__name__}: {error}`"
def export_hf_model_to_onnx(model_id, task, optimization):
"""
Export a Hugging Face Hub model to ONNX with Optimum.
Example model IDs:
- distilbert/distilbert-base-uncased-finetuned-sst-2-english
- google-bert/bert-base-uncased
"""
if not model_id or not model_id.strip():
return None, "❌ Enter a Hugging Face model ID."
model_id = model_id.strip()
output_dir = tempfile.mkdtemp(prefix="optimum_onnx_")
command = [
"optimum-cli",
"export",
"onnx",
"--model",
model_id,
]
if task and task != "auto":
command.extend(["--task", task])
if optimization and optimization != "None":
command.extend(["--optimize", optimization])
command.append(output_dir)
try:
result = subprocess.run(
command,
capture_output=True,
text=True,
timeout=1800,
check=False,
)
if result.returncode != 0:
error_message = result.stderr.strip() or result.stdout.strip()
shutil.rmtree(output_dir, ignore_errors=True)
return None, (
"❌ ONNX export failed.\n\n"
f"`{error_message[-3500:]}`"
)
safe_name = model_id.replace("/", "_").replace("\\", "_")
archive_base = os.path.join(
tempfile.gettempdir(),
f"{safe_name}_onnx_export",
)
zip_path = shutil.make_archive(archive_base, "zip", output_dir)
onnx_files = list(Path(output_dir).rglob("*.onnx"))
onnx_count = len(onnx_files)
shutil.rmtree(output_dir, ignore_errors=True)
return zip_path, (
f"✅ ONNX export finished. Found {onnx_count} ONNX file(s). "
"Download the ZIP and extract it before using the model."
)
except subprocess.TimeoutExpired:
shutil.rmtree(output_dir, ignore_errors=True)
return None, (
"❌ Export timed out after 30 minutes. "
"Try a smaller model or use more powerful Space hardware."
)
except FileNotFoundError:
shutil.rmtree(output_dir, ignore_errors=True)
return None, (
"❌ `optimum-cli` was not found. Add "
"`optimum-onnx[onnxruntime]` to `requirements.txt`."
)
except Exception as error:
shutil.rmtree(output_dir, ignore_errors=True)
return None, f"❌ ONNX export error: `{type(error).__name__}: {error}`"
with gr.Blocks(theme=gr.themes.Soft(), title="Conversion Station") as demo:
gr.Markdown(
"""
# 🔄 Universal Model Weight Converter
Convert compatible **weight dictionaries** among PyTorch, SafeTensors, NumPy NPZ, and HDF5.
- **Weight inputs:** `.pt`, `.pth`, `.bin`, `.ckpt`, `.safetensors`, `.npz`, `.h5`, `.hdf5`
- **Weight outputs:** `.safetensors`, `.pt`, `.pth`, `.bin`, `.ckpt`, `.npz`, `.h5`, `.hdf5`
- **ONNX:** Use the separate Hugging Face → ONNX tab. ONNX requires a full model architecture, not only a weights file.
- **Security:** Only upload checkpoint files you trust. Pickle-based PyTorch files can be unsafe.
"""
)
with gr.Tab("Weight Converter"):
with gr.Row():
with gr.Column():
input_file = gr.File(
label="Upload model weights",
file_types=sorted(SUPPORTED_INPUTS),
type="filepath",
)
target_format = gr.Dropdown(
choices=[
".safetensors",
".pt",
".pth",
".bin",
".ckpt",
".npz",
".h5",
".hdf5",
".onnx",
],
value=".safetensors",
label="Target format",
)
convert_button = gr.Button("🚀 Convert model", variant="primary")
with gr.Column():
status_text = gr.Textbox(
label="Status",
interactive=False,
lines=4,
)
output_file = gr.File(label="Download converted model")
convert_button.click(
fn=convert_model,
inputs=[input_file, target_format],
outputs=[output_file, status_text],
)
with gr.Tab("Hugging Face → ONNX"):
gr.Markdown(
"""
Enter a public Hugging Face Transformers model ID to export it to ONNX.
Examples:
- `distilbert/distilbert-base-uncased-finetuned-sst-2-english`
- `google-bert/bert-base-uncased`
- `sentence-transformers/all-MiniLM-L6-v2`
The download is a ZIP because ONNX exports can include several ONNX graphs and supporting configuration files.
"""
)
with gr.Row():
onnx_model_id = gr.Textbox(
label="Hugging Face model ID",
placeholder="distilbert/distilbert-base-uncased-finetuned-sst-2-english",
)
onnx_task = gr.Dropdown(
choices=ONNX_TASKS,
value="auto",
label="Model task",
)
onnx_optimization = gr.Dropdown(
choices=["None", "O1", "O2", "O3", "O4"],
value="O2",
label="Optimization level",
)
onnx_export_button = gr.Button(
"⚡ Export Hugging Face Model to ONNX",
variant="primary",
)
onnx_status = gr.Textbox(
label="ONNX export status",
interactive=False,
lines=5,
)
onnx_output_file = gr.File(
label="Download ONNX export ZIP",
file_types=[".zip"],
)
onnx_export_button.click(
fn=export_hf_model_to_onnx,
inputs=[onnx_model_id, onnx_task, onnx_optimization],
outputs=[onnx_output_file, onnx_status],
)
if __name__ == "__main__":
demo.launch() |