feat: FastAPI+SSE API server, JRXML auto-reorder, session integrity fixes
This commit is contained in:
@@ -17,13 +17,16 @@ try:
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
import base64
|
||||
import tempfile
|
||||
import time
|
||||
from pathlib import Path
|
||||
|
||||
import streamlit as st
|
||||
import streamlit.components.v1 as components
|
||||
|
||||
from dotenv import load_dotenv
|
||||
load_dotenv()
|
||||
load_dotenv(override=True)
|
||||
|
||||
from agent.graph import build_graph, create_initial_state
|
||||
from backend.session import (
|
||||
@@ -109,86 +112,6 @@ def _render_jrxml(jrxml: str, max_lines: int = 30):
|
||||
st.code(preview, language="xml")
|
||||
|
||||
|
||||
# ---- 共享文件上传处理 ----
|
||||
def _process_uploaded_file(uploaded_file, suffix: str) -> dict:
|
||||
"""处理单个上传文件:保存临时文件、解析、布局分析。
|
||||
|
||||
返回: {"name": str, "text": str, "type": str, "tmp_path": str|None}
|
||||
"""
|
||||
import tempfile
|
||||
from backend.file_parser import parse_file
|
||||
from backend.layout_analyzer import analyze_layout
|
||||
|
||||
with tempfile.NamedTemporaryFile(suffix=suffix, delete=False) as tmp:
|
||||
tmp.write(uploaded_file.getvalue())
|
||||
tmp_path = tmp.name
|
||||
|
||||
result = parse_file(tmp_path, suffix)
|
||||
parsed_text = result["text"]
|
||||
parsed_type = result["file_type"]
|
||||
|
||||
# 对图片/PDF 进行 A4 模板布局分析
|
||||
if suffix in (".png", ".jpg", ".jpeg", ".bmp", ".webp", ".pdf"):
|
||||
layout = analyze_layout(tmp_path)
|
||||
tt = layout.get("template_type", "unknown")
|
||||
current_jrxml = st.session_state.agent_state.get("current_jrxml", "")
|
||||
|
||||
if tt == "full_a4":
|
||||
parsed_text = layout["description"]
|
||||
parsed_type = "a4_template"
|
||||
# 存储布局 schema 供分层精确生成使用
|
||||
from backend.layout_analyzer import extract_layout_schema
|
||||
schema = extract_layout_schema(layout)
|
||||
st.session_state.agent_state["layout_schema"] = schema
|
||||
st.session_state.agent_state["ocr_elements"] = layout.get("rows", [])
|
||||
elif tt == "partial_rows":
|
||||
parsed_type = "a4_partial"
|
||||
if current_jrxml.strip():
|
||||
from backend.layout_analyzer import match_rows_to_jrxml
|
||||
match = match_rows_to_jrxml(layout, current_jrxml)
|
||||
parsed_text = (
|
||||
f"[行片段修改] 上传图片包含 {layout['total_rows']} 行,"
|
||||
f"视为 A4 报表的一部分。\n\n"
|
||||
f"{match['description']}\n\n"
|
||||
f"--- 行结构 ---\n{layout['description']}"
|
||||
)
|
||||
else:
|
||||
parsed_text = layout["description"]
|
||||
else:
|
||||
has_ocr = result.get("method") not in ("metadata_only", None)
|
||||
img_w, img_h = layout["image_size"]
|
||||
ratio = layout["aspect_ratio"]
|
||||
if has_ocr:
|
||||
parsed_text = (
|
||||
f"[图片上传] 尺寸 {img_w}x{img_h}px, 比例 {ratio}。"
|
||||
f"未检测到 A4 报表结构,图片将被视为参考样式。\n"
|
||||
f"请根据用户的文字描述生成报表。"
|
||||
)
|
||||
else:
|
||||
parsed_text = (
|
||||
f"[图片上传] 尺寸 {img_w}x{img_h}px, 比例 {ratio}。\n"
|
||||
f"⚠ OCR 引擎未安装,无法识别图片中的文字内容。\n"
|
||||
f"请严格根据用户的文字描述来推断图片中的报表需求。\n"
|
||||
f"(提示:如需图片文字识别,请运行 pip install paddleocr)"
|
||||
)
|
||||
parsed_type = "image_reference"
|
||||
|
||||
elif suffix in (".pdf", ".docx", ".xlsx", ".xls", ".doc"):
|
||||
parsed_type = suffix.lstrip(".")
|
||||
|
||||
keep_temp = (
|
||||
suffix in (".png", ".jpg", ".jpeg", ".bmp", ".webp")
|
||||
and result.get("method") not in ("metadata_only", None)
|
||||
)
|
||||
|
||||
return {
|
||||
"name": uploaded_file.name,
|
||||
"text": parsed_text,
|
||||
"type": parsed_type,
|
||||
"tmp_path": tmp_path if keep_temp else None,
|
||||
}
|
||||
|
||||
|
||||
# ---- URL 参数 ----
|
||||
query_params = st.query_params
|
||||
url_session_id = query_params.get("session_id", "")
|
||||
@@ -344,14 +267,6 @@ def run_agent(user_input: str):
|
||||
if stream_active:
|
||||
streaming_placeholder.empty()
|
||||
|
||||
# 清理已处理的临时文件
|
||||
for p in st.session_state.get("uploaded_temp_paths", []):
|
||||
try:
|
||||
Path(p).unlink(missing_ok=True)
|
||||
except Exception:
|
||||
pass
|
||||
st.session_state.uploaded_temp_paths = []
|
||||
|
||||
# ---- 总结卡片 ----
|
||||
# 注:node_state 只含变更字段,用 agent_state(被所有节点就地修改)获取完整状态
|
||||
final_state = agent_state
|
||||
@@ -557,62 +472,12 @@ with st.sidebar:
|
||||
run_agent("重新来,清空当前报表")
|
||||
st.rerun()
|
||||
|
||||
st.divider()
|
||||
st.markdown("### 上传文件")
|
||||
st.caption("支持图片 (OCR)、PDF、Word、文本文件。内容将附加到您的下一条消息中。")
|
||||
|
||||
if "uploaded_files" not in st.session_state:
|
||||
st.session_state.uploaded_files = [] # [{name, text, type}]
|
||||
|
||||
if "uploaded_temp_paths" not in st.session_state:
|
||||
st.session_state.uploaded_temp_paths = [] # 待清理的临时文件路径
|
||||
|
||||
uploaded = st.file_uploader(
|
||||
"选择文件",
|
||||
type=["png", "jpg", "jpeg", "bmp", "webp", "pdf", "docx", "xlsx", "xls", "doc",
|
||||
"txt", "csv", "json", "xml"],
|
||||
accept_multiple_files=True,
|
||||
key="file_uploader",
|
||||
label_visibility="collapsed",
|
||||
)
|
||||
|
||||
if uploaded:
|
||||
for uf in uploaded:
|
||||
# 去重
|
||||
if any(f["name"] == uf.name for f in st.session_state.uploaded_files):
|
||||
continue
|
||||
|
||||
suffix = Path(uf.name).suffix.lower()
|
||||
result = _process_uploaded_file(uf, suffix)
|
||||
|
||||
if result["text"]:
|
||||
st.session_state.uploaded_files.append({
|
||||
"name": result["name"],
|
||||
"text": result["text"],
|
||||
"type": result["type"],
|
||||
})
|
||||
|
||||
tmp_path = result["tmp_path"]
|
||||
if tmp_path:
|
||||
st.session_state.agent_state["uploaded_file_path"] = tmp_path
|
||||
st.session_state.uploaded_temp_paths.append(tmp_path)
|
||||
|
||||
if st.session_state.uploaded_files:
|
||||
for i, f in enumerate(st.session_state.uploaded_files):
|
||||
cols = st.columns([5, 1])
|
||||
with cols[0]:
|
||||
st.caption(f"📎 {f['name']} ({f['type']}, {len(f['text'])} 字符)")
|
||||
with cols[1]:
|
||||
if st.button("✕", key=f"rm_uf_{i}", help="移除"):
|
||||
st.session_state.uploaded_files.pop(i)
|
||||
st.rerun()
|
||||
|
||||
st.divider()
|
||||
st.markdown("### 配置")
|
||||
llm_backend = os.getenv("LLM_BACKEND", "cloud")
|
||||
llm_model = os.getenv("LLM_MODEL", os.getenv("LOCAL_LLM_MODEL", "gpt-4o"))
|
||||
st.caption(f"大语言模型: {llm_backend} / {llm_model}")
|
||||
st.caption(f"最大重试次数: {os.getenv('MAX_RETRY', '3')}")
|
||||
st.caption(f"最大重试次数: {os.getenv('MAX_RETRY', '5')}")
|
||||
st.caption(f"验证服务: {os.getenv('VALIDATION_SERVICE_URL', 'http://localhost:8001/validate')}")
|
||||
|
||||
st.divider()
|
||||
@@ -666,106 +531,396 @@ for msg in st.session_state.messages:
|
||||
else:
|
||||
st.markdown(msg["content"])
|
||||
|
||||
# ---- 聊天输入(支持粘贴/拖拽文件) ----
|
||||
from st_multimodal_chatinput import multimodal_chatinput
|
||||
import base64
|
||||
import io
|
||||
from pathlib import Path as _Path
|
||||
# ---- 统一聊天输入组件 ----
|
||||
UNIFIED_CHAT_HTML = r"""
|
||||
<!DOCTYPE html>
|
||||
<html lang="zh-CN">
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<style>
|
||||
* { box-sizing: border-box; margin: 0; padding: 0; }
|
||||
body {
|
||||
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
|
||||
background: transparent;
|
||||
padding: 4px 0;
|
||||
}
|
||||
.chat-container {
|
||||
position: relative;
|
||||
border: 1px solid #d1d5db;
|
||||
border-radius: 12px;
|
||||
padding: 8px 12px;
|
||||
background: #ffffff;
|
||||
transition: border-color 0.2s, box-shadow 0.2s;
|
||||
}
|
||||
.chat-container:focus-within {
|
||||
border-color: #3b82f6;
|
||||
box-shadow: 0 0 0 2px rgba(59,130,246,0.15);
|
||||
}
|
||||
.chat-container.drag-active {
|
||||
border-color: #3b82f6;
|
||||
background: rgba(59,130,246,0.04);
|
||||
}
|
||||
.file-chips {
|
||||
display: flex;
|
||||
flex-wrap: wrap;
|
||||
gap: 6px;
|
||||
margin-bottom: 6px;
|
||||
}
|
||||
.file-chips:empty { display: none; }
|
||||
.file-chip {
|
||||
display: inline-flex;
|
||||
align-items: center;
|
||||
gap: 4px;
|
||||
padding: 2px 8px;
|
||||
background: #f3f4f6;
|
||||
border-radius: 14px;
|
||||
font-size: 12px;
|
||||
color: #374151;
|
||||
max-width: 200px;
|
||||
}
|
||||
.file-chip .chip-icon { font-size: 13px; }
|
||||
.file-chip .chip-name {
|
||||
overflow: hidden;
|
||||
text-overflow: ellipsis;
|
||||
white-space: nowrap;
|
||||
}
|
||||
.file-chip .chip-remove {
|
||||
border: none;
|
||||
background: none;
|
||||
cursor: pointer;
|
||||
color: #9ca3af;
|
||||
font-size: 14px;
|
||||
line-height: 1;
|
||||
padding: 0 2px;
|
||||
flex-shrink: 0;
|
||||
}
|
||||
.file-chip .chip-remove:hover { color: #ef4444; }
|
||||
.input-row {
|
||||
display: flex;
|
||||
align-items: flex-end;
|
||||
gap: 8px;
|
||||
}
|
||||
.attach-btn {
|
||||
border: none;
|
||||
background: none;
|
||||
cursor: pointer;
|
||||
padding: 4px 6px;
|
||||
font-size: 20px;
|
||||
line-height: 1;
|
||||
color: #6b7280;
|
||||
border-radius: 6px;
|
||||
transition: background 0.15s, color 0.15s;
|
||||
flex-shrink: 0;
|
||||
}
|
||||
.attach-btn:hover { background: #f3f4f6; color: #374151; }
|
||||
textarea {
|
||||
flex: 1;
|
||||
border: none;
|
||||
outline: none;
|
||||
resize: none;
|
||||
font-size: 15px;
|
||||
line-height: 1.5;
|
||||
font-family: inherit;
|
||||
color: #111827;
|
||||
background: transparent;
|
||||
padding: 4px 0;
|
||||
min-height: 24px;
|
||||
max-height: 120px;
|
||||
overflow-y: auto;
|
||||
}
|
||||
textarea::placeholder { color: #9ca3af; }
|
||||
.send-btn {
|
||||
border: none;
|
||||
cursor: pointer;
|
||||
padding: 4px 10px;
|
||||
font-size: 16px;
|
||||
background: #e5e7eb;
|
||||
color: #9ca3af;
|
||||
border-radius: 8px;
|
||||
transition: all 0.15s;
|
||||
flex-shrink: 0;
|
||||
}
|
||||
.send-btn.active { background: #3b82f6; color: #fff; }
|
||||
.send-btn.active:hover { background: #2563eb; }
|
||||
.send-btn:disabled { opacity: 0.5; cursor: default; }
|
||||
.error-toast {
|
||||
position: fixed;
|
||||
bottom: 12px;
|
||||
left: 50%;
|
||||
transform: translateX(-50%);
|
||||
background: #ef4444;
|
||||
color: #fff;
|
||||
padding: 6px 16px;
|
||||
border-radius: 8px;
|
||||
font-size: 13px;
|
||||
z-index: 9999;
|
||||
animation: toastOut 2.5s forwards;
|
||||
pointer-events: none;
|
||||
}
|
||||
@keyframes toastOut {
|
||||
0%, 70% { opacity: 1; }
|
||||
100% { opacity: 0; }
|
||||
}
|
||||
|
||||
# MIME type → 文件扩展名映射(用于剪贴板粘贴无扩展名的文件)
|
||||
MIME_TO_EXT = {
|
||||
"image/png": ".png",
|
||||
"image/jpeg": ".jpg",
|
||||
"image/bmp": ".bmp",
|
||||
"image/webp": ".webp",
|
||||
"application/pdf": ".pdf",
|
||||
"application/vnd.openxmlformats-officedocument.wordprocessingml.document": ".docx",
|
||||
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet": ".xlsx",
|
||||
"application/vnd.ms-excel": ".xls",
|
||||
"application/msword": ".doc",
|
||||
"text/plain": ".txt",
|
||||
"text/csv": ".csv",
|
||||
"application/json": ".json",
|
||||
"text/xml": ".xml",
|
||||
}
|
||||
@media (prefers-color-scheme: dark) {
|
||||
.chat-container { background: #1f2937; border-color: #374151; }
|
||||
.chat-container:focus-within { border-color: #3b82f6; }
|
||||
.file-chip { background: #374151; color: #e5e7eb; }
|
||||
.file-chip .chip-remove { color: #6b7280; }
|
||||
.attach-btn { color: #9ca3af; }
|
||||
.attach-btn:hover { background: #374151; color: #e5e7eb; }
|
||||
textarea { color: #f9fafb; }
|
||||
textarea::placeholder { color: #6b7280; }
|
||||
.send-btn { background: #374151; }
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div class="chat-container" id="container">
|
||||
<div class="file-chips" id="chips"></div>
|
||||
<div class="input-row">
|
||||
<button class="attach-btn" id="attachBtn" title="附加文件">📎</button>
|
||||
<textarea id="textInput" placeholder="描述您的报表需求..." rows="1"></textarea>
|
||||
<button class="send-btn" id="sendBtn" title="发送">➤</button>
|
||||
</div>
|
||||
<input type="file" id="fileInput" multiple hidden
|
||||
accept=".png,.jpg,.jpeg,.bmp,.webp,.pdf,.docx,.xlsx,.xls,.doc,.txt">
|
||||
</div>
|
||||
<script>
|
||||
const container = document.getElementById('container');
|
||||
const chipsEl = document.getElementById('chips');
|
||||
const textInput = document.getElementById('textInput');
|
||||
const sendBtn = document.getElementById('sendBtn');
|
||||
const attachBtn = document.getElementById('attachBtn');
|
||||
const fileInput = document.getElementById('fileInput');
|
||||
|
||||
chat_result = multimodal_chatinput()
|
||||
if chat_result:
|
||||
prompt = (chat_result.get("textInput") or "").strip()
|
||||
chat_files = chat_result.get("uploadedFiles") or []
|
||||
let attachedFiles = [];
|
||||
const MAX_FILES = 10;
|
||||
const MAX_SIZE = 20 * 1024 * 1024;
|
||||
|
||||
# 处理聊天中上传/粘贴的文件
|
||||
uploaded_texts = []
|
||||
uploaded_files_info = []
|
||||
function getIcon(type) {
|
||||
if (type.startsWith('image/')) return '🖼';
|
||||
if (type.includes('pdf')) return '📄';
|
||||
if (type.includes('document')) return '📝';
|
||||
if (type.includes('spreadsheet') || type.includes('excel')) return '📊';
|
||||
return '📎';
|
||||
}
|
||||
|
||||
# 先收集侧边栏已上传的文件
|
||||
if st.session_state.get("uploaded_files"):
|
||||
for f in st.session_state.uploaded_files:
|
||||
uploaded_texts.append(f"[上传文件: {f['name']}]\n{f['text']}")
|
||||
uploaded_files_info.append({"name": f["name"], "type": f["type"], "length": len(f["text"])})
|
||||
st.session_state.uploaded_files = []
|
||||
function updateSendBtn() {
|
||||
var canSend = textInput.value.trim() || attachedFiles.length > 0;
|
||||
sendBtn.classList.toggle('active', canSend);
|
||||
}
|
||||
|
||||
# 处理聊天中的文件
|
||||
class _Base64File:
|
||||
"""包装 base64 文件为类 UploadedFile 接口。"""
|
||||
def __init__(self, name, data_bytes):
|
||||
self.name = name
|
||||
self._data = data_bytes
|
||||
function renderChips() {
|
||||
chipsEl.innerHTML = '';
|
||||
attachedFiles.forEach(function(f, i) {
|
||||
var chip = document.createElement('span');
|
||||
chip.className = 'file-chip';
|
||||
var name = f.name.length > 16 ? f.name.slice(0,14)+'..' : f.name;
|
||||
chip.innerHTML = '<span class="chip-icon">'+getIcon(f.type)+'</span>' +
|
||||
'<span class="chip-name">'+name+'</span>' +
|
||||
'<button class="chip-remove">×</button>';
|
||||
chip.querySelector('.chip-remove').onclick = (function(idx) {
|
||||
return function() {
|
||||
attachedFiles.splice(idx, 1);
|
||||
renderChips();
|
||||
updateSendBtn();
|
||||
};
|
||||
})(i);
|
||||
chipsEl.appendChild(chip);
|
||||
});
|
||||
updateSendBtn();
|
||||
}
|
||||
|
||||
def getvalue(self):
|
||||
return self._data
|
||||
function addFiles(fileList) {
|
||||
for (var i = 0; i < fileList.length; i++) {
|
||||
var file = fileList[i];
|
||||
if (attachedFiles.length >= MAX_FILES) { showToast('最多附加 '+MAX_FILES+' 个文件'); break; }
|
||||
if (file.size > MAX_SIZE) { showToast(file.name+' 超过 20MB 限制'); continue; }
|
||||
if (attachedFiles.some(function(f) { return f.name === file.name && f.size === file.size; })) continue;
|
||||
attachedFiles.push({name: file.name, type: file.type, file: file});
|
||||
}
|
||||
renderChips();
|
||||
}
|
||||
|
||||
for cf in chat_files:
|
||||
name = cf.get("name", "clipboard_file")
|
||||
mime = cf.get("type", "")
|
||||
content_b64 = cf.get("content", "")
|
||||
if not content_b64:
|
||||
continue
|
||||
function showToast(msg) {
|
||||
var t = document.createElement('div');
|
||||
t.className = 'error-toast';
|
||||
t.textContent = msg;
|
||||
document.body.appendChild(t);
|
||||
setTimeout(function() { t.remove(); }, 2600);
|
||||
}
|
||||
|
||||
function readFile(file) {
|
||||
return new Promise(function(resolve, reject) {
|
||||
var reader = new FileReader();
|
||||
reader.onload = function() { resolve(reader.result); };
|
||||
reader.onerror = reject;
|
||||
reader.readAsDataURL(file);
|
||||
});
|
||||
}
|
||||
|
||||
async function handleSend() {
|
||||
var text = textInput.value.trim();
|
||||
if (!text && attachedFiles.length === 0) return;
|
||||
|
||||
sendBtn.disabled = true;
|
||||
var files = [];
|
||||
for (var i = 0; i < attachedFiles.length; i++) {
|
||||
var f = attachedFiles[i];
|
||||
try {
|
||||
var dataUrl = await readFile(f.file);
|
||||
files.push({name: f.name, type: f.type, data: dataUrl, size: f.file.size});
|
||||
} catch(e) {
|
||||
showToast(f.name+' 读取失败');
|
||||
}
|
||||
}
|
||||
|
||||
Streamlit.setComponentValue({text: text, files: files});
|
||||
|
||||
textInput.value = '';
|
||||
attachedFiles = [];
|
||||
renderChips();
|
||||
sendBtn.disabled = false;
|
||||
textInput.style.height = 'auto';
|
||||
}
|
||||
|
||||
attachBtn.onclick = function() { fileInput.click(); };
|
||||
fileInput.onchange = function() { addFiles(fileInput.files); fileInput.value = ''; };
|
||||
|
||||
textInput.oninput = function() {
|
||||
updateSendBtn();
|
||||
textInput.style.height = 'auto';
|
||||
textInput.style.height = Math.min(textInput.scrollHeight, 120) + 'px';
|
||||
};
|
||||
|
||||
textInput.onkeydown = function(e) {
|
||||
if (e.key === 'Enter' && !e.shiftKey) {
|
||||
e.preventDefault();
|
||||
handleSend();
|
||||
}
|
||||
};
|
||||
|
||||
sendBtn.onclick = handleSend;
|
||||
|
||||
document.addEventListener('paste', function(e) {
|
||||
var items = e.clipboardData && e.clipboardData.items;
|
||||
if (!items) return;
|
||||
var files = [];
|
||||
for (var i = 0; i < items.length; i++) {
|
||||
if (items[i].kind === 'file') files.push(items[i].getAsFile());
|
||||
}
|
||||
if (files.length) { e.preventDefault(); addFiles(files); }
|
||||
});
|
||||
|
||||
var containerDiv = document.getElementById('container');
|
||||
containerDiv.addEventListener('dragover', function(e) {
|
||||
e.preventDefault();
|
||||
containerDiv.classList.add('drag-active');
|
||||
});
|
||||
containerDiv.addEventListener('dragleave', function() {
|
||||
containerDiv.classList.remove('drag-active');
|
||||
});
|
||||
containerDiv.addEventListener('drop', function(e) {
|
||||
e.preventDefault();
|
||||
containerDiv.classList.remove('drag-active');
|
||||
addFiles(e.dataTransfer.files);
|
||||
});
|
||||
|
||||
updateSendBtn();
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
"""
|
||||
|
||||
chat_result = components.html(UNIFIED_CHAT_HTML, height=180)
|
||||
|
||||
if chat_result and isinstance(chat_result, dict):
|
||||
prompt = chat_result.get("text", "")
|
||||
files = chat_result.get("files", [])
|
||||
|
||||
from backend.file_parser import parse_file
|
||||
from backend.layout_analyzer import analyze_layout, extract_layout_schema
|
||||
|
||||
file_texts = []
|
||||
attached_info = []
|
||||
first_image_path = None
|
||||
temp_paths = []
|
||||
|
||||
for f in files:
|
||||
header, b64data = f.get("data", ",").split(",", 1)
|
||||
raw = base64.b64decode(b64data)
|
||||
|
||||
mime = f.get("type", "")
|
||||
mime_to_suffix = {
|
||||
"image/png": ".png", "image/jpeg": ".jpg", "image/bmp": ".bmp",
|
||||
"image/webp": ".webp", "application/pdf": ".pdf",
|
||||
"application/vnd.openxmlformats-officedocument.wordprocessingml.document": ".docx",
|
||||
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet": ".xlsx",
|
||||
"application/vnd.ms-excel": ".xls", "application/msword": ".doc",
|
||||
"text/plain": ".txt",
|
||||
}
|
||||
suffix = mime_to_suffix.get(mime, Path(f["name"]).suffix.lower())
|
||||
|
||||
with tempfile.NamedTemporaryFile(suffix=suffix, delete=False) as tmp:
|
||||
tmp.write(raw)
|
||||
tmp_path = tmp.name
|
||||
temp_paths.append(tmp_path)
|
||||
|
||||
result = parse_file(tmp_path, suffix)
|
||||
text = result["text"]
|
||||
file_type = result["file_type"]
|
||||
|
||||
img_suffixes = (".png", ".jpg", ".jpeg", ".bmp", ".webp")
|
||||
if suffix in img_suffixes and result.get("method") not in ("metadata_only", None):
|
||||
try:
|
||||
layout = analyze_layout(tmp_path)
|
||||
tt = layout.get("template_type", "unknown")
|
||||
if tt == "full_a4":
|
||||
text = layout["description"]
|
||||
file_type = "a4_template"
|
||||
schema = extract_layout_schema(layout)
|
||||
st.session_state.agent_state["layout_schema"] = schema
|
||||
st.session_state.agent_state["ocr_elements"] = layout.get("rows", [])
|
||||
elif tt == "partial_rows":
|
||||
file_type = "a4_partial"
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
file_texts.append(f"[附加文件: {f['name']} ({file_type})]\n{text}")
|
||||
attached_info.append({"name": f["name"], "type": file_type, "length": len(text)})
|
||||
|
||||
if not first_image_path and file_type in ("image", "a4_template", "a4_partial"):
|
||||
first_image_path = tmp_path
|
||||
|
||||
if file_texts:
|
||||
full_prompt = "\n\n".join(file_texts) + "\n\n---\n用户需求:\n" + prompt
|
||||
else:
|
||||
full_prompt = prompt
|
||||
|
||||
if first_image_path:
|
||||
st.session_state.agent_state["uploaded_file_path"] = first_image_path
|
||||
|
||||
_app_log.info(
|
||||
"收到用户输入",
|
||||
extra={
|
||||
"session_id": current_session_id,
|
||||
"prompt_preview": prompt[:200],
|
||||
"prompt_length": len(prompt),
|
||||
"has_uploaded_files": bool(attached_info),
|
||||
"uploaded_files": attached_info,
|
||||
},
|
||||
)
|
||||
|
||||
st.session_state.messages.append({"role": "user", "content": prompt})
|
||||
with st.chat_message("user"):
|
||||
st.markdown(prompt)
|
||||
run_agent(full_prompt)
|
||||
|
||||
for p in temp_paths:
|
||||
try:
|
||||
data = base64.b64decode(content_b64)
|
||||
Path(p).unlink(missing_ok=True)
|
||||
except Exception:
|
||||
continue
|
||||
pass
|
||||
|
||||
suffix = _Path(name).suffix.lower()
|
||||
if not suffix and mime in MIME_TO_EXT:
|
||||
suffix = MIME_TO_EXT[mime]
|
||||
name = f"{_Path(name).stem}{suffix}"
|
||||
|
||||
wrapper = _Base64File(name, data)
|
||||
result = _process_uploaded_file(wrapper, suffix)
|
||||
|
||||
if result["text"]:
|
||||
uploaded_texts.append(f"[上传文件: {result['name']}]\n{result['text']}")
|
||||
uploaded_files_info.append({"name": result["name"], "type": result["type"], "length": len(result["text"])})
|
||||
|
||||
tmp_path = result["tmp_path"]
|
||||
if tmp_path:
|
||||
st.session_state.agent_state["uploaded_file_path"] = tmp_path
|
||||
st.session_state.uploaded_temp_paths.append(tmp_path)
|
||||
|
||||
if prompt or uploaded_texts:
|
||||
if uploaded_texts:
|
||||
full_prompt = "\n\n".join(uploaded_texts)
|
||||
if prompt:
|
||||
full_prompt += "\n\n---\n用户需求:\n" + prompt
|
||||
else:
|
||||
full_prompt = prompt
|
||||
|
||||
displayed_prompt = prompt or "(已上传文件,未输入文字)"
|
||||
|
||||
_app_log.info(
|
||||
"收到用户输入",
|
||||
extra={
|
||||
"session_id": current_session_id,
|
||||
"prompt_preview": displayed_prompt[:200],
|
||||
"prompt_length": len(full_prompt),
|
||||
"has_uploaded_files": bool(uploaded_files_info),
|
||||
"uploaded_files": uploaded_files_info,
|
||||
},
|
||||
)
|
||||
|
||||
st.session_state.messages.append({"role": "user", "content": displayed_prompt})
|
||||
with st.chat_message("user"):
|
||||
st.markdown(displayed_prompt)
|
||||
run_agent(full_prompt)
|
||||
st.rerun()
|
||||
st.rerun()
|
||||
|
||||
Reference in New Issue
Block a user