Hugging Face에 AI 모델을 업로드하는 과정은 다음과 같다.
특히 config.json
에 대한 문제를 해결하는 방법도 포함되어 있다.
pip install transformers
pytorch_model.bin
, tf_model.h5
, model.bin
등tokenizer.json
, vocab.txt
, merges.txt
등config.json
config.json
파일 준비모델에 필요한 구성 파일을 작성합니다. 일반적으로 다음과 같은 내용이 포함된다:
{
"architectures": [
"BertForSequenceClassification"
],
"hidden_size": 768,
"num_attention_heads": 12,
"num_hidden_layers": 12,
"type_vocab_size": 2,
"vocab_size": 30522
}
여기서 architectures
, hidden_size
, num_attention_heads
, num_hidden_layers
등은 모델의 아키텍처에 따라 다를 수 있다.
pip install huggingface_hub
huggingface-cli login
API 토큰을 입력하여 로그인힌다.
from huggingface_hub import HfApi, Repository
# API 객체 생성
api = HfApi()
# 모델 저장소 생성
repo_url = api.create_repo(repo_id="your-username/your-model-name", repo_type="model")
# 저장소 클론
repo = Repository(local_dir="model_repo", clone_from=repo_url)
# 모델과 토크나이저 파일 복사
import shutil
import os
# 현재 작업 디렉토리에서 모델 파일 복사
for file_name in ["pytorch_model.bin", "config.json", "tokenizer.json"]:
shutil.copy(file_name, "model_repo/")
# 커밋 및 푸시
repo.git_add()
repo.git_commit("Initial commit")
repo.git_push()
여기서 your-username/your-model-name
을 자신의 사용자명과 모델명으로 바꿔야 한다.
Hugging Face에 모델이 업로드되면, 모델을 로드하고 사용하는 방법은 다음과 같다:
from transformers import BertForSequenceClassification, BertTokenizer
model_name = "your-username/your-model-name"
# 모델과 토크나이저 로드
model = BertForSequenceClassification.from_pretrained(model_name)
tokenizer = BertTokenizer.from_pretrained(model_name)
# 예측
inputs = tokenizer("Sample text", return_tensors="pt")
outputs = model(**inputs)
이제 Hugging Face에 모델을 업로드하고, 로드 및 사용할 준비가 끝난다.
폴더 안에 구성하지 말고, 바로 config.json 이랑 Ai 파이 ㄹ있어야 한다.