Anicet commited on
Commit ·
405b2f1
1
Parent(s): b7a229d
update: review config for gpu machine
Browse files- .dockerignore +0 -1
- Dockerfile +11 -7
- README.md +4 -0
- functions/speech_to_text.py +6 -1
- functions/translation.py +6 -4
- functions/utils.py +10 -0
- language/dioula/dyu_stt.py +8 -5
- language/dioula/dyu_tts.py +6 -2
- language/fr_mos.py +8 -5
- language/moore/mos_stt.py +8 -5
- language/moore/mos_tts.py +6 -2
- main.py +2 -1
- requirements.txt +4 -3
.dockerignore
DELETED
|
@@ -1 +0,0 @@
|
|
| 1 |
-
venv
|
|
|
|
|
|
Dockerfile
CHANGED
|
@@ -1,12 +1,16 @@
|
|
| 1 |
-
FROM
|
| 2 |
-
# FROM pytorch/pytorch:2.4.1-cuda12.4-cudnn9-runtime
|
| 3 |
|
| 4 |
WORKDIR /app
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
COPY . .
|
| 12 |
|
|
@@ -14,7 +18,7 @@ RUN pip install --no-cache-dir --upgrade pip
|
|
| 14 |
RUN pip install --no-cache-dir -r requirements.txt
|
| 15 |
|
| 16 |
# CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"]
|
| 17 |
-
CMD ["python", "-m", "uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"]
|
| 18 |
|
| 19 |
# docker buildx build --platform linux/amd64 --no-cache -t ai-api .
|
| 20 |
# docker tag ai-api kora3/ai-api:latest
|
|
|
|
| 1 |
+
FROM pytorch/pytorch:2.6.0-cuda12.4-cudnn9-runtime
|
|
|
|
| 2 |
|
| 3 |
WORKDIR /app
|
| 4 |
|
| 5 |
+
ENV PYTHONUNBUFFERED=1 \
|
| 6 |
+
HF_HOME=/tmp/huggingface \
|
| 7 |
+
TRANSFORMERS_CACHE=/tmp/huggingface/transformers \
|
| 8 |
+
HF_DATASETS_CACHE=/tmp/huggingface/datasets \
|
| 9 |
+
NVIDIA_VISIBLE_DEVICES=all \
|
| 10 |
+
NVIDIA_DRIVER_CAPABILITIES=compute,utility
|
| 11 |
+
|
| 12 |
+
RUN apt-get update && apt-get install -y --no-install-recommends git ffmpeg && \
|
| 13 |
+
rm -rf /var/lib/apt/lists/*
|
| 14 |
|
| 15 |
COPY . .
|
| 16 |
|
|
|
|
| 18 |
RUN pip install --no-cache-dir -r requirements.txt
|
| 19 |
|
| 20 |
# CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"]
|
| 21 |
+
CMD ["python", "-m", "uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860", "--workers", "1"]
|
| 22 |
|
| 23 |
# docker buildx build --platform linux/amd64 --no-cache -t ai-api .
|
| 24 |
# docker tag ai-api kora3/ai-api:latest
|
README.md
CHANGED
|
@@ -10,3 +10,7 @@ license: apache-2.0
|
|
| 10 |
---
|
| 11 |
|
| 12 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
---
|
| 11 |
|
| 12 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
| 13 |
+
|
| 14 |
+
## GPU
|
| 15 |
+
|
| 16 |
+
L'API utilise CUDA automatiquement quand un GPU NVIDIA est disponible. Les modèles de traduction, MMS et TTS sont alors chargés sur le GPU ; `faster-whisper` utilise CUDA en `float16`.
|
functions/speech_to_text.py
CHANGED
|
@@ -1,7 +1,12 @@
|
|
| 1 |
from faster_whisper import WhisperModel
|
| 2 |
import base64, tempfile, os
|
|
|
|
| 3 |
|
| 4 |
-
model = WhisperModel(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
|
| 7 |
def speechToText(audioBase64: str, sourceLang: str) -> dict:
|
|
|
|
| 1 |
from faster_whisper import WhisperModel
|
| 2 |
import base64, tempfile, os
|
| 3 |
+
import torch
|
| 4 |
|
| 5 |
+
model = WhisperModel(
|
| 6 |
+
"base",
|
| 7 |
+
device="cuda" if torch.cuda.is_available() else "cpu",
|
| 8 |
+
compute_type="float16" if torch.cuda.is_available() else "int8",
|
| 9 |
+
)
|
| 10 |
|
| 11 |
|
| 12 |
def speechToText(audioBase64: str, sourceLang: str) -> dict:
|
functions/translation.py
CHANGED
|
@@ -1,17 +1,19 @@
|
|
| 1 |
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
| 2 |
import torch
|
|
|
|
| 3 |
|
| 4 |
MODEL_NAME = "facebook/nllb-200-distilled-600M" # facebook/nllb-200-3.3B
|
| 5 |
-
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 6 |
-
|
| 7 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
| 8 |
-
model = AutoModelForSeq2SeqLM.from_pretrained(
|
|
|
|
|
|
|
|
|
|
| 9 |
model.eval()
|
| 10 |
|
| 11 |
|
| 12 |
def translateText(text: str, sourceLang: str, targetLang: str) -> str:
|
| 13 |
tokenizer.src_lang = sourceLang
|
| 14 |
-
inputs = tokenizer(text, return_tensors="pt").to(
|
| 15 |
|
| 16 |
with torch.no_grad():
|
| 17 |
tokens = model.generate(
|
|
|
|
| 1 |
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
| 2 |
import torch
|
| 3 |
+
from functions.utils import DEVICE, TORCH_DTYPE
|
| 4 |
|
| 5 |
MODEL_NAME = "facebook/nllb-200-distilled-600M" # facebook/nllb-200-3.3B
|
|
|
|
|
|
|
| 6 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
| 7 |
+
model = AutoModelForSeq2SeqLM.from_pretrained(
|
| 8 |
+
MODEL_NAME,
|
| 9 |
+
torch_dtype=TORCH_DTYPE,
|
| 10 |
+
).to(DEVICE)
|
| 11 |
model.eval()
|
| 12 |
|
| 13 |
|
| 14 |
def translateText(text: str, sourceLang: str, targetLang: str) -> str:
|
| 15 |
tokenizer.src_lang = sourceLang
|
| 16 |
+
inputs = tokenizer(text, return_tensors="pt").to(DEVICE)
|
| 17 |
|
| 18 |
with torch.no_grad():
|
| 19 |
tokens = model.generate(
|
functions/utils.py
CHANGED
|
@@ -1,4 +1,14 @@
|
|
| 1 |
import soundfile as sf
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
|
| 4 |
def getAudioDuration(filePath: str) -> float:
|
|
|
|
| 1 |
import soundfile as sf
|
| 2 |
+
import torch
|
| 3 |
+
|
| 4 |
+
|
| 5 |
+
DEVICE = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 6 |
+
CUDA_AVAILABLE = DEVICE.type == "cuda"
|
| 7 |
+
TORCH_DTYPE = torch.float16 if CUDA_AVAILABLE else torch.float32
|
| 8 |
+
|
| 9 |
+
print(f"Using device: {DEVICE}")
|
| 10 |
+
if CUDA_AVAILABLE:
|
| 11 |
+
print(f"CUDA device: {torch.cuda.get_device_name(DEVICE)}")
|
| 12 |
|
| 13 |
|
| 14 |
def getAudioDuration(filePath: str) -> float:
|
language/dioula/dyu_stt.py
CHANGED
|
@@ -1,11 +1,14 @@
|
|
| 1 |
-
import base64, tempfile, os
|
| 2 |
from transformers import pipeline
|
| 3 |
-
from functions.utils import getAudioDuration
|
| 4 |
|
| 5 |
MODEL_NAME = "facebook/mms-1b-all"
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
|
| 11 |
def dioulaSTT(audioBase64: str) -> dict:
|
|
|
|
| 1 |
+
import base64, tempfile, os
|
| 2 |
from transformers import pipeline
|
| 3 |
+
from functions.utils import CUDA_AVAILABLE, TORCH_DTYPE, getAudioDuration
|
| 4 |
|
| 5 |
MODEL_NAME = "facebook/mms-1b-all"
|
| 6 |
+
pipe = pipeline(
|
| 7 |
+
"automatic-speech-recognition",
|
| 8 |
+
model=MODEL_NAME,
|
| 9 |
+
model_kwargs={"target_lang": "dyu", "torch_dtype": TORCH_DTYPE},
|
| 10 |
+
device=0 if CUDA_AVAILABLE else -1,
|
| 11 |
+
)
|
| 12 |
|
| 13 |
|
| 14 |
def dioulaSTT(audioBase64: str) -> dict:
|
language/dioula/dyu_tts.py
CHANGED
|
@@ -1,14 +1,18 @@
|
|
| 1 |
import torch, base64, tempfile, os
|
| 2 |
import scipy.io.wavfile as wavfile
|
| 3 |
from transformers import VitsModel, VitsTokenizer
|
|
|
|
| 4 |
|
| 5 |
MODEL_NAME = "facebook/mms-tts-dyu"
|
| 6 |
tokenizer = VitsTokenizer.from_pretrained(MODEL_NAME)
|
| 7 |
-
model = VitsModel.from_pretrained(
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
|
| 10 |
def dioulaTTS(text: str) -> str:
|
| 11 |
-
inputs = tokenizer(text, return_tensors="pt")
|
| 12 |
|
| 13 |
with torch.no_grad():
|
| 14 |
output = model(**inputs)
|
|
|
|
| 1 |
import torch, base64, tempfile, os
|
| 2 |
import scipy.io.wavfile as wavfile
|
| 3 |
from transformers import VitsModel, VitsTokenizer
|
| 4 |
+
from functions.utils import DEVICE, TORCH_DTYPE
|
| 5 |
|
| 6 |
MODEL_NAME = "facebook/mms-tts-dyu"
|
| 7 |
tokenizer = VitsTokenizer.from_pretrained(MODEL_NAME)
|
| 8 |
+
model = VitsModel.from_pretrained(
|
| 9 |
+
MODEL_NAME,
|
| 10 |
+
torch_dtype=TORCH_DTYPE,
|
| 11 |
+
).to(DEVICE)
|
| 12 |
|
| 13 |
|
| 14 |
def dioulaTTS(text: str) -> str:
|
| 15 |
+
inputs = tokenizer(text, return_tensors="pt").to(DEVICE)
|
| 16 |
|
| 17 |
with torch.no_grad():
|
| 18 |
output = model(**inputs)
|
language/fr_mos.py
CHANGED
|
@@ -1,18 +1,21 @@
|
|
| 1 |
import torch
|
| 2 |
from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
|
|
|
|
| 3 |
|
| 4 |
MODEL_NAME = "code-li/nllb-moore"
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
|
|
|
|
|
|
| 8 |
model.eval()
|
| 9 |
|
| 10 |
|
| 11 |
def translateFRMOS(text: str, sourceLang: str, targetLang: str):
|
| 12 |
-
tokenizer
|
| 13 |
TGT_LANG_ID = tokenizer.convert_tokens_to_ids(targetLang)
|
| 14 |
|
| 15 |
-
inputs = tokenizer(text, return_tensors="pt", truncation=True, max_length=128).to(
|
| 16 |
with torch.no_grad():
|
| 17 |
tokens = model.generate(
|
| 18 |
**inputs,
|
|
|
|
| 1 |
import torch
|
| 2 |
from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
|
| 3 |
+
from functions.utils import DEVICE, TORCH_DTYPE
|
| 4 |
|
| 5 |
MODEL_NAME = "code-li/nllb-moore"
|
| 6 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
| 7 |
+
model = AutoModelForSeq2SeqLM.from_pretrained(
|
| 8 |
+
MODEL_NAME,
|
| 9 |
+
torch_dtype=TORCH_DTYPE,
|
| 10 |
+
).to(DEVICE)
|
| 11 |
model.eval()
|
| 12 |
|
| 13 |
|
| 14 |
def translateFRMOS(text: str, sourceLang: str, targetLang: str):
|
| 15 |
+
tokenizer.src_lang = sourceLang
|
| 16 |
TGT_LANG_ID = tokenizer.convert_tokens_to_ids(targetLang)
|
| 17 |
|
| 18 |
+
inputs = tokenizer(text, return_tensors="pt", truncation=True, max_length=128).to(DEVICE)
|
| 19 |
with torch.no_grad():
|
| 20 |
tokens = model.generate(
|
| 21 |
**inputs,
|
language/moore/mos_stt.py
CHANGED
|
@@ -1,11 +1,14 @@
|
|
| 1 |
-
import base64, tempfile, os
|
| 2 |
from transformers import pipeline
|
| 3 |
-
from functions.utils import getAudioDuration
|
| 4 |
|
| 5 |
MODEL_NAME = "facebook/mms-1b-all"
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
# MODEL_NAME = "burkimbia/BIA-WHISPER-LARGE-SACHI_V3"
|
| 11 |
# pipe = pipeline("automatic-speech-recognition", model=MODEL_NAME)
|
|
|
|
| 1 |
+
import base64, tempfile, os
|
| 2 |
from transformers import pipeline
|
| 3 |
+
from functions.utils import CUDA_AVAILABLE, TORCH_DTYPE, getAudioDuration
|
| 4 |
|
| 5 |
MODEL_NAME = "facebook/mms-1b-all"
|
| 6 |
+
pipe = pipeline(
|
| 7 |
+
"automatic-speech-recognition",
|
| 8 |
+
model=MODEL_NAME,
|
| 9 |
+
model_kwargs={"target_lang": "mos", "torch_dtype": TORCH_DTYPE},
|
| 10 |
+
device=0 if CUDA_AVAILABLE else -1,
|
| 11 |
+
)
|
| 12 |
|
| 13 |
# MODEL_NAME = "burkimbia/BIA-WHISPER-LARGE-SACHI_V3"
|
| 14 |
# pipe = pipeline("automatic-speech-recognition", model=MODEL_NAME)
|
language/moore/mos_tts.py
CHANGED
|
@@ -1,14 +1,18 @@
|
|
| 1 |
import torch, base64, tempfile, os
|
| 2 |
import scipy.io.wavfile as wavfile
|
| 3 |
from transformers import VitsModel, VitsTokenizer
|
|
|
|
| 4 |
|
| 5 |
MODEL_NAME = "facebook/mms-tts-mos"
|
| 6 |
tokenizer = VitsTokenizer.from_pretrained(MODEL_NAME)
|
| 7 |
-
model = VitsModel.from_pretrained(
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
|
| 10 |
def mooreTTS(text: str) -> str:
|
| 11 |
-
inputs = tokenizer(text, return_tensors="pt")
|
| 12 |
|
| 13 |
with torch.no_grad():
|
| 14 |
output = model(**inputs)
|
|
|
|
| 1 |
import torch, base64, tempfile, os
|
| 2 |
import scipy.io.wavfile as wavfile
|
| 3 |
from transformers import VitsModel, VitsTokenizer
|
| 4 |
+
from functions.utils import DEVICE, TORCH_DTYPE
|
| 5 |
|
| 6 |
MODEL_NAME = "facebook/mms-tts-mos"
|
| 7 |
tokenizer = VitsTokenizer.from_pretrained(MODEL_NAME)
|
| 8 |
+
model = VitsModel.from_pretrained(
|
| 9 |
+
MODEL_NAME,
|
| 10 |
+
torch_dtype=TORCH_DTYPE,
|
| 11 |
+
).to(DEVICE)
|
| 12 |
|
| 13 |
|
| 14 |
def mooreTTS(text: str) -> str:
|
| 15 |
+
inputs = tokenizer(text, return_tensors="pt").to(DEVICE)
|
| 16 |
|
| 17 |
with torch.no_grad():
|
| 18 |
output = model(**inputs)
|
main.py
CHANGED
|
@@ -13,7 +13,8 @@ from language.fr_mos import translateFRMOS
|
|
| 13 |
|
| 14 |
import os
|
| 15 |
from huggingface_hub import login
|
| 16 |
-
|
|
|
|
| 17 |
|
| 18 |
|
| 19 |
|
|
|
|
| 13 |
|
| 14 |
import os
|
| 15 |
from huggingface_hub import login
|
| 16 |
+
if token := os.getenv("HF_TOKEN"):
|
| 17 |
+
login(token=token)
|
| 18 |
|
| 19 |
|
| 20 |
|
requirements.txt
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
|
|
| 1 |
absl-py==2.4.0
|
| 2 |
aiohappyeyeballs==2.6.2
|
| 3 |
aiohttp==3.14.1
|
|
@@ -128,15 +129,15 @@ srsly==2.5.3
|
|
| 128 |
starlette==1.2.1
|
| 129 |
SudachiDict-core==20260428
|
| 130 |
SudachiPy==0.6.11
|
| 131 |
-
sympy==1.
|
|
|
|
|
|
|
| 132 |
tabulate==0.10.0
|
| 133 |
tensorboard==2.20.0
|
| 134 |
tensorboard-data-server==0.7.2
|
| 135 |
thinc==8.3.13
|
| 136 |
threadpoolctl==3.6.0
|
| 137 |
tokenizers==0.22.2
|
| 138 |
-
torch==2.12.0
|
| 139 |
-
torchaudio==2.11.0
|
| 140 |
tqdm==4.68.1
|
| 141 |
trainer==0.0.36
|
| 142 |
transformers==5.12.1
|
|
|
|
| 1 |
+
--extra-index-url https://download.pytorch.org/whl/cu124
|
| 2 |
absl-py==2.4.0
|
| 3 |
aiohappyeyeballs==2.6.2
|
| 4 |
aiohttp==3.14.1
|
|
|
|
| 129 |
starlette==1.2.1
|
| 130 |
SudachiDict-core==20260428
|
| 131 |
SudachiPy==0.6.11
|
| 132 |
+
sympy==1.13.1
|
| 133 |
+
torch==2.6.0
|
| 134 |
+
torchaudio==2.6.0
|
| 135 |
tabulate==0.10.0
|
| 136 |
tensorboard==2.20.0
|
| 137 |
tensorboard-data-server==0.7.2
|
| 138 |
thinc==8.3.13
|
| 139 |
threadpoolctl==3.6.0
|
| 140 |
tokenizers==0.22.2
|
|
|
|
|
|
|
| 141 |
tqdm==4.68.1
|
| 142 |
trainer==0.0.36
|
| 143 |
transformers==5.12.1
|