Spaces:
Sahaltech
/
Running on CPU Upgrade

Anicet commited on
Commit
a6299ef
·
1 Parent(s): 6a7cb13

update: add dioula models for STT and TTS

Browse files
.gitignore CHANGED
@@ -1 +1,2 @@
1
- venv
 
 
1
+ venv
2
+ .env
Dockerfile CHANGED
@@ -9,4 +9,6 @@ COPY . .
9
 
10
  RUN pip install --no-cache-dir -r requirements.txt
11
 
 
 
12
  CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"]
 
9
 
10
  RUN pip install --no-cache-dir -r requirements.txt
11
 
12
+ # EXPOSE 8000
13
+ # CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]
14
  CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"]
functions/utils.py ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import soundfile as sf
2
+
3
+
4
+ def getAudioDuration(filePath: str) -> float:
5
+ try:
6
+ data, samplerate = sf.read(filePath)
7
+ duration = len(data) / samplerate
8
+ return duration
9
+ except Exception as e:
10
+ print(f"Error getting audio duration: {e}")
11
+ return 0.0
language/dioula/dyu_stt.py ADDED
@@ -0,0 +1,23 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ pipe = pipeline("automatic-speech-recognition", model=MODEL_NAME, model_kwargs={"target_lang": "dyu"})
7
+
8
+
9
+ def dioulaSTT(audioBase64: str) -> dict:
10
+ audioBytes = base64.b64decode(audioBase64)
11
+
12
+ with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as tempFile:
13
+ tempFile.write(audioBytes)
14
+ tempAudioPath = tempFile.name
15
+
16
+ try:
17
+ result = pipe(tempAudioPath)
18
+ text = result["text"]
19
+ duration = getAudioDuration(tempAudioPath)
20
+ finally:
21
+ os.remove(tempAudioPath)
22
+
23
+ return {'text': text, 'language': 'mos', 'duration': duration}
language/dioula/dyu_tts.py ADDED
@@ -0,0 +1,28 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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(MODEL_NAME)
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)
15
+
16
+ waveform = output.waveform[0].cpu().numpy()
17
+
18
+ with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as tempFile:
19
+ wavfile.write(tempFile.name, rate=model.config.sampling_rate, data=waveform)
20
+ tempAudioPath = tempFile.name
21
+
22
+ try:
23
+ with open(tempAudioPath, "rb") as file:
24
+ audioBase64 = base64.b64encode(file.read()).decode("utf-8")
25
+ finally:
26
+ os.remove(tempAudioPath)
27
+
28
+ return audioBase64
language/{mos_stt.py → moore/mos_stt.py} RENAMED
@@ -1,6 +1,6 @@
1
  import base64, tempfile, os
2
  from transformers import pipeline
3
- import soundfile as sf
4
  # from huggingface_hub import login
5
 
6
  MODEL_NAME = "facebook/mms-1b-all"
@@ -25,13 +25,3 @@ def mooreSTT(audioBase64: str) -> dict:
25
  os.remove(tempAudioPath)
26
 
27
  return {'text': text, 'language': 'mos', 'duration': duration}
28
-
29
-
30
- def getAudioDuration(filePath: str) -> float:
31
- try:
32
- data, samplerate = sf.read(filePath)
33
- duration = len(data) / samplerate
34
- return duration
35
- except Exception as e:
36
- print(f"Error getting audio duration: {e}")
37
- return 0.0
 
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"
 
25
  os.remove(tempAudioPath)
26
 
27
  return {'text': text, 'language': 'mos', 'duration': duration}
 
 
 
 
 
 
 
 
 
 
language/{mos_tts.py → moore/mos_tts.py} RENAMED
File without changes
main.py CHANGED
@@ -2,8 +2,11 @@ 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.mos_stt import mooreSTT
6
- from language.mos_tts import mooreTTS
 
 
 
7
 
8
 
9
  app = FastAPI(
@@ -12,7 +15,7 @@ app = FastAPI(
12
  )
13
 
14
 
15
- @app.post("/translateText")
16
  async def translate(request: Request):
17
  body: dict = await request.json()
18
  try:
@@ -27,8 +30,8 @@ async def translate(request: Request):
27
  raise HTTPException(status_code=400, detail=f"Translate error: {e}")
28
 
29
 
30
- @app.post("/speechToText")
31
- async def convertSpeechToText(request: Request):
32
  body: dict = await request.json()
33
  try:
34
  audioBase64 = body.get('audioBase64')
@@ -41,8 +44,8 @@ async def convertSpeechToText(request: Request):
41
  raise HTTPException(status_code=400, detail=f"STT error: {e}")
42
 
43
 
44
- @app.post("/textToSpeech")
45
- async def convertTextToSpeech(request: Request):
46
  body: dict = await request.json()
47
  try:
48
  text = body.get('text')
@@ -55,27 +58,41 @@ async def convertTextToSpeech(request: Request):
55
  raise HTTPException(status_code=400, detail=f"TTS error: {e}")
56
 
57
 
58
- @app.post("/moore/speechToText")
59
- async def mooreSpeechToText(request: Request):
60
  body: dict = await request.json()
61
  try:
62
  audioBase64 = body.get('audioBase64')
63
-
64
- data = mooreSTT(audioBase64=audioBase64)
65
- return data
 
 
 
 
 
 
 
66
  except Exception as e:
67
  print(f"STT error: {e}")
68
  raise HTTPException(status_code=400, detail=f"STT error: {e}")
69
 
70
 
71
- @app.post("/moore/textToSpeech")
72
- async def mooreTextToSpeech(request: Request):
73
  body: dict = await request.json()
74
  try:
75
  text = body.get('text')
 
76
 
77
- audioBase64 = mooreTTS(text=text)
78
- return { 'audioBase64': audioBase64 }
 
 
 
 
 
 
79
  except Exception as e:
80
  print(f"TTS error: {e}")
81
  raise HTTPException(status_code=400, detail=f"TTS error: {e}")
 
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(
 
15
  )
16
 
17
 
18
+ @app.post("/nllb/translateText")
19
  async def translate(request: Request):
20
  body: dict = await request.json()
21
  try:
 
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()
36
  try:
37
  audioBase64 = body.get('audioBase64')
 
44
  raise HTTPException(status_code=400, detail=f"STT error: {e}")
45
 
46
 
47
+ @app.post("/edge/textToSpeech")
48
+ async def edgeTextToSpeech(request: Request):
49
  body: dict = await request.json()
50
  try:
51
  text = body.get('text')
 
58
  raise HTTPException(status_code=400, detail=f"TTS error: {e}")
59
 
60
 
61
+ @app.post("/mms/speechToText")
62
+ async def mmsSpeechToText(request: Request):
63
  body: dict = await request.json()
64
  try:
65
  audioBase64 = body.get('audioBase64')
66
+ sourceLang = body.get('sourceLang')
67
+
68
+ if sourceLang == 'mos':
69
+ data = mooreSTT(audioBase64=audioBase64)
70
+ return data
71
+ elif sourceLang == 'dyu':
72
+ data = dioulaSTT(audioBase64=audioBase64)
73
+ return data
74
+ else:
75
+ raise HTTPException(status_code=400, detail=f"STT error: Invalid sourceLang - {sourceLang}")
76
  except Exception as e:
77
  print(f"STT error: {e}")
78
  raise HTTPException(status_code=400, detail=f"STT error: {e}")
79
 
80
 
81
+ @app.post("/mms/textToSpeech")
82
+ async def mmsTextToSpeech(request: Request):
83
  body: dict = await request.json()
84
  try:
85
  text = body.get('text')
86
+ sourceLang = body.get('sourceLang')
87
 
88
+ if sourceLang == 'mos':
89
+ audioBase64 = mooreTTS(text=text)
90
+ return { 'audioBase64': audioBase64 }
91
+ elif sourceLang == 'dyu':
92
+ audioBase64 = dioulaTTS(text=text)
93
+ return { 'audioBase64': audioBase64 }
94
+ else:
95
+ raise HTTPException(status_code=400, detail=f"STT error: Invalid sourceLang - {sourceLang}")
96
  except Exception as e:
97
  print(f"TTS error: {e}")
98
  raise HTTPException(status_code=400, detail=f"TTS error: {e}")