asahi417 commited on
Commit
596ecab
·
1 Parent(s): 53f94ee
Files changed (1) hide show
  1. download_audio.py +16 -7
download_audio.py CHANGED
@@ -91,6 +91,7 @@ def to_json_serializable(val):
91
 
92
  def get_audio(dataframe: pd.DataFrame):
93
  features = {"line_no": int(dataframe.pop('line_no').values[0])}
 
94
  for side, df in dataframe.groupby("side"):
95
  df.pop("side")
96
  features.update({f"{side}.{k}": to_json_serializable(v) for k, v in df.iloc[0].to_dict().items()})
@@ -101,6 +102,8 @@ def get_audio(dataframe: pd.DataFrame):
101
  flag = wget(features[f"{side}.url"], output_file=features[f"{side}.path"])
102
  if not flag:
103
  # create a dummy file to avoid the url again
 
 
104
  return None
105
  else:
106
  try:
@@ -109,12 +112,16 @@ def get_audio(dataframe: pd.DataFrame):
109
  sf.write(features[f"{side}.path"], wav["array"][start:end], wav["sampling_rate"])
110
  else:
111
  os.remove(features[f"{side}.path"])
 
 
112
  return None
113
  except Exception as e:
114
  print(e)
115
  os.remove(features[f"{side}.path"])
 
 
116
  return None
117
- with open(p_join(cache_dir_feature, f'{features["line_no"]}.json'), "w") as f:
118
  json.dump(features, f)
119
  return features["line_no"]
120
 
@@ -122,12 +129,14 @@ def get_audio(dataframe: pd.DataFrame):
122
  if __name__ == '__main__':
123
  df_metadata = get_metadata()
124
  print(f"metadata: {len(df_metadata)}, {line_no_start} --> {line_no_end}")
125
- inputs = [
126
- g for line_no, g in df_metadata.groupby("line_no")
127
- if line_no_start <= line_no < line_no_end and not os.path.exists(
128
- p_join(cache_dir_feature, f'{int(line_no)}.json')
129
- )
130
- ]
 
 
131
  print(f"filtered unique lines: {len(inputs)}")
132
  if direction == "enA-jaA":
133
  inputs = [g for g in inputs if len(g["side"].unique()) == 2 and set(g["side"].unique()) == sides]
 
91
 
92
  def get_audio(dataframe: pd.DataFrame):
93
  features = {"line_no": int(dataframe.pop('line_no').values[0])}
94
+ feature_file = p_join(cache_dir_feature, f'{features["line_no"]}.json')
95
  for side, df in dataframe.groupby("side"):
96
  df.pop("side")
97
  features.update({f"{side}.{k}": to_json_serializable(v) for k, v in df.iloc[0].to_dict().items()})
 
102
  flag = wget(features[f"{side}.url"], output_file=features[f"{side}.path"])
103
  if not flag:
104
  # create a dummy file to avoid the url again
105
+ if os.path.exists(feature_file):
106
+ os.remove(feature_file)
107
  return None
108
  else:
109
  try:
 
112
  sf.write(features[f"{side}.path"], wav["array"][start:end], wav["sampling_rate"])
113
  else:
114
  os.remove(features[f"{side}.path"])
115
+ if os.path.exists(feature_file):
116
+ os.remove(feature_file)
117
  return None
118
  except Exception as e:
119
  print(e)
120
  os.remove(features[f"{side}.path"])
121
+ if os.path.exists(feature_file):
122
+ os.remove(feature_file)
123
  return None
124
+ with open(feature_file, "w") as f:
125
  json.dump(features, f)
126
  return features["line_no"]
127
 
 
129
  if __name__ == '__main__':
130
  df_metadata = get_metadata()
131
  print(f"metadata: {len(df_metadata)}, {line_no_start} --> {line_no_end}")
132
+ # inputs = [
133
+ # g for line_no, g in df_metadata.groupby("line_no")
134
+ # if line_no_start <= line_no < line_no_end and not os.path.exists(
135
+ # p_join(cache_dir_feature, f'{int(line_no)}.json')
136
+ # )
137
+ # ]
138
+ inputs = [g for line_no, g in df_metadata.groupby("line_no") if line_no_start <= line_no < line_no_end]
139
+
140
  print(f"filtered unique lines: {len(inputs)}")
141
  if direction == "enA-jaA":
142
  inputs = [g for g in inputs if len(g["side"].unique()) == 2 and set(g["side"].unique()) == sides]