jayliqinzhang commited on
Commit
6663a58
·
verified ·
1 Parent(s): 50a4920

Upload mumospee_small.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. mumospee_small.py +53 -49
mumospee_small.py CHANGED
@@ -14,8 +14,8 @@ This is the small version include no more 1000 rows.
14
 
15
  _LICENSE = "Creative Commons Attribution 4.0 International"
16
 
17
- # Defining categories or tags for filtering
18
- _LANGUAGES = ["en", "bg", "de", "es", "it", "ar"] # Example languages
19
  _TAGS = ["CoVoST", "GigaSpeech", "peoples_speech", "Librispeech", "LibriTTS", "Emilia", "MOSEL"]
20
 
21
  _SPLITS = ["train", "validation", "test"]
@@ -25,12 +25,27 @@ class MumospeeDatasetConfig(datasets.BuilderConfig):
25
  def __init__(self, split, language=None, tag=None, **kwargs):
26
  super().__init__(**kwargs)
27
  self.split=split
28
- self.language = language
29
- self.tag = tag
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30
 
31
 
32
  class MumospeeDataset(datasets.GeneratorBasedBuilder):
33
- """Your custom dataset for Hugging Face based on the CSV metadata and audio URLs."""
34
 
35
  VERSION = datasets.Version("1.0.0")
36
 
@@ -75,6 +90,24 @@ class MumospeeDataset(datasets.GeneratorBasedBuilder):
75
 
76
  csv_path = dl_manager.download_and_extract("dataset.csv")
77
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
78
  # Define the splits and pass the language and tag filters to _generate_examples
79
  return [
80
  datasets.SplitGenerator(
@@ -93,37 +126,36 @@ class MumospeeDataset(datasets.GeneratorBasedBuilder):
93
 
94
 
95
  def _generate_examples(self, filepath):
96
- """Generate examples from the CSV data and audio URLs."""
97
- # Use dl_manager to get the CSV file from the Hugging Face repo
98
- # csv_file = dl_manager.download_and_extract("metadata.csv")
99
- # Read the CSV metadata
100
-
101
  data = pd.read_csv(filepath)
102
- # Filter by primary split
103
  split=self.config.split
104
 
105
 
106
  language=self.config.language
107
  tag=self.config.tag
108
 
109
- print(f"filepath is {filepath}, split is {split}, lang is {self.config.language}, tag is {self.config.tag}")
110
-
111
 
112
  data_split = data[data["split"] == split]
 
 
113
  if language:
114
- data_split = data_split[data_split["language"] == language]
115
  if tag:
116
- data_split = data_split[data_split["tag"] == tag]
117
-
118
- for i, row in data_split.iterrows():
119
 
120
- # Construct the full audio path or URL
121
- audio_url = row["url"]
 
 
122
 
123
- # Prepare the data entry for this example
 
124
  yield i, {
125
  "path": row["path"],
126
- "audio": audio_url,
 
 
127
  "duration": float(row["duration"]),
128
  "language": row["language"],
129
  "transcript": row["transcript"],
@@ -134,34 +166,6 @@ class MumospeeDataset(datasets.GeneratorBasedBuilder):
134
 
135
 
136
 
137
- # with open(csv_file, mode='r', encoding='utf-8') as f:
138
- # reader = csv.DictReader(f)
139
- # for row in reader:
140
- # # Filter by tag or language here if necessary
141
- # if row["language"] not in _LANGUAGES:
142
- # continue # Skip entries with unsupported languages
143
- # if row["tag"] not in _TAGS:
144
- # continue # Skip entries with unsupported tags
145
-
146
-
147
-
148
-
149
- # # Construct the full audio path or URL
150
- # audio_url = row["url"]
151
-
152
- # # Prepare the data entry for this example
153
- # yield row["path"], {
154
- # "audio": audio_url,
155
- # "duration": float(row["duration"]),
156
- # "language": row["language"],
157
- # "transcript": row["transcript"],
158
- # "tag": row["tag"],
159
- # "split": row["split"],
160
- # "license": row["license"]
161
- # }
162
-
163
-
164
-
165
  def _download_audio(self, audio_url):
166
  """Download audio from a URL if needed (you could also implement streaming)."""
167
  # This is an example function for downloading audio if it's needed
 
14
 
15
  _LICENSE = "Creative Commons Attribution 4.0 International"
16
 
17
+ _LANGUAGES = ["en", "bg", "de", "ar"]
18
+
19
  _TAGS = ["CoVoST", "GigaSpeech", "peoples_speech", "Librispeech", "LibriTTS", "Emilia", "MOSEL"]
20
 
21
  _SPLITS = ["train", "validation", "test"]
 
25
  def __init__(self, split, language=None, tag=None, **kwargs):
26
  super().__init__(**kwargs)
27
  self.split=split
28
+
29
+ # Convert string inputs to single-item lists, or use as-is if already a list
30
+ self.language = [language] if isinstance(language, str) else language or []
31
+ self.tag = [tag] if isinstance(tag, str) else tag or []
32
+
33
+ # Validate each provided language
34
+ for lang in self.language:
35
+ if lang not in self._LANGUAGES:
36
+ raise ValueError(
37
+ f"Invalid language: '{lang}'. Accepted languages are: {', '.join(self.ACCEPTED_LANGUAGES)}."
38
+ )
39
+
40
+ # Validate each provided tag
41
+ for t in self.tag:
42
+ if t not in self._TAGS:
43
+ raise ValueError(
44
+ f"Invalid dataset origin: '{t}'. Accepted tags are: {', '.join(self.ACCEPTED_TAGS)}."
45
+ )
46
 
47
 
48
  class MumospeeDataset(datasets.GeneratorBasedBuilder):
 
49
 
50
  VERSION = datasets.Version("1.0.0")
51
 
 
90
 
91
  csv_path = dl_manager.download_and_extract("dataset.csv")
92
 
93
+
94
+ # ===To download the url
95
+ # Load CSV to retrieve URLs for audio files
96
+ # data = pd.read_csv(csv_path)
97
+ # url_list = data["url"].tolist() # List of all URLs in the CSV file
98
+ # url_list = list(set(url_list))
99
+
100
+ # # Download all files listed in the 'url' column and store the local paths
101
+ # downloaded_files = dl_manager.download(url_list)
102
+
103
+ # # Add the downloaded file paths to the DataFrame to make them accessible in `_generate_examples`
104
+ # data["local_path"] = downloaded_files
105
+
106
+ #===
107
+
108
+
109
+
110
+
111
  # Define the splits and pass the language and tag filters to _generate_examples
112
  return [
113
  datasets.SplitGenerator(
 
126
 
127
 
128
  def _generate_examples(self, filepath):
129
+
 
 
 
 
130
  data = pd.read_csv(filepath)
 
131
  split=self.config.split
132
 
133
 
134
  language=self.config.language
135
  tag=self.config.tag
136
 
137
+ print(f'Return {split} dataset in langauge of {self.config.language}, originally from {self.config.tag}.')
 
138
 
139
  data_split = data[data["split"] == split]
140
+
141
+
142
  if language:
143
+ data_split = data_split[data_split["language"].isin(language)]
144
  if tag:
145
+ data_split = data_split[data_split["tag"].isin(tag)]
 
 
146
 
147
+
148
+ if data_split.empty:
149
+ print(f"No data found for split='{split}', language='{language}', tag='{tag}'. Returning None.")
150
+ return # This exits the generator without yielding any examples
151
 
152
+
153
+ for i, row in data_split.iterrows():
154
  yield i, {
155
  "path": row["path"],
156
+ #"local_path": row["local_path"],
157
+ "url": row["url"],
158
+ "type": row["type"],
159
  "duration": float(row["duration"]),
160
  "language": row["language"],
161
  "transcript": row["transcript"],
 
166
 
167
 
168
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
169
  def _download_audio(self, audio_url):
170
  """Download audio from a URL if needed (you could also implement streaming)."""
171
  # This is an example function for downloading audio if it's needed