File size: 2,548 Bytes
f725827
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17eef9f
f725827
 
17eef9f
f725827
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
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
61
62
63
64
from __future__ import annotations

import os

import gradio as gr

from pyharp import *
from gradio_client import Client, handle_file


_BACKEND_SPACE = "amaai-lab/SonicMaster"
_BACKEND_API_NAME = "/enhance_audio_ui"
_BACKEND_TOKEN_ENV = "HF_TOKEN"
_client = None


def _backend_client():
    # Lazily create and cache one warm connection to the backend Space.
    global _client
    if _client is None:
        _token = os.environ.get(_BACKEND_TOKEN_ENV) or None
        _client = Client(_BACKEND_SPACE, hf_token=_token)
    return _client


model_card = ModelCard(
    name="SonicMaster",
    description="Text-controlled all-in-one music restoration and mastering (fix reverb, clipping, EQ/tonal balance, dynamics, and stereo image) from a natural-language instruction. This is a thin HARP frontend that proxies to the authors' official SonicMaster Gradio Space via gradio_client, so none of its heavy/gated dependencies (torch, diffusers, the gated stable-audio VAE) are installed here.",
    author="AMAAI-Lab",
    tags=["audio-to-audio", "music-restoration", "mastering", "text-guided"],
)


def process_fn(input_audio, prompt):
    _raw = _backend_client().predict(
        handle_file(input_audio),
        prompt,
        api_name="/enhance_audio_ui",
    )
    _values = list(_raw) if isinstance(_raw, (list, tuple)) else [_raw]
    _detail = " | ".join(str(_v) for _v in _values if isinstance(_v, str) and _v.strip())
    _out_output_audio = _values[0] if len(_values) > 0 else None
    if not _out_output_audio:
        raise gr.Error(_detail or "The backend Space returned no 'output_audio' output. Check the backend Space's logs; if it uses ZeroGPU it may need a moment to warm up.")
    return _out_output_audio


with gr.Blocks() as demo:
    input_components = [
        gr.Audio(type="filepath", label="Input audio").harp_required(True).set_info("The music/audio to restore or master."),
        gr.Textbox(label="Instruction", value="Enhance the input audio", info="Natural-language edit, e.g. 'reduce reverb and brighten the vocals', 'make it louder', 'dereverb', 'widen the stereo image'. Leave as-is for general restoration."),
    ]
    output_components = [
        gr.Audio(type="filepath", label="Enhanced audio").set_info("SonicMaster output returned by the backend Space."),
    ]
    build_endpoint(
        model_card=model_card,
        input_components=input_components,
        output_components=output_components,
        process_fn=process_fn,
    )

demo.queue().launch(share=True, show_error=False, pwa=True)