harp-dev commited on
Commit
977d5f4
·
verified ·
1 Parent(s): 16a90c5

Remove the app and requirements the Docker image never uses; start.sh runs frontend_app.py.

Browse files
Files changed (2) hide show
  1. app.py +0 -170
  2. requirements.txt +0 -17
app.py DELETED
@@ -1,170 +0,0 @@
1
- from __future__ import annotations
2
-
3
- import gradio as gr
4
- try:
5
- import spaces
6
- except ImportError: # 'spaces' is only provided by Hugging Face Spaces
7
- import types as _types
8
-
9
- def _gpu(*args, **kwargs):
10
- if len(args) == 1 and callable(args[0]) and not kwargs:
11
- return args[0]
12
-
13
- def _decorator(func):
14
- return func
15
-
16
- return _decorator
17
-
18
- spaces = _types.SimpleNamespace(GPU=_gpu)
19
-
20
-
21
- import tensorflow as tf
22
- import gin
23
- import ddsp.training.inference
24
- import ddsp.core
25
- import librosa
26
- import soundfile
27
- import numpy as np
28
- import requests
29
- import zipfile
30
- import io
31
- import tempfile
32
- import os
33
- import crepe
34
-
35
- # Global variables for model and parameters
36
- MODEL = None
37
- SAMPLE_RATE = None
38
- N_SAMPLES = None
39
- N_FRAMES = None
40
-
41
- def _download_and_extract_model(model_name):
42
- model_url = f'https://storage.googleapis.com/ddsp/models/{model_name}/{model_name}.zip'
43
- print(f"Downloading model from: {model_url}")
44
- response = requests.get(model_url)
45
- response.raise_for_status() # Raise an exception for HTTP errors
46
-
47
- # Create a temporary directory for the model
48
- temp_dir = tempfile.mkdtemp()
49
- zip_path = os.path.join(temp_dir, f'{model_name}.zip')
50
-
51
- with open(zip_path, 'wb') as f:
52
- f.write(response.content)
53
-
54
- with zipfile.ZipFile(zip_path, 'r') as zip_ref:
55
- zip_ref.extractall(temp_dir)
56
-
57
- # The actual model checkpoint is usually in a subdirectory
58
- # Find the directory that contains the 'operative_config.gin'
59
- model_ckpt_dir = None
60
- for root, dirs, files in os.walk(temp_dir):
61
- if 'operative_config.gin' in files:
62
- model_ckpt_dir = root
63
- break
64
-
65
- if not model_ckpt_dir:
66
- raise FileNotFoundError("Could not find operative_config.gin in the downloaded model.")
67
-
68
- print(f"Model extracted to: {model_ckpt_dir}")
69
- return model_ckpt_dir
70
-
71
- # --- Model Loading ---
72
- MODEL_NAME = 'violin_ddsp_2020_03_03' # Example model from DDSP demos
73
- CKPT_DIR = _download_and_extract_model(MODEL_NAME)
74
-
75
- # Initialize the model with fixed parameters for this deployment
76
- LENGTH_SECONDS = 4 # Default length for output audio
77
- REMOVE_REVERB = True # Whether to remove reverb from the output
78
-
79
- # Need to unlock gin config before parsing
80
- with gin.unlock_config():
81
- MODEL = ddsp.training.inference.AutoencoderInference(
82
- ckpt=CKPT_DIR,
83
- length_seconds=LENGTH_SECONDS,
84
- remove_reverb=REMOVE_REVERB
85
- )
86
-
87
- # Extract model parameters after initialization
88
- SAMPLE_RATE = MODEL.sample_rate
89
- N_SAMPLES = MODEL.n_samples
90
- N_FRAMES = MODEL.n_frames
91
-
92
- # Build the network by running a fake batch to initialize weights
93
- MODEL.build_network()
94
-
95
- # --- Helper functions for audio processing ---
96
- def _load_audio_for_inference(audio_path, sr, n_samples):
97
- audio, _ = librosa.load(audio_path, sr=sr, mono=True)
98
- # Pad or trim audio to n_samples
99
- if len(audio) < n_samples:
100
- audio = np.pad(audio, (0, n_samples - len(audio)), mode='constant')
101
- else:
102
- audio = audio[:n_samples]
103
- return audio
104
-
105
- def _save_audio_for_inference(audio_tensor, sr):
106
- output_path = tempfile.NamedTemporaryFile(suffix=".wav", delete=False).name
107
- audio_np = audio_tensor.numpy() if tf.is_tensor(audio_tensor) else audio_tensor
108
- soundfile.write(output_path, audio_np, sr)
109
- return output_path
110
-
111
-
112
- @spaces.GPU
113
- def predict(input_audio):
114
- # Load audio
115
- audio_np = _load_audio_for_inference(input_audio, SAMPLE_RATE, N_SAMPLES)
116
-
117
- # Extract pitch using crepe
118
- # crepe.predict expects a 1D numpy array
119
- # Calculate step_size in milliseconds for crepe based on model's frame rate
120
- step_size_ms = int(1000 * N_SAMPLES / (SAMPLE_RATE * N_FRAMES))
121
- _, f0_hz, _, _ = crepe.predict(
122
- audio_np, SAMPLE_RATE, viterbi=True, step_size=step_size_ms
123
- )
124
-
125
- # Resample f0_hz to N_FRAMES
126
- f0_hz_resampled = ddsp.core.resample(
127
- tf.convert_to_tensor(f0_hz, dtype=tf.float32), N_FRAMES
128
- )
129
-
130
- # Compute loudness
131
- loudness_db = ddsp.core.compute_loudness(
132
- tf.expand_dims(tf.convert_to_tensor(audio_np, dtype=tf.float32), axis=0),
133
- SAMPLE_RATE
134
- )
135
-
136
- # Prepare features dictionary
137
- audio_features = {
138
- 'audio': tf.expand_dims(tf.convert_to_tensor(audio_np, dtype=tf.float32), axis=0),
139
- 'f0_hz': tf.expand_dims(f0_hz_resampled, axis=0),
140
- 'loudness_db': loudness_db,
141
- }
142
-
143
- # Preprocess features using the model's preprocessor
144
- processed_features = MODEL.preprocessor(audio_features)
145
-
146
- # Run inference
147
- outputs = MODEL(processed_features, training=False)
148
-
149
- # Get synthesized audio from outputs
150
- synthesized_audio = MODEL.get_audio_from_outputs(outputs)
151
-
152
- # Save and return the output audio file path
153
- output_audio_path = _save_audio_for_inference(synthesized_audio[0], SAMPLE_RATE)
154
- return output_audio_path
155
-
156
-
157
- demo = gr.Interface(
158
- fn=predict,
159
- inputs=[
160
- gr.Audio(type="filepath", label="Input Audio"),
161
- ],
162
- outputs=[
163
- gr.Audio(type="filepath", label="Output Audio"),
164
- ],
165
- title="DDSP Autoencoder Inference",
166
- description="Resynthesize input audio using a pre-trained DDSP autoencoder model. This model takes an audio input, extracts its fundamental frequency (f0) and loudness, and then uses a DDSP autoencoder to synthesize a new audio output, effectively performing a timbre transfer based on the loaded model.",
167
- )
168
-
169
- if __name__ == "__main__":
170
- demo.queue().launch(server_name="0.0.0.0", server_port=7860, show_error=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
requirements.txt DELETED
@@ -1,17 +0,0 @@
1
- absl-py
2
- crepe<=0.0.12
3
- dill<=0.3.4
4
- gin-config>=0.3.0
5
- hmmlearn<=0.2.7
6
- librosa<=0.10
7
- protobuf<=3.20
8
- numpy<1.24
9
- scipy<=1.10.1
10
- six
11
- tensorflow<=2.11
12
- tensorflow-probability<=0.19
13
- soundfile
14
- requests
15
- git+https://github.com/magenta/ddsp.git@main
16
- git+https://github.com/TEAMuP-dev/pyharp.git@v0.4.2
17
- spaces>=0.51.1