File size: 1,284 Bytes
833fa59
4018801
833fa59
8e5144c
 
833fa59
8e5144c
2241a78
 
 
 
 
 
8e5144c
2241a78
 
 
 
 
 
 
 
 
 
 
 
 
 
4018801
 
 
 
 
 
 
 
 
 
8d1e199
4018801
 
 
 
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
from huggingface_hub import hf_hub_download
from onnxruntime.quantization import quantize_dynamic, QuantType
import shutil
import os

os.makedirs("./model", exist_ok=True)

# Try model repo first, then space repo
configs = [
    {"repo_id": "toandev/OCR-for-Captcha", "repo_type": "model"},
    {"repo_id": "toandev/OCR-for-Captcha", "repo_type": "space"},
    {"repo_id": "toandev/OCR-for-Captcha", "repo_type": "dataset"},
]

model_path = None
for cfg in configs:
    try:
        print(f"Trying repo_type='{cfg['repo_type']}'...")
        model_path = hf_hub_download(
            repo_id=cfg["repo_id"],
            filename="model.onnx",
            repo_type=cfg["repo_type"]
        )
        print(f"✅ Found with repo_type='{cfg['repo_type']}'")
        break
    except Exception as e:
        print(f"❌ Failed: {e}")

if not model_path:
    raise RuntimeError("Could not find model.onnx in any repo type")

print("Saving original model...")
shutil.copy(model_path, "./model/model_original.onnx")

print("Quantizing to INT8...")
quantize_dynamic(
    model_input="./model/model_original.onnx",
    model_output="./model/model.onnx",
    weight_type=QuantType.QUInt8
)

os.remove("./model/model_original.onnx")
print("✅ Quantized ONNX model saved to ./model/model.onnx")