Spaces:
Sleeping
Sleeping
fix: quantization_config 역직렬화 오류를 config.json 패치로 완전 해결 (로컬 통합 테스트 통과)
Browse files
app.py
CHANGED
|
@@ -13,7 +13,9 @@ if not hasattr(huggingface_hub, "HfFolder"):
|
|
| 13 |
|
| 14 |
import gradio as gr
|
| 15 |
import os
|
| 16 |
-
import
|
|
|
|
|
|
|
| 17 |
import numpy as np
|
| 18 |
import pandas as pd
|
| 19 |
import tensorflow as tf
|
|
@@ -28,6 +30,29 @@ os.environ['TF_ENABLE_ONEDNN_OPTS'] = '0'
|
|
| 28 |
_models = {"predictor": None, "consultant": None}
|
| 29 |
REPO_ID = "dev-yuje/gardio_test"
|
| 30 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
def load_all_models():
|
| 32 |
if _models["predictor"] is None:
|
| 33 |
try:
|
|
@@ -71,27 +96,11 @@ def load_all_models():
|
|
| 71 |
self.load_error = f"파일이 너무 작음({fsize}B). LFS 포인터일 가능성 있음."
|
| 72 |
return
|
| 73 |
|
| 74 |
-
|
| 75 |
-
# Keras 버전 차이로 Dense 레이어에 quantization_config 미지원 시 custom_objects로 패치
|
| 76 |
-
try:
|
| 77 |
-
self.model = tf.keras.models.load_model(target_path, compile=False)
|
| 78 |
-
print(f"✅ 모델 로드 성공 1차 시도 (파일 크기: {fsize:,}B)")
|
| 79 |
-
except Exception as e1:
|
| 80 |
-
print(f"⚠️ 1차 로드 실패, 2차 시도: {str(e1)[:200]}")
|
| 81 |
-
# 2차: safe_mode=False로 재시도 (Keras 3.x 옵션)
|
| 82 |
-
try:
|
| 83 |
-
self.model = tf.keras.models.load_model(target_path, compile=False, safe_mode=False)
|
| 84 |
-
print(f"✅ 모델 로드 성공 2차 시도 (safe_mode=False)")
|
| 85 |
-
except Exception as e2:
|
| 86 |
-
print(f"⚠️ 2차 로드 실패, 3차 시도: {str(e2)[:200]}")
|
| 87 |
-
# 3차: keras.saving으로 직접 로드
|
| 88 |
-
import keras
|
| 89 |
-
self.model = keras.saving.load_model(target_path, compile=False)
|
| 90 |
-
print(f"✅ 모델 로드 성공 3차 시도 (keras.saving)")
|
| 91 |
-
|
| 92 |
self.load_error = "성공"
|
|
|
|
| 93 |
except Exception as model_e:
|
| 94 |
-
self.load_error = f"모델 로드 실패: {str(model_e)}
|
| 95 |
except Exception as e:
|
| 96 |
self.load_error = f"리소스 로드 통합 에러: {e}"
|
| 97 |
|
|
|
|
| 13 |
|
| 14 |
import gradio as gr
|
| 15 |
import os
|
| 16 |
+
import re
|
| 17 |
+
import zipfile
|
| 18 |
+
import tempfile
|
| 19 |
import numpy as np
|
| 20 |
import pandas as pd
|
| 21 |
import tensorflow as tf
|
|
|
|
| 30 |
_models = {"predictor": None, "consultant": None}
|
| 31 |
REPO_ID = "dev-yuje/gardio_test"
|
| 32 |
|
| 33 |
+
def load_keras_model_compat(model_path):
|
| 34 |
+
"""quantization_config 역직렬화 오류를 config.json 패치로 우회"""
|
| 35 |
+
with tempfile.TemporaryDirectory() as tmpdir:
|
| 36 |
+
with zipfile.ZipFile(model_path, 'r') as z:
|
| 37 |
+
z.extractall(tmpdir)
|
| 38 |
+
config_path = os.path.join(tmpdir, 'config.json')
|
| 39 |
+
with open(config_path, 'r', encoding='utf-8') as f:
|
| 40 |
+
config_str = f.read()
|
| 41 |
+
config_str = re.sub(r',\s*"quantization_config":\s*null', '', config_str)
|
| 42 |
+
config_str = re.sub(r'"quantization_config":\s*null,?\s*', '', config_str)
|
| 43 |
+
with open(config_path, 'w', encoding='utf-8') as f:
|
| 44 |
+
f.write(config_str)
|
| 45 |
+
fixed_path = model_path + '.tmp_fixed.keras'
|
| 46 |
+
with zipfile.ZipFile(fixed_path, 'w', zipfile.ZIP_DEFLATED) as z:
|
| 47 |
+
for root, dirs, files in os.walk(tmpdir):
|
| 48 |
+
for file in files:
|
| 49 |
+
fp = os.path.join(root, file)
|
| 50 |
+
arcname = os.path.relpath(fp, tmpdir)
|
| 51 |
+
z.write(fp, arcname)
|
| 52 |
+
model = tf.keras.models.load_model(fixed_path, compile=False)
|
| 53 |
+
os.remove(fixed_path)
|
| 54 |
+
return model
|
| 55 |
+
|
| 56 |
def load_all_models():
|
| 57 |
if _models["predictor"] is None:
|
| 58 |
try:
|
|
|
|
| 96 |
self.load_error = f"파일이 너무 작음({fsize}B). LFS 포인터일 가능성 있음."
|
| 97 |
return
|
| 98 |
|
| 99 |
+
self.model = load_keras_model_compat(target_path)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
self.load_error = "성공"
|
| 101 |
+
print(f"✅ 모델 로드 성공 (파일 크기: {fsize:,}B)")
|
| 102 |
except Exception as model_e:
|
| 103 |
+
self.load_error = f"모델 로드 실패: {str(model_e)[:300]}"
|
| 104 |
except Exception as e:
|
| 105 |
self.load_error = f"리소스 로드 통합 에러: {e}"
|
| 106 |
|