stardate69 commited on
Commit
305c299
·
verified ·
1 Parent(s): 9e1df64

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -33
app.py CHANGED
@@ -1,4 +1,4 @@
1
- import gradio as gr
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 (ensure this folder is accessible and writable)
24
  OUTPUT_PATH = "./generated_audio"
25
  os.makedirs(OUTPUT_PATH, exist_ok=True)
26
 
27
- # Function to generate audio from prompt
 
 
 
28
  @spaces.GPU
29
- def generate_audio(prompt: str):
30
- # Generate the audio using StableAudioPipeline
31
- generator = torch.Generator(device).manual_seed(42)
32
- audio_output = pipe(prompt=prompt,
33
- negative_prompt='Low Quality',
34
- num_inference_steps=int(10), # Number of diffusion steps
35
- audio_end_in_s=1,
36
- num_waveforms_per_prompt=1,
37
- generator=generator).audios
38
-
39
- # Convert to numpy and save to a WAV file
40
- output_audio = audio_output[0].T.float().cpu().numpy()
41
- output_filename = "output.wav"
42
- output_path = os.path.join(OUTPUT_PATH, output_filename)
43
- sf.write(output_path, output_audio, pipe.vae.sampling_rate)
44
-
45
- # Return the file path so Gradio serves the file
46
- return output_path
47
-
48
- # Gradio Interface setup
49
- interface = gr.Interface(
50
- fn=generate_audio,
51
- inputs=gr.Textbox(label="Enter a text prompt to generate audio"),
52
- outputs=gr.File(label="Generated Audio File"),
53
- title="StableAudioOpen",
54
- description="Generate audio from a text prompt using Hugging Face StableAudio Pipeline."
55
- )
56
-
57
- # Launch the Gradio interface
58
- interface.launch(share=True)
 
 
 
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)