Skip to content

Commit

Permalink
feat: Added function to partition the text input into an array of 4 t…
Browse files Browse the repository at this point in the history
…uples (4 threads). Now working on creating threads
  • Loading branch information
nhatkhangcs committed Apr 18, 2023
1 parent 7f8c439 commit 4d4e707
Showing 1 changed file with 54 additions and 32 deletions.
86 changes: 54 additions & 32 deletions apis/routes/texttospeech.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,33 +9,47 @@

import threading
import concurrent.futures
import nltk

MAX_THREADS = 4


class SpeakRoute(BaseRoute):
def __init__(self):
super(SpeakRoute, self).__init__(prefix="/speak")

# improvement: use a thread pool to process the input text
# partition the input text into 4 chunks and process them in parallel
def partition_input(self, input_text):
"""we are only allowed to use up to 4 threads"""
split_input = input_text.split()
len_of_input = len(split_input)
chunk_size = len_of_input // 4
# Split the input text into sentences using the nltk library
# nltk.download('punkt')
sentences = nltk.sent_tokenize(input_text)
num_sentences = len(sentences)
print(num_sentences)

# Determine the chunk size based on the number of sentences
chunk_size = num_sentences // 4
remainder = num_sentences % 4

# Initialize the partitioned array
partitioned = []
if chunk_size > 0:
for i in range(MAX_THREADS):
start = i * chunk_size
end = (i+1) * chunk_size
if i == MAX_THREADS - 1:
end = len_of_input
partitioned.append(' '.join(split_input[start:end]))
else:
partitioned = split_input

# Divide the sentences into 4 tuples and append them to the partitioned array
start = 0
for i in range(4):
end = start + chunk_size
if i < remainder:
end += 1
partitioned.append(tuple(sentences[start:end]))
start = end

# print(partitioned)
return partitioned

def make_audio(self, y):
with torch.no_grad():
audio = hifigan.forward(y).cpu().squeeze().clamp(-1, 1).detach().numpy()
audio = hifigan.forward(
y).cpu().squeeze().clamp(-1, 1).detach().numpy()
audio = audio * 4
bytes_wav = bytes()
byte_io = io.BytesIO(bytes_wav)
Expand All @@ -46,34 +60,42 @@ def make_audio(self, y):
return audio_data

def translate_func(self, data: DataSpeech):
input_text = data.text

processed_text = self.partition_input(data.text)
print(processed_text)

if data.gender:
gender = data.gender
else:
gender = "both"

# generate_wav_file should take a wav file as argument
if gender == "male":
y = infer(input_text, generator, dct)
elif gender == "female":
y = infer(input_text, generator_fm, dct_fm)
else:
y = infer(input_text, generator, dct)
y_fm = infer(input_text, generator_fm, dct_fm)

audio_data = self.make_audio(y)
# process input_text into 4 chunks (multithreading)
output_data = self.process_audio(processed_text, gender)

if gender == "both":
audio_data_fm = self.make_audio(y_fm)
return OutDataSpeech(speech=audio_data, speech_fm=audio_data_fm)

return OutDataSpeech(speech=audio_data)
return output_data

def create_routes(self):
router = self.router

@router.post("/vi_ba")
async def translate(data: DataSpeech):
print(self.partition_input(data.text))
# self.partition_input(data.text)
return await self.wait(self.translate_func, data)

def process_audio(self, processed_text, gender):
threads_m = []
threads_fm = []

results = []

if gender == "male":


elif gender == "female":

else:


if gender == "both":

else:

0 comments on commit 4d4e707

Please sign in to comment.