diff --git a/AI_Web_Scraper/ai_agent.py b/AI_Web_Scraper/ai_agent.py index c932e26..5e9e598 100644 --- a/AI_Web_Scraper/ai_agent.py +++ b/AI_Web_Scraper/ai_agent.py @@ -75,6 +75,23 @@ class AIAgent: hf_logging.set_verbosity_error() except Exception: pass + # 안전한 __repr__ 패치로 f-string 로깅 중 예외 회피 + try: + from transformers.configuration_utils import PretrainedConfig as _HFPC + _orig_repr = getattr(_HFPC, '__repr__', None) + def _safe_repr(self): + try: + # 가능한 경우 최소 정보만 반환 + name = getattr(self, '__class__', type('X', (), {})).__name__ + model_type = getattr(self, 'model_type', 'unknown') + return f"{name}(model_type={model_type})" + except Exception: + return f"PretrainedConfig(unknown)" + if _orig_repr is not None and getattr(_HFPC.__repr__, '_aiws_safe', None) is None: + _safe_repr._aiws_safe = True + _HFPC.__repr__ = _safe_repr + except Exception: + pass model_settings = self.config.get('model_settings', {}) use_quantization = bool(model_settings.get('use_quantization', False)) @@ -150,21 +167,11 @@ class AIAgent: if max_memory: load_kwargs["max_memory"] = max_memory - # use_quantization=True면 8bit 우선 시도 (repo의 다른 양자화 경로 우회) + # use_quantization=True면 8bit 우선 시도 (항상 레거시 플래그 사용) if use_quantization: - try: - from transformers import BitsAndBytesConfig - tmp = BitsAndBytesConfig(load_in_8bit=True, llm_int8_enable_fp32_cpu_offload=True) - if hasattr(tmp, 'get_loading_attributes'): - load_kwargs["quantization_config"] = tmp - print("8bit 양자화 적용 (1차 시도, bnb 신 API)") - else: - # 레거시 API 시도 - load_kwargs["load_in_8bit"] = True - load_kwargs["llm_int8_enable_fp32_cpu_offload"] = True - print("8bit 양자화 적용 (1차 시도, 레거시 API)") - except Exception as _: - print("bitsandbytes 감지 실패: 비양자화로 1차 시도 진행") + load_kwargs["load_in_8bit"] = True + load_kwargs["llm_int8_enable_fp32_cpu_offload"] = True + print("8bit 양자화 적용 (레거시 플래그)") self.model = AutoModelForCausalLM.from_pretrained( model_source, @@ -200,9 +207,7 @@ class AIAgent: print(f"비양자화 재시도 실패: {e_noq}") # 2b. 8-bit 양자화로 재시도 (가능 시) - tried_int8 = False try: - from transformers import BitsAndBytesConfig print("8bit 양자화로 재시도합니다...") self.tokenizer = AutoTokenizer.from_pretrained(model_source, trust_remote_code=True) # config 재생성 및 quantization_config 제거 @@ -219,20 +224,15 @@ class AIAgent: offload_state_dict=True, trust_remote_code=True, config=cfg, + load_in_8bit=True, + llm_int8_enable_fp32_cpu_offload=True, ) if dtype is not None: retry_kwargs["torch_dtype"] = dtype if max_memory: retry_kwargs["max_memory"] = max_memory - tmp = BitsAndBytesConfig(load_in_8bit=True, llm_int8_enable_fp32_cpu_offload=True) - if hasattr(tmp, 'get_loading_attributes'): - retry_kwargs["quantization_config"] = tmp - else: - retry_kwargs["load_in_8bit"] = True - retry_kwargs["llm_int8_enable_fp32_cpu_offload"] = True self.model = AutoModelForCausalLM.from_pretrained(model_source, **retry_kwargs) - tried_int8 = True except Exception as e_int8: print(f"8bit 재시도 실패: {e_int8}")