GPTfree api
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -1,60 +1,82 @@
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
-
|
3 |
-
|
4 |
-
#
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
)
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
58 |
)
|
|
|
|
|
59 |
|
60 |
-
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import torch
|
3 |
+
import requests
|
4 |
import gradio as gr
|
5 |
+
import torchaudio
|
6 |
+
|
7 |
+
# モデルのダウンロード関数
|
8 |
+
def download_model(model_url, output_path):
|
9 |
+
if not os.path.exists(output_path):
|
10 |
+
print(f"Downloading model from {model_url} to {output_path}")
|
11 |
+
response = requests.get(model_url, stream=True)
|
12 |
+
if response.status_code == 200:
|
13 |
+
with open(output_path, 'wb') as f:
|
14 |
+
f.write(response.content)
|
15 |
+
print("Model downloaded successfully.")
|
16 |
+
else:
|
17 |
+
raise ValueError(f"Failed to download model: {response.status_code}")
|
18 |
+
else:
|
19 |
+
print(f"Model already exists at {output_path}")
|
20 |
+
|
21 |
+
# モデルロード用の関数
|
22 |
+
def load_model(model_path):
|
23 |
+
if not os.path.exists(model_path):
|
24 |
+
raise FileNotFoundError(f"Model file not found: {model_path}")
|
25 |
+
print(f"Loading model from {model_path}")
|
26 |
+
try:
|
27 |
+
model = torch.load(model_path, map_location=torch.device('cpu'))
|
28 |
+
model.eval()
|
29 |
+
return model
|
30 |
+
except Exception as e:
|
31 |
+
raise ValueError(f"Failed to load model. Please ensure it is a valid PyTorch model file: {e}")
|
32 |
+
|
33 |
+
# 音声処理関数
|
34 |
+
def process_audio(audio_filepath, model_path):
|
35 |
+
try:
|
36 |
+
# モデルをロード
|
37 |
+
model = load_model(model_path)
|
38 |
+
|
39 |
+
# 入力音声のテンソル化
|
40 |
+
waveform, sample_rate = torchaudio.load(audio_filepath)
|
41 |
+
print(f"Loaded audio with shape {waveform.shape} and sample rate {sample_rate}")
|
42 |
+
|
43 |
+
# モデルに音声を入力し処理
|
44 |
+
with torch.no_grad():
|
45 |
+
processed_waveform = model(waveform)
|
46 |
+
|
47 |
+
# 処理結果の確認
|
48 |
+
if processed_waveform is None or processed_waveform.shape[1] == 0:
|
49 |
+
raise ValueError("Model returned empty waveform")
|
50 |
+
|
51 |
+
# 出力を保存
|
52 |
+
output_path = "processed_audio.wav"
|
53 |
+
torchaudio.save(output_path, processed_waveform, sample_rate)
|
54 |
+
print(f"Processed audio saved to {output_path}")
|
55 |
+
|
56 |
+
return output_path
|
57 |
+
except Exception as e:
|
58 |
+
print(f"Error: {str(e)}")
|
59 |
+
return f"Error: {str(e)}"
|
60 |
+
|
61 |
+
# Gradioインターフェース
|
62 |
+
def create_interface():
|
63 |
+
model_url = "https://huggingface.co/spaces/adhisetiawan/anime-voice-generator/raw/main/pretrained_models/alice/alice.pth"
|
64 |
+
model_path = "alice.pth" # ローカルに保存するモデルファイル名
|
65 |
+
|
66 |
+
# モデルをダウンロード
|
67 |
+
download_model(model_url, model_path)
|
68 |
+
|
69 |
+
# Gradioインターフェース
|
70 |
+
interface = gr.Interface(
|
71 |
+
fn=lambda audio_filepath: process_audio(audio_filepath, model_path),
|
72 |
+
inputs=gr.Audio(type="filepath", label="Source Audio"), # 修正ポイント: type="filepath"
|
73 |
+
outputs=gr.Audio(type="filepath", label="Processed Audio"), # 修正ポイント: type="filepath"
|
74 |
+
title="Anime Voice Filter",
|
75 |
+
description="指定されたモデルを使用して音声にフィルターをかけます。"
|
76 |
)
|
77 |
+
|
78 |
+
return interface
|
79 |
|
80 |
+
if __name__ == "__main__":
|
81 |
+
interface = create_interface()
|
82 |
+
interface.launch()
|