Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Spycsh committed Dec 17, 2024
1 parent c955e5e commit ccb8aae
Show file tree
Hide file tree
Showing 16 changed files with 285 additions and 86 deletions.
27 changes: 27 additions & 0 deletions comps/asr/deployment/docker_compose/compose_whisper.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Copyright (C) 2024 Intel Corporation
# SPDX-License-Identifier: Apache-2.0

services:
whisper-service:
image: ${REGISTRY:-opea}/whisper:${TAG:-latest}
container_name: whisper-service
ports:
- "7066:7066"
ipc: host
environment:
no_proxy: ${no_proxy}
http_proxy: ${http_proxy}
https_proxy: ${https_proxy}
restart: unless-stopped
asr:
image: ${REGISTRY:-opea}/asr:${TAG:-latest}
container_name: asr-service
ports:
- "9099:9099"
ipc: host
environment:
ASR_ENDPOINT: ${ASR_ENDPOINT}

networks:
default:
driver: bridge
32 changes: 32 additions & 0 deletions comps/asr/deployment/docker_compose/compose_whisper_hpu.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Copyright (C) 2024 Intel Corporation
# SPDX-License-Identifier: Apache-2.0

services:
whisper-service:
image: ${REGISTRY:-opea}/whisper-gaudi:${TAG:-latest}
container_name: whisper-service
ports:
- "7066:7066"
ipc: host
environment:
no_proxy: ${no_proxy}
http_proxy: ${http_proxy}
https_proxy: ${https_proxy}
HABANA_VISIBLE_DEVICES: all
OMPI_MCA_btl_vader_single_copy_mechanism: none
runtime: habana
cap_add:
- SYS_NICE
restart: unless-stopped
asr:
image: ${REGISTRY:-opea}/asr:${TAG:-latest}
container_name: asr-service
ports:
- "3001:9099"
ipc: host
environment:
ASR_ENDPOINT: ${ASR_ENDPOINT}

networks:
default:
driver: bridge
8 changes: 4 additions & 4 deletions comps/asr/whisper/Dockerfile → comps/asr/src/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ COPY comps /home/user/comps
RUN pip install --no-cache-dir --upgrade pip setuptools && \
if [ "${ARCH}" = "cpu" ]; then \
pip install --no-cache-dir torch torchvision --index-url https://download.pytorch.org/whl/cpu ; \
pip install --no-cache-dir --extra-index-url https://download.pytorch.org/whl/cpu -r /home/user/comps/asr/whisper/requirements.txt ; \
pip install --no-cache-dir --extra-index-url https://download.pytorch.org/whl/cpu -r /home/user/comps/asr/src/requirements.txt ; \
else \
pip install --no-cache-dir -r /home/user/comps/asr/whisper/requirements.txt ; \
pip install --no-cache-dir -r /home/user/comps/asr/src/requirements.txt ; \
fi

ENV PYTHONPATH=$PYTHONPATH:/home/user

WORKDIR /home/user/comps/asr/whisper
WORKDIR /home/user/comps/asr/src

ENTRYPOINT ["python", "asr.py"]
ENTRYPOINT ["python", "opea_asr_microservice.py"]
27 changes: 15 additions & 12 deletions comps/asr/whisper/README.md → comps/asr/src/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pip install -r requirements.txt
- Xeon CPU

```bash
cd dependency/
cd integrations/dependency/whisper
nohup python whisper_server.py --device=cpu &
python check_whisper_server.py
```
Expand Down Expand Up @@ -51,15 +51,15 @@ curl http://localhost:7066/v1/audio/transcriptions \
### 1.3 Start ASR Service/Test

```bash
cd ../
python asr.py
cd ../../..
python opea_asr_microservice.py
python check_asr_server.py
```

While the Whisper service is running, you can start the ASR service. If the ASR service is running properly, you should see the output similar to the following:

```bash
{'id': '0e686efd33175ce0ebcf7e0ed7431673', 'text': 'who is pat gelsinger'}
{'text': 'who is pat gelsinger'}
```

## 🚀2. Start Microservice with Docker (Option 2)
Expand All @@ -74,20 +74,20 @@ Alternatively, you can also start the ASR microservice with Docker.

```bash
cd ../..
docker build -t opea/whisper:latest --build-arg https_proxy=$https_proxy --build-arg http_proxy=$http_proxy -f comps/asr/whisper/dependency/Dockerfile .
docker build -t opea/whisper:latest --build-arg https_proxy=$https_proxy --build-arg http_proxy=$http_proxy -f comps/asr/src/integrations/dependency/Dockerfile .
```

- Gaudi2 HPU

```bash
cd ../..
docker build -t opea/whisper-gaudi:latest --build-arg https_proxy=$https_proxy --build-arg http_proxy=$http_proxy -f comps/asr/whisper/dependency/Dockerfile.intel_hpu .
docker build -t opea/whisper-gaudi:latest --build-arg https_proxy=$https_proxy --build-arg http_proxy=$http_proxy -f comps/asr/src/integrations/dependency/Dockerfile.intel_hpu .
```

#### 2.1.2 ASR Service Image

```bash
docker build -t opea/asr:latest --build-arg https_proxy=$https_proxy --build-arg http_proxy=$http_proxy -f comps/asr/whisper/Dockerfile .
docker build -t opea/asr:latest --build-arg https_proxy=$https_proxy --build-arg http_proxy=$http_proxy -f comps/asr/src/Dockerfile .
```

### 2.2 Start Whisper and ASR Service
Expand All @@ -97,21 +97,21 @@ docker build -t opea/asr:latest --build-arg https_proxy=$https_proxy --build-arg
- Xeon

```bash
docker run -p 7066:7066 --ipc=host -e http_proxy=$http_proxy -e https_proxy=$https_proxy opea/whisper:latest
docker run -p 7066:7066 --ipc=host -e http_proxy=$http_proxy -e https_proxy=$https_proxy -e no_proxy=$no_proxy opea/whisper:latest
```

- Gaudi2 HPU

```bash
docker run -p 7066:7066 --runtime=habana -e HABANA_VISIBLE_DEVICES=all -e OMPI_MCA_btl_vader_single_copy_mechanism=none --cap-add=sys_nice --ipc=host -e http_proxy=$http_proxy -e https_proxy=$https_proxy opea/whisper-gaudi:latest
docker run -p 7066:7066 --runtime=habana -e HABANA_VISIBLE_DEVICES=all -e OMPI_MCA_btl_vader_single_copy_mechanism=none --cap-add=sys_nice --ipc=host -e http_proxy=$http_proxy -e https_proxy=$https_proxy -e no_proxy=$no_proxy opea/whisper-gaudi:latest
```

#### 2.2.2 Start ASR service

```bash
ip_address=$(hostname -I | awk '{print $1}')

docker run -d -p 9099:9099 --ipc=host -e http_proxy=$http_proxy -e https_proxy=$https_proxy -e ASR_ENDPOINT=http://$ip_address:7066 opea/asr:latest
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
```

#### 2.2.3 Test
Expand All @@ -120,8 +120,11 @@ docker run -d -p 9099:9099 --ipc=host -e http_proxy=$http_proxy -e https_proxy=$
# Use curl or python

# curl
http_proxy="" curl http://localhost:9099/v1/audio/transcriptions -XPOST -d '{"byte_str": "UklGRigAAABXQVZFZm10IBIAAAABAAEARKwAAIhYAQACABAAAABkYXRhAgAAAAEA"}' -H 'Content-Type: application/json'

wget https://github.com/intel/intel-extension-for-transformers/raw/main/intel_extension_for_transformers/neural_chat/assets/audio/sample.wav
curl http://localhost:9099/v1/audio/transcriptions \
-H "Content-Type: multipart/form-data" \
-F file="@./sample.wav" \
-F model="openai/whisper-small"

# python
python check_asr_server.py
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,24 @@
file_name,
)

with open(file_name, "rb") as f:
test_audio_base64_str = base64.b64encode(f.read()).decode("utf-8")
os.remove(file_name)

endpoint = "http://localhost:9099/v1/audio/transcriptions"
inputs = {"byte_str": test_audio_base64_str}
response = requests.post(url=endpoint, data=json.dumps(inputs), proxies={"http": None})
print(response.json())
headers = {"accept": "application/json"}

# Prepare the data and files
data = {
"model": "openai/whisper-small",
"language": "english",
}

try:
with open(file_name, "rb") as audio_file:
files = {"file": (file_name, audio_file)}
response = requests.post(endpoint, headers=headers, data=data, files=files)
if response.status_code != 200:
print(f"Failure with {response.reason}!")
else:
print(response.json())
except Exception as e:
print(f"Failure with {e}!")

os.remove(file_name)
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,16 @@ COPY --chown=user:user comps /home/user/comps
USER user

RUN pip install --no-cache-dir --upgrade pip setuptools && \
pip install --no-cache-dir -r /home/user/comps/asr/whisper/requirements.txt && \
pip install --no-cache-dir -r /home/user/comps/asr/src/requirements.txt && \
if [ "${ARCH}" = "cpu" ]; then \
pip install --no-cache-dir torch torchvision --index-url https://download.pytorch.org/whl/cpu ; \
pip install --no-cache-dir --extra-index-url https://download.pytorch.org/whl/cpu -r /home/user/comps/asr/whisper/requirements.txt ; \
pip install --no-cache-dir --extra-index-url https://download.pytorch.org/whl/cpu -r /home/user/comps/asr/src/requirements.txt ; \
else \
pip install --no-cache-dir -r /home/user/comps/asr/whisper/requirements.txt ; \
pip install --no-cache-dir -r /home/user/comps/asr/src/requirements.txt ; \
fi

ENV PYTHONPATH=$PYTHONPATH:/home/user

WORKDIR /home/user/comps/asr/whisper/dependency
WORKDIR /home/user/comps/asr/src/integations/dependency/whisper

ENTRYPOINT ["python", "whisper_server.py", "--device", "cpu"]
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ USER user

# Install requirements and optimum habana
RUN pip install --no-cache-dir --upgrade pip && \
pip install --no-cache-dir -r /home/user/comps/asr/whisper/requirements.txt && \
pip install --no-cache-dir -r /home/user/comps/asr/src/requirements.txt && \
pip install --no-cache-dir optimum[habana]

ENV PYTHONPATH=$PYTHONPATH:/home/users

WORKDIR /home/user/comps/asr/whisper/dependency
WORKDIR /home/user/comps/asr/src/integations/dependency/whisper

ENTRYPOINT ["python", "whisper_server.py", "--device", "hpu"]
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
import base64
import os
import uuid
from typing import List, Optional, Union

from typing import List
import uvicorn
from fastapi import FastAPI, File, Form, Request, UploadFile
from fastapi.responses import Response
Expand Down
81 changes: 81 additions & 0 deletions comps/asr/src/integrations/opea_whisper_asr.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
# Copyright (C) 2024 Intel Corporation
# SPDX-License-Identifier: Apache-2.0

import aiohttp
from typing import List
import os
import requests
from fastapi import File, Form, UploadFile

from comps import OpeaComponent, CustomLogger, ServiceType
from comps.cores.proto.api_protocol import (
AudioTranscriptionResponse,
)

logger = CustomLogger("opea_whisper_asr")
logflag = os.getenv("LOGFLAG", False)


class OpeaWhisperAsr(OpeaComponent):
"""
A specialized ASR (Automatic Speech Recognition) component derived from OpeaComponent for Whisper ASR services.
Attributes:
model_name (str): The name of the ASR model used.
"""
def __init__(self, name: str, description: str, config: dict = None):
super().__init__(name, ServiceType.ASR.name.lower(), description, config)
self.base_url = os.getenv("ASR_ENDPOINT", "http://localhost:7066/v1/audio/transcriptions")

async def invoke(
self,
file: UploadFile = File(...), # Handling the uploaded file directly
model: str = Form("openai/whisper-small"),
language: str = Form("english"),
prompt: str = Form(None),
response_format: str = Form("json"),
temperature: float = Form(0),
timestamp_granularities: List[str] = Form(None),
) -> AudioTranscriptionResponse:
"""
Invole the ASR service to generate transcription for the provided input.
"""
# Read the uploaded file
file_contents = await file.read()

# Prepare the files and data for requests.post
files = {
"file": (file.filename, file_contents, file.content_type),
}
data = {
"model": model,
"language": language,
"prompt": prompt,
"response_format": response_format,
"temperature": temperature,
"timestamp_granularities": timestamp_granularities,
}

# Send the file and model to the server
response = requests.post(self.base_url, files=files, data=data)
res = response.json()["text"]
return AudioTranscriptionResponse(text=res)


async def check_health(self) -> bool:
"""
Checks the health of the embedding service.
Returns:
bool: True if the service is reachable and healthy, False otherwise.
"""
try:
async with aiohttp.ClientSession() as client:
async with client.get(f"{self.base_url}/health") as response:
# If status is 200, the service is considered alive
if response.status == 200:
return True
except aiohttp.ClientError as e:
# Handle connection errors, timeouts, etc.
print(f"Health check failed: {e}")
return False
Loading

0 comments on commit ccb8aae

Please sign in to comment.