DeepAFx-ST / app.py
harp-dev's picture
Drop the comment on ssr_mode
c6b4dda verified
Raw History Blame Contribute Delete
5.21 kB
from __future__ import annotations
import spaces # imported first, as ZeroGPU requires
import json
import uuid
from pathlib import Path
from tempfile import gettempdir
import gradio as gr
import soundfile as sf
import torchaudio
from pyharp import ModelCard, build_endpoint
from pyharp.tags import Channels, SampleRate, Subcategory
from deepafx_st_runtime import load_model, style_transfer
MIN_AUDIO_SECONDS = 1
MAX_AUDIO_SECONDS = 30
OUTPUT_ROOT = Path(gettempdir()) / "deepafx_st_outputs"
# A lossless input keeps its container. A lossy one comes back as WAV, since encoding the result
# lossily again would expose the artifacts of the first encode and add a second set.
LOSSLESS = {".wav", ".flac"}
model_card = ModelCard(
name="DeepAFx-ST",
description=(
"Transfer the production style (EQ, compression) of a reference "
"recording onto your audio. The model is fixed-rate and mono, so the "
"result is resampled back to the input's rate and copied across its "
"channels: the output has the format it came in with, but no stereo "
"image. Repository: "
"https://github.com/adobe-research/DeepAFx-ST Paper: "
"https://arxiv.org/abs/2207.08759 Released under the Adobe Research "
"License, which permits noncommercial research use only."
),
author="Christian J. Steinmetz, Nicholas J. Bryan, Joshua D. Reiss",
tags=[Subcategory.MIXING_STYLE_TRANSFER, SampleRate(24000), Channels(1), 'style-transfer', 'domain:music', 'domain:speech'],
)
def _validate_audio(path: str | None, label: str) -> str:
if not path:
raise gr.Error(f"Please upload {label}.")
try:
duration = sf.info(path).duration
except Exception as exc:
raise gr.Error(f"Could not read {label}: {exc}") from exc
if duration < MIN_AUDIO_SECONDS:
raise gr.Error(
f"{label} must be at least {MIN_AUDIO_SECONDS} second long."
)
if duration > MAX_AUDIO_SECONDS:
raise gr.Error(
f"{label} must be no longer than {MAX_AUDIO_SECONDS} seconds. "
f"Received {duration:.1f} seconds."
)
return path
def process_fn(
input_path: str | None,
reference_path: str | None,
) -> tuple[str, str]:
# Checked before the GPU is reserved, so a request the model cannot take spends none of the
# caller's ZeroGPU allowance
input_path = _validate_audio(input_path, "input audio")
reference_path = _validate_audio(reference_path, "reference audio")
return transfer(input_path, reference_path)
@spaces.GPU(duration=120)
def transfer(input_path: str, reference_path: str) -> tuple[str, str]:
try:
audio, sample_rate, params = style_transfer(input_path, reference_path)
except Exception as exc:
raise gr.Error(f"DeepAFx-ST inference failed: {exc}") from exc
output_dir = OUTPUT_ROOT / uuid.uuid4().hex
output_dir.mkdir(parents=True, exist_ok=True)
# The model is mono at 24 kHz, so the result is resampled back to the input's rate and copied
# across its channels
input_info = sf.info(input_path)
if input_info.samplerate != sample_rate:
audio = torchaudio.functional.resample(audio, sample_rate, input_info.samplerate)
audio = audio.unsqueeze(-1).repeat(1, input_info.channels)
input_ext = Path(input_path).suffix.lower()
audio_path = output_dir / f"styled_output{input_ext if input_ext in LOSSLESS else '.wav'}"
sf.write(str(audio_path), audio.numpy(), input_info.samplerate)
params_path = output_dir / "dsp_parameters.json"
params_path.write_text(
json.dumps(
{"model": "DeepAFx-ST", "variant": "autodiff", **params},
indent=2,
)
+ "\n",
encoding="utf-8",
)
return str(audio_path), str(params_path)
# pyharp runs process_fn in a worker, which imports this file as __mp_main__ and starts with the
# server, so the model is loaded there, before the first request rather than during it.
if __name__ != "__main__":
load_model()
if __name__ == "__main__":
with gr.Blocks(title="DeepAFx-ST") as demo:
input_components = [
gr.Audio(type="filepath", label="Input Audio")
.harp_required(True)
.set_info("Audio to process, 1 to 30 seconds."),
gr.Audio(type="filepath", label="Reference Audio")
.harp_required(True)
.set_info("Target style — your input will be EQ'd and compressed to match this recording."),
]
output_components = [
gr.Audio(type="filepath", label="Styled Output")
.set_info("Processed audio with the reference's production style."),
gr.File(
type="filepath",
file_types=[".json"],
label="DSP Parameters",
).set_info("Predicted EQ and compressor parameters."),
]
build_endpoint(
model_card=model_card,
input_components=input_components,
output_components=output_components,
process_fn=process_fn,
)
demo.queue(default_concurrency_limit=1).launch(show_error=True, pwa=True, ssr_mode=False)