stardate69 commited on
Commit
eed32d8
·
verified ·
1 Parent(s): 7fea2c3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -5
app.py CHANGED
@@ -5,7 +5,7 @@ import soundfile as sf
5
  from huggingface_hub import login
6
  from diffusers import StableAudioPipeline
7
  import os
8
- #import tempfile
9
 
10
  # Load Hugging Face token securely
11
  HUGGINGFACE_TOKEN = os.getenv("HF_TOKEN")
@@ -22,8 +22,8 @@ pipe = StableAudioPipeline.from_pretrained("stabilityai/stable-audio-open-1.0",
22
  pipe = pipe.to(device)
23
 
24
  # Path to store generated audio files
25
- OUTPUT_PATH = "./generated_audio"
26
- os.makedirs(OUTPUT_PATH, exist_ok=True)
27
 
28
  # Initialize Flask app
29
  app = Flask(__name__)
@@ -48,17 +48,29 @@ def generate_audio():
48
  num_waveforms_per_prompt=1,
49
  generator=generator
50
  ).audios
51
-
 
 
 
 
 
 
 
 
 
 
52
  # Convert to numpy and save to a WAV file
53
  output_audio = audio_output[0].T.float().cpu().numpy()
54
  output_filename = "output.wav"
55
- output_path = os.path.join(OUTPUT_PATH, output_filename)
56
  sf.write(output_path, output_audio, pipe.vae.sampling_rate)
57
 
58
  if os.path.exists(output_path):
59
  return send_file(output_path, as_attachment=True)
60
  else:
61
  return {"error": f"File not found at {output_path}"}, 404
 
 
62
 
63
  # Return the WAV file
64
  #return jsonify({"file_path": absolute_output_path}), 200
 
5
  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")
 
22
  pipe = pipe.to(device)
23
 
24
  # Path to store generated audio files
25
+ #OUTPUT_PATH = "./generated_audio"
26
+ #os.makedirs(OUTPUT_PATH, exist_ok=True)
27
 
28
  # Initialize Flask app
29
  app = Flask(__name__)
 
48
  num_waveforms_per_prompt=1,
49
  generator=generator
50
  ).audios
51
+
52
+ # Convert audio to BytesIO in memory
53
+ output_io = io.BytesIO()
54
+ output_audio = audio_output[0].T.float().cpu().numpy()
55
+ sf.write(output_io, output_audio, pipe.vae.sampling_rate) # Save as WAV or your preferred format
56
+ output_io.seek(0) # Reset buffer pointer to beginning
57
+
58
+ # Send the file in response as attachment for download
59
+ return send_file(output_io, as_attachment=True, download_name="output.wav", mimetype='audio/wav')
60
+
61
+ '''
62
  # Convert to numpy and save to a WAV file
63
  output_audio = audio_output[0].T.float().cpu().numpy()
64
  output_filename = "output.wav"
65
+ #output_path = os.path.join(OUTPUT_PATH, output_filename)
66
  sf.write(output_path, output_audio, pipe.vae.sampling_rate)
67
 
68
  if os.path.exists(output_path):
69
  return send_file(output_path, as_attachment=True)
70
  else:
71
  return {"error": f"File not found at {output_path}"}, 404
72
+
73
+ '''
74
 
75
  # Return the WAV file
76
  #return jsonify({"file_path": absolute_output_path}), 200