FastAPI ML Skeleton 项目教程
1. 项目的目录结构及介绍
FastAPI ML Skeleton 项目的目录结构如下:
fastapi-ml-skeleton/
├── docs/
│ └── sample_payload.json
├── fastapi_skeleton/
│ ├── __init__.py
│ └── main.py
├── sample_model/
│ └── __init__.py
├── scripts/
│ ├── linting.sh
│ └── test.sh
├── tests/
│ └── __init__.py
├── .env.example
├── .flake8
├── .gitignore
├── LICENSE
├── MANIFEST.in
├── README.md
├── poetry.lock
├── pyproject.toml
├── setup.cfg
└── tox.ini
目录结构介绍
docs/
: 包含项目文档和示例 payload 文件。fastapi_skeleton/
: 包含 FastAPI 应用的主要代码。sample_model/
: 包含示例机器学习模型。scripts/
: 包含用于 linting 和测试的脚本。tests/
: 包含测试代码。.env.example
: 环境变量示例文件。.flake8
: Flake8 配置文件。.gitignore
: Git 忽略文件配置。LICENSE
: 项目许可证。MANIFEST.in
: 打包配置文件。README.md
: 项目说明文档。poetry.lock
: Poetry 依赖锁定文件。pyproject.toml
: 项目配置文件。setup.cfg
: 安装配置文件。tox.ini
: Tox 配置文件。
2. 项目的启动文件介绍
项目的启动文件位于 fastapi_skeleton/main.py
。该文件包含 FastAPI 应用的入口点。
from fastapi import FastAPI
app = FastAPI()
@app.get("/")
def read_root():
return {"message": "Hello World"}
启动命令
可以使用以下命令启动 FastAPI 应用:
uvicorn fastapi_skeleton.main:app --reload
启动后,可以通过 http://localhost:8000/docs
访问 Swagger UI 文档。
3. 项目的配置文件介绍
pyproject.toml
pyproject.toml
是项目的主要配置文件,包含项目元数据和依赖管理信息。
[tool.poetry]
name = "fastapi-ml-skeleton"
version = "1.0.0"
description = "FastAPI Skeleton App to serve machine learning models production-ready"
authors = ["Your Name <you@example.com>"]
license = "Apache-2.0"
[tool.poetry.dependencies]
python = "^3.8"
fastapi = "^0.68.0"
uvicorn = "^0.15.0"
[tool.poetry.dev-dependencies]
pytest = "^6.2.5"
[build-system]
requires = ["poetry-core>=1.0.0"]
build-backend = "poetry.core.masonry.api"
.env.example
.env.example
是环境变量示例文件,用于配置应用的环境变量。
# API Key
API_KEY=your_api_key
# Database URL
DATABASE_URL=sqlite:///./test.db
.flake8
.flake8
是 Flake8 配置文件,用于代码风格检查。
[flake8]
max-line-length = 88
ignore = E203, E266, E501, W503
setup.cfg
setup.cfg
是安装配置文件,包含项目打包和安装的相关配置。
[metadata]
name = fastapi-ml-skeleton
version = 1.0.0
description = FastAPI Skeleton App to serve machine learning models production-ready
author = Your Name
author_email = you@example.com
license = Apache-2.0
[options]
packages = find:
install_requires =
fastapi
uvicorn
[options.packages