Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,7 +6,6 @@ from huggingface_hub import login
|
|
| 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")
|
|
@@ -25,10 +24,6 @@ pipe = pipe.to(device)
|
|
| 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"])
|
|
@@ -49,20 +44,7 @@ def generate_audio():
|
|
| 49 |
num_waveforms_per_prompt=1,
|
| 50 |
generator=generator
|
| 51 |
).audios
|
| 52 |
-
|
| 53 |
-
# Create an absolute file path in the "static/generated_audio" directory
|
| 54 |
-
output_audio = audio_output[0].T.float().cpu().numpy()
|
| 55 |
-
output_filename = f"{prompt.replace(' ', '_')}_output.wav"
|
| 56 |
-
output_file_path = os.path.join(PUBLIC_DIRECTORY, output_filename)
|
| 57 |
-
|
| 58 |
-
# Write the generated audio to the file
|
| 59 |
-
sf.write(output_file_path, output_audio, pipe.vae.sampling_rate)
|
| 60 |
-
|
| 61 |
-
# Return the absolute URL to the file
|
| 62 |
-
download_url = f"/download/{output_filename}"
|
| 63 |
-
return jsonify({"download_url": download_url}), 200
|
| 64 |
|
| 65 |
-
'''
|
| 66 |
# Convert audio to BytesIO in memory
|
| 67 |
output_io = io.BytesIO()
|
| 68 |
output_audio = audio_output[0].T.float().cpu().numpy()
|
|
@@ -71,7 +53,7 @@ def generate_audio():
|
|
| 71 |
|
| 72 |
# Send the file in response as attachment for download
|
| 73 |
return send_file(output_io, as_attachment=False, download_name="output.wav", mimetype='audio/wav')
|
| 74 |
-
|
| 75 |
|
| 76 |
except Exception as e:
|
| 77 |
return jsonify({"error": str(e)}), 500
|
|
|
|
| 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")
|
|
|
|
| 24 |
# Initialize Flask app
|
| 25 |
app = Flask(__name__)
|
| 26 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
# Route to generate audio
|
| 28 |
|
| 29 |
@app.route("/generate", methods=["GET"])
|
|
|
|
| 44 |
num_waveforms_per_prompt=1,
|
| 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 |
|
| 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 |
|
| 58 |
except Exception as e:
|
| 59 |
return jsonify({"error": str(e)}), 500
|