Skip to content

Commit ba26ea3

Browse files
add funasr paraformer asr service impl (#1914)
* add funasr paraformer asr service impl * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * fix requirements deps; modify ASR READMEs * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * add funasr-paraformer dockerfile in github workflow --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent caed793 commit ba26ea3

File tree

16 files changed

+1129
-4
lines changed

16 files changed

+1129
-4
lines changed

.github/workflows/docker/compose/third_parties-compose.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,3 +117,7 @@ services:
117117
build:
118118
dockerfile: comps/third_parties/sglang/src/Dockerfile
119119
image: ${REGISTRY:-opea}/sglang:${TAG:-latest}
120+
funasr-paraformer:
121+
build:
122+
dockerfile: comps/third_parties/funasr/src/Dockerfile
123+
image: ${REGISTRY:-opea}/funasr-paraformer:${TAG:-latest}

comps/asr/deployment/docker_compose/compose.yaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
include:
55
- ../../../third_parties/whisper/deployment/docker_compose/compose.yaml
6+
- ../../../third_parties/funasr/deployment/docker_compose/compose.yaml
67

78
services:
89
asr:
@@ -33,6 +34,15 @@ services:
3334
depends_on:
3435
whisper-gaudi-service:
3536
condition: service_healthy
37+
asr-funasr-paraformer:
38+
extends: asr
39+
container_name: asr-funasr-paraformer-service
40+
environment:
41+
ASR_COMPONENT_NAME: ${ASR_COMPONENT_NAME:-OPEA_PARAFORMER_ASR}
42+
ENABLE_MCP: ${ENABLE_MCP:-False}
43+
depends_on:
44+
funasr-paraformer-service:
45+
condition: service_healthy
3646

3747
networks:
3848
default:

comps/asr/src/README.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,16 @@ ASR (Audio-Speech-Recognition) microservice helps users convert speech to text.
1212

1313
- **ASR Server**: This microservice is responsible for converting speech audio into text. It receives an audio file as input and returns the transcribed text, enabling downstream applications such as conversational bots to process spoken language. The ASR server supports deployment on both CPU and HPU platforms.
1414
- **Whisper Server**: This microservice is responsible for converting speech audio into text using the Whisper model. It exposes an API endpoint that accepts audio files and returns the transcribed text, supporting both CPU and HPU deployments. The Whisper server acts as the backend for ASR functionality in the overall architecture.
15+
- **FunASR Paraformer Server**: This microservice is responsible for converting speech audio into text using the Paraformer model with the FunASR toolkit. Similar to the Whisper Server, it exposes an API endpoint that accepts audio files and returns the transcribed text, supporting CPU deployments. The FunASR Paraformer server acts as the backend for ASR functionality in the overall architecture.
1516

1617
## Deployment Options
1718

1819
For detailed, step-by-step instructions on how to deploy the ASR microservice using Docker Compose on different Intel platforms, please refer to the deployment guide. The guide contains all necessary steps, including building images, configuring the environment, and running the service.
1920

20-
| Platform | Deployment Method | Link |
21-
| ----------------- | ----------------- | ---------------------------------------------------------- |
22-
| Intel Xeon/Gaudi2 | Docker Compose | [Deployment Guide](../deployment/docker_compose/README.md) |
21+
| Platform | Deployment Method | Link |
22+
| ----------------- | ----------------- | ------------------------------------------ |
23+
| Intel Xeon/Gaudi2 | Docker Compose | [Deployment Guide](./README_whisper.md) |
24+
| Intel Core | Docker Compose | [Deployment Guide](./README_paraformer.md) |
2325

2426
## Validated Configurations
2527

@@ -28,3 +30,4 @@ The following configurations have been validated for the ASR microservice.
2830
| **Deploy Method** | **Core Models** | **Platform** |
2931
| ----------------- | --------------- | ----------------- |
3032
| Docker Compose | Whisper | Intel Xeon/Gaudi2 |
33+
| Docker Compose | Paraformer | Intel Core |

comps/asr/src/README_paraformer.md

Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
# Deploying ASR Service
2+
3+
This document provides a comprehensive guide to deploying the ASR microservice pipeline with the Paraformer model on Intel platforms.
4+
5+
**Note:** This is an alternative of the [Whisper ASR service](./README.md). The Paraformer model supports both English and Mandarin audio input, and empirically it shows better performance in Mandarin than English.
6+
7+
## Table of contents
8+
9+
- [🚀 1. Quick Start with Docker Compose](#-1-quick-start-with-docker-compose): The recommended method for a fast and easy setup.
10+
- [🚀 2. Manual Step-by-Step Deployment (Advanced)](#-2-manual-step-by-step-deployment-advanced): For users who want to build and run each container individually.
11+
- [🚀 3. Start Microservice with Python](#-3-start-microservice-with-python): For users who prefer to run the ASR microservice directly with Python scripts.
12+
13+
## 🚀 1. Quick Start with Docker Compose
14+
15+
This method uses Docker Compose to start all necessary services with a single command. It is the fastest and easiest way to get the service running.
16+
17+
### 1.1. Access the Code
18+
19+
Clone the repository and navigate to the deployment directory:
20+
21+
```bash
22+
git clone https://github.com/opea-project/GenAIComps.git
23+
cd GenAIComps/comps/asr/deployment/docker_compose
24+
```
25+
26+
### 1.2. Deploy the Service
27+
28+
Choose the command corresponding to your target platform.
29+
30+
```bash
31+
export ip_address=$(hostname -I | awk '{print $1}')
32+
export ASR_ENDPOINT=http://$ip_address:7066
33+
export no_proxy=localhost,$no_proxy
34+
```
35+
36+
- **For Intel® Core® CPU:**
37+
```bash
38+
docker compose -f ../docker_compose/compose.yaml up funasr-paraformer-service asr-funasr-paraformer -d
39+
```
40+
**Note:** it might take some time for `funasr-paraformer-service` to get ready, depending on the model download time in your network environment. If it fails to start with error message `dependency failed to start: container funasr-paraformer-service is unhealthy`, try increasing healthcheck retries in `GenAIComps/comps/third_parties/funasr/deployment/docker_compose/compose.yaml`
41+
42+
### 1.3. Validate the Service
43+
44+
Once the containers are running, you can validate the service. **Note:** Run these commands from the root of the `GenAIComps` repository.
45+
46+
```bash
47+
# Test
48+
wget https://github.com/intel/intel-extension-for-transformers/raw/main/intel_extension_for_transformers/neural_chat/assets/audio/sample.wav
49+
curl http://localhost:9099/v1/audio/transcriptions \
50+
-H "Content-Type: multipart/form-data" \
51+
-F file="@./sample.wav" \
52+
-F model="paraformer-zh"
53+
```
54+
55+
### 1.4. Clean Up the Deployment
56+
57+
To stop and remove the containers, run the following command from the `comps/asr/deployment/docker_compose` directory:
58+
59+
```bash
60+
docker compose down
61+
```
62+
63+
---
64+
65+
## 🚀 2. Manual Step-by-Step Deployment (Advanced)
66+
67+
This section provides detailed instructions for building the Docker images and running each microservice container individually.
68+
69+
### 2.1. Clone the Repository
70+
71+
If you haven't already, clone the repository and navigate to the root directory:
72+
73+
```bash
74+
git clone https://github.com/opea-project/GenAIComps.git
75+
cd GenAIComps
76+
```
77+
78+
### 2.2. Build the Docker Images
79+
80+
#### 2.2.1. Build FunASR Paraformer Server Image
81+
82+
- **For Intel® Core® CPU:**
83+
```bash
84+
docker build -t opea/funasr-paraformer:latest --build-arg https_proxy=$https_proxy --build-arg http_proxy=$http_proxy -f comps/third_parties/funasr/src/Dockerfile .
85+
```
86+
87+
#### 2.2.2. Build ASR Service Image
88+
89+
```bash
90+
docker build -t opea/asr:latest --build-arg https_proxy=$https_proxy --build-arg http_proxy=$http_proxy -f comps/asr/src/Dockerfile .
91+
```
92+
93+
### 2.3 Start FunASR Paraformer and ASR Service
94+
95+
#### 2.3.1 Start FunASR Paraformer Server
96+
97+
- Core CPU
98+
99+
```bash
100+
docker run -p 7066:7066 --ipc=host -e http_proxy=$http_proxy -e https_proxy=$https_proxy -e no_proxy=$no_proxy opea/funasr-paraformer:latest
101+
```
102+
103+
#### 2.3.2 Start ASR service
104+
105+
```bash
106+
ip_address=$(hostname -I | awk '{print $1}')
107+
108+
docker run -d -p 9099:9099 --ipc=host -e http_proxy=$http_proxy -e https_proxy=$https_proxy -e no_proxy=$no_proxy -e ASR_ENDPOINT=http://$ip_address:7066 opea/asr:latest
109+
```
110+
111+
### 2.4 Validate the Service
112+
113+
After starting both containers, test the asr service endpoint. Make sure you are in the root directory of the `GenAIComps` repository.
114+
115+
```bash
116+
# Use curl or python
117+
118+
# curl
119+
wget https://github.com/intel/intel-extension-for-transformers/raw/main/intel_extension_for_transformers/neural_chat/assets/audio/sample.wav
120+
curl http://localhost:9099/v1/audio/transcriptions \
121+
-H "Content-Type: multipart/form-data" \
122+
-F file="@./sample.wav" \
123+
-F model="paraformer-zh"
124+
125+
# python
126+
python check_asr_server.py
127+
```
128+
129+
### 2.6. Clean Up the Deployment
130+
131+
To stop and remove the containers you started manually, use the `docker stop` and `docker rm` commands.
132+
133+
- **For Intel® Core® CPU:**
134+
```bash
135+
docker stop funasr-paraformer-service asr-funasr-paraformer-service
136+
docker rm funasr-paraformer-service asr-funasr-paraformer-service
137+
```
138+
139+
## 🚀 3. Start Microservice with Python
140+
141+
To start the ASR microservice with Python, you need to first install python packages.
142+
143+
### 3.1 Install Requirements
144+
145+
```bash
146+
pip install -r requirements-cpu.txt
147+
```
148+
149+
### 3.2 Start FunASR Paraformer Service/Test
150+
151+
- Core CPU
152+
153+
```bash
154+
cd comps/third_parties/funasr/src
155+
nohup python funasr_server.py --device=cpu &
156+
python check_funasr_server.py
157+
```
158+
159+
Note: please make sure that port 7066 is not occupied by other services. Otherwise, use the command `npx kill-port 7066` to free the port.
160+
161+
### 3.3 Start ASR Service/Test
162+
163+
```bash
164+
cd ../../..
165+
python opea_asr_microservice.py
166+
python check_asr_server.py
167+
```

comps/asr/deployment/docker_compose/README.md renamed to comps/asr/src/README_whisper.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
This document provides a comprehensive guide to deploying the ASR microservice pipeline on Intel platforms.
44

5-
This guide covers two deployment methods:
5+
## Table of contents
66

77
- [🚀 1. Quick Start with Docker Compose](#-1-quick-start-with-docker-compose): The recommended method for a fast and easy setup.
88
- [🚀 2. Manual Step-by-Step Deployment (Advanced)](#-2-manual-step-by-step-deployment-advanced): For users who want to build and run each container individually.
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
# Copyright (C) 2025 Intel Corporation
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
import asyncio
5+
import os
6+
from typing import List, Union
7+
8+
import requests
9+
from fastapi import File, Form, UploadFile
10+
11+
from comps import CustomLogger, OpeaComponent, OpeaComponentRegistry, ServiceType
12+
from comps.cores.proto.api_protocol import AudioTranscriptionResponse
13+
14+
logger = CustomLogger("opea_paraformer")
15+
logflag = os.getenv("LOGFLAG", False)
16+
17+
18+
@OpeaComponentRegistry.register("OPEA_PARAFORMER_ASR")
19+
class OpeaParaformerAsr(OpeaComponent):
20+
"""A specialized ASR (Automatic Speech Recognition) component derived from OpeaComponent for FUNASR Paraformer ASR services.
21+
22+
Attributes:
23+
model_name (str): The name of the ASR model used.
24+
"""
25+
26+
def __init__(self, name: str, description: str, config: dict = None):
27+
super().__init__(name, ServiceType.ASR.name.lower(), description, config)
28+
self.base_url = os.getenv("ASR_ENDPOINT", "http://localhost:7066")
29+
health_status = self.check_health()
30+
if not health_status:
31+
logger.error("OpeaParaformerAsr health check failed.")
32+
33+
async def invoke(
34+
self,
35+
file: Union[str, UploadFile], # accept base64 string or UploadFile
36+
model: str = Form("paraformer-zh"),
37+
language: str = Form("english"),
38+
prompt: str = Form(None),
39+
response_format: str = Form("json"),
40+
temperature: float = Form(0),
41+
timestamp_granularities: List[str] = Form(None),
42+
) -> AudioTranscriptionResponse:
43+
"""Involve the ASR service to generate transcription for the provided input."""
44+
if isinstance(file, str):
45+
data = {"audio": file}
46+
# Send the file and model to the server
47+
response = await asyncio.to_thread(
48+
requests.post,
49+
f"{self.base_url}/v1/asr",
50+
json=data,
51+
)
52+
res = response.json()["asr_result"]
53+
return AudioTranscriptionResponse(text=res)
54+
else:
55+
# Read the uploaded file
56+
file_contents = await file.read()
57+
58+
# Prepare the files and data
59+
files = {
60+
"file": (file.filename, file_contents, file.content_type),
61+
}
62+
data = {
63+
"model": model,
64+
"language": language,
65+
"prompt": prompt,
66+
"response_format": response_format,
67+
"temperature": temperature,
68+
"timestamp_granularities": timestamp_granularities,
69+
}
70+
71+
# Send the file and model to the server
72+
response = await asyncio.to_thread(
73+
requests.post, f"{self.base_url}/v1/audio/transcriptions", files=files, data=data
74+
)
75+
res = response.json()["text"]
76+
return AudioTranscriptionResponse(text=res)
77+
78+
def check_health(self) -> bool:
79+
"""Checks the health of the embedding service.
80+
81+
Returns:
82+
bool: True if the service is reachable and healthy, False otherwise.
83+
"""
84+
try:
85+
response = requests.get(f"{self.base_url}/health")
86+
if response.status_code == 200:
87+
return True
88+
else:
89+
return False
90+
except Exception as e:
91+
# Handle connection errors, timeouts, etc.
92+
logger.error(f"Health check failed: {e}")
93+
return False

comps/asr/src/opea_asr_microservice.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from typing import List, Union
77

88
from fastapi import File, Form, UploadFile
9+
from integrations.funasr_paraformer import OpeaParaformerAsr
910
from integrations.whisper import OpeaWhisperAsr
1011

1112
from comps import (
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Copyright (C) 2025 Intel Corporation
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
services:
5+
funasr-paraformer-service:
6+
image: ${REGISTRY:-opea}/funasr-paraformer:${TAG:-latest}
7+
container_name: funasr-paraformer-service
8+
ports:
9+
- ${FUNASR_PARAFORMER_PORT:-7066}:7066
10+
ipc: host
11+
environment:
12+
no_proxy: ${no_proxy}
13+
http_proxy: ${http_proxy}
14+
https_proxy: ${https_proxy}
15+
restart: unless-stopped
16+
healthcheck:
17+
test: ["CMD", "curl", "-f", "http://localhost:7066/health"]
18+
interval: 10s
19+
timeout: 6s
20+
retries: 60
21+
# mount a host directory to cache models if needed, make sure it exists before starting the container
22+
# volumes:
23+
# - /home/user/.cache:/home/user/.cache:rw
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Copyright (C) 2025 Intel Corporation
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
FROM python:3.11-slim
5+
6+
RUN useradd -m -s /bin/bash user && \
7+
mkdir -p /home/user && \
8+
chown -R user /home/user/
9+
10+
# Set environment variables
11+
ENV LANG=en_US.UTF-8
12+
ARG ARCH=cpu
13+
14+
# Install system dependencies
15+
RUN apt-get update && apt-get install -y --no-install-recommends --fix-missing \
16+
curl \
17+
ffmpeg
18+
19+
COPY --chown=user:user comps /home/user/comps
20+
21+
ARG uvpip='uv pip install --system --no-cache-dir'
22+
RUN pip install --no-cache-dir --upgrade pip setuptools uv && \
23+
$uvpip torch torchaudio --index-url https://download.pytorch.org/whl/cpu ; \
24+
$uvpip -r /home/user/comps/third_parties/funasr/src/requirements-cpu.txt
25+
26+
27+
ENV PYTHONPATH=$PYTHONPATH:/home/user
28+
USER user
29+
WORKDIR /home/user/comps/third_parties/funasr/src
30+
31+
ENTRYPOINT ["python", "funasr_server.py", "--device", "cpu"]
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Copyright (C) 2025 Intel Corporation
2+
# SPDX-License-Identifier: Apache-2.0

0 commit comments

Comments
 (0)