marianna13 commited on
Commit
4766c38
1 Parent(s): 2b05ce7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -29
app.py CHANGED
@@ -6,20 +6,12 @@ import string
6
  import pandas as pd
7
  import os
8
  import requests
9
-
10
- os.system('python -m spacy download en_core_web_sm')
11
 
12
  nlp = spacy.load("en_core_web_sm")
13
  nlp.add_pipe('sentencizer')
14
 
15
 
16
-
17
- def read_gs(sheet_url):
18
- s_url = sheet_url.replace('/edit#gid=', '/export?format=csv&gid=')
19
- df = pd.read_csv(s_url)
20
- return df
21
-
22
-
23
  def download_and_save_file(URL, audio_dir):
24
  headers = {
25
  'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36',
@@ -33,45 +25,45 @@ def download_and_save_file(URL, audio_dir):
33
  file_name = URL.split('/')[-1].split('?')[0]
34
  audio_path = f'{audio_dir}/{file_name}'
35
  with open(audio_path, 'wb') as f:
36
- f.write(doc.content)
37
  return audio_path
38
 
39
- def select_samples():
40
-
41
- df = read_gs('https://docs.google.com/spreadsheets/d/17QG4puJRXN8V5froIv8YrJIMsns0GTt4/edit#gid=1020901598')
42
- audio_dir = 'AUDIO'
43
- os.makedirs(audio_dir, exist_ok=True)
44
- df = df.sample(1)
45
 
46
 
47
- audio_url = df.url.values[0]
 
 
 
48
 
49
- audio_path = download_and_save_file(audio_url, audio_dir)
50
- return audio_path, df['text'].values[0]
51
 
 
 
 
 
 
52
 
53
  title = '🎵 Annotate audio'
54
  description = '''Choose a sentence that describes audio the best if there's no such sentence please choose `No audio description`'''
55
 
56
- audio_path, full_text = select_samples()
 
 
 
 
57
  full_text = full_text.translate(str.maketrans('', '', string.punctuation))
58
- sents = [re.sub(r'###audio###\d###', '', s.text) for s in nlp(full_text).sents]
59
  sents.append('No audio description')
60
 
61
- def audio_demo(text, audio, audio_id):
62
 
63
- with open('data.json', 'w') as f:
64
- data = {
65
- 'audio':audio_id,
66
- 'text':text
67
- }
68
- json.dump(data, f)
69
  return 'success!'
70
 
71
 
72
  iface = gr.Interface(
73
  audio_demo,
74
- inputs=[gr.Dropdown(sents, label='audio description'), gr.Audio(audio_path, type="filepath"), gr.Textbox(value=audio_path, visible=False)],
75
  outputs=[gr.Textbox(label="output")],
76
  allow_flagging="never",
77
  title=title,
 
6
  import pandas as pd
7
  import os
8
  import requests
9
+ from textwrap import wrap
 
10
 
11
  nlp = spacy.load("en_core_web_sm")
12
  nlp.add_pipe('sentencizer')
13
 
14
 
 
 
 
 
 
 
 
15
  def download_and_save_file(URL, audio_dir):
16
  headers = {
17
  'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36',
 
25
  file_name = URL.split('/')[-1].split('?')[0]
26
  audio_path = f'{audio_dir}/{file_name}'
27
  with open(audio_path, 'wb') as f:
28
+ f.write(doc.content)
29
  return audio_path
30
 
 
 
 
 
 
 
31
 
32
 
33
+ credentials = os.environ['CREDENTIALS']
34
+ data = json.loads(credentials, strict=False)
35
+ with open('credentials.json', 'w') as f:
36
+ json.dump(data, f)
37
 
 
 
38
 
39
+ gc = gspread.service_account(filename='credentials.json')
40
+ sh = gc.open('Annotated CC Audio')
41
+ worksheet = sh.sheet1
42
+ df = pd.DataFrame(worksheet.get_all_records())
43
+ sample_df = df[df['caption']==''].sample(1)
44
 
45
  title = '🎵 Annotate audio'
46
  description = '''Choose a sentence that describes audio the best if there's no such sentence please choose `No audio description`'''
47
 
48
+ audio_dir = 'AUDIO'
49
+ os.makedirs(audio_dir, exist_ok=True)
50
+
51
+ audio_id, audio_url, full_text, _ = sample_df.values[0]
52
+ audio_path = download_and_save_file(audio_url, audio_dir)
53
  full_text = full_text.translate(str.maketrans('', '', string.punctuation))
54
+ sents = ['\n'.join(wrap(re.sub(r'###audio###\d###', '', s.text), width=70) )for s in nlp(full_text).sents]
55
  sents.append('No audio description')
56
 
57
+ def audio_demo(cap, audio, audio_id):
58
 
59
+ df.at[int(audio_id)-1, 'caption'] = cap
60
+ worksheet.update([df.columns.values.tolist()] + df.values.tolist())
 
 
 
 
61
  return 'success!'
62
 
63
 
64
  iface = gr.Interface(
65
  audio_demo,
66
+ inputs=[gr.Dropdown(sents, label='audio description'), gr.Audio(audio_path, type="filepath"), gr.Textbox(value=audio_id, visible=False)],
67
  outputs=[gr.Textbox(label="output")],
68
  allow_flagging="never",
69
  title=title,