Spaces:
Sleeping
Sleeping
Download model_loader.py from xfojj00/NACE_Clasifier: direct link, hf CLI and curl.
- Browser
- Download file 5.89 kB
-
https://huggingface.co/spaces/xfojj00/NACE_Clasifier/resolve/main/model_loader.py
- Command line
-
hf download hf://spaces/xfojj00/NACE_Clasifier/model_loader.py
-
curl -L -o model_loader.py https://huggingface.co/spaces/xfojj00/NACE_Clasifier/resolve/main/model_loader.py
5.89 kB
| from pathlib import Path | |
| import json | |
| import joblib | |
| import torch | |
| import pandas as pd | |
| from transformers import AutoTokenizer, AutoModelForSequenceClassification | |
| # ============================================================ | |
| # ZÁKLADNÍ CESTY | |
| # ============================================================ | |
| BASE_DIR = Path(__file__).resolve().parent | |
| NACE_MODEL_DIR = BASE_DIR / "models" / "NACE_4 Model" / "robeczech-base" | |
| BUSINESS_MODEL_DIR = BASE_DIR / "models" / "Economical Binary Model" / "Seznam_small-e-czech" | |
| # ============================================================ | |
| # KONTROLA CEST | |
| # ============================================================ | |
| if not NACE_MODEL_DIR.exists(): | |
| raise FileNotFoundError(f"NACE model složka neexistuje: {NACE_MODEL_DIR}") | |
| if not BUSINESS_MODEL_DIR.exists(): | |
| raise FileNotFoundError(f"BUSINESS model složka neexistuje: {BUSINESS_MODEL_DIR}") | |
| # ============================================================ | |
| # ZAŘÍZENÍ | |
| # ============================================================ | |
| device = torch.device("cuda" if torch.cuda.is_available() else "cpu") | |
| # ============================================================ | |
| # POMOCNÉ FUNKCE | |
| # ============================================================ | |
| def load_json_if_exists(path: Path) -> dict: | |
| if path.exists(): | |
| with open(path, "r", encoding="utf-8") as f: | |
| return json.load(f) | |
| return {} | |
| def ensure_file_exists(path: Path, name: str): | |
| if not path.exists(): | |
| raise FileNotFoundError(f"{name} neexistuje: {path}") | |
| # ============================================================ | |
| # 1) NAČTENÍ NACE MODELU | |
| # ============================================================ | |
| nace_metadata_path = NACE_MODEL_DIR / "metadata.json" | |
| ensure_file_exists(nace_metadata_path, "NACE metadata.json") | |
| nace_metadata = load_json_if_exists(nace_metadata_path) | |
| NACE_MAX_LENGTH = nace_metadata.get("max_length", 96) | |
| nace_tokenizer = AutoTokenizer.from_pretrained(NACE_MODEL_DIR) | |
| nace_label_encoder_path = NACE_MODEL_DIR / "label_encoder.joblib" | |
| ensure_file_exists(nace_label_encoder_path, "NACE label_encoder.joblib") | |
| nace_label_encoder = joblib.load(nace_label_encoder_path) | |
| nace_model = AutoModelForSequenceClassification.from_pretrained( | |
| NACE_MODEL_DIR | |
| ) | |
| nace_model.to(device) | |
| nace_model.eval() | |
| # ============================================================ | |
| # 2) NAČTENÍ NACE MAPPINGU | |
| # ============================================================ | |
| nace_mapping_path = NACE_MODEL_DIR / "nace_mapping.csv" | |
| ensure_file_exists(nace_mapping_path, "nace_mapping.csv") | |
| nace_mapping = pd.read_csv( | |
| nace_mapping_path, | |
| sep=";", | |
| dtype=str | |
| ) | |
| nace_mapping["NACE"] = ( | |
| nace_mapping["NACE"] | |
| .astype(str) | |
| .str.zfill(4) | |
| .str[:4] | |
| ) | |
| nace_mapping_map = ( | |
| nace_mapping | |
| .drop_duplicates("NACE") | |
| .set_index("NACE") | |
| .to_dict("index") | |
| ) | |
| # ============================================================ | |
| # 3) NAČTENÍ BUSINESS MODELU | |
| # ============================================================ | |
| business_metadata_path = BUSINESS_MODEL_DIR / "metadata.json" | |
| business_metadata = load_json_if_exists(business_metadata_path) | |
| BUSINESS_MAX_LENGTH = business_metadata.get("max_length", 96) | |
| BUSINESS_THRESHOLD = business_metadata.get("threshold", 0.70) | |
| business_tokenizer = AutoTokenizer.from_pretrained(BUSINESS_MODEL_DIR) | |
| business_model = AutoModelForSequenceClassification.from_pretrained( | |
| BUSINESS_MODEL_DIR | |
| ) | |
| business_model.to(device) | |
| business_model.eval() | |
| # ============================================================ | |
| # 4) BUSINESS LABEL MAP | |
| # ============================================================ | |
| business_label_encoder_path = BUSINESS_MODEL_DIR / "label_encoder.joblib" | |
| business_label_encoder = None | |
| if business_label_encoder_path.exists(): | |
| business_label_encoder = joblib.load(business_label_encoder_path) | |
| # Primární zdroj: metadata.json | |
| business_id2label = business_metadata.get("id2label", None) | |
| business_label2id = business_metadata.get("label2id", None) | |
| # Sekundární zdroj: config.json načtený z modelu | |
| if business_id2label is None: | |
| business_id2label = business_model.config.id2label | |
| if business_label2id is None: | |
| business_label2id = business_model.config.label2id | |
| # Normalizace klíčů, protože JSON ukládá klíče jako string | |
| business_id2label = { | |
| int(k): str(v) | |
| for k, v in business_id2label.items() | |
| } | |
| business_label2id = { | |
| str(k): int(v) | |
| for k, v in business_label2id.items() | |
| } | |
| # Fallback, pokud config obsahuje obecné LABEL_0 / LABEL_1 | |
| if business_id2label.get(0) in ["LABEL_0", "0"] or business_id2label.get(1) in ["LABEL_1", "1"]: | |
| business_id2label = { | |
| 0: "unknown", | |
| 1: "economic_activity" | |
| } | |
| business_label2id = { | |
| "unknown": 0, | |
| "economic_activity": 1 | |
| } | |
| BUSINESS_UNKNOWN_LABEL_ID = business_label2id.get("unknown", 0) | |
| BUSINESS_ACTIVITY_LABEL_ID = business_label2id.get("economic_activity", 1) | |
| # ============================================================ | |
| # KONTROLNÍ VÝPIS | |
| # ============================================================ | |
| print("Modely načteny:") | |
| print(f"NACE model: {NACE_MODEL_DIR}") | |
| print(f"BUSINESS model: {BUSINESS_MODEL_DIR}") | |
| print(f"Zařízení: {device}") | |
| print(f"NACE max length: {NACE_MAX_LENGTH}") | |
| print(f"BUSINESS max length: {BUSINESS_MAX_LENGTH}") | |
| print(f"BUSINESS threshold: {BUSINESS_THRESHOLD}") | |
| print(f"BUSINESS unknown label id: {BUSINESS_UNKNOWN_LABEL_ID}") | |
| print(f"BUSINESS activity label id: {BUSINESS_ACTIVITY_LABEL_ID}") | |
| print(f"BUSINESS id2label: {business_id2label}") |