Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
import
|
| 2 |
import spaces
|
| 3 |
import torch
|
| 4 |
import soundfile as sf
|
|
@@ -20,39 +20,44 @@ torch_dtype = torch.float16 if device == "cuda" else torch.float32
|
|
| 20 |
pipe = StableAudioPipeline.from_pretrained("stabilityai/stable-audio-open-1.0", torch_dtype=torch_dtype)
|
| 21 |
pipe = pipe.to(device)
|
| 22 |
|
| 23 |
-
# Path to store generated audio files
|
| 24 |
OUTPUT_PATH = "./generated_audio"
|
| 25 |
os.makedirs(OUTPUT_PATH, exist_ok=True)
|
| 26 |
|
| 27 |
-
#
|
|
|
|
|
|
|
|
|
|
| 28 |
@spaces.GPU
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
generator=
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
|
|
|
|
|
|
|
|
| 1 |
+
from flask import Flask, request, jsonify, send_file
|
| 2 |
import spaces
|
| 3 |
import torch
|
| 4 |
import soundfile as sf
|
|
|
|
| 20 |
pipe = StableAudioPipeline.from_pretrained("stabilityai/stable-audio-open-1.0", torch_dtype=torch_dtype)
|
| 21 |
pipe = pipe.to(device)
|
| 22 |
|
| 23 |
+
# Path to store generated audio files
|
| 24 |
OUTPUT_PATH = "./generated_audio"
|
| 25 |
os.makedirs(OUTPUT_PATH, exist_ok=True)
|
| 26 |
|
| 27 |
+
# Initialize Flask app
|
| 28 |
+
app = Flask(__name__)
|
| 29 |
+
|
| 30 |
+
# Route to generate audio
|
| 31 |
@spaces.GPU
|
| 32 |
+
@app.route("/generate", methods=["GET"])
|
| 33 |
+
def generate_audio():
|
| 34 |
+
prompt = request.args.get("prompt")
|
| 35 |
+
if not prompt:
|
| 36 |
+
return jsonify({"error": "Missing prompt parameter"}), 400
|
| 37 |
+
|
| 38 |
+
try:
|
| 39 |
+
# Generate the audio using StableAudioPipeline
|
| 40 |
+
generator = torch.Generator(device).manual_seed(42)
|
| 41 |
+
audio_output = pipe(
|
| 42 |
+
prompt=prompt,
|
| 43 |
+
negative_prompt='Low Quality',
|
| 44 |
+
num_inference_steps=10, # Number of diffusion steps
|
| 45 |
+
audio_end_in_s=1,
|
| 46 |
+
num_waveforms_per_prompt=1,
|
| 47 |
+
generator=generator
|
| 48 |
+
).audios
|
| 49 |
+
|
| 50 |
+
# Convert to numpy and save to a WAV file
|
| 51 |
+
output_audio = audio_output[0].T.float().cpu().numpy()
|
| 52 |
+
output_filename = "output.wav"
|
| 53 |
+
output_path = os.path.join(OUTPUT_PATH, output_filename)
|
| 54 |
+
sf.write(output_path, output_audio, pipe.vae.sampling_rate)
|
| 55 |
+
|
| 56 |
+
# Return the WAV file
|
| 57 |
+
return send_file(output_path, as_attachment=True)
|
| 58 |
+
except Exception as e:
|
| 59 |
+
return jsonify({"error": str(e)}), 500
|
| 60 |
+
|
| 61 |
+
# Run the Flask app
|
| 62 |
+
if __name__ == "__main__":
|
| 63 |
+
app.run(host="0.0.0.0", port=7860)
|