AI 生成: 一个基于 Flask 的 Web 应用,用户输入主题后由 AI 生成结构化的登记手册,包含封面、目录、正文与附录,并支持导出为 Markdown 文件。
This commit is contained in:
@@ -0,0 +1,53 @@
|
||||
import io
|
||||
import os
|
||||
|
||||
from flask import Flask, jsonify, render_template, request, send_file
|
||||
|
||||
from handbook.generator import generate_handbook
|
||||
|
||||
app = Flask(__name__)
|
||||
|
||||
|
||||
@app.route("/")
|
||||
def index():
|
||||
return render_template("index.html")
|
||||
|
||||
|
||||
@app.route("/api/generate", methods=["POST"])
|
||||
def api_generate():
|
||||
data = request.get_json(silent=True) or {}
|
||||
topic = (data.get("topic") or "").strip()
|
||||
if not topic:
|
||||
return jsonify({"error": "主题不能为空"}), 400
|
||||
|
||||
try:
|
||||
markdown = generate_handbook(topic)
|
||||
except Exception as exc: # noqa: BLE001
|
||||
return jsonify({"error": f"生成失败: {exc}"}), 500
|
||||
|
||||
return jsonify({"topic": topic, "markdown": markdown})
|
||||
|
||||
|
||||
@app.route("/api/export", methods=["POST"])
|
||||
def api_export():
|
||||
data = request.get_json(silent=True) or {}
|
||||
topic = (data.get("topic") or "handbook").strip() or "handbook"
|
||||
markdown = data.get("markdown") or ""
|
||||
|
||||
if not markdown:
|
||||
return jsonify({"error": "内容不能为空"}), 400
|
||||
|
||||
buffer = io.BytesIO(markdown.encode("utf-8"))
|
||||
buffer.seek(0)
|
||||
safe_name = "".join(c for c in topic if c.isalnum() or c in "-_ ").strip() or "handbook"
|
||||
return send_file(
|
||||
buffer,
|
||||
mimetype="text/markdown",
|
||||
as_attachment=True,
|
||||
download_name=f"{safe_name}-登记手册.md",
|
||||
)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
port = int(os.environ.get("PORT", "5000"))
|
||||
app.run(host="0.0.0.0", port=port, debug=True)
|
||||
Reference in New Issue
Block a user