fosters commited on
Commit
758fe09
·
verified ·
1 Parent(s): b3e655c

Upload app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +8 -3
app.py CHANGED
@@ -1,10 +1,12 @@
 
1
  import os
2
  import gradio as gr
3
  import numpy as np
4
  import pandas as pd
 
5
  import torch
6
  import torchaudio
7
- from datasets import load_dataset
8
  from sklearn.cluster import AgglomerativeClustering
9
  from transformers import Wav2Vec2FeatureExtractor, WavLMForXVector
10
 
@@ -63,12 +65,15 @@ def identify_speakers(
63
  progress((i + 0.5) / len(repos), desc=f"[{i+1}/{len(repos)}] {short}")
64
  try:
65
  ds = load_dataset(repo, split="train", streaming=True, token=token)
 
66
  embs = []
67
  for j, row in enumerate(ds):
68
  if j >= int(samples_per_book):
69
  break
70
- audio = row["audio"]
71
- embs.append(_embed(np.array(audio["array"]), audio["sampling_rate"]))
 
 
72
  if embs:
73
  embeddings[repo] = np.mean(embs, axis=0)
74
  else:
 
1
+ import io
2
  import os
3
  import gradio as gr
4
  import numpy as np
5
  import pandas as pd
6
+ import soundfile as sf
7
  import torch
8
  import torchaudio
9
+ from datasets import load_dataset, Audio
10
  from sklearn.cluster import AgglomerativeClustering
11
  from transformers import Wav2Vec2FeatureExtractor, WavLMForXVector
12
 
 
65
  progress((i + 0.5) / len(repos), desc=f"[{i+1}/{len(repos)}] {short}")
66
  try:
67
  ds = load_dataset(repo, split="train", streaming=True, token=token)
68
+ ds = ds.cast_column("audio", Audio(decode=False))
69
  embs = []
70
  for j, row in enumerate(ds):
71
  if j >= int(samples_per_book):
72
  break
73
+ raw = row["audio"]
74
+ audio_bytes = raw.get("bytes") or open(raw["path"], "rb").read()
75
+ audio_array, sr = sf.read(io.BytesIO(audio_bytes))
76
+ embs.append(_embed(audio_array, sr))
77
  if embs:
78
  embeddings[repo] = np.mean(embs, axis=0)
79
  else: