|
import argparse |
|
import csv |
|
import os |
|
import shutil |
|
import tarfile |
|
import tempfile |
|
from tqdm import tqdm |
|
|
|
from pydub import AudioSegment |
|
import requests |
|
from pocketbase import PocketBase |
|
|
|
parser = argparse.ArgumentParser(description="Command description.") |
|
|
|
pb = PocketBase('https://pocketbase.nenadb.dev/') |
|
|
|
def get_examples(): |
|
examples = pb.collection("examples").get_full_list(query_params={ |
|
"expand": "dialect", |
|
"filter": "validated=true", |
|
}) |
|
|
|
return examples |
|
|
|
def split_examples(examples, test_split=0.10, dev_split=0.10): |
|
subsets = {} |
|
|
|
for example in examples: |
|
dialect = example.expand['dialect'].name.lower() |
|
if not subsets.get(dialect): |
|
subsets[dialect] = { 'all': [] } |
|
subsets[dialect]['all'].append(example) |
|
|
|
for subset in subsets.values(): |
|
for i, example in enumerate(subset['all']): |
|
prog = i / len(subset['all']) |
|
|
|
if prog < test_split: |
|
split = 'test' |
|
elif prog < dev_split + test_split: |
|
split = 'dev' |
|
else: |
|
split = 'train' |
|
|
|
if not subset.get(split): |
|
subset[split] = [] |
|
subset[split].append(example) |
|
|
|
del subset['all'] |
|
|
|
return subsets |
|
|
|
def save_data(subsets): |
|
total_examples = sum( |
|
sum(len(split) for split in subset.values()) |
|
for subset in subsets.values() |
|
) |
|
|
|
with tqdm(total=total_examples) as pbar: |
|
for dialect, subset in subsets.items(): |
|
for split, examples in subset.items(): |
|
audio_dir_path = os.path.join("audio", dialect, split) |
|
os.makedirs(audio_dir_path, exist_ok=True) |
|
|
|
transcripts = [] |
|
transcript_dir_path = os.path.join("transcript", dialect) |
|
os.makedirs(transcript_dir_path, exist_ok=True) |
|
|
|
for example in examples: |
|
pbar.set_description(f"Downloading audios ({dialect} / {split})") |
|
pbar.update(1) |
|
audio_url = pb.get_file_url(example, example.speech, {}) |
|
response = requests.get(audio_url) |
|
with tempfile.NamedTemporaryFile() as f: |
|
f.write(response.content) |
|
f.flush() |
|
audio = AudioSegment.from_file(f.name) |
|
audio = audio.set_frame_rate(48000) |
|
audio_file_name = f"nena_speech_{example.id}.mp3" |
|
audio_file_path = os.path.join(audio_dir_path, audio_file_name) |
|
audio.export(audio_file_path, format="mp3") |
|
|
|
transcripts.append({ |
|
'transcription': example.transcription, |
|
'translation': example.translation, |
|
'path': audio_file_name, |
|
'age': example.age, |
|
}) |
|
|
|
break |
|
|
|
pbar.set_description(f"Saving audios ({dialect}/{split})") |
|
audio_tar_path = f"{audio_dir_path}.tar" |
|
with tarfile.open(audio_tar_path, 'w') as tar: |
|
tar.add(audio_dir_path, arcname=os.path.basename(audio_dir_path)) |
|
|
|
pbar.set_description(f"Saving transcripts ({dialect} / {split})") |
|
with open(os.path.join(transcript_dir_path, f"{split}.tsv"), 'w', newline='') as f: |
|
writer = csv.DictWriter(f, fieldnames=transcripts[0].keys(), delimiter='\t') |
|
writer.writeheader() |
|
writer.writerows(transcripts) |
|
|
|
shutil.rmtree(audio_dir_path) |
|
|
|
|
|
if __name__ == "__main__": |
|
parser = argparse.ArgumentParser(description="Generate text from prompt") |
|
|
|
parser.add_argument( |
|
"-b", |
|
"--build", |
|
action="store_true", |
|
help="Download text prompts from GCS bucket", |
|
) |
|
|
|
args = parser.parse_args() |
|
|
|
examples = get_examples() |
|
subsets = split_examples(examples) |
|
save_data(subsets) |
|
|