""" Hugging Face 토큰 관리 모듈 Google Gemini API 키 관리 방식과 동일한 패턴으로 구현 """ import os from typing import Optional def get_huggingface_token() -> str: """ Hugging Face 토큰 가져오기 (환경 변수 우선, 없으면 DB에서) Returns: Hugging Face 토큰 문자열 (없으면 빈 문자열) """ # 환경 변수에서 먼저 확인 token = os.getenv('HUGGINGFACE_HUB_TOKEN', '').strip() if not token: token = os.getenv('HF_TOKEN', '').strip() if token: print(f"[Hugging Face] 환경 변수에서 토큰 가져옴 (길이: {len(token)}자)") return token # DB에서 가져오기 (순환 참조 방지를 위해 여기서 임포트) try: from app.database import SystemConfig token = SystemConfig.get_config('huggingface_token', '').strip() if token: print(f"[Hugging Face] DB에서 토큰 가져옴 (길이: {len(token)}자)") else: print(f"[Hugging Face] DB에 토큰이 없거나 비어있음") return token except Exception as e: print(f"[Hugging Face] DB에서 토큰 조회 실패: {e}") return '' def reset_huggingface_token(): """ Hugging Face 토큰 캐시 초기화 (토큰이 업데이트된 후 호출) """ # 현재는 단순히 함수를 다시 호출하면 되지만, # 향후 캐싱이 추가될 경우를 대비한 함수 pass