soiz commited on
Commit
f586543
·
verified ·
1 Parent(s): 3eb82c2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -21
app.py CHANGED
@@ -1,30 +1,29 @@
 
1
  import torch
2
  import torchaudio
3
- import gradio as gr
 
4
 
5
- def process_audio_file(audio):
6
- # 音声ファイルの読み込み
7
- waveform, sample_rate = torchaudio.load(audio)
8
 
9
- # Melスペクトログラム特徴量の抽出
10
- transform = torchaudio.transforms.MelSpectrogram(sample_rate=sample_rate)
11
- mel_spec = transform(waveform)
12
 
13
- # 特徴量を .pth ファイルに保存
14
- pth_file_path = "audio_features.pth"
15
- torch.save(mel_spec, pth_file_path)
16
 
17
- # pthファイルのパスを返す
18
- return pth_file_path
19
 
20
- # Gradioインターフェースの作成
21
- interface = gr.Interface(
22
- fn=process_audio_file,
23
- inputs=gr.Audio(type="filepath"), # source="upload" を削除
24
- outputs=gr.File(label="Download .pth File"),
25
- title="Audio to .pth Converter",
26
- description="Upload an audio file to convert it into a .pth file containing Mel Spectrogram features."
27
  )
28
 
29
- # アプリの実行
30
- interface.launch()
 
1
+ import gradio as gr
2
  import torch
3
  import torchaudio
4
+ import librosa
5
+ import numpy as np
6
 
7
+ def audio_to_pth(audio):
8
+ # 音声ファイル(ファイルパス)を読み込む
9
+ y, sr = librosa.load(audio, sr=None)
10
 
11
+ # 音声データをテンソルに変換
12
+ tensor = torch.tensor(y)
 
13
 
14
+ # テンソルを .pth ファイルに保存
15
+ output_path = "audio_features.pth"
16
+ torch.save(tensor, output_path)
17
 
18
+ return output_path
 
19
 
20
+ # Gradio インターフェースの設定
21
+ iface = gr.Interface(
22
+ fn=audio_to_pth,
23
+ inputs=gr.Audio(source="upload", type="filepath"),
24
+ outputs="file",
25
+ title="Audio to .PTH Converter",
26
+ description="Upload an audio file to convert it to a .pth file containing audio features."
27
  )
28
 
29
+ iface.launch()