Anicet commited on
Commit ·
b7a229d
1
Parent(s): a6299ef
update: minors corrections
Browse files- .dockerignore +1 -0
- Dockerfile +11 -4
- functions/translation.py +8 -6
- language/dioula/dyu_stt.py +5 -3
- language/fr_mos.py +23 -0
- language/moore/mos_stt.py +5 -4
- main.py +23 -0
- requirements.txt +1 -1
.dockerignore
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
venv
|
Dockerfile
CHANGED
|
@@ -1,14 +1,21 @@
|
|
| 1 |
FROM python:3.10-slim
|
| 2 |
-
# FROM pytorch/pytorch:2.
|
| 3 |
|
| 4 |
WORKDIR /app
|
| 5 |
|
| 6 |
RUN apt-get update && apt-get install -y git ffmpeg
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
COPY . .
|
| 9 |
|
|
|
|
| 10 |
RUN pip install --no-cache-dir -r requirements.txt
|
| 11 |
|
| 12 |
-
#
|
| 13 |
-
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
FROM python:3.10-slim
|
| 2 |
+
# FROM pytorch/pytorch:2.4.1-cuda12.4-cudnn9-runtime
|
| 3 |
|
| 4 |
WORKDIR /app
|
| 5 |
|
| 6 |
RUN apt-get update && apt-get install -y git ffmpeg
|
| 7 |
+
# RUN sed -i 's|http://archive.ubuntu.com/ubuntu|http://fr.archive.ubuntu.com/ubuntu|g' /etc/apt/sources.list && \
|
| 8 |
+
# apt-get update && apt-get install -y --no-install-recommends git && \
|
| 9 |
+
# rm -rf /var/lib/apt/lists/*
|
| 10 |
|
| 11 |
COPY . .
|
| 12 |
|
| 13 |
+
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
|
| 21 |
+
# docker push kora3/ai-api:latest
|
functions/translation.py
CHANGED
|
@@ -1,23 +1,25 @@
|
|
| 1 |
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
| 2 |
import torch
|
| 3 |
|
| 4 |
-
MODEL_NAME = "facebook/nllb-200-distilled-600M"
|
| 5 |
-
|
| 6 |
-
model = AutoModelForSeq2SeqLM.from_pretrained(MODEL_NAME)
|
| 7 |
|
|
|
|
|
|
|
| 8 |
model.eval()
|
| 9 |
|
| 10 |
|
| 11 |
def translateText(text: str, sourceLang: str, targetLang: str) -> str:
|
| 12 |
tokenizer.src_lang = sourceLang
|
| 13 |
-
inputs = tokenizer(text, return_tensors="pt")
|
| 14 |
|
| 15 |
with torch.no_grad():
|
| 16 |
tokens = model.generate(
|
| 17 |
**inputs,
|
| 18 |
forced_bos_token_id=tokenizer.convert_tokens_to_ids(targetLang),
|
| 19 |
-
|
| 20 |
-
|
|
|
|
| 21 |
)
|
| 22 |
|
| 23 |
translatedText = tokenizer.batch_decode(tokens, skip_special_tokens=True)[0]
|
|
|
|
| 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(MODEL_NAME).to(device)
|
| 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(device)
|
| 15 |
|
| 16 |
with torch.no_grad():
|
| 17 |
tokens = model.generate(
|
| 18 |
**inputs,
|
| 19 |
forced_bos_token_id=tokenizer.convert_tokens_to_ids(targetLang),
|
| 20 |
+
max_new_tokens=512,
|
| 21 |
+
num_beams=4,
|
| 22 |
+
early_stopping=True,
|
| 23 |
)
|
| 24 |
|
| 25 |
translatedText = tokenizer.batch_decode(tokens, skip_special_tokens=True)[0]
|
language/dioula/dyu_stt.py
CHANGED
|
@@ -1,9 +1,11 @@
|
|
| 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 |
def dioulaSTT(audioBase64: str) -> dict:
|
|
@@ -20,4 +22,4 @@ def dioulaSTT(audioBase64: str) -> dict:
|
|
| 20 |
finally:
|
| 21 |
os.remove(tempAudioPath)
|
| 22 |
|
| 23 |
-
return {'text': text, 'language': '
|
|
|
|
| 1 |
+
import base64, tempfile, os, torch
|
| 2 |
from transformers import pipeline
|
| 3 |
from functions.utils import getAudioDuration
|
| 4 |
|
| 5 |
MODEL_NAME = "facebook/mms-1b-all"
|
| 6 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 7 |
+
|
| 8 |
+
pipe = pipeline("automatic-speech-recognition", model=MODEL_NAME, model_kwargs={"target_lang": "dyu"}, device=device)
|
| 9 |
|
| 10 |
|
| 11 |
def dioulaSTT(audioBase64: str) -> dict:
|
|
|
|
| 22 |
finally:
|
| 23 |
os.remove(tempAudioPath)
|
| 24 |
|
| 25 |
+
return {'text': text, 'language': 'dyu', 'duration': duration}
|
language/fr_mos.py
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
|
| 3 |
+
|
| 4 |
+
MODEL_NAME = "code-li/nllb-moore"
|
| 5 |
+
|
| 6 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 7 |
+
model = AutoModelForSeq2SeqLM.from_pretrained(MODEL_NAME).to(device)
|
| 8 |
+
model.eval()
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
def translateFRMOS(text: str, sourceLang: str, targetLang: str):
|
| 12 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME, src_lang=sourceLang)
|
| 13 |
+
TGT_LANG_ID = tokenizer.convert_tokens_to_ids(targetLang)
|
| 14 |
+
|
| 15 |
+
inputs = tokenizer(text, return_tensors="pt", truncation=True, max_length=128).to(device)
|
| 16 |
+
with torch.no_grad():
|
| 17 |
+
tokens = model.generate(
|
| 18 |
+
**inputs,
|
| 19 |
+
forced_bos_token_id=TGT_LANG_ID,
|
| 20 |
+
max_length=128,
|
| 21 |
+
num_beams=4,
|
| 22 |
+
)
|
| 23 |
+
return tokenizer.decode(tokens[0], skip_special_tokens=True)
|
language/moore/mos_stt.py
CHANGED
|
@@ -1,11 +1,12 @@
|
|
| 1 |
-
import base64, tempfile, os
|
| 2 |
from transformers import pipeline
|
| 3 |
from functions.utils import getAudioDuration
|
| 4 |
-
# from huggingface_hub import login
|
| 5 |
|
| 6 |
MODEL_NAME = "facebook/mms-1b-all"
|
| 7 |
-
|
| 8 |
-
|
|
|
|
|
|
|
| 9 |
# MODEL_NAME = "burkimbia/BIA-WHISPER-LARGE-SACHI_V3"
|
| 10 |
# pipe = pipeline("automatic-speech-recognition", model=MODEL_NAME)
|
| 11 |
|
|
|
|
| 1 |
+
import base64, tempfile, os, torch
|
| 2 |
from transformers import pipeline
|
| 3 |
from functions.utils import getAudioDuration
|
|
|
|
| 4 |
|
| 5 |
MODEL_NAME = "facebook/mms-1b-all"
|
| 6 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 7 |
+
|
| 8 |
+
pipe = pipeline("automatic-speech-recognition", model=MODEL_NAME, model_kwargs={"target_lang": "mos"}, device=device)
|
| 9 |
+
|
| 10 |
# MODEL_NAME = "burkimbia/BIA-WHISPER-LARGE-SACHI_V3"
|
| 11 |
# pipe = pipeline("automatic-speech-recognition", model=MODEL_NAME)
|
| 12 |
|
main.py
CHANGED
|
@@ -2,11 +2,19 @@ from fastapi import FastAPI, Request, HTTPException
|
|
| 2 |
from functions.translation import translateText
|
| 3 |
from functions.speech_to_text import speechToText
|
| 4 |
from functions.text_to_speech import textToSpeech
|
|
|
|
| 5 |
from language.moore.mos_stt import mooreSTT
|
| 6 |
from language.moore.mos_tts import mooreTTS
|
|
|
|
| 7 |
from language.dioula.dyu_stt import dioulaSTT
|
| 8 |
from language.dioula.dyu_tts import dioulaTTS
|
| 9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
|
| 12 |
app = FastAPI(
|
|
@@ -30,6 +38,21 @@ async def translate(request: Request):
|
|
| 30 |
raise HTTPException(status_code=400, detail=f"Translate error: {e}")
|
| 31 |
|
| 32 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
@app.post("/whisper/speechToText")
|
| 34 |
async def whisperSpeechToText(request: Request):
|
| 35 |
body: dict = await request.json()
|
|
|
|
| 2 |
from functions.translation import translateText
|
| 3 |
from functions.speech_to_text import speechToText
|
| 4 |
from functions.text_to_speech import textToSpeech
|
| 5 |
+
|
| 6 |
from language.moore.mos_stt import mooreSTT
|
| 7 |
from language.moore.mos_tts import mooreTTS
|
| 8 |
+
|
| 9 |
from language.dioula.dyu_stt import dioulaSTT
|
| 10 |
from language.dioula.dyu_tts import dioulaTTS
|
| 11 |
|
| 12 |
+
from language.fr_mos import translateFRMOS
|
| 13 |
+
|
| 14 |
+
import os
|
| 15 |
+
from huggingface_hub import login
|
| 16 |
+
login(token=os.environ["HF_TOKEN"])
|
| 17 |
+
|
| 18 |
|
| 19 |
|
| 20 |
app = FastAPI(
|
|
|
|
| 38 |
raise HTTPException(status_code=400, detail=f"Translate error: {e}")
|
| 39 |
|
| 40 |
|
| 41 |
+
@app.post("/codeLi/translateText")
|
| 42 |
+
async def translateMoore(request: Request):
|
| 43 |
+
body: dict = await request.json()
|
| 44 |
+
try:
|
| 45 |
+
text = body.get('text')
|
| 46 |
+
sourceLang = body.get('sourceLang')
|
| 47 |
+
targetLang = body.get('targetLang')
|
| 48 |
+
|
| 49 |
+
translatedText = translateFRMOS(text=text, sourceLang=sourceLang, targetLang=targetLang)
|
| 50 |
+
return { 'translatedText': translatedText }
|
| 51 |
+
except Exception as e:
|
| 52 |
+
print(f"Translate mos error: {e}")
|
| 53 |
+
raise HTTPException(status_code=400, detail=f"Translate mos error: {e}")
|
| 54 |
+
|
| 55 |
+
|
| 56 |
@app.post("/whisper/speechToText")
|
| 57 |
async def whisperSpeechToText(request: Request):
|
| 58 |
body: dict = await request.json()
|
requirements.txt
CHANGED
|
@@ -86,7 +86,7 @@ networkx==2.8.8
|
|
| 86 |
nltk==3.9.4
|
| 87 |
num2words==0.5.14
|
| 88 |
numba==0.65.1
|
| 89 |
-
numpy=
|
| 90 |
onnxruntime==1.23.2
|
| 91 |
packaging==26.2
|
| 92 |
pandas==1.5.3
|
|
|
|
| 86 |
nltk==3.9.4
|
| 87 |
num2words==0.5.14
|
| 88 |
numba==0.65.1
|
| 89 |
+
numpy>=1.23.2,<2.0.0
|
| 90 |
onnxruntime==1.23.2
|
| 91 |
packaging==26.2
|
| 92 |
pandas==1.5.3
|