Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -34,40 +34,42 @@ def preprocess_audio_to_npz(audio_path):
|
|
34 |
Returns:
|
35 |
str: Path to the generated .npz file.
|
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 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
|
|
|
|
71 |
|
72 |
return npz_path
|
73 |
|
|
|
34 |
Returns:
|
35 |
str: Path to the generated .npz file.
|
36 |
"""
|
37 |
+
# Set device to CPU
|
38 |
+
with torch.device("cpu"):
|
39 |
+
# Load and resample audio to Bark's SAMPLE_RATE (24kHz)
|
40 |
+
audio, sr = librosa.load(audio_path, sr=SAMPLE_RATE, mono=True)
|
41 |
+
|
42 |
+
# Ensure audio is a float32 array
|
43 |
+
audio = audio.astype(np.float32)
|
44 |
+
|
45 |
+
# Load HuBERT models for semantic token extraction
|
46 |
+
hubert_manager = load_model(model_type="hubert")
|
47 |
+
hubert_tokenizer = load_model(model_type="hubert_tokenizer")
|
48 |
+
|
49 |
+
# Generate semantic tokens
|
50 |
+
tokens = _tokenize(audio, hubert_manager, hubert_tokenizer)
|
51 |
+
semantic_tokens = tokens[0] # Extract semantic tokens
|
52 |
+
|
53 |
+
# Load coarse model for coarse tokens
|
54 |
+
coarse_model = load_model(model_type="coarse")
|
55 |
+
|
56 |
+
# Generate coarse tokens
|
57 |
+
coarse_tokens = generate_text_semantic(
|
58 |
+
semantic_tokens=semantic_tokens,
|
59 |
+
model=coarse_model,
|
60 |
+
max_gen_len=512
|
61 |
+
)
|
62 |
+
|
63 |
+
# Create history prompt dictionary
|
64 |
+
history_prompt = {
|
65 |
+
"semantic_prompt": semantic_tokens,
|
66 |
+
"coarse_prompt": coarse_tokens
|
67 |
+
}
|
68 |
+
|
69 |
+
# Save to temporary .npz file
|
70 |
+
with tempfile.NamedTemporaryFile(suffix=".npz", delete=False) as temp_file:
|
71 |
+
np.savez(temp_file.name, **history_prompt)
|
72 |
+
npz_path = temp_file.name
|
73 |
|
74 |
return npz_path
|
75 |
|