콘텐츠로 이동

11주 실습: 계약과 판단보류

  • 먼저 ../week10 에서 train_tiny.py 를 한 번 돌려 artifacts/model.pt 를 만들어 두세요. 이 폴더의 app.py 가 그 파일과 train_tiny.py 를 가져다 써요.
  • CPU 만으로 충분해요. 예상 시간: 1~2시간

1. 시험 돌리기

이 폴더에서 실행해요.

uv run --no-project --python 3.12 --with torch --with numpy --with fastapi --with httpx --with pytest pytest -q

이 강좌 PC 에서 11 passed 였어요. (httpx 관련 deprecated 경고가 하나 보일 수 있어요. 통과에는 영향 없어요.)

시험이 확인하는 약속: 정상 응답의 칸 이름, 판단보류, 422(5가지 잘못된 모양), 400(한글 없음), 503(모델 준비 전), 500(안쪽 오류를 밖에 안 보임).

2. 서버 켜기

uv run --no-project --python 3.12 --with torch --with numpy --with fastapi --with uvicorn \
  uvicorn app:app --port 10022

문턱을 바꾸려면 환경 변수를 붙여요(기본 0.75).

THRESHOLD=0.9 uv run --no-project --python 3.12 --with torch --with numpy --with fastapi --with uvicorn \
  uvicorn app:app --port 10022

3. 부르기

curl -s -X POST localhost:10022/predict -H "Content-Type: application/json" -d '{"text":"정말 재미있고 감동적인 영화였어요"}'
curl -s -X POST localhost:10022/predict -H "Content-Type: application/json" -d '{"text":"great movie"}'
curl -s -X POST localhost:10022/predict -H "Content-Type: application/json" -d '{"text":""}'

이 강좌 PC 에서(측정값):

{"label":"긍정","confidence":0.9879,"abstained":false,"threshold":0.75}   200
{"detail":"한글이 한 글자도 없어요. 한국어 리뷰만 받아요"}                  400
{"detail":[{"type":"string_too_short", ...}]}                              422

서버 터미널에는 요청마다 한 줄이 찍혀요: id=... len=10 label=부정 conf=0.857 abstained=False ms=0.3

확인할 것

  • pytest 가 모두 통과한다
  • 같은 문장이 THRESHOLD 에 따라 판단보류가 되기도 한다
  • 로그에 문장 내용이 없고 길이만 있다
  • 끝나면 서버를 끈다