Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,6 +6,7 @@ from huggingface_hub import login
|
|
| 6 |
from diffusers import StableAudioPipeline
|
| 7 |
import os
|
| 8 |
import io
|
|
|
|
| 9 |
|
| 10 |
# Load Hugging Face token securely
|
| 11 |
HUGGINGFACE_TOKEN = os.getenv("HF_TOKEN")
|
|
@@ -45,6 +46,18 @@ def generate_audio():
|
|
| 45 |
generator=generator
|
| 46 |
).audios
|
| 47 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
# Convert audio to BytesIO in memory
|
| 49 |
output_io = io.BytesIO()
|
| 50 |
output_audio = audio_output[0].T.float().cpu().numpy()
|
|
@@ -53,6 +66,7 @@ def generate_audio():
|
|
| 53 |
|
| 54 |
# Send the file in response as attachment for download
|
| 55 |
return send_file(output_io, as_attachment=False, download_name="output.wav", mimetype='audio/wav')
|
|
|
|
| 56 |
|
| 57 |
except Exception as e:
|
| 58 |
return jsonify({"error": str(e)}), 500
|
|
|
|
| 6 |
from diffusers import StableAudioPipeline
|
| 7 |
import os
|
| 8 |
import io
|
| 9 |
+
import tempfile
|
| 10 |
|
| 11 |
# Load Hugging Face token securely
|
| 12 |
HUGGINGFACE_TOKEN = os.getenv("HF_TOKEN")
|
|
|
|
| 46 |
generator=generator
|
| 47 |
).audios
|
| 48 |
|
| 49 |
+
# Create a temporary file to store the generated audio
|
| 50 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as tmpfile:
|
| 51 |
+
# Write the audio to the temporary file
|
| 52 |
+
sf.write(tmpfile.name, audio_output.T.cpu().numpy(), pipe.vae.sampling_rate)
|
| 53 |
+
|
| 54 |
+
# Store the temporary file path
|
| 55 |
+
temp_file_path = tmpfile.name
|
| 56 |
+
|
| 57 |
+
# Create a downloadable URL by returning the temporary file path
|
| 58 |
+
return jsonify({"download_url": f"/download/{os.path.basename(temp_file_path)}"}), 200
|
| 59 |
+
|
| 60 |
+
'''
|
| 61 |
# Convert audio to BytesIO in memory
|
| 62 |
output_io = io.BytesIO()
|
| 63 |
output_audio = audio_output[0].T.float().cpu().numpy()
|
|
|
|
| 66 |
|
| 67 |
# Send the file in response as attachment for download
|
| 68 |
return send_file(output_io, as_attachment=False, download_name="output.wav", mimetype='audio/wav')
|
| 69 |
+
'''
|
| 70 |
|
| 71 |
except Exception as e:
|
| 72 |
return jsonify({"error": str(e)}), 500
|