chrisjay commited on
Commit
694e002
1 Parent(s): 52064f2

test with states

Browse files
Files changed (1) hide show
  1. app.py +109 -64
app.py CHANGED
@@ -2,83 +2,133 @@ import os
2
  import csv
3
  import pandas as pd
4
  import gradio as gr
5
- import huggingface_hub
6
  import scipy.io.wavfile as wavf
7
- from huggingface_hub import Repository
 
 
 
8
 
9
  HF_TOKEN = os.environ.get("HF_TOKEN")
10
 
11
 
12
  DATASET_REPO_URL = "https://huggingface.co/datasets/chrisjay/crowd-speech-africa"
13
- DATA_FILENAME = "data.csv"
14
- AUDIO_PATH = os.path.join("data",'wav')
15
- DATA_FILE = os.path.join("data", DATA_FILENAME)
16
-
 
17
  # Get a dropdown of all African languages
18
- DEFAULT_LANGS = {'Igbo':'ibo','Yoruba':'yor','Hausa':'hau'}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
 
20
  repo = Repository(
21
  local_dir="data", clone_from=DATASET_REPO_URL, use_auth_token=HF_TOKEN
22
  )
23
  repo.git_pull()
24
 
25
- os.makedirs(AUDIO_PATH,exist_ok=True)
26
 
27
 
28
- def push_record():
29
- # Push the wav to a folder and reference the location
30
- commit_url = repo.push_to_hub()
31
- output = f'Recordings successfully pushed!'
32
- output_string = "<html> <body> <div class='output' style='color:green; font-size:13px'>"+output+"</div> </body> </html>"
33
- return output_string
34
-
35
 
 
 
36
 
37
- def save_record(language,text,record):
38
  # Save text and its corresponding record to flag
39
-
40
- if language!=None and language!='Choose language' and record is not None:
 
 
 
 
 
 
41
  lang_id = DEFAULT_LANGS[language]
42
  text =text.strip()
43
 
44
  # Write audio to file
45
- audio_output_filename = os.path.join(AUDIO_PATH,f'{len(os.listdir(AUDIO_PATH))}.wav')
46
- import pdb; pdb.set_trace()
 
 
47
  wavf.write(audio_output_filename,record[0],record[1])
48
 
49
- if not os.path.exists(DATA_FILE):
50
- # Add header (necessary for listen)
51
- with open(DATA_FILE, "a") as csvfile:
52
- writer = csv.DictWriter(csvfile, fieldnames=["language", "audio","text"])
53
- writer.writeheader()
54
-
55
- with open(DATA_FILE, "a") as csvfile:
56
- writer = csv.DictWriter(csvfile, fieldnames=["language", "audio","text"])
57
-
58
- writer.writerow(
59
- {"language": lang_id, "audio": audio_output_filename,"text": text}
60
- )
61
 
62
- output = f'Recording successfully saved! Click `Push` when you are done to send your recordings to the repo.'
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
63
 
 
 
64
  if record is None:
65
  output="No recording found!"
66
  if language is None or language=='Choose language':
67
  output = 'Language must be specified!'
68
  output_string = "<html> <body> <div class='output' style='color:green; font-size:13px'>"+output+"</div> </body> </html>"
 
 
69
  return output_string
70
 
71
 
72
  def display_records():
73
- df = pd.read_csv(DATA_FILE)
74
-
75
- langs=df['language'].values
76
- df['audio'] = df['audio'].apply(lambda x: x.replace('data/wav/','https://huggingface.co/datasets/chrisjay/crowd-speech-africa/resolve/main/wav/'))
77
- audios = df['audio'].values
78
- texts=df['text'].values
 
 
 
 
 
 
 
 
 
79
 
80
  html = """<div>
81
- <table>
82
  <tr>
83
  <th>language</th>
84
  <th>audio</th>
@@ -93,20 +143,17 @@ def display_records():
93
  html+="</table></div>"
94
  return html
95
 
 
 
96
 
 
97
 
98
 
99
- title = 'African Crowdsource Speech'
100
- description = 'A platform to contribute to your African language by recording your voice'
101
-
102
- markdown = """# Africa Crowdsource Speech
103
 
104
- ### a platform to contribute to your African language by recording your voice"""
 
105
 
106
 
107
- GENDER = ['Choose Gender','Male','Female','Other','Prefer not to say']
108
- NUMBERS = [i for i in range(21)]
109
-
110
  # Interface design begins
111
  block = gr.Blocks()
112
  with block:
@@ -114,26 +161,24 @@ with block:
114
  with gr.Tabs():
115
 
116
  with gr.TabItem('Record'):
117
- #with gr.Row():
118
- language = gr.inputs.Dropdown(choices = list(DEFAULT_LANGS.keys()),label="Choose language",default="Choose language")
119
-
120
  with gr.Row():
121
- name = gr.inputs.Textbox(placeholder='e.g John Doe',label="Your name")
122
- gender = gr.inputs.Dropdown(choices=GENDER, type="value", default=None, label="Gender")
123
- accent = gr.inputs.Textbox(label="Accent (if any)")
124
-
125
- number = gr.inputs.Radio(choices=NUMBERS, type="value", default=None, label="Choose your number")
126
- with gr.Row():
127
- text = gr.inputs.Textbox(placeholder='e.g `one` is `otu` in Igbo or `ọkan` in Yoruba',label="Number in your language")
128
- record = gr.inputs.Audio(source="microphone",label='Record your voice')
 
129
 
130
  output_result = gr.outputs.HTML()
131
- with gr.Row():
132
- save = gr.Button("Save")
133
- push = gr.Button('Submit')
 
134
 
135
- save.click(save_record, inputs=[language,text,record],outputs=output_result)
136
- push.click(push_record, inputs=[],outputs=output_result)
137
 
138
  with gr.TabItem('Listen') as listen_tab:
139
  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>.")
2
  import csv
3
  import pandas as pd
4
  import gradio as gr
5
+ from utils import *
6
  import scipy.io.wavfile as wavf
7
+ from huggingface_hub import Repository, upload_file
8
+
9
+
10
+
11
 
12
  HF_TOKEN = os.environ.get("HF_TOKEN")
13
 
14
 
15
  DATASET_REPO_URL = "https://huggingface.co/datasets/chrisjay/crowd-speech-africa"
16
+ #DATA_FILENAME = "data.csv"
17
+ REPOSITORY_DIR = "data"
18
+ LOCAL_DIR = 'data_local'
19
+ #DATA_FILE = os.path.join("data", DATA_FILENAME)
20
+ os.makedirs(LOCAL_DIR,exist_ok=True)
21
  # Get a dropdown of all African languages
22
+ #DEFAULT_LANGS = {'Igbo':'ibo','Yoruba':'yor','Hausa':'hau'}
23
+
24
+ GENDER = ['Choose Gender','Male','Female','Other','Prefer not to say']
25
+ NUMBERS = [i for i in range(21)]
26
+
27
+
28
+ #------------------Work on Languages--------------------
29
+ DEFAULT_LANGS = {}
30
+ languages = read_json_lines('clean_languages.json')
31
+ languages_lower=[l for l in languages]
32
+
33
+ _ = [DEFAULT_LANGS.update({l['full'].lower():l['id'].lower()}) for l in languages_lower]
34
+ #_ = [DEFAULT_LANGS.update({l_other.lower():[l['id'].lower()]}) for l in languages_lower for l_other in l['others'] if l_other.lower()!=l['full'].lower()]
35
+
36
+ #------------------Work on Languages--------------------
37
 
38
  repo = Repository(
39
  local_dir="data", clone_from=DATASET_REPO_URL, use_auth_token=HF_TOKEN
40
  )
41
  repo.git_pull()
42
 
 
43
 
44
 
 
 
 
 
 
 
 
45
 
46
+ def save_record(language,text,record,number,age,gender,accent,number_history):
47
+ number_history = number_history or [0]
48
 
 
49
  # Save text and its corresponding record to flag
50
+ speaker_metadata={}
51
+ speaker_metadata['gender'] = gender if gender!=GENDER[0] else ''
52
+ speaker_metadata['age'] = age if age !='' else ''
53
+ speaker_metadata['accent'] = accent if accent!='' else ''
54
+
55
+
56
+ if language!=None and language!='Choose language' and record is not None and number is not None:
57
+ language = language.lower()
58
  lang_id = DEFAULT_LANGS[language]
59
  text =text.strip()
60
 
61
  # Write audio to file
62
+ audio_name = get_unique_name()
63
+ SAVE_FILE_DIR = os.path.join(LOCAL_DIR,audio_name)
64
+ os.makedirs(SAVE_FILE_DIR,exist_ok=True)
65
+ audio_output_filename = os.path.join(SAVE_FILE_DIR,'audio.wav')
66
  wavf.write(audio_output_filename,record[0],record[1])
67
 
68
+ # Write metadata.json to file
69
+ json_file_path = os.path.join(SAVE_FILE_DIR,'metadata.jsonl')
70
+ metadata= {'id':audio_name,'file_name':'audio.wav',
71
+ 'language_name':language,'language_id':lang_id,
72
+ 'number':number, 'text':text,'frequency':record[0],
73
+ 'age': speaker_metadata['age'],'gender': speaker_metadata['gender'],
74
+ 'accent': speaker_metadata['accent']
75
+ }
 
 
 
 
76
 
77
+ dump_json(metadata,json_file_path)
78
+
79
+ # Simply upload the audio file and metadata using the hub's upload_file
80
+ # Upload the audio
81
+ repo_audio_path = os.path.join(REPOSITORY_DIR,os.path.join(audio_name,'audio.wav'))
82
+
83
+ _ = upload_file(path_or_fileobj = audio_output_filename,
84
+ path_in_repo =repo_audio_path,
85
+ repo_id='chrisjay/crowd-speech-africa',
86
+ repo_type='dataset',
87
+ token=HF_TOKEN
88
+ )
89
+
90
+ # Upload the metadata
91
+ repo_json_path = os.path.join(REPOSITORY_DIR,os.path.join(audio_name,'metadata.jsonl'))
92
+ _ = upload_file(path_or_fileobj = json_file_path,
93
+ path_in_repo =repo_json_path,
94
+ repo_id='chrisjay/crowd-speech-africa',
95
+ repo_type='dataset',
96
+ token=HF_TOKEN
97
+ )
98
+
99
+ output = f'Recording successfully saved!'
100
 
101
+ if number is None:
102
+ output = "Number must be specified!"
103
  if record is None:
104
  output="No recording found!"
105
  if language is None or language=='Choose language':
106
  output = 'Language must be specified!'
107
  output_string = "<html> <body> <div class='output' style='color:green; font-size:13px'>"+output+"</div> </body> </html>"
108
+
109
+ # return output_string, next image and state
110
  return output_string
111
 
112
 
113
  def display_records():
114
+ repo.git_pull()
115
+ REPOSITORY_DATA_DIR = os.path.join(REPOSITORY_DIR,'data')
116
+ repo_recordings = [os.path.join(REPOSITORY_DATA_DIR,f.name) for f in os.scandir(REPOSITORY_DATA_DIR)] if os.path.isdir(REPOSITORY_DATA_DIR) else []
117
+
118
+ audio_repo = [os.path.join(f,'audio.wav') for f in repo_recordings]
119
+ audio_repo = [a.replace('data/data/','https://huggingface.co/datasets/chrisjay/crowd-speech-africa/resolve/main/data/') for a in audio_repo]
120
+ metadata_repo = [read_json_lines(os.path.join(f,'metadata.jsonl'))[0] for f in repo_recordings]
121
+ audios_all = audio_repo
122
+ metadata_all = metadata_repo
123
+
124
+
125
+ langs=[m['language_name'] for m in metadata_all]
126
+ audios = [a for a in audios_all]
127
+ texts = [m['text'] for m in metadata_all]
128
+
129
 
130
  html = """<div>
131
+ <table style="width:100%; text-align:center">
132
  <tr>
133
  <th>language</th>
134
  <th>audio</th>
143
  html+="</table></div>"
144
  return html
145
 
146
+ NUMBER_DIR = './number'
147
+ number_files = [f.name for f in os.scandir(NUMBER_DIR)]
148
 
149
+ NUMBERS = [{'image':os.path.join(NUMBER_DIR,f),'number':int(f.split('.')[0])} for f in number_files]
150
 
151
 
 
 
 
 
152
 
153
+ markdown = """<div style="text-align: center"><p style="font-size: 40px"> Africa Crowdsource Speech </p> <br>
154
+ This is a platform to contribute to your African language by recording your voice </div>"""
155
 
156
 
 
 
 
157
  # Interface design begins
158
  block = gr.Blocks()
159
  with block:
161
  with gr.Tabs():
162
 
163
  with gr.TabItem('Record'):
 
 
 
164
  with gr.Row():
165
+ language = gr.inputs.Dropdown(choices = sorted([lang_.title() for lang_ in list(DEFAULT_LANGS.keys())]),label="Choose language",default="Choose language")
166
+ age = gr.inputs.Textbox(placeholder='e.g. 21',label="Your age (optional)",default='')
167
+ gender = gr.inputs.Dropdown(choices=GENDER, type="value", default=None, label="Gender (optional)")
168
+ accent = gr.inputs.Textbox(label="Accent (optional)",default='')
169
+
170
+ number = gr.Image('number/0.jpg',image_mode="L")
171
+ #number = gr.inputs.Radio(choices=NUMBERS, type="value", default=None, label="Choose your number")
172
+ text = gr.inputs.Textbox(placeholder='e.g. `one` is `otu` in Igbo or `ọkan` in Yoruba',label="Number in your language")
173
+ record = gr.inputs.Audio(source="microphone",label='Record your voice')
174
 
175
  output_result = gr.outputs.HTML()
176
+ state = gr.inputs.State()
177
+
178
+ save = gr.Button("Submit")
179
+
180
 
181
+ save.click(save_record, inputs=[language,text,record,number,age,gender,accent,state],outputs=[output_result,number,state])
 
182
 
183
  with gr.TabItem('Listen') as listen_tab:
184
  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>.")