afro-speech / app.py
chrisjay's picture
work on digits interface
52064f2
raw history blame
No virus
5.34 kB
import os
import csv
import pandas as pd
import gradio as gr
import huggingface_hub
import scipy.io.wavfile as wavf
from huggingface_hub import Repository
HF_TOKEN = os.environ.get("HF_TOKEN")
DATASET_REPO_URL = "https://huggingface.co/datasets/chrisjay/crowd-speech-africa"
DATA_FILENAME = "data.csv"
AUDIO_PATH = os.path.join("data",'wav')
DATA_FILE = os.path.join("data", DATA_FILENAME)
# Get a dropdown of all African languages
DEFAULT_LANGS = {'Igbo':'ibo','Yoruba':'yor','Hausa':'hau'}
repo = Repository(
local_dir="data", clone_from=DATASET_REPO_URL, use_auth_token=HF_TOKEN
)
repo.git_pull()
os.makedirs(AUDIO_PATH,exist_ok=True)
def push_record():
# Push the wav to a folder and reference the location
commit_url = repo.push_to_hub()
output = f'Recordings successfully pushed!'
output_string = "<html> <body> <div class='output' style='color:green; font-size:13px'>"+output+"</div> </body> </html>"
return output_string
def save_record(language,text,record):
# Save text and its corresponding record to flag
if language!=None and language!='Choose language' and record is not None:
lang_id = DEFAULT_LANGS[language]
text =text.strip()
# Write audio to file
audio_output_filename = os.path.join(AUDIO_PATH,f'{len(os.listdir(AUDIO_PATH))}.wav')
import pdb; pdb.set_trace()
wavf.write(audio_output_filename,record[0],record[1])
if not os.path.exists(DATA_FILE):
# Add header (necessary for listen)
with open(DATA_FILE, "a") as csvfile:
writer = csv.DictWriter(csvfile, fieldnames=["language", "audio","text"])
writer.writeheader()
with open(DATA_FILE, "a") as csvfile:
writer = csv.DictWriter(csvfile, fieldnames=["language", "audio","text"])
writer.writerow(
{"language": lang_id, "audio": audio_output_filename,"text": text}
)
output = f'Recording successfully saved! Click `Push` when you are done to send your recordings to the repo.'
if record is None:
output="No recording found!"
if language is None or language=='Choose language':
output = 'Language must be specified!'
output_string = "<html> <body> <div class='output' style='color:green; font-size:13px'>"+output+"</div> </body> </html>"
return output_string
def display_records():
df = pd.read_csv(DATA_FILE)
langs=df['language'].values
df['audio'] = df['audio'].apply(lambda x: x.replace('data/wav/','https://huggingface.co/datasets/chrisjay/crowd-speech-africa/resolve/main/wav/'))
audios = df['audio'].values
texts=df['text'].values
html = """<div>
<table>
<tr>
<th>language</th>
<th>audio</th>
<th>text</th>
</tr>"""
for lang, audio, text in zip(langs,audios,texts):
html+= f"""<tr>
<td>{lang}</td>
<td><audio controls><source src="{audio}" type="audio/wav"> </audio></td>
<td>{text}</td>
</tr>"""
html+="</table></div>"
return html
title = 'African Crowdsource Speech'
description = 'A platform to contribute to your African language by recording your voice'
markdown = """# Africa Crowdsource Speech
### a platform to contribute to your African language by recording your voice"""
GENDER = ['Choose Gender','Male','Female','Other','Prefer not to say']
NUMBERS = [i for i in range(21)]
# Interface design begins
block = gr.Blocks()
with block:
gr.Markdown(markdown)
with gr.Tabs():
with gr.TabItem('Record'):
#with gr.Row():
language = gr.inputs.Dropdown(choices = list(DEFAULT_LANGS.keys()),label="Choose language",default="Choose language")
with gr.Row():
name = gr.inputs.Textbox(placeholder='e.g John Doe',label="Your name")
gender = gr.inputs.Dropdown(choices=GENDER, type="value", default=None, label="Gender")
accent = gr.inputs.Textbox(label="Accent (if any)")
number = gr.inputs.Radio(choices=NUMBERS, type="value", default=None, label="Choose your number")
with gr.Row():
text = gr.inputs.Textbox(placeholder='e.g `one` is `otu` in Igbo or `ọkan` in Yoruba',label="Number in your language")
record = gr.inputs.Audio(source="microphone",label='Record your voice')
output_result = gr.outputs.HTML()
with gr.Row():
save = gr.Button("Save")
push = gr.Button('Submit')
save.click(save_record, inputs=[language,text,record],outputs=output_result)
push.click(push_record, inputs=[],outputs=output_result)
with gr.TabItem('Listen') as listen_tab:
gr.Markdown("Listen to the recordings contributed. You can find them <a href='https://huggingface.co/datasets/chrisjay/crowd-speech-africa' target='blank'>here</a>.")
listen = gr.Button("Listen")
listen.click(display_records,inputs=[],outputs=gr.outputs.HTML() )
block.launch()