Candidate: Refactor app.py into modular services
Files Affected: app.py
→
Proposed:
inference_pipeline.py,
model_loader.py,
preprocessor.py,
postprocessor.py
The Problem
The main application file (app.py) is acting as a "god object". It manages web framework setup, model loading, inference pipelines, preprocessing, postprocessing, utilities, and API endpoints simultaneously. This creates **low architectural depth** (the interface is nearly as complex as its implementation), **poor testability** (isolated pipeline units cannot be targeted), and **low locality** (unrelated logic must be parsed to fix individual features).
Proposed Solution
Split operational concerns into highly focused, cleanly encapsulated modules:
ModelLoader: Handles asynchronous loading and persistent caching of PyTorch weights (angle, inflammation, segmentation models).Preprocessor: Isolates structural image transformations and CLAHE enhancement parameters.InferencePipeline: Orchestrates execution flows: angle classification → inflammation detection → conditional segmentation → sizing/severity metrics.Postprocessor: Houses structural thickness evaluations, severity metrics calculations, and visual overlay generators.APIHandler: A thin, declarative FastAPI endpoint wrapper delegating exclusively to the primaryInferencePipeline.
Expected Architecture Benefits
-
Architectural Depth High complexity is hidden beneath simple interfaces (e.g.,
analyze_image(img) -> Result). - High Code Locality Bugs in sizing algorithms map cleanly to the postprocessor without risking model caching logic.
- Simplified Testing Individual pipeline states and weights can be unit-tested seamlessly using mock behaviors.
- AI and Dev Navigation Highly categorized file trees accelerate context gathering for language models and onboarding engineers.