Spaces:
Sleeping
Sleeping
| import os | |
| import wave | |
| import math | |
| import struct | |
| from flask import Flask, render_template, send_from_directory | |
| app = Flask(__name__) | |
| def generate_default_audio(): | |
| path = 'static/demo.wav' | |
| if os.path.exists(path): | |
| return | |
| try: | |
| if not os.path.exists('static'): | |
| os.makedirs('static') | |
| print("Generating default audio...") | |
| duration = 5 | |
| frequency = 440.0 | |
| framerate = 44100 | |
| n_frames = int(duration * framerate) | |
| with wave.open(path, 'w') as wav_file: | |
| wav_file.setnchannels(1) | |
| wav_file.setsampwidth(2) | |
| wav_file.setframerate(framerate) | |
| for i in range(n_frames): | |
| value = int(32767.0 * math.sin(2.0 * math.pi * frequency * i / framerate)) | |
| data = struct.pack('<h', value) | |
| wav_file.writeframesraw(data) | |
| print(f"Generated {path}") | |
| except Exception as e: | |
| print(f"Failed to generate audio: {e}") | |
| # Generate on import/start | |
| generate_default_audio() | |
| def index(): | |
| return render_template('index.html') | |
| def serve_static(path): | |
| return send_from_directory('static', path) | |
| if __name__ == '__main__': | |
| # Hugging Face Spaces defaults to 7860 | |
| port = int(os.environ.get('PORT', 7860)) | |
| app.run(debug=True, host='0.0.0.0', port=port) | |