Spaces:
Sleeping
Sleeping
Hugo Rodrigues
commited on
Commit
·
cce0bf1
1
Parent(s):
b96cdfc
initial commit
Browse files- .dockerignore +34 -0
- .gitignore +22 -0
- Dockerfile +43 -0
- README.Docker.md +22 -0
- README.md +39 -3
- compose.yaml +51 -0
- main.py +54 -0
- packages.txt +1 -0
- requirements.txt +9 -0
.dockerignore
ADDED
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Include any files or directories that you don't want to be copied to your
|
2 |
+
# container here (e.g., local build artifacts, temporary files, etc.).
|
3 |
+
#
|
4 |
+
# For more help, visit the .dockerignore file reference guide at
|
5 |
+
# https://docs.docker.com/go/build-context-dockerignore/
|
6 |
+
|
7 |
+
**/.DS_Store
|
8 |
+
**/__pycache__
|
9 |
+
**/.venv
|
10 |
+
**/.classpath
|
11 |
+
**/.dockerignore
|
12 |
+
**/.env
|
13 |
+
**/.git
|
14 |
+
**/.gitignore
|
15 |
+
**/.project
|
16 |
+
**/.settings
|
17 |
+
**/.toolstarget
|
18 |
+
**/.vs
|
19 |
+
**/.vscode
|
20 |
+
**/*.*proj.user
|
21 |
+
**/*.dbmdl
|
22 |
+
**/*.jfm
|
23 |
+
**/bin
|
24 |
+
**/charts
|
25 |
+
**/docker-compose*
|
26 |
+
**/compose*
|
27 |
+
**/Dockerfile*
|
28 |
+
**/node_modules
|
29 |
+
**/npm-debug.log
|
30 |
+
**/obj
|
31 |
+
**/secrets.dev.yaml
|
32 |
+
**/values.dev.yaml
|
33 |
+
LICENSE
|
34 |
+
README.md
|
.gitignore
ADDED
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Build Artifacts
|
2 |
+
build/
|
3 |
+
|
4 |
+
# Core Dumps
|
5 |
+
core
|
6 |
+
|
7 |
+
# Byte-Compiled Modules
|
8 |
+
__pycache__/
|
9 |
+
|
10 |
+
# Extension Modules
|
11 |
+
*.so
|
12 |
+
|
13 |
+
# Packaging Artifacts
|
14 |
+
*.egg-info
|
15 |
+
*.whl
|
16 |
+
|
17 |
+
# IDEs and Tools
|
18 |
+
.idea/
|
19 |
+
.gdb_history
|
20 |
+
.vscode/
|
21 |
+
# Other
|
22 |
+
.DS_Store
|
Dockerfile
ADDED
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
FROM nvidia/cuda:12.1.0-cudnn8-devel-ubuntu20.04
|
2 |
+
LABEL maintainer="Hugging Face"
|
3 |
+
|
4 |
+
ARG DEBIAN_FRONTEND=noninteractive
|
5 |
+
|
6 |
+
RUN apt update
|
7 |
+
RUN apt install -y git libsndfile1-dev tesseract-ocr espeak-ng python3 python3-pip ffmpeg
|
8 |
+
RUN python3 -m pip install --no-cache-dir --upgrade pip
|
9 |
+
# RUN apt-get install -y git libsndfile1-dev tesseract-ocr espeak-ng ffmpeg
|
10 |
+
# Prevents Python from writing pyc files.
|
11 |
+
ENV PYTHONDONTWRITEBYTECODE=1
|
12 |
+
ENV OMP_NUM_THREADS=1
|
13 |
+
|
14 |
+
# Keeps Python from buffering stdout and stderr to avoid situations where
|
15 |
+
# the application crashes without emitting any logs due to buffering.
|
16 |
+
ENV PYTHONUNBUFFERED=1
|
17 |
+
|
18 |
+
# ENV HF_HUB_CACHE="/hub"
|
19 |
+
|
20 |
+
WORKDIR /app
|
21 |
+
|
22 |
+
# Create a non-privileged user that the app will run under.
|
23 |
+
# See https://docs.docker.com/go/dockerfile-user-best-practices/
|
24 |
+
RUN useradd -m -u 1000 user
|
25 |
+
# Download dependencies as a separate step to take advantage of Docker's caching.
|
26 |
+
# Leverage a cache mount to /root/.cache/pip to speed up subsequent builds.
|
27 |
+
# Leverage a bind mount to requirements.txt to avoid having to copy them into
|
28 |
+
# into this layer.
|
29 |
+
RUN --mount=type=cache,target=/root/.cache/pip \
|
30 |
+
--mount=type=bind,source=requirements.txt,target=requirements.txt \
|
31 |
+
python3 -m pip install -r requirements.txt
|
32 |
+
|
33 |
+
# Switch to the non-privileged user to run the application.
|
34 |
+
USER user
|
35 |
+
|
36 |
+
# Copy the source code into the container.
|
37 |
+
COPY . .
|
38 |
+
|
39 |
+
# Expose the port that the application listens on.
|
40 |
+
EXPOSE 8088
|
41 |
+
|
42 |
+
# Run the application.
|
43 |
+
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"]
|
README.Docker.md
ADDED
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
### Building and running your application
|
2 |
+
|
3 |
+
When you're ready, start your application by running:
|
4 |
+
`docker compose up --build`.
|
5 |
+
|
6 |
+
Your application will be available at http://localhost:8088.
|
7 |
+
|
8 |
+
### Deploying your application to the cloud
|
9 |
+
|
10 |
+
First, build your image, e.g.: `docker build -t myapp .`.
|
11 |
+
If your cloud uses a different CPU architecture than your development
|
12 |
+
machine (e.g., you are on a Mac M1 and your cloud provider is amd64),
|
13 |
+
you'll want to build the image for that platform, e.g.:
|
14 |
+
`docker build --platform=linux/amd64 -t myapp .`.
|
15 |
+
|
16 |
+
Then, push it to your registry, e.g. `docker push myregistry.com/myapp`.
|
17 |
+
|
18 |
+
Consult Docker's [getting started](https://docs.docker.com/go/get-started-sharing/)
|
19 |
+
docs for more detail on building and pushing.
|
20 |
+
|
21 |
+
### References
|
22 |
+
* [Docker's Python guide](https://docs.docker.com/language/python/)
|
README.md
CHANGED
@@ -1,10 +1,46 @@
|
|
1 |
---
|
2 |
-
title:
|
3 |
-
emoji:
|
4 |
-
colorFrom:
|
5 |
colorTo: indigo
|
6 |
sdk: docker
|
7 |
pinned: false
|
|
|
8 |
---
|
9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
---
|
2 |
+
title: Whisper
|
3 |
+
emoji: 🚀
|
4 |
+
colorFrom: indigo
|
5 |
colorTo: indigo
|
6 |
sdk: docker
|
7 |
pinned: false
|
8 |
+
license: apache-2.0
|
9 |
---
|
10 |
|
11 |
+
# Whisper
|
12 |
+
|
13 |
+
## TODO
|
14 |
+
|
15 |
+
- Use volumes cache export HF_DATASETS_CACHE="/path/to/another/directory"
|
16 |
+
|
17 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
18 |
+
|
19 |
+
mac book pro CPU
|
20 |
+
9.87 sec
|
21 |
+
9.51 sec
|
22 |
+
|
23 |
+
CPU free
|
24 |
+
29.15 sec
|
25 |
+
29.97 sec
|
26 |
+
28.38 sec
|
27 |
+
|
28 |
+
T4 small
|
29 |
+
1.127 sec
|
30 |
+
1.058 sec
|
31 |
+
1.054 sec
|
32 |
+
|
33 |
+
A10g small
|
34 |
+
0.92 sec
|
35 |
+
0.90 sec
|
36 |
+
0.94 sec
|
37 |
+
0.89 sec
|
38 |
+
|
39 |
+
OpenAI
|
40 |
+
1.027
|
41 |
+
1.297
|
42 |
+
1.904
|
43 |
+
1.303
|
44 |
+
1.509
|
45 |
+
1.870
|
46 |
+
1.587
|
compose.yaml
ADDED
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Comments are provided throughout this file to help you get started.
|
2 |
+
# If you need more help, visit the Docker compose reference guide at
|
3 |
+
# https://docs.docker.com/go/compose-spec-reference/
|
4 |
+
|
5 |
+
# Here the instructions define your application as a service called "server".
|
6 |
+
# This service is built from the Dockerfile in the current directory.
|
7 |
+
# You can add other services your application may depend on here, such as a
|
8 |
+
# database or a cache. For examples, see the Awesome Compose repository:
|
9 |
+
# https://github.com/docker/awesome-compose
|
10 |
+
services:
|
11 |
+
server:
|
12 |
+
build:
|
13 |
+
context: .
|
14 |
+
ports:
|
15 |
+
- 8088:7860
|
16 |
+
volumes:
|
17 |
+
- /Users/hugorodrigues/Projects/AI/HF/whisper:/app:rw
|
18 |
+
|
19 |
+
# The commented out section below is an example of how to define a PostgreSQL
|
20 |
+
# database that your application can use. `depends_on` tells Docker Compose to
|
21 |
+
# start the database before your application. The `db-data` volume persists the
|
22 |
+
# database data between container restarts. The `db-password` secret is used
|
23 |
+
# to set the database password. You must create `db/password.txt` and add
|
24 |
+
# a password of your choosing to it before running `docker compose up`.
|
25 |
+
# depends_on:
|
26 |
+
# db:
|
27 |
+
# condition: service_healthy
|
28 |
+
# db:
|
29 |
+
# image: postgres
|
30 |
+
# restart: always
|
31 |
+
# user: postgres
|
32 |
+
# secrets:
|
33 |
+
# - db-password
|
34 |
+
# volumes:
|
35 |
+
# - db-data:/var/lib/postgresql/data
|
36 |
+
# environment:
|
37 |
+
# - POSTGRES_DB=example
|
38 |
+
# - POSTGRES_PASSWORD_FILE=/run/secrets/db-password
|
39 |
+
# expose:
|
40 |
+
# - 5432
|
41 |
+
# healthcheck:
|
42 |
+
# test: [ "CMD", "pg_isready" ]
|
43 |
+
# interval: 10s
|
44 |
+
# timeout: 5s
|
45 |
+
# retries: 5
|
46 |
+
# volumes:
|
47 |
+
# db-data:
|
48 |
+
# secrets:
|
49 |
+
# db-password:
|
50 |
+
# file: db/password.txt
|
51 |
+
|
main.py
ADDED
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import time
|
2 |
+
|
3 |
+
from fastapi import FastAPI, UploadFile
|
4 |
+
from fastapi.middleware.cors import CORSMiddleware
|
5 |
+
|
6 |
+
|
7 |
+
import torch
|
8 |
+
from transformers import pipeline
|
9 |
+
|
10 |
+
app = FastAPI(docs_url="/api/docs")
|
11 |
+
|
12 |
+
app.add_middleware(
|
13 |
+
CORSMiddleware,
|
14 |
+
allow_origins=["*"],
|
15 |
+
allow_methods=["*"],
|
16 |
+
allow_headers=["*"],
|
17 |
+
allow_credentials=True,
|
18 |
+
)
|
19 |
+
|
20 |
+
device = "cuda:0" if torch.cuda.is_available() else "cpu"
|
21 |
+
torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32
|
22 |
+
|
23 |
+
BATCH_SIZE = 8
|
24 |
+
|
25 |
+
|
26 |
+
pipe = pipeline("automatic-speech-recognition",
|
27 |
+
"openai/whisper-large-v3",
|
28 |
+
torch_dtype=torch_dtype,
|
29 |
+
device=device)
|
30 |
+
|
31 |
+
|
32 |
+
@app.get("/device")
|
33 |
+
def getDevice():
|
34 |
+
start_time = time.time()
|
35 |
+
print("Time took to process the request and return response is {} sec".format(
|
36 |
+
time.time() - start_time))
|
37 |
+
return device
|
38 |
+
|
39 |
+
|
40 |
+
@app.post("/transcribe")
|
41 |
+
def transcribe(soundFile: UploadFile, task="transcribe"):
|
42 |
+
start_time = time.time()
|
43 |
+
|
44 |
+
if soundFile is None:
|
45 |
+
raise "No audio file submitted! Please upload or record an audio file before submitting your request."
|
46 |
+
|
47 |
+
inputFile = soundFile.file.read()
|
48 |
+
|
49 |
+
text = pipe(inputFile, batch_size=BATCH_SIZE, generate_kwargs={
|
50 |
+
"task": task}, return_timestamps=True)["text"]
|
51 |
+
|
52 |
+
print("Time took to process the request and return response is {} sec".format(
|
53 |
+
time.time() - start_time))
|
54 |
+
return text
|
packages.txt
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
ffmpeg
|
requirements.txt
ADDED
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
fastapi
|
2 |
+
pydantic
|
3 |
+
typing
|
4 |
+
transformers
|
5 |
+
python-multipart
|
6 |
+
torch
|
7 |
+
python-multipart
|
8 |
+
uvicorn[standard]
|
9 |
+
ffmpeg
|