Files
ppt/ppt_manager_v2/web_socket_app.py

97 lines
3.1 KiB
Python
Raw Permalink Normal View History

import sys
from pathlib import Path
from flask import Flask, render_template, jsonify, send_file, request
from flask_socketio import SocketIO, emit
import threading
from datetime import datetime
sys.path.insert(0, str(Path(__file__).parent))
from orchestrator import Orchestrator
from loguru import logger
base_dir = Path(__file__).parent
template_dir = base_dir / "web" / "templates"
app = Flask(__name__, template_folder=str(template_dir))
app.config['SECRET_KEY'] = 'ppt_manager_v2_secret!'
socketio = SocketIO(app, cors_allowed_origins="*")
@app.route('/')
def index():
return render_template('index_v2.html')
@app.route('/api/projects')
def list_projects():
orch = Orchestrator()
plugins = orch.plugin_manager.list_generators()
return jsonify({
'success': True,
'plugins': plugins,
'config': orch.config
})
@app.route('/api/files')
def list_files():
output_dir = base_dir.parent / "output"
files = []
if output_dir.exists():
for f in sorted(output_dir.glob("*.pptx"), reverse=True):
files.append({
'name': f.name,
'size': round(f.stat().st_size / 1024 / 1024, 2),
'modified': datetime.fromtimestamp(f.stat().st_mtime).isoformat()
})
return jsonify({'success': True, 'files': files})
@app.route('/download/<filename>')
def download(filename):
f = base_dir.parent / "output" / filename
if f.exists():
return send_file(str(f), as_attachment=True)
return jsonify(success=False, error="File not found"), 404
@socketio.on('start_generation')
def handle_start_generation(data):
params = data.get('params', {})
def progress_callback(message):
socketio.emit('progress', message, room=request.sid)
logger.debug(f"WebSocket进度: {message['percent']}% - {message['message']}")
orch = Orchestrator()
orch.set_progress_callback(progress_callback)
try:
socketio.emit('progress', {
'step': 'START',
'message': '开始执行PPT生成管线...',
'percent': 0
}, room=request.sid)
output_path = orch.run_full_pipeline(params=params)
socketio.emit('complete', {
'success': True,
'output_file': Path(output_path).name,
'download_url': f"/download/{Path(output_path).name}",
'results': {k: v.get('success', False) for k,v in orch.results.get('plugins', {}).items()}
}, room=request.sid)
except Exception as e:
logger.exception("生成过程出错")
socketio.emit('complete', {
'success': False,
'error': str(e)
}, room=request.sid)
def run_server():
print("\n" + "=" * 65)
print(" 🚀 PPT智能管理系统 V2.0 - WebSocket实时日志版")
print(" 请在浏览器打开: http://localhost:5001")
print("=" * 65 + "\n")
socketio.run(app, host='0.0.0.0', port=5001, debug=True)
if __name__ == '__main__':
run_server()