feat: 웹 스크래핑 에이전트에 URL 유효성 검사 기능 추가

This commit is contained in:
2025-08-28 11:17:56 +09:00
parent 57f9bba80e
commit ba4393c906

View File

@@ -75,6 +75,23 @@ class AIAgent:
hf_logging.set_verbosity_error() hf_logging.set_verbosity_error()
except Exception: except Exception:
pass 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', {}) model_settings = self.config.get('model_settings', {})
use_quantization = bool(model_settings.get('use_quantization', False)) use_quantization = bool(model_settings.get('use_quantization', False))
@@ -150,21 +167,11 @@ class AIAgent:
if max_memory: if max_memory:
load_kwargs["max_memory"] = max_memory load_kwargs["max_memory"] = max_memory
# use_quantization=True면 8bit 우선 시도 (repo의 다른 양자화 경로 우회) # use_quantization=True면 8bit 우선 시도 (항상 레거시 플래그 사용)
if use_quantization: if use_quantization:
try: load_kwargs["load_in_8bit"] = True
from transformers import BitsAndBytesConfig load_kwargs["llm_int8_enable_fp32_cpu_offload"] = True
tmp = BitsAndBytesConfig(load_in_8bit=True, llm_int8_enable_fp32_cpu_offload=True) print("8bit 양자화 적용 (레거시 플래그)")
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차 시도 진행")
self.model = AutoModelForCausalLM.from_pretrained( self.model = AutoModelForCausalLM.from_pretrained(
model_source, model_source,
@@ -200,9 +207,7 @@ class AIAgent:
print(f"비양자화 재시도 실패: {e_noq}") print(f"비양자화 재시도 실패: {e_noq}")
# 2b. 8-bit 양자화로 재시도 (가능 시) # 2b. 8-bit 양자화로 재시도 (가능 시)
tried_int8 = False
try: try:
from transformers import BitsAndBytesConfig
print("8bit 양자화로 재시도합니다...") print("8bit 양자화로 재시도합니다...")
self.tokenizer = AutoTokenizer.from_pretrained(model_source, trust_remote_code=True) self.tokenizer = AutoTokenizer.from_pretrained(model_source, trust_remote_code=True)
# config 재생성 및 quantization_config 제거 # config 재생성 및 quantization_config 제거
@@ -219,20 +224,15 @@ class AIAgent:
offload_state_dict=True, offload_state_dict=True,
trust_remote_code=True, trust_remote_code=True,
config=cfg, config=cfg,
load_in_8bit=True,
llm_int8_enable_fp32_cpu_offload=True,
) )
if dtype is not None: if dtype is not None:
retry_kwargs["torch_dtype"] = dtype retry_kwargs["torch_dtype"] = dtype
if max_memory: if max_memory:
retry_kwargs["max_memory"] = 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) self.model = AutoModelForCausalLM.from_pretrained(model_source, **retry_kwargs)
tried_int8 = True
except Exception as e_int8: except Exception as e_int8:
print(f"8bit 재시도 실패: {e_int8}") print(f"8bit 재시도 실패: {e_int8}")