Spaces:
Running on Zero
Running on Zero
Size the GPU reservation by the recording's length and check requests before the GPU is reserved.
Browse files
app.py
CHANGED
|
@@ -1,6 +1,7 @@
|
|
| 1 |
import os
|
| 2 |
import tempfile
|
| 3 |
|
|
|
|
| 4 |
import spaces
|
| 5 |
import torch
|
| 6 |
import torchaudio
|
|
@@ -72,12 +73,36 @@ model = model.to(device)
|
|
| 72 |
model.eval()
|
| 73 |
print("✓ Model loaded successfully")
|
| 74 |
|
| 75 |
-
#
|
| 76 |
-
|
| 77 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
def process_fn(
|
| 79 |
input_audio_path: str
|
| 80 |
) -> str:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
|
| 82 |
sig = load_audio(input_audio_path)
|
| 83 |
|
|
@@ -102,7 +127,7 @@ if __name__ == "__main__":
|
|
| 102 |
# Define input Gradio Components
|
| 103 |
input_components = [
|
| 104 |
gr.Audio(type="filepath",
|
| 105 |
-
label="Input Audio
|
| 106 |
.harp_required(True).set_info((
|
| 107 |
"A lossy recording to restore, such as a low-bitrate MP3. Feeding "
|
| 108 |
"it audio that was never compressed gives it nothing to repair."
|
|
|
|
| 1 |
import os
|
| 2 |
import tempfile
|
| 3 |
|
| 4 |
+
import soundfile as sf
|
| 5 |
import spaces
|
| 6 |
import torch
|
| 7 |
import torchaudio
|
|
|
|
| 73 |
model.eval()
|
| 74 |
print("✓ Model loaded successfully")
|
| 75 |
|
| 76 |
+
# The whole recording is restored in one pass, so the cost grows with its length
|
| 77 |
+
MAX_INPUT_SECONDS = 420
|
| 78 |
+
|
| 79 |
+
|
| 80 |
+
def gpu_seconds(input_audio_path: str) -> float:
|
| 81 |
+
return 10 + 0.4 * sf.info(input_audio_path).duration
|
| 82 |
+
|
| 83 |
+
|
| 84 |
def process_fn(
|
| 85 |
input_audio_path: str
|
| 86 |
) -> str:
|
| 87 |
+
# Checked before the GPU is reserved, so a request the model cannot take spends none of the
|
| 88 |
+
# caller's ZeroGPU allowance
|
| 89 |
+
if not input_audio_path:
|
| 90 |
+
raise gr.Error("Provide the recording to restore.")
|
| 91 |
+
try:
|
| 92 |
+
duration = sf.info(input_audio_path).duration
|
| 93 |
+
except Exception as exc:
|
| 94 |
+
raise gr.Error(f"Could not read the audio file: {exc}") from exc
|
| 95 |
+
if duration > MAX_INPUT_SECONDS:
|
| 96 |
+
raise gr.Error(
|
| 97 |
+
f"The recording is {duration:.1f} s long. Trim it to {MAX_INPUT_SECONDS // 60} "
|
| 98 |
+
"minutes or less."
|
| 99 |
+
)
|
| 100 |
+
return restore(input_audio_path)
|
| 101 |
+
|
| 102 |
+
|
| 103 |
+
@spaces.GPU(duration=gpu_seconds)
|
| 104 |
+
@torch.inference_mode()
|
| 105 |
+
def restore(input_audio_path: str) -> str:
|
| 106 |
|
| 107 |
sig = load_audio(input_audio_path)
|
| 108 |
|
|
|
|
| 127 |
# Define input Gradio Components
|
| 128 |
input_components = [
|
| 129 |
gr.Audio(type="filepath",
|
| 130 |
+
label="Input Audio")
|
| 131 |
.harp_required(True).set_info((
|
| 132 |
"A lossy recording to restore, such as a low-bitrate MP3. Feeding "
|
| 133 |
"it audio that was never compressed gives it nothing to repair."
|