Vansh Chugh commited on
Commit
67ce405
·
1 Parent(s): 3d70032

restoring pyharp version

Browse files
Files changed (2) hide show
  1. app.py +49 -47
  2. requirements.txt +1 -0
app.py CHANGED
@@ -1,6 +1,8 @@
1
  import sys
2
  sys.stdout.reconfigure(line_buffering=True)
3
 
 
 
4
  import torch
5
  import os
6
  import threading
@@ -10,6 +12,7 @@ import soundfile as sf
10
  import gradio as gr
11
  import time
12
  from huggingface_hub import snapshot_download
 
13
 
14
  from anyaccomp.inference_utils import Sing2SongInferencePipeline
15
 
@@ -63,12 +66,21 @@ def load_model():
63
  model_loading = False
64
 
65
 
 
 
 
 
 
 
 
 
66
  # Start model loading in the background so the server can start immediately.
67
  threading.Thread(target=load_model, daemon=True).start()
68
 
69
 
70
  # @spaces.GPU # uncomment when running on ZeroGPU
71
- def sing2song_inference(vocal_filepath, n_timesteps, cfg_scale):
 
72
  if model_loading:
73
  raise gr.Error("Model is still loading, please wait a moment and try again.")
74
  if inference_pipeline is None:
@@ -90,8 +102,7 @@ def sing2song_inference(vocal_filepath, n_timesteps, cfg_scale):
90
 
91
  vocal_mel = inference_pipeline.encode_vocal(vocal_tensor)
92
 
93
- # on GPU: torch.amp.autocast("cuda", dtype=torch.bfloat16)
94
- autocast_ctx = torch.amp.autocast("cuda", dtype=torch.bfloat16) if torch.cuda.is_available() else torch.inference_mode()
95
  with autocast_ctx:
96
  mel = inference_pipeline.model.reverse_diffusion(
97
  vocal_mel=vocal_mel,
@@ -116,56 +127,47 @@ def sing2song_inference(vocal_filepath, n_timesteps, cfg_scale):
116
  sf.write(accompaniment_path, wav, 24000)
117
  sf.write(mixture_path, mixture_wav, 24000)
118
 
119
- return accompaniment_path, mixture_path, "Status: Complete!"
120
 
121
  except Exception as e:
122
- import traceback
123
  traceback.print_exc()
124
  raise gr.Error(f"An error occurred during processing: {e}")
125
 
126
 
127
-
128
- with gr.Blocks(theme=gr.themes.Soft()) as demo:
129
- gr.Markdown(
130
- """
131
- # AnyAccomp: GENERALIZABLE ACCOMPANIMENT GENERATION
132
- Upload a 3-30 second vocal or instrument track (.wav or .mp3) and the model will generate an accompaniment for it.
133
- """
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
134
  )
135
 
136
- with gr.Row():
137
- with gr.Column(scale=1):
138
- gr.Markdown("### 1. Upload Audio")
139
- vocal_input = gr.Audio(
140
- type="filepath",
141
- label="Upload Vocal or Instrument Audio",
142
- sources=["upload", "microphone"],
143
- )
144
-
145
- gr.Markdown("### 2. Adjust Parameters (Optional)")
146
- with gr.Accordion("Advanced Settings", open=True):
147
- n_timesteps_slider = gr.Slider(
148
- minimum=10, maximum=100, value=50, step=1,
149
- label="Inference Steps (n_timesteps)",
150
- )
151
- cfg_slider = gr.Slider(
152
- minimum=1.0, maximum=10.0, value=3.0, step=0.1,
153
- label="CFG Scale",
154
- )
155
-
156
- with gr.Column(scale=1):
157
- gr.Markdown("### 3. Generate and Listen")
158
- status_text = gr.Markdown("Status: Ready")
159
- accompaniment_output = gr.Audio(label="Generated Accompaniment", type="filepath")
160
- mixture_output = gr.Audio(label="Mixture (Vocal + Accompaniment)", type="filepath")
161
- submit_btn = gr.Button("Generate Accompaniment", variant="primary")
162
-
163
- submit_btn.click(
164
- fn=sing2song_inference,
165
- inputs=[vocal_input, n_timesteps_slider, cfg_slider],
166
- outputs=[accompaniment_output, mixture_output, status_text],
167
- )
168
-
169
-
170
 
171
- demo.launch(server_name="0.0.0.0", ssr_mode=False)
 
 
1
  import sys
2
  sys.stdout.reconfigure(line_buffering=True)
3
 
4
+ import traceback
5
+ import contextlib
6
  import torch
7
  import os
8
  import threading
 
12
  import gradio as gr
13
  import time
14
  from huggingface_hub import snapshot_download
15
+ from pyharp import ModelCard, build_endpoint
16
 
17
  from anyaccomp.inference_utils import Sing2SongInferencePipeline
18
 
 
66
  model_loading = False
67
 
68
 
69
+ # Create a ModelCard describing this endpoint for HARP.
70
+ model_card = ModelCard(
71
+ name="AnyAccomp",
72
+ description="Upload a 3-30 second vocal or instrument track and the model will generate an accompaniment.",
73
+ author="Amphion",
74
+ tags=["audio", "music", "accompaniment"],
75
+ )
76
+
77
  # Start model loading in the background so the server can start immediately.
78
  threading.Thread(target=load_model, daemon=True).start()
79
 
80
 
81
  # @spaces.GPU # uncomment when running on ZeroGPU
82
+ @torch.inference_mode()
83
+ def process_fn(vocal_filepath, n_timesteps, cfg_scale):
84
  if model_loading:
85
  raise gr.Error("Model is still loading, please wait a moment and try again.")
86
  if inference_pipeline is None:
 
102
 
103
  vocal_mel = inference_pipeline.encode_vocal(vocal_tensor)
104
 
105
+ autocast_ctx = torch.amp.autocast("cuda", dtype=torch.bfloat16) if torch.cuda.is_available() else contextlib.nullcontext()
 
106
  with autocast_ctx:
107
  mel = inference_pipeline.model.reverse_diffusion(
108
  vocal_mel=vocal_mel,
 
127
  sf.write(accompaniment_path, wav, 24000)
128
  sf.write(mixture_path, mixture_wav, 24000)
129
 
130
+ return accompaniment_path, mixture_path
131
 
132
  except Exception as e:
 
133
  traceback.print_exc()
134
  raise gr.Error(f"An error occurred during processing: {e}")
135
 
136
 
137
+ with gr.Blocks() as demo:
138
+ input_components = [
139
+ gr.Audio(
140
+ type="filepath",
141
+ label="Input Vocal or Instrument Audio",
142
+ ).harp_required(True),
143
+ gr.Slider(
144
+ minimum=10, maximum=100, value=50, step=1,
145
+ label="Inference Steps (n_timesteps)",
146
+ info="Number of diffusion steps. More steps = slower but higher quality",
147
+ ),
148
+ gr.Slider(
149
+ minimum=1.0, maximum=10.0, value=3.0, step=0.1,
150
+ label="CFG Scale",
151
+ info="Classifier-free guidance scale",
152
+ ),
153
+ ]
154
+
155
+ output_components = [
156
+ gr.Audio(type="filepath", label="Generated Accompaniment").set_info(
157
+ "The generated instrumental accompaniment."
158
+ ),
159
+ gr.Audio(type="filepath", label="Mixture (Vocal + Accompaniment)").set_info(
160
+ "Vocal mixed with the generated accompaniment."
161
+ ),
162
+ ]
163
+
164
+ build_endpoint(
165
+ model_card=model_card,
166
+ input_components=input_components,
167
+ output_components=output_components,
168
+ process_fn=process_fn,
169
  )
170
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
171
 
172
+ import time; time.sleep(60) # TEST: simulate blocking startup
173
+ demo.queue().launch()
requirements.txt CHANGED
@@ -1,4 +1,5 @@
1
  # spaces # uncomment when running on ZeroGPU
 
2
  torch==2.6.0
3
  torchaudio==2.6.0
4
  numpy==1.26.4
 
1
  # spaces # uncomment when running on ZeroGPU
2
+ git+https://github.com/TEAMuP-dev/pyharp.git@v0.3.0
3
  torch==2.6.0
4
  torchaudio==2.6.0
5
  numpy==1.26.4