stardate69 commited on
Commit
b8a2725
·
verified ·
1 Parent(s): d1feff4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -9
app.py CHANGED
@@ -25,6 +25,10 @@ pipe = pipe.to(device)
25
  # Initialize Flask app
26
  app = Flask(__name__)
27
 
 
 
 
 
28
  # Route to generate audio
29
 
30
  @app.route("/generate", methods=["GET"])
@@ -46,17 +50,16 @@ def generate_audio():
46
  generator=generator
47
  ).audios
48
 
49
- # Create a temporary file to store the generated audio
50
- output_audio = audio_output[0].T.float().cpu().numpy()
51
- with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as tmpfile:
52
- # Write the audio to the temporary file
53
- sf.write(tmpfile.name, output_audio, pipe.vae.sampling_rate)
54
 
55
- # Store the temporary file path
56
- temp_file_path = tmpfile.name
57
 
58
- # Create a downloadable URL by returning the temporary file path
59
- return jsonify({"download_url": f"/download/{os.path.basename(temp_file_path)}"}), 200
 
60
 
61
  '''
62
  # Convert audio to BytesIO in memory
 
25
  # Initialize Flask app
26
  app = Flask(__name__)
27
 
28
+ # Set up a folder for public files (you can choose a different directory)
29
+ PUBLIC_DIRECTORY = os.path.join(os.getcwd(), 'static', 'generated_audio')
30
+ os.makedirs(PUBLIC_DIRECTORY, exist_ok=True)
31
+
32
  # Route to generate audio
33
 
34
  @app.route("/generate", methods=["GET"])
 
50
  generator=generator
51
  ).audios
52
 
53
+ # Create an absolute file path in the "static/generated_audio" directory
54
+ output_filename = f"{prompt.replace(' ', '_')}_output.wav"
55
+ output_file_path = os.path.join(PUBLIC_DIRECTORY, output_filename)
 
 
56
 
57
+ # Write the generated audio to the file
58
+ sf.write(output_file_path, audio_output.T.cpu().numpy(), pipe.vae.sampling_rate)
59
 
60
+ # Return the absolute URL to the file
61
+ download_url = f"/download/{output_filename}"
62
+ return jsonify({"download_url": download_url}), 200
63
 
64
  '''
65
  # Convert audio to BytesIO in memory