Deep Dive into Backend & AI Integration (Stages 4–6)

DODO·2026년 9월 23일

Project

목록 보기
3/3

This journey was all about repeating a solid development cycle across multiple domain models.

"""
Requirements Definition ➔ API Specification & Design ➔ Infrastructure & Core Implementation ➔ Swagger API Testing ➔ Git PR & Code Review
"""

📌 Stage 4 — User Authentication & Authorization APIs (9 Endpoints)

Design & Review: Converted requirement specifications (REQ-USER-001~009) into detailed API mapping tables. During reviews, I corrected common mistakes like putting hardcoded data into endpoints, swapping Request Body with Response structures, and properly distinguishing Authentication (login status) from Authorization (role permissions).

Implementation: Built core security features using JWT and password hashing (core/security.py). I manually filled in logic for 9 endpoints, catching frequent async bugs like missing await statements.

Testing & Git: Verified full user workflows in Swagger UI (Registration ➔ Login ➔ Auth Header ➔ Profile Fetch). Encountered a Git branching mistake by accidentally setting main as the base branch for a PR, which taught me how to fast-forward sync dev with main.

📌 Stage 5 — Patient & Medical Record APIs with File Upload (8 Endpoints)

Design & Domain Logic: Handled requirements for Patients and Medical Records. Discovered key database behaviors like how ON DELETE CASCADE automatically deletes associated records and X-rays when a patient is removed. Also mapped out multi-part file uploads for X-ray images.

New Concepts Learned: Applied Query Parameters (search/filtering), Path Parameters (/{patient_id}), and Multipart/Form-Data file handling.

Swagger Testing: Successfully tested the end-to-end flow: Creating a patient ➔ Uploading actual X-ray images to the media/xray/ directory ➔ Retrieving medical records with linked media.

📌 Stage 6 — AI Pneumonia Prediction (Full Workflow)

  1. Model Pipeline & Ensemble Setup

Environment & Setup: Installed PyTorch CPU version (uv add torch torchvision --index ...) and analyzed 10 pre-trained .pth checkpoints (ConvNeXt-Tiny 5-fold + DenseNet121 5-fold).

Domain Verification: Re-verified preprocessing specifications (224x224 Resize, ImageNet Normalization) and the OR-ensemble strategy (predicts pneumonia if either model flags it) from an earlier project report (폐렴Xray분류프로젝트보고서.docx).

Git Strategy & Debugging: Added heavy model weights (worker/models/*.pth) to .gitignore to stay under GitHub's 100MB limit. Debugged key issues, including a loop return bug that loaded only a single fold, and a tuple syntax mistake in Resize((224, 224)).

Inference Test: Tested with actual X-ray images—achieved accurate predictions with a 0.9986 / 0.9888 confidence score in just 0.78 seconds!

  1. API Design & Requirements Alignment

Requirements Reconciliation: Compared past design documents against current requirements (REQ-PRED-001/002). Identified key updates: expanded access rights from admin/doctor-only to all departments, and made heatmap URLs optional.

Database Precision Update: Updated DB precision for prediction confidence scores from DECIMAL(5,2) to DECIMAL(5,4) and allowed heatmap_url to be nullable.

  1. API Implementation & Asynchronous Execution

Database Migration: Ran alembic revision --autogenerate and alembic upgrade head to apply schema updates, verifying accuracy directly via DESCRIBE table queries.

Non-blocking AI Inference: Integrated FastAPI’s run_in_threadpool to run heavy, synchronous PyTorch CPU inference on a background thread without blocking the async event loop!

Logic Implementation: Written and refined run_ai_analysis and list_ai_analysis functions after resolving missing await keywords.

  1. End-to-End Testing & Git Repository Clean-up

Swagger Testing: Verified full user flows: User Registration ➔ Login ➔ POST /ai-analysis (201 Created with is_pneumonia: true, confidence: 0.9986) ➔ Duplicate request caching check (200 OK) ➔ Error handling (404/401/403).

Git Clean-up: Created PR #9 to dev, synchronized dev to main via fast-forward, and cleaned up the repository by deleting 9 merged branches (both local and remote), leaving a clean repository with only main and dev!

Google_drive
github

0개의 댓글