chore: sync bridge/app.py
This commit is contained in:
1 parent
be22f506a7
commit
17252e9c6c
1 file changed
+46
-2
+46
-2
@@ -1,5 +1,8 @@
|
||||
import json, os, re, time, hashlib, uuid, math
|
||||
import requests
|
||||
import matplotlib
|
||||
matplotlib.use("Agg")
|
||||
import matplotlib.pyplot as plt
|
||||
from fastapi import FastAPI, Request
|
||||
from fastapi.responses import JSONResponse, StreamingResponse, FileResponse
|
||||
from fastapi.staticfiles import StaticFiles
|
||||
@@ -19,9 +22,14 @@ MYSQL = ("192.168.50.176", 3308, "erp", "erp_pass_123")
|
||||
LOCAL_LLM = os.environ.get("LOCAL_LLM", "qwen2.5:7b")
|
||||
COLL = "mini_erp_docs"
|
||||
PUBLIC_BASE = os.environ.get("PUBLIC_BASE", "http://localhost:8010")
|
||||
CHART_DIR = os.environ.get("CHART_DIR", "/app/charts")
|
||||
plt.rcParams["font.family"] = ["Noto Sans CJK SC", "sans-serif"]
|
||||
plt.rcParams["axes.unicode_minus"] = False
|
||||
|
||||
app = FastAPI(title="mini-erp analyst bridge")
|
||||
app.mount("/static", StaticFiles(directory=os.path.join(os.path.dirname(__file__), "static")), name="static")
|
||||
os.makedirs(CHART_DIR, exist_ok=True)
|
||||
app.mount("/charts", StaticFiles(directory=CHART_DIR), name="charts")
|
||||
SESSIONS = {}
|
||||
|
||||
SYS_PROMPT = """你是 mini-ERP 数据编织平台的问数分析师。依据【元数据】与【业务文档召回】回答用户问题,结合对话历史保持多轮一致。
|
||||
@@ -252,6 +260,39 @@ def svg_chart(spec):
|
||||
"svg{max-width:100%;height:auto}</style></head><body>" + svg + "</body></html>")
|
||||
|
||||
|
||||
def chart_image(spec):
|
||||
chart, cols, rows = spec["chart"], spec["columns"], spec["rows"]
|
||||
xi = cols.index(chart["x"]) if chart["x"] in cols else 0
|
||||
yi = cols.index(chart["y"]) if chart["y"] in cols else 1
|
||||
xs = [str(r[xi]) for r in rows]
|
||||
ys = [float(r[yi] or 0) for r in rows]
|
||||
ctype = chart.get("type", "bar")
|
||||
fig, ax = plt.subplots(figsize=(7.6, 4.2), dpi=100)
|
||||
colors = [PALETTE[i % 8] for i in range(len(ys))]
|
||||
if ctype == "pie":
|
||||
ax.pie(ys, labels=xs, autopct="%1.1f%%", colors=colors, startangle=90,
|
||||
textprops={"fontsize": 10})
|
||||
ax.axis("equal")
|
||||
else:
|
||||
if ctype == "line":
|
||||
ax.plot(xs, ys, marker="o", color=PALETTE[0])
|
||||
else:
|
||||
ax.bar(xs, ys, color=PALETTE[0], width=0.6)
|
||||
for i, v in enumerate(ys):
|
||||
ax.text(i, v, _fmt(v), ha="center", va="bottom", fontsize=9)
|
||||
ax.grid(axis="y", color="#e3e6ea")
|
||||
ax.set_axisbelow(True)
|
||||
ax.set_ylabel(chart["y"])
|
||||
if max((len(x) for x in xs), default=0) > 6:
|
||||
ax.tick_params(axis="x", rotation=25)
|
||||
ax.set_title(chart.get("title", ""))
|
||||
fig.tight_layout()
|
||||
name = uuid.uuid4().hex[:12] + ".png"
|
||||
fig.savefig(os.path.join(CHART_DIR, name), facecolor="white")
|
||||
plt.close(fig)
|
||||
return PUBLIC_BASE + "/charts/" + name
|
||||
|
||||
|
||||
def chart_block(spec):
|
||||
rows = spec["rows"]
|
||||
cols = spec["columns"]
|
||||
@@ -260,8 +301,11 @@ def chart_block(spec):
|
||||
ys = [float(r[yi] or 0) for r in rows]
|
||||
top = max(range(len(ys)), key=lambda i: ys[i]) if ys else 0
|
||||
summary = (f"**数据摘要**({len(rows)} 行):合计 {_fmt(sum(ys))};"
|
||||
f"最高 {rows[top][xi]} {_fmt(ys[top])}。明细见上表;图表点代码块右上“预览”在工件面板查看。")
|
||||
return summary + "\n\n```html\n" + svg_chart(spec) + "\n```"
|
||||
f"最高 {rows[top][xi]} {_fmt(ys[top])}。明细见上表,图表见下图;"
|
||||
f"HTML 工件默认折叠,点代码块右上“预览”可在工件面板查看。")
|
||||
img = chart_image(spec)
|
||||
title = _esc(spec["chart"].get("title", "图表"))
|
||||
return summary + "\n\n" + f"" + "\n\n```html\n" + svg_chart(spec) + "\n```"
|
||||
|
||||
|
||||
def render(plan, cols, rows, engine, err, sources, rmode, mmode):
|
||||
|
||||
Reference in new issue
Block a user