高性能的HTTP Python后端项目,提供模块化、可扩展的AI功能服务。
/workspace/ ├── app/ # 应用主目录 │ ├── __init__.py │ ├── main.py # FastAPI入口 │ ├── config.py # 配置管理 │ ├── core/ # 核心模块 │ │ ├── __init__.py │ │ └── base.py # AI服务基类 │ ├── services/ # AI服务模块 │ │ ├── __init__.py # 服务注册表 │ │ └── whisper_align/ # Whisper对齐服务 │ │ ├── __init__.py │ │ └── service.py │ ├── api/ # API路由 │ │ ├── __init__.py │ │ └── v1/ │ │ ├── __init__.py │ │ └── endpoints/ │ │ ├── __init__.py │ │ └── whisper.py │ ├── models/ # 数据模型 │ │ ├── __init__.py │ │ └── schemas.py │ └── utils/ # 工具类(待添加) ├── requirements.txt # 依赖包 ├── .env.example # 配置示例 └── README.md
# 创建虚拟环境
python -m venv venv
# Linux/Mac (bash/zsh) 激活虚拟环境
source venv/bin/activate
# Linux/Mac (sh) 激活虚拟环境
. venv/bin/activate
# Windows 激活虚拟环境
venv\Scripts\activate
pip install -r requirements.txt
cp .env.example .env
# 根据需要修改.env配置
# 方式1: 直接运行
python -m app.main
# 方式2: 使用uvicorn
. venv/bin/activate && uvicorn app.main:app --host 0.0.0.0 --port 8000 --reload
uvicorn app.main:app --host 0.0.0.0 --port 8000 --reload
GET /health
GET /services
获取服务信息
GET /api/v1/whisper/info
上传文件对齐(推荐)
POST /api/v1/whisper/align Content-Type: multipart/form-data 参数: - audio: 音频文件 - text: 文案文件 - language: 语言代码 (默认: zh) - model: Whisper模型 (默认: medium) - device: 计算设备 (默认: cpu)
通过路径对齐
POST /api/v1/whisper/align-path Content-Type: application/json 参数: - audio_file: 音频文件路径 - text_file: 文案文件路径 - language: 语言代码 - model: Whisper模型 - device: 计算设备
在 app/services/ 下创建新目录:
# app/services/my_service/__init__.py
from .service import MyService
__all__ = ["MyService"]
# app/services/my_service/service.py
from app.core.base import BaseAIService
class MyService(BaseAIService):
@property
def service_name(self) -> str:
return "my_service"
@property
def service_description(self) -> str:
return "服务描述"
async def process(self, **kwargs):
# 实现处理逻辑
return {"result": "success"}
# app/services/__init__.py
from .my_service import MyService
SERVICE_REGISTRY = {
"my_service": MyService,
}
在 app/api/v1/endpoints/ 创建对应的路由文件。
| 配置项 | 说明 | 默认值 |
|---|---|---|
| APP_NAME | 应用名称 | AI功能服务 |
| DEBUG | 调试模式 | True |
| HOST | 监听地址 | 0.0.0.0 |
| PORT | 监听端口 | 8000 |
| WHISPER_MODEL | Whisper模型 | medium |
| WHISPER_DEVICE | 计算设备 | cpu |
| WHISPER_LANGUAGE | 默认语言 | zh |