atsushieee commited on
Commit
cbdd616
·
1 Parent(s): 8bec389

Update hubert/inference.py

Browse files
Files changed (1) hide show
  1. hubert/inference.py +35 -2
hubert/inference.py CHANGED
@@ -4,6 +4,8 @@ import numpy as np
4
  import argparse
5
  import torch
6
  import librosa
 
 
7
 
8
  from hubert import hubert_model
9
 
@@ -22,6 +24,37 @@ def load_model(path, device):
22
  return model
23
 
24
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25
  def pred_vec(model, wavPath, vecPath, device):
26
  audio = load_audio(wavPath)
27
  audln = audio.shape[0]
@@ -62,6 +95,6 @@ if __name__ == "__main__":
62
  vecPath = args.vec
63
 
64
  device = "cuda" if torch.cuda.is_available() else "cpu"
65
- hubert = load_model(os.path.join(
66
- "hubert_pretrain", "hubert-soft-0d54a1f4.pt"), device)
67
  pred_vec(hubert, wavPath, vecPath, device)
 
4
  import argparse
5
  import torch
6
  import librosa
7
+ import requests
8
+ from tqdm import tqdm
9
 
10
  from hubert import hubert_model
11
 
 
24
  return model
25
 
26
 
27
+ def check_and_download_model():
28
+ temp_dir = "/tmp"
29
+ model_path = os.path.join(temp_dir, "hubert-soft-0d54a1f4.pt")
30
+
31
+ if os.path.exists(model_path):
32
+ return f"モデルは既に存在します: {model_path}"
33
+
34
+ url = "https://github.com/bshall/hubert/releases/download/v0.1/hubert-soft-0d54a1f4.pt"
35
+
36
+ try:
37
+ response = requests.get(url, stream=True)
38
+ response.raise_for_status()
39
+ total_size = int(response.headers.get('content-length', 0))
40
+
41
+ with open(model_path, 'wb') as f, tqdm(
42
+ desc=model_path,
43
+ total=total_size,
44
+ unit='iB',
45
+ unit_scale=True,
46
+ unit_divisor=1024,
47
+ ) as pbar:
48
+ for data in response.iter_content(chunk_size=1024):
49
+ size = f.write(data)
50
+ pbar.update(size)
51
+
52
+ return f"モデルのダウンロードが完了しました: {model_path}"
53
+
54
+ except Exception as e:
55
+ return f"エラーが発生しました: {e}"
56
+
57
+
58
  def pred_vec(model, wavPath, vecPath, device):
59
  audio = load_audio(wavPath)
60
  audln = audio.shape[0]
 
95
  vecPath = args.vec
96
 
97
  device = "cuda" if torch.cuda.is_available() else "cpu"
98
+ _ = check_and_download_model()
99
+ hubert = load_model("/tmp/hubert-soft-0d54a1f4.pt", device)
100
  pred_vec(hubert, wavPath, vecPath, device)