57 lines
1.9 KiB
Python
57 lines
1.9 KiB
Python
"""政策指引接口。"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from flask import Blueprint, request
|
|
|
|
from app.utils.guidance_analysis import GuidanceAnalyzer
|
|
from app.utils.response import error, success
|
|
from app.utils.storage import DomainStorage
|
|
|
|
guidance_bp = Blueprint("guidance", __name__)
|
|
|
|
|
|
def public_guidance_file(record: dict) -> dict:
|
|
return {key: value for key, value in record.items() if key != "stored_path"}
|
|
|
|
|
|
@guidance_bp.post("/api/guidance/upload")
|
|
def upload_guidance():
|
|
token_id = request.form.get("token_id") or request.form.get("token") or ""
|
|
file = (
|
|
request.files.get("file")
|
|
or request.files.get("files")
|
|
or request.files.get("guidance_file")
|
|
)
|
|
if not token_id or not file:
|
|
return error("上传失败:缺少 token/token_id 或文件", 400)
|
|
try:
|
|
storage = DomainStorage()
|
|
record = storage.save_guidance_file(token_id, file.stream, file.filename)
|
|
except ValueError as exc:
|
|
return error(f"上传失败:{exc}", 404)
|
|
return success(public_guidance_file(record), message="上传成功")
|
|
|
|
|
|
@guidance_bp.post("/api/guidance/analyze")
|
|
def analyze_guidance():
|
|
payload = request.get_json(silent=True) or request.form.to_dict() or {}
|
|
granularity = payload.get("granularity", "high")
|
|
analyzer = GuidanceAnalyzer()
|
|
if not analyzer.is_supported_granularity(granularity):
|
|
return error("分析失败:不支持的 granularity", 400)
|
|
normalized = analyzer.normalize_granularity(granularity)
|
|
token_id = payload.get("token_id", "")
|
|
try:
|
|
results = DomainStorage().analyze_guidance(
|
|
analysis_options={"granularity": normalized},
|
|
token_id=token_id,
|
|
)
|
|
except ValueError as exc:
|
|
return error(f"分析失败:{exc}", 400)
|
|
return success({
|
|
"results": results,
|
|
"total": len(results),
|
|
"analysis_options": {"granularity": normalized, "token_id": token_id},
|
|
}, message="分析成功")
|