νκ΅μ΄ ν μ€νΈμ μ μμ±μ νμ§νκ³ μμ€/λΉμμ΄λ₯Ό μλμΌλ‘ μνν΄μ£Όλ FastAPI μλ²μ λλ€.
- μ μμ± νμ§: νμΈνλλ AI λͺ¨λΈλ‘ ν μ€νΈ μ μμ± μ μ κ³μ° (0.0-1.0)
- μλ μν: HyperCLOVA X APIλ₯Ό νμ©ν μ§λ₯ν ν μ€νΈ μν
- RESTful API: FastAPI κΈ°λ°μ κ°λ¨ν HTTP API
- ν¬μ€μ²΄ν¬: μλ² μν λͺ¨λν°λ§ μλν¬μΈνΈ
git clone <your-repo-url>
cd <repo-name># νμ¬ λλ ν 리 νμΈ
pwd
# /home/ubuntu/AI λλ /home/ubuntu/moderation-api μ¬μΌ ν¨
# κ°μνκ²½ νμ±ν (μ€μ!)
source venv/bin/activate
# ν둬ννΈκ° (venv) ubuntu@... λ‘ λ°λμ΄μΌ ν¨# Python 3.11+ κΆμ₯
# requestsμ tqdm μ€μΉ
pip install requests tqdm
# λλ requirements.txtλ‘ λͺ¨λ ν¨ν€μ§ μ€μΉ
pip install -r requirements.txt# gdown μ€μΉ (Google Drive μ μ© λ€μ΄λ‘λ)
pip install gdown
# gdownμΌλ‘ λ€μ΄λ‘λ
gdown "{κ΅¬κΈ λλΌμ΄λΈμ 곡μ λ§ν¬ id}" -O final_moderation_model.zip# νκ²½λ³μλ‘ μ€μ
export HYPERCLOVA_API_KEY="your-api-key-here"
# λλ .env νμΌ μμ±
echo "HYPERCLOVA_API_KEY=your-api-key-here" > .env- λ€μ΄λ² ν΄λΌμ°λ μ½μ μ μ
- AI Services β CLOVA Studio β API ν€
- [ν μ€νΈ] νμμ ν μ€νΈ API ν€ λ°κΈ
uvicorn main:app --host 0.0.0.0 --port 8000μλ²κ° μμλλ©΄ http://localhost:8000μμ APIλ₯Ό μ¬μ©ν μ μμ΅λλ€.
curl -X POST "http://localhost:8000/moderate" \
-H "Content-Type: application/json" \
-d '{"text": "ν
μ€νΈν ν
μ€νΈλ₯Ό μ
λ ₯νμΈμ"}'μλ΅ μμ:
μΌλ° ν μ€νΈ (μμ ):
{
"text": "μλ
νμΈμ μ’μ ν루μ
λλ€",
"malice_score": 0.1234,
"is_harmful": false,
"confidence": "μμ ",
"purified_text": null
}μ ν΄ ν μ€νΈ (μνλ¨):
{
"text": "λ κΌ¬λΌλ΄ μ’κ°μλμ",
"malice_score": 0.9759,
"is_harmful": true,
"confidence": "λ§€μ° μν",
"purified_text": "λ μ§Έλ €λ΄ μ μ’μ λμ"
}curl http://localhost:8000/quick-healthμλ΅:
{"status": "healthy"}- EC2 μΈμ€ν΄μ€ μμ± (Ubuntu 22.04, t3.small κΆμ₯)
- 보μ κ·Έλ£Ή μ€μ : SSH(22), HTTP(8000) ν¬νΈ μ€ν
- μλ² μ€μ :
# SSH μ μ ν
sudo apt update && sudo apt upgrade -y
# Python 3.11 μ€μΉ
sudo apt install -y software-properties-common
sudo add-apt-repository ppa:deadsnakes/ppa -y
sudo apt update
sudo apt install -y python3.11 python3.11-pip python3.11-venv
# νλ‘μ νΈ ν΄λ‘
git clone <your-repo-url>
cd <repo-name>
# κ°μνκ²½ μ€μ
python3.11 -m venv venv
source venv/bin/activate
pip install -r requirements.txt
# νκ²½λ³μ μ€μ
export HYPERCLOVA_API_KEY="your-api-key"
# λͺ¨λΈ λ€μ΄λ‘λ
python download_model.py
# μλ² μ€ν (λ°±κ·ΈλΌμ΄λ)
nohup uvicorn main:app --host 0.0.0.0 --port 8000 > app.log 2>&1 &
# κ·Έλ₯ μ€ν
python -m uvicorn main:app --host 0.0.0.0 --port 8000- μλΉμ€ λ±λ‘ (μλ μμ):
sudo tee /etc/systemd/system/moderation-api.service > /dev/null <<EOF
[Unit]
Description=Moderation API
After=network.target
[Service]
Type=simple
User=ubuntu
WorkingDirectory=$(pwd)
Environment=PATH=$(pwd)/venv/bin
Environment=HYPERCLOVA_API_KEY=your-api-key
ExecStart=$(pwd)/venv/bin/uvicorn main:app --host 0.0.0.0 --port 8000
Restart=always
[Install]
WantedBy=multi-user.target
EOF
sudo systemctl daemon-reload
sudo systemctl enable moderation-api
sudo systemctl start moderation-apiβββ main.py # λ©μΈ FastAPI μ ν리μΌμ΄μ
βββ requirements.txt # Python ν¨ν€μ§ μμ‘΄μ±
βββ download_model.py # Google Drive λͺ¨λΈ μλ λ€μ΄λ‘λ
βββ README.md # νλ‘μ νΈ λ¬Έμ
βββ .gitignore # Git μ μΈ νμΌ λͺ©λ‘
βββ final_moderation_model/ # AI λͺ¨λΈ νμΌλ€ (μλ λ€μ΄λ‘λ)
βββ pytorch_model-001.pth # νμΈνλλ λͺ¨λΈ κ°μ€μΉ
βββ tokenizer.json # ν ν¬λμ΄μ μ€μ
βββ tokenizer_config.json # ν ν¬λμ΄μ ꡬμ±
βββ ... # κΈ°ν λͺ¨λΈ νμΌλ€
# Google Drive νμΌ ID νμΈ
# download_model.pyμμ MODEL_FILE_ID μμ νμ
# μλ λ€μ΄λ‘λ
python download_model.py# API ν€ νμΈ
echo $HYPERCLOVA_API_KEY
# μ API ν€ λ°κΈ (ν
μ€νΈ νμμ)
# Bearer ν ν° λ°©μμΌλ‘ λ³κ²½λ¨# ν¬νΈ μ¬μ© μ¬λΆ νμΈ
lsof -i :8000
# μμ‘΄μ± μ¬μ€μΉ
pip install -r requirements.txt
# λ‘κ·Έ νμΈ
tail -f app.log# μ ν리μΌμ΄μ
λ‘κ·Έ
tail -f app.log
# μμ€ν
μλΉμ€ λ‘κ·Έ (μλΉμ€ λ±λ‘ν κ²½μ°)
sudo journalctl -u moderation-api -f# μλ² λ¦¬μμ€ νμΈ
htop
# API μλ΅ μκ° ν
μ€νΈ
time curl -X POST "http://localhost:8000/moderate" \
-H "Content-Type: application/json" \
-d '{"text": "ν
μ€νΈ"}'- Python: 3.11+
- λ©λͺ¨λ¦¬: μ΅μ 2GB (λͺ¨λΈ λ‘λ©μ©)
- λμ€ν¬: μ΅μ 3GB (λͺ¨λΈ νμΌμ©)
- λ€νΈμν¬: HyperCLOVA X API μ κ·Ό κ°λ₯
- API ν€λ₯Ό νκ²½λ³μλ‘ μμ νκ² κ΄λ¦¬
- HTTPS μ¬μ© κΆμ₯ (νλ‘λμ νκ²½)
- λ°©νλ²½μΌλ‘ νμν ν¬νΈλ§ μ€ν
- μ κΈ°μ μΈ λ³΄μ μ λ°μ΄νΈ
π― μμ μλνλ ν μ€νΈ λͺ¨λλ μ΄μ μ루μ μ κ²½νν΄λ³΄μΈμ!