Spaces:
Sleeping
Sleeping
| import os | |
| from flask import Flask, render_template, request, jsonify | |
| app = Flask(__name__) | |
| # Configure upload folder and max size (e.g., 100MB) | |
| # Use /tmp for Hugging Face Spaces compatibility or local temp dir | |
| import tempfile | |
| UPLOAD_FOLDER = os.path.join(tempfile.gettempdir(), 'uploads') | |
| if not os.path.exists(UPLOAD_FOLDER): | |
| os.makedirs(UPLOAD_FOLDER) | |
| app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER | |
| app.config['MAX_CONTENT_LENGTH'] = 100 * 1024 * 1024 # 100MB limit | |
| def index(): | |
| return render_template('index.html') | |
| def upload_file(): | |
| if 'file' not in request.files: | |
| return jsonify({'error': '没有文件部分'}), 400 | |
| file = request.files['file'] | |
| if file.filename == '': | |
| return jsonify({'error': '未选择文件'}), 400 | |
| if file: | |
| filename = file.filename | |
| # Ensure safe filename if needed, but for now just save it | |
| # In a real app, use werkzeug.utils.secure_filename | |
| file_path = os.path.join(app.config['UPLOAD_FOLDER'], filename) | |
| try: | |
| file.save(file_path) | |
| return jsonify({ | |
| 'message': '文件上传成功', | |
| 'filename': filename, | |
| 'size': os.path.getsize(file_path) | |
| }), 200 | |
| except Exception as e: | |
| return jsonify({'error': str(e)}), 500 | |
| if __name__ == '__main__': | |
| app.run(host='0.0.0.0', port=5001, debug=True) | |