chrisjay commited on
Commit
391decb
1 Parent(s): b829e6a

app.py with blocks

Browse files
Files changed (5) hide show
  1. README.md +1 -1
  2. app.py +82 -15
  3. data +1 -0
  4. gradio_queue.db +0 -0
  5. requirements.txt +1 -0
README.md CHANGED
@@ -4,7 +4,7 @@ emoji: 💻
4
  colorFrom: indigo
5
  colorTo: blue
6
  sdk: gradio
7
- sdk_version: 2.9.4
8
  app_file: app.py
9
  pinned: false
10
  ---
4
  colorFrom: indigo
5
  colorTo: blue
6
  sdk: gradio
7
+ sdk_version: 2.9.0b8
8
  app_file: app.py
9
  pinned: false
10
  ---
app.py CHANGED
@@ -1,46 +1,113 @@
1
  import os
 
 
2
  import gradio as gr
 
 
 
 
 
3
 
 
4
 
5
- #HF_TOKEN = os.environ.get("HF_TOKEN")
6
- #print("is none?", HF_TOKEN is None)
 
 
 
7
 
8
  # Get a dropdown of all African languages
9
  DEFAULT_LANGS = {'Igbo':'ibo','Yoruba':'yor','Hausa':'hau'}
10
 
11
- def get_record(language,text,record):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
  # Save text and its corresponding record to flag
 
13
  if language!=None and language!='Choose language':
14
  lang_id = DEFAULT_LANGS[language]
15
  text =text.strip()
16
- output = f'Recording successfully saved to dataset! Thank You.'+'\n'+f'(Text: <i>{text}</i>, language: {lang_id}).'
 
 
 
17
 
 
 
 
 
 
 
 
 
 
18
  else:
19
  output = 'Language must be specified!'
20
  output_string = "<html> <body> <div class='output' style='color:green; font-size:13px'>"+output+"</div> </body> </html>"
21
  return output_string
22
 
 
 
 
 
 
 
 
 
 
 
 
 
23
  title = 'African Crowdsource Speech'
24
  description = 'A platform to contribute to your African language by recording your voice'
25
 
26
- markdown = """African Crowdsource Speech: a platform to contribute to your African language by recording your voice"""
 
 
27
 
28
 
29
  # Interface design begins
30
  block = gr.Blocks()
31
  with block:
32
  gr.Markdown(markdown)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33
 
34
- #with gr.Tab('version1'):
35
- #with gr.Row():
36
- language = gr.inputs.Dropdown(choices = list(DEFAULT_LANGS.keys()),label="language",default="Choose language")
37
- text = gr.inputs.Textbox(placeholder='Write your text',label="text to record")
38
- record = gr.inputs.Audio(source="microphone",label='Record your voice')
39
- output_result = gr.outputs.HTML()
40
-
41
 
42
- save = gr.Button("Save")
43
-
44
- save.click(get_record, inputs=[language,text,record],outputs=output_result)
45
 
46
  block.launch(enable_queue=True)
1
  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
+ from IPython.display import Audio
9
+ from IPython.core.display import display
10
 
11
+ HF_TOKEN = os.environ.get("HF_TOKEN")
12
 
13
+
14
+ DATASET_REPO_URL = "https://huggingface.co/datasets/chrisjay/crowd-speech-africa"
15
+ DATA_FILENAME = "data.csv"
16
+ AUDIO_PATH = os.path.join("data",'wav')
17
+ DATA_FILE = os.path.join("data", DATA_FILENAME)
18
 
19
  # Get a dropdown of all African languages
20
  DEFAULT_LANGS = {'Igbo':'ibo','Yoruba':'yor','Hausa':'hau'}
21
 
22
+ repo = Repository(
23
+ local_dir="data", clone_from=DATASET_REPO_URL, use_auth_token=HF_TOKEN
24
+ )
25
+ #repo.git_pull()
26
+
27
+ os.makedirs(AUDIO_PATH,exist_ok=True)
28
+
29
+
30
+ def push_record():
31
+ # Push the wav to a folder and reference the location
32
+ commit_url = repo.push_to_hub()
33
+ output = f'Recordings successfully pushed!'
34
+ output_string = "<html> <body> <div class='output' style='color:green; font-size:13px'>"+output+"</div> </body> </html>"
35
+ return output_string
36
+
37
+
38
+
39
+ def save_record(language,text,record):
40
  # Save text and its corresponding record to flag
41
+
42
  if language!=None and language!='Choose language':
43
  lang_id = DEFAULT_LANGS[language]
44
  text =text.strip()
45
+
46
+ # Write audio to file
47
+ audio_output_filename = os.path.join(AUDIO_PATH,f'{len(os.listdir(AUDIO_PATH))}.wav')
48
+ wavf.write(audio_output_filename,record[0],record[1])
49
 
50
+ with open(DATA_FILE, "a") as csvfile:
51
+ writer = csv.DictWriter(csvfile, fieldnames=["language", "audio","text"])
52
+
53
+ writer.writerow(
54
+ {"language": lang_id, "audio": audio_output_filename,"text": text}
55
+ )
56
+
57
+ output = f'Recording successfully saved! Click `Push` when you are done to send your recordings to the repo.'
58
+
59
  else:
60
  output = 'Language must be specified!'
61
  output_string = "<html> <body> <div class='output' style='color:green; font-size:13px'>"+output+"</div> </body> </html>"
62
  return output_string
63
 
64
+
65
+ def display_records():
66
+ df = pd.read_csv(DATA_FILE)
67
+ #df['audio'] = df['audio'].apply(lambda x: display(Audio(x,autoplay=True)))
68
+ langs=df['language'].values
69
+ audios = df['audio'].values
70
+ texts=df['text'].values
71
+ return langs[0],audios[0],texts[0]
72
+ #return df
73
+
74
+
75
+
76
  title = 'African Crowdsource Speech'
77
  description = 'A platform to contribute to your African language by recording your voice'
78
 
79
+ markdown = """# Africa Crowdsource Speech
80
+
81
+ ### a platform to contribute to your African language by recording your voice"""
82
 
83
 
84
  # Interface design begins
85
  block = gr.Blocks()
86
  with block:
87
  gr.Markdown(markdown)
88
+ with gr.Tabs():
89
+
90
+ with gr.TabItem('Record'):
91
+ #with gr.Row():
92
+ language = gr.inputs.Dropdown(choices = list(DEFAULT_LANGS.keys()),label="language",default="Choose language")
93
+ text = gr.inputs.Textbox(placeholder='Write your text',label="text to record")
94
+ record = gr.inputs.Audio(source="microphone",label='Record your voice')
95
+ output_result = gr.outputs.HTML()
96
+
97
+
98
+ save = gr.Button("Save")
99
+ push = gr.Button('Push')
100
+
101
+ save.click(save_record, inputs=[language,text,record],outputs=output_result)
102
+ push.click(push_record, inputs=[],outputs=output_result)
103
+
104
+ with gr.TabItem('Listen') as listen_tab:
105
+ gr.Markdown("Listen to the recordings contributed. You can find them [here](https://huggingface.co/datasets/chrisjay/crowd-speech-africa).")
106
+ listen = gr.Button("Listen")
107
+ listen.click(display_records,inputs=[],outputs =[gr.outputs.Textbox(label='language'),gr.outputs.Audio(),gr.outputs.Textbox(label='text')] )
108
+
109
+
110
 
 
 
 
 
 
 
 
111
 
 
 
 
112
 
113
  block.launch(enable_queue=True)
data ADDED
@@ -0,0 +1 @@
 
1
+ Subproject commit f894808a582c943f6f7c034bf2cef6f37a202d34
gradio_queue.db ADDED
Binary file (16.4 kB). View file
requirements.txt CHANGED
@@ -0,0 +1 @@
 
1
+ pandas