Spaces:
Sleeping
Sleeping
File size: 1,476 Bytes
7e13234 7a8dae8 7e13234 7a8dae8 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | 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
@app.route('/')
def index():
return render_template('index.html')
@app.route('/upload', methods=['POST'])
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)
|