{"id":50371693,"url":"https://github.com/hczs/funasr-fastapi","last_synced_at":"2026-05-30T07:04:47.925Z","repository":{"id":327917954,"uuid":"1113614587","full_name":"hczs/funasr-fastapi","owner":"hczs","description":"基于 FastAPI 开发的 FunASR HTTP 服务，可 Docker 部署","archived":false,"fork":false,"pushed_at":"2025-12-10T08:25:19.000Z","size":114,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2025-12-10T10:59:10.405Z","etag":null,"topics":["fastapi","funasr"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/hczs.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2025-12-10T08:14:45.000Z","updated_at":"2025-12-10T08:28:29.000Z","dependencies_parsed_at":null,"dependency_job_id":null,"html_url":"https://github.com/hczs/funasr-fastapi","commit_stats":null,"previous_names":["hczs/funasr-fastapi"],"tags_count":null,"template":false,"template_full_name":null,"purl":"pkg:github/hczs/funasr-fastapi","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hczs%2Ffunasr-fastapi","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hczs%2Ffunasr-fastapi/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hczs%2Ffunasr-fastapi/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hczs%2Ffunasr-fastapi/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/hczs","download_url":"https://codeload.github.com/hczs/funasr-fastapi/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hczs%2Ffunasr-fastapi/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":33683025,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-26T15:22:16.424Z","status":"online","status_checked_at":"2026-05-30T02:00:06.278Z","response_time":92,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["fastapi","funasr"],"created_at":"2026-05-30T07:04:47.057Z","updated_at":"2026-05-30T07:04:47.920Z","avatar_url":"https://github.com/hczs.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# funasr-fastapi\n\n基于 FastAPI 与 FunASR 的轻量语音识别服务，提供文件上传推理接口，可通过 Docker 一键部署，支持自定义模型、日志级别和运行设备配置。\n\n## 目录结构\n- `src/main.py`：FastAPI 入口，注册路由和 CORS。\n- `src/core/`：配置加载、日志、音频校验与解析、启动生命周期。\n- `src/routes/asr.py`：ASR 推理接口。\n- `src/routes/health.py`：健康检查。\n- `src/service/asr_service.py`：FunASR 推理实现。\n- `download_model.py`：从 ModelScope 下载模型到本地。\n- `docker-compose.yml`、`Dockerfile`：容器化配置。\n- `models/`：本地模型存放目录（默认示例为 SenseVoiceSmall）。\n\n## 前置要求\n- Python 3.11+\n- uv（推荐）或 pip/virtualenv\n- 本地已准备好 FunASR 模型目录，或使用下方脚本下载\n\n## 快速开始（本地）\n1. 安装依赖：`uv sync`\n2. 复制模型到本地或下载：\n   ```bash\n   uv run python download_model.py \\\n     -mid iic/SenseVoiceSmall \\\n     -ld ./models/SenseVoiceSmall\n   ```\n3. 创建 `.env`（示例）：\n   ```\n   MODEL_DIR=./models/SenseVoiceSmall\n   DEVICE=cpu            # 可设 cuda / mps\n   LOG_LEVEL=INFO        # TRACE/DEBUG/INFO/WARNING/ERROR\n   ```\n4. 启动服务：`uv run uvicorn src.main:app --host 0.0.0.0 --port 8000`\n\n## Docker 运行\n```bash\ndocker compose up -d\n# 默认通过 8000 端口暴露\n# 环境变量可在 docker-compose.yml 中调整：MODEL_DIR / DEVICE / LOG_LEVEL\n```\n\n## API\n- 健康检查：`GET /health/`\n- 语音识别：`POST /v1/asr/recognize`\n  - 请求：`multipart/form-data`\n    - `file`：音频文件（仅单声道，默认支持 `.wav` / `.mp3`）\n    - `language`（可选）：`auto` | `zh` | `en` ...\n    - `use_itn`（可选）：`true/false`（是否启用 ITN）\n    - `other_params`（可选）：JSON 字符串，透传给 `AutoModel.generate`（如 `{\"batch_size_s\":30}`）\n  - 示例：\n    ```bash\n    curl -X POST http://localhost:8000/v1/asr/recognize \\\n      -F \"file=@/path/to/audio.wav\" \\\n      -F \"language=auto\" \\\n      -F \"use_itn=true\" \\\n      -F 'other_params={\"cache\":{}}'\n    ```\n  - 响应：纯文本转写结果（经过 `rich_transcription_postprocess` 处理）\n\n## 关键行为\n- 启动时自动加载模型（`lifespan` 钩子，单例缓存）。\n- 音频校验：文件缺失/格式不符/多声道/空数据会返回 400。\n- 日志：通过 Loguru 输出 JSON，便于容器日志收集。\n- 测试：`uv run pytest`（需要设置 `MODEL_DIR` 环境变量，见 `tests/core/test_config.py`）。\n\n## 开发提示\n- 调整运行设备：`.env` 或环境变量 `DEVICE=cpu|cuda|mps`。\n- 若需下载其他模型：`uv run python download_model.py -mid \u003cmodel_id\u003e -ld ./models/\u003cname\u003e`。\n- WebSocket 路由已预留在 `/v1/ws`，可按需扩展。\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhczs%2Ffunasr-fastapi","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhczs%2Ffunasr-fastapi","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhczs%2Ffunasr-fastapi/lists"}