-
Notifications
You must be signed in to change notification settings - Fork 344
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
98 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# FastAPI Web Single Pattern | ||
- 목적 : FastAPI를 사용해 Web Single 패턴을 구현합니다 | ||
- 상황 : 데이터 과학자가 model.py을 만들었고(model.joblib이 학습 결과), 그 model을 FastAPI을 사용해 Online Serving을 구현해야 함 | ||
- model.py는 추후에 수정될 수 있으므로, model.py를 수정하지 않음(데이터 과학자쪽에서 수정) | ||
|
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
from sklearn.datasets import load_iris | ||
from sklearn.ensemble import RandomForestClassifier | ||
from sklearn.model_selection import train_test_split | ||
|
||
|
||
def get_dataset(): | ||
iris = load_iris() | ||
X, y = iris.data, iris.target | ||
return X, y | ||
|
||
|
||
def get_model(): | ||
model = RandomForestClassifier(n_estimators=100) | ||
return model | ||
|
||
|
||
def train(model, X_train, y_train): | ||
model.fit(X_train, y_train) | ||
return model | ||
|
||
|
||
def predict(model, X_test): | ||
return model.predict(X_test) | ||
|
||
|
||
def evaluate(model, X_test, y_test): | ||
return model.score(X_test, y_test) | ||
|
||
|
||
def save_model(model, model_path: str): | ||
import joblib | ||
|
||
joblib.dump(model, model_path) | ||
|
||
|
||
def load_model(model_path: str): | ||
import joblib | ||
|
||
return joblib.load(model_path) | ||
|
||
|
||
def main(): | ||
X, y = get_dataset() | ||
X_train, X_test, y_train, y_test = train_test_split(X, y) | ||
model = get_model() | ||
model = train(model, X_train, y_train) | ||
score = evaluate(model, X_test, y_test) | ||
print(f"model score: {score}") | ||
save_model(model, "model.joblib") | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
40 changes: 40 additions & 0 deletions
40
02-online-serving(fastapi)/projects/starter_code/requirements.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
annotated-types==0.6.0 | ||
anyio==3.7.1 | ||
certifi==2023.11.17 | ||
click==8.1.7 | ||
dnspython==2.4.2 | ||
email-validator==2.1.0.post1 | ||
exceptiongroup==1.2.0 | ||
fastapi==0.105.0 | ||
h11==0.14.0 | ||
httpcore==1.0.2 | ||
httptools==0.6.1 | ||
httpx==0.25.2 | ||
idna==3.6 | ||
itsdangerous==2.1.2 | ||
Jinja2==3.1.2 | ||
joblib==1.3.2 | ||
loguru==0.7.2 | ||
MarkupSafe==2.1.3 | ||
numpy==1.26.2 | ||
orjson==3.9.10 | ||
pydantic==2.5.2 | ||
pydantic-extra-types==2.2.0 | ||
pydantic-settings==2.1.0 | ||
pydantic_core==2.14.5 | ||
python-dotenv==1.0.0 | ||
python-multipart==0.0.6 | ||
PyYAML==6.0.1 | ||
scikit-learn==1.3.2 | ||
scipy==1.11.4 | ||
sniffio==1.3.0 | ||
SQLAlchemy==2.0.23 | ||
sqlmodel==0.0.14 | ||
starlette==0.27.0 | ||
threadpoolctl==3.2.0 | ||
typing_extensions==4.9.0 | ||
ujson==5.9.0 | ||
uvicorn==0.24.0.post1 | ||
uvloop==0.19.0 | ||
watchfiles==0.21.0 | ||
websockets==12.0 |