λͺ©ν‘œ

  1. MLflow Tracking Server ꡬ성
  2. μ‹€ν—˜(Experiment), νŒŒλΌλ―Έν„°, λ©”νŠΈλ¦­, μ•„ν‹°νŒ©νŠΈ 기둝
  3. λͺ¨λΈ 등둝 β†’ Stage 이동 β†’ API μ—°λ™κΉŒμ§€

πŸ‘‰ μ‹€μŠ΅ μ½”λ“œλŠ” πŸ”— GitHub (Mlflow - Tracking + FastAPI)


🧭 μ‹€μŠ΅ 전체 흐름 μš”μ•½

[1단계] MLflow Tracking Server ꡬ성 (둜컬 ν™˜κ²½μ—μ„œ μ‹€ν–‰)
[2단계] μ‹€ν—˜ μ‹€ν–‰ (train.py) β†’ λͺ¨λΈ ν•™μŠ΅, 기둝
[3단계] λͺ¨λΈ 등둝 및 Stage μ„€μ • (Production 이동)
[4단계] FastAPI 연동 β†’ 예츑 API μ„œλΉ„μŠ€

🧩 μ‹€μŠ΅ 디렉토리 μ˜ˆμ‹œ

mlops-mlflow/
β”œβ”€β”€ app/
β”‚   β”œβ”€β”€ train.py         # λͺ¨λΈ ν›ˆλ ¨ 및 μ‹€ν—˜ 기둝
β”‚   └── model.pkl        # μ €μž₯된 λͺ¨λΈ
β”œβ”€β”€ mlruns/              # μ‹€ν—˜ 데이터 μžλ™ 생성
β”œβ”€β”€ fastapi_app/
β”‚   └── app.py           # FastAPI 예츑 API
β”œβ”€β”€ Dockerfile (선택)
└── README.md

βœ… [1단계] MLflow μ„€μΉ˜ & μ‹€ν–‰

πŸ› οΈ 가상 ν™˜κ²½ μ„€μ •

# 1. venv μ„€μΉ˜
sudo apt install python3-venv -y

# 2. κ°€μƒν™˜κ²½ 생성
python3 -m venv .venv

# 3. κ°€μƒν™˜κ²½ ν™œμ„±ν™”
source .venv/bin/activate

# 4. νŒ¨ν‚€μ§€ μ„€μΉ˜
pip install mlflow scikit-learn pandas fastapi uvicorn

# 5. λ‚˜κ°ˆ λ•Œ
deactivate

πŸ”§ MLflow μ„œλ²„ μ‹€ν–‰

mlflow ui --port 5000  # http://localhost:5000 μ—μ„œ UI 확인

πŸ§ͺ [2단계] μ‹€ν—˜ μ‹€ν–‰ (train.py)

# app/train.py

import mlflow
import mlflow.sklearn
from sklearn.ensemble import RandomForestClassifier
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split

# MLflow μ„€μ •
mlflow.set_tracking_uri("http://localhost:5000")
mlflow.set_experiment("iris-rf-exp")

with mlflow.start_run():
    iris = load_iris()
    X_train, X_test, y_train, y_test = train_test_split(iris.data, iris.target, test_size=0.2)
    clf = RandomForestClassifier(n_estimators=100, max_depth=3)
    clf.fit(X_train, y_train)
    acc = clf.score(X_test, y_test)

    mlflow.log_param("n_estimators", 100)
    mlflow.log_param("max_depth", 3)
    mlflow.log_metric("accuracy", acc)
    mlflow.sklearn.log_model(clf, "model")
# μ‹€ν—˜ μ‹€ν–‰
python app/train.py

μ‹€ν—˜μ΄ λλ‚˜λ©΄ mlruns/ 폴더에 μ‹€ν—˜ 기둝 및 λͺ¨λΈμ΄ μ €μž₯


🧱 [3단계] λͺ¨λΈ 등둝 및 Stage μ„€μ •

  1. λΈŒλΌμš°μ €μ—μ„œ http://localhost:5000 접속
  2. μ‹€ν—˜μ—μ„œ μƒμ„±λœ Run β†’ β€œRegister Model” 클릭
  3. Model Registry β†’ iris-rf λͺ¨λΈ 등둝
  4. λͺ¨λΈμ— Stage(Production) μ„€μ •
    • models:/iris-rf@production ν˜•μ‹μœΌλ‘œ 호좜

πŸš€ [4단계] FastAPI둜 예츑 API 연동

# fastapi_app/app.py

import mlflow
import mlflow.pyfunc
from fastapi import FastAPI
from pydantic import BaseModel
import pandas as pd

# MLflow URI μ„€μ •
mlflow.set_tracking_uri("http://localhost:5000")  # MLflow μ„œλ²„ URI μ„€μ •

# FastAPI μΈμŠ€ν„΄μŠ€ 생성
app = FastAPI()

# MLflow λͺ¨λΈ λ‘œλ“œ
model = mlflow.pyfunc.load_model("models:/iris-rf@production")  # λͺ¨λΈ aliasλ₯Ό 이용

# μž…λ ₯ 데이터 ꡬ쑰 μ •μ˜
class InputData(BaseModel):
    features: list  # 4개의 νŠΉμ„±κ°’μ„ λ°›μŒ

# 예츑 API μ—”λ“œν¬μΈνŠΈ
@app.post("/predict")
def predict(data: InputData):
    input_df = pd.DataFrame([data.features], columns=["sepal_length", "sepal_width", "petal_length", "petal_width"])
    pred = model.predict(input_df)
    return {"prediction": int(pred[0])}
# FastAPI μ„œλ²„ μ‹€ν–‰
uvicorn fastapi_app.app:app --reload --port 8000

예츑 API ν…ŒμŠ€νŠΈ

curl -X POST http://localhost:8000/predict \
     -H "Content-Type: application/json" \
     -d '{"features": [5.1, 3.5, 1.4, 0.2]}'

예츑 κ²°κ³Ό: { “prediction”: 0 } β†’ setosa ν’ˆμ’…


🎯 핡심 μš”μ•½

ν•­λͺ©μ„€λͺ…
MLflowμ‹€ν—˜ 기둝, λͺ¨λΈ 관리, νŒŒλΌλ―Έν„°/λ©”νŠΈλ¦­ λ‘œκΉ…
FastAPIλͺ¨λΈμ„ API둜 μ œκ³΅ν•˜λŠ” μ›Ή ν”„λ ˆμž„μ›Œν¬
Model Registryλͺ¨λΈ 버전 관리 및 Stage μ„€μ •
μ‹€ν—˜ 기둝mlruns/ 디렉토리에 μžλ™ μ €μž₯

🧩 싀무 팁

  • MLflow둜 λͺ¨λΈ 버전 관리 및 Stage 섀정이 κ°€λŠ₯ν•΄μ Έ 배포가 μ‰¬μ›Œμ§
  • FastAPI둜 λΉ λ₯Έ ν”„λ‘œν† νƒ€μž…κ³Ό λ°°ν¬κΉŒμ§€ μ΄μ–΄μ§€λŠ” μ™„μ „ν•œ MLOps νŒŒμ΄ν”„λΌμΈ ꡬ좕 κ°€λŠ₯

πŸ”§ MLOps μ‹€μ „ μ—°κ²°

싀무 상황Kubernetes μ‚¬μš© 방식
λͺ¨λΈ νŠΈλž˜ν‚Ή & 관리MLflow둜 λͺ¨λΈ 좔적, GitOps와 연동 κ°€λŠ₯
API 연동FastAPI둜 μ„œλΉ„μŠ€ 배포 및 μ‹€μ‹œκ°„ 예츑 API 제곡
λͺ¨λΈ 버전 관리Model Registryλ₯Ό 톡해 배포 ν™˜κ²½ 뢄리