""" EXAONE-3.0-7.8B-Instruct 모델을 Hugging Face에서 다운로드하고 Ollama에서 사용할 수 있도록 준비하는 스크립트 """ import os import sys from huggingface_hub import snapshot_download, login # 프로젝트 루트를 Python 경로에 추가 sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) def get_huggingface_token(): """ Hugging Face 토큰 가져오기 (환경 변수 우선, 없으면 DB에서) Google API 키 관리 방식과 동일한 패턴 """ # 환경 변수에서 먼저 확인 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)}자)") return token except Exception as e: print(f"[Hugging Face] DB에서 토큰 조회 실패 (환경 변수 사용): {e}") return '' # Hugging Face 토큰 가져오기 HF_TOKEN = get_huggingface_token() if not HF_TOKEN: raise ValueError("HUGGINGFACE_HUB_TOKEN 또는 HF_TOKEN 환경 변수를 설정하거나, 관리 페이지에서 토큰을 설정해주세요.") MODEL_NAME = "LGAI-EXAONE/EXAONE-3.0-7.8B-Instruct" def download_model(): """Hugging Face에서 모델 다운로드""" print("=" * 60) print("EXAONE-3.0-7.8B-Instruct 모델 다운로드") print("=" * 60) # Hugging Face 로그인 try: login(token=HF_TOKEN) print("[OK] Hugging Face 로그인 성공") except Exception as e: print(f"[ERROR] Hugging Face 로그인 실패: {e}") return False # 모델 다운로드 try: print(f"\n모델 다운로드 시작: {MODEL_NAME}") print("주의: 모델 크기가 약 15GB이므로 시간이 걸릴 수 있습니다...") download_path = snapshot_download( repo_id=MODEL_NAME, token=HF_TOKEN, local_dir="./models/EXAONE-3.0-7.8B-Instruct", local_dir_use_symlinks=False ) print(f"\n[OK] 모델 다운로드 완료!") print(f"저장 위치: {download_path}") return True except Exception as e: print(f"[ERROR] 모델 다운로드 실패: {e}") return False if __name__ == "__main__": import sys print("\n이 스크립트는 EXAONE 모델을 Hugging Face에서 다운로드합니다.") print("다운로드된 모델은 Ollama에서 직접 사용할 수 없으며,") print("GGUF 형식으로 변환하는 추가 작업이 필요합니다.") print("\n참고: Ollama는 일반적으로 GGUF 형식의 모델만 지원합니다.") response = input("\n계속하시겠습니까? (y/n): ") if response.lower() != 'y': print("다운로드를 취소했습니다.") sys.exit(0) success = download_model() if success: print("\n" + "=" * 60) print("다운로드 완료!") print("=" * 60) print("\n다음 단계:") print("1. llama.cpp를 사용하여 GGUF 형식으로 변환") print("2. 변환된 모델을 Ollama에 추가") print("\n자세한 내용은 EXAONE_설치_가이드.md를 참고하세요.") print("=" * 60) else: print("\n다운로드에 실패했습니다.")