Hugo Rodrigues commited on
Commit
357cae7
1 Parent(s): 6f5929d

initial commit

Browse files
Files changed (8) hide show
  1. .gitignore +24 -0
  2. Dockerfile +45 -0
  3. README.md +54 -2
  4. audio.py +58 -0
  5. compose.yaml +54 -0
  6. main.py +95 -0
  7. packages.txt +1 -0
  8. requirements.txt +11 -0
.gitignore ADDED
@@ -0,0 +1,24 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
23
+
24
+ *.wav
Dockerfile ADDED
@@ -0,0 +1,45 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+
11
+ # Prevents Python from writing pyc files.
12
+ ENV PYTHONDONTWRITEBYTECODE=1
13
+ # Open MP threads. It may need to change in production env.
14
+ ENV OMP_NUM_THREADS=1
15
+
16
+ # Keeps Python from buffering stdout and stderr to avoid situations where
17
+ # the application crashes without emitting any logs due to buffering.
18
+ ENV PYTHONUNBUFFERED=1
19
+
20
+ # ENV HF_HUB_CACHE="/hub"
21
+
22
+ WORKDIR /app
23
+
24
+ # Create a non-privileged user that the app will run under.
25
+ # See https://docs.docker.com/go/dockerfile-user-best-practices/
26
+ RUN useradd -m -u 1000 user
27
+ # Download dependencies as a separate step to take advantage of Docker's caching.
28
+ # Leverage a cache mount to /root/.cache/pip to speed up subsequent builds.
29
+ # Leverage a bind mount to requirements.txt to avoid having to copy them into
30
+ # into this layer.
31
+ RUN --mount=type=cache,target=/root/.cache/pip \
32
+ --mount=type=bind,source=requirements.txt,target=requirements.txt \
33
+ python3 -m pip install -r requirements.txt
34
+
35
+ # Switch to the non-privileged user to run the application.
36
+ USER user
37
+
38
+ # Copy the source code into the container.
39
+ COPY . .
40
+
41
+ # Expose the port that the application listens on.
42
+ EXPOSE 8088
43
+
44
+ # Run the application.
45
+ CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"]
README.md CHANGED
@@ -1,10 +1,62 @@
1
  ---
2
  title: Translate
3
- emoji: 📈
4
- colorFrom: yellow
5
  colorTo: green
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: Translate
3
+ emoji: 📊
4
+ colorFrom: purple
5
  colorTo: green
6
  sdk: docker
7
  pinned: false
8
+ license: cc-by-4.0
9
  ---
10
 
11
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
12
+
13
+ # Translate
14
+
15
+ ## Usage
16
+
17
+ ```
18
+ conda activate hf
19
+ ```
20
+
21
+ VS Code Python select interpreter hf
22
+
23
+ ## Composa
24
+
25
+ ```
26
+ docker compose up --build
27
+ ```
28
+
29
+ ## Tests
30
+
31
+ Translate from English (eng) to Portuguese (por) the following text: "we the people of the united states in order to form a more perfect union establish justice ensure domestic tranquillity provide for the common defense"
32
+
33
+ mac book pro M1 16GB device = cpu.
34
+
35
+ - Do not run. Not enougth memory
36
+
37
+ HF CPU free
38
+
39
+ - 8.99 sec
40
+ - 9.06 sec
41
+ - 8.77 sec
42
+
43
+ T4 small
44
+
45
+ - 1.18 sec
46
+ - 1.12 sec
47
+ - 1.12 sec
48
+
49
+ A10G small
50
+
51
+ - 1.02 sec
52
+ - 1.00 sec
53
+ - 1.06 sec
54
+ - 1.01 sec
55
+
56
+ 2xA10G large
57
+
58
+ - 0.97 sec
59
+ - 0.95 sec
60
+ - 0.95 sec
61
+ - 0.95 sec
62
+ - 0.95 sec
audio.py ADDED
@@ -0,0 +1,58 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # %%
2
+ import time
3
+ from IPython.display import Audio
4
+ import numpy as np
5
+ from scipy.io.wavfile import write
6
+ from IPython.display import Audio
7
+
8
+ import torch
9
+ # from transformers import pipeline
10
+
11
+ from transformers import SeamlessM4Tv2Model
12
+ from transformers import AutoProcessor
13
+
14
+ model_name = "facebook/seamless-m4t-v2-large"
15
+ # model_name = "facebook/hf-seamless-m4t-medium"
16
+
17
+ processor = AutoProcessor.from_pretrained(model_name)
18
+ model = SeamlessM4Tv2Model.from_pretrained(model_name)
19
+
20
+
21
+ device = "cuda:0" if torch.cuda.is_available() else "cpu"
22
+ # torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32
23
+
24
+ model.to(device)
25
+
26
+ start_time = time.time()
27
+
28
+ src_lang = "eng"
29
+ tgt_lang = "por"
30
+
31
+ text_to_translate = "My life is a beautifull thing"
32
+
33
+ text_inputs = processor(text=text_to_translate,
34
+ src_lang=src_lang, return_tensors="pt").to(device)
35
+
36
+ # output_tokens = model.generate(
37
+ # **text_inputs, tgt_lang=tgt_lang, generate_speech=False)
38
+
39
+ # translated_text_from_text = processor.decode(
40
+ # output_tokens[0].tolist()[0], skip_special_tokens=True)
41
+
42
+ # %%
43
+ print(text_inputs)
44
+
45
+ # %%
46
+ audio_array_from_text = model.generate(
47
+ **text_inputs, tgt_lang=tgt_lang)[0].cpu().numpy().squeeze()
48
+
49
+ # %%
50
+ print(audio_array_from_text)
51
+
52
+ # %%
53
+
54
+ a = Audio(audio_array_from_text, rate=model.config.sampling_rate)
55
+
56
+ print(a)
57
+
58
+ # %%
compose.yaml ADDED
@@ -0,0 +1,54 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ - 8089:7860
16
+ volumes:
17
+ # - ${PWD}:/app:rw
18
+ - /Users/hugorodrigues/.cache/huggingface/hub:/hub:rw
19
+ environment:
20
+ - HF_HUB_CACHE=/hub
21
+
22
+ # The commented out section below is an example of how to define a PostgreSQL
23
+ # database that your application can use. `depends_on` tells Docker Compose to
24
+ # start the database before your application. The `db-data` volume persists the
25
+ # database data between container restarts. The `db-password` secret is used
26
+ # to set the database password. You must create `db/password.txt` and add
27
+ # a password of your choosing to it before running `docker compose up`.
28
+ # depends_on:
29
+ # db:
30
+ # condition: service_healthy
31
+ # db:
32
+ # image: postgres
33
+ # restart: always
34
+ # user: postgres
35
+ # secrets:
36
+ # - db-password
37
+ # volumes:
38
+ # - db-data:/var/lib/postgresql/data
39
+ # environment:
40
+ # - POSTGRES_DB=example
41
+ # - POSTGRES_PASSWORD_FILE=/run/secrets/db-password
42
+ # expose:
43
+ # - 5432
44
+ # healthcheck:
45
+ # test: [ "CMD", "pg_isready" ]
46
+ # interval: 10s
47
+ # timeout: 5s
48
+ # retries: 5
49
+ # volumes:
50
+ # db-data:
51
+ # secrets:
52
+ # db-password:
53
+ # file: db/password.txt
54
+
main.py ADDED
@@ -0,0 +1,95 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import time
2
+ from scipy.io.wavfile import write
3
+
4
+
5
+ # from typing import Union
6
+ # from pydantic import BaseModel
7
+ from fastapi import FastAPI
8
+ from fastapi.middleware.cors import CORSMiddleware
9
+ from fastapi.responses import FileResponse
10
+
11
+ # from fastapi.staticfiles import StaticFiles
12
+ # from fastapi.responses import FileResponse
13
+
14
+ import torch
15
+ # from transformers import pipeline
16
+
17
+ from transformers import SeamlessM4Tv2Model
18
+ from transformers import AutoProcessor
19
+
20
+ model_name = "facebook/seamless-m4t-v2-large"
21
+ # model_name = "facebook/hf-seamless-m4t-medium"
22
+
23
+ processor = AutoProcessor.from_pretrained(model_name)
24
+ model = SeamlessM4Tv2Model.from_pretrained(model_name)
25
+
26
+
27
+ device = "cuda:0" if torch.cuda.is_available() else "cpu"
28
+ # torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32
29
+
30
+ model.to(device)
31
+
32
+ app = FastAPI(docs_url="/api/docs")
33
+
34
+ app.add_middleware(
35
+ CORSMiddleware,
36
+ allow_origins=["*"],
37
+ allow_methods=["*"],
38
+ allow_headers=["*"],
39
+ allow_credentials=True,
40
+ )
41
+
42
+ BATCH_SIZE = 8
43
+
44
+
45
+ @app.get("/device")
46
+ def getDevice():
47
+ start_time = time.time()
48
+ print("Time took to process the request and return response is {} sec".format(
49
+ time.time() - start_time))
50
+ return device
51
+
52
+
53
+ @app.get("/translate")
54
+ def transcribe(inputs, src_lang="eng", tgt_lang="por"):
55
+ start_time = time.time()
56
+
57
+ if inputs is None:
58
+ raise "No audio file submitted! Please upload or record an audio file before submitting your request."
59
+
60
+ text_inputs = processor(text=inputs,
61
+ src_lang=src_lang, return_tensors="pt").to(device)
62
+
63
+ output_tokens = model.generate(
64
+ **text_inputs, tgt_lang=tgt_lang, generate_speech=False)
65
+
66
+ translated_text_from_text = processor.decode(
67
+ output_tokens[0].tolist()[0], skip_special_tokens=True)
68
+
69
+ print("Time took to process the request and return response is {} sec".format(
70
+ time.time() - start_time))
71
+ return translated_text_from_text
72
+
73
+
74
+ @app.get("/audio")
75
+ async def audio(inputs, src_lang="eng", tgt_lang="por", speaker_id=5):
76
+ start_time = time.time()
77
+
78
+ if inputs is None:
79
+ raise "No audio file submitted! Please upload or record an audio file before submitting your request."
80
+
81
+ text_inputs = processor(text=inputs,
82
+ src_lang=src_lang, return_tensors="pt").to(device)
83
+
84
+ audio_array_from_text = model.generate(
85
+ **text_inputs, tgt_lang=tgt_lang, speaker_id=int(speaker_id))[0].cpu().numpy().squeeze()
86
+
87
+ print("Time took to process the request and return response is {} sec".format(
88
+ time.time() - start_time))
89
+
90
+ print(f"sampling_rate {model.config.sampling_rate}")
91
+
92
+ write(f"/tmp/output{start_time}.wav", model.config.sampling_rate,
93
+ audio_array_from_text)
94
+
95
+ return FileResponse(f"/tmp/output{start_time}.wav", media_type="audio/mpeg")
packages.txt ADDED
@@ -0,0 +1 @@
 
 
1
+ ffmpeg
requirements.txt ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ fastapi
2
+ pydantic
3
+ typing
4
+ transformers
5
+ python-multipart
6
+ sentencepiece
7
+ protobuf
8
+ torch
9
+ uvicorn[standard]
10
+ ffmpeg
11
+ scipy