最新哦,分词策略放到cli了
This commit is contained in:
1 parent
f9f9645fa1
commit
31fba5a9e9
1 file changed
+20
-7
+20
-7
@@ -27,13 +27,21 @@ if str(_SRC) not in sys.path:
|
|||||||
from agent import chatbot_agent as ca # noqa: E402
|
from agent import chatbot_agent as ca # noqa: E402
|
||||||
from web import ui_session as ui # noqa: E402
|
from web import ui_session as ui # noqa: E402
|
||||||
from agent.utils.image_utils import IMAGE_FILE_EXTENSIONS # noqa: E402
|
from agent.utils.image_utils import IMAGE_FILE_EXTENSIONS # noqa: E402
|
||||||
from agent.utils.agent_utils import DOC_EXT_MAP # noqa: E402
|
|
||||||
|
|
||||||
_WEB_DIR = Path(__file__).resolve().parent
|
_WEB_DIR = Path(__file__).resolve().parent
|
||||||
_IMAGE_DIR = _WEB_DIR / "image"
|
_IMAGE_DIR = _WEB_DIR / "image"
|
||||||
|
|
||||||
_AUDIO_EXT = frozenset({".wav", ".mp3", ".m4a", ".flac", ".ogg", ".webm"})
|
_AUDIO_EXT = frozenset({".wav", ".mp3", ".m4a", ".flac", ".ogg", ".webm"})
|
||||||
_FILE_EXT = frozenset(DOC_EXT_MAP.keys())
|
_IMAGE_EXT = frozenset(IMAGE_FILE_EXTENSIONS)
|
||||||
|
|
||||||
|
|
||||||
|
def _infer_upload_kind(fp: str) -> str | None:
|
||||||
|
ext = os.path.splitext(fp)[1].lower()
|
||||||
|
if ext in _IMAGE_EXT:
|
||||||
|
return "image"
|
||||||
|
if ext in _AUDIO_EXT:
|
||||||
|
return "audio"
|
||||||
|
return "file"
|
||||||
|
|
||||||
|
|
||||||
def _data_url_for_file(fp: str) -> str | None:
|
def _data_url_for_file(fp: str) -> str | None:
|
||||||
@@ -84,6 +92,7 @@ def _serialize_content(content: Any, *, meta: dict[str, Any] | None = None, role
|
|||||||
parts.append({"type": "text", "text": piece.get("text") or ""})
|
parts.append({"type": "text", "text": piece.get("text") or ""})
|
||||||
continue
|
continue
|
||||||
fp = piece.get("path") or (piece.get("file") or {}).get("path")
|
fp = piece.get("path") or (piece.get("file") or {}).get("path")
|
||||||
|
display_name = piece.get("name")
|
||||||
if piece.get("type") == "image_url":
|
if piece.get("type") == "image_url":
|
||||||
fp = fp or (piece.get("image_url") or {}).get("url")
|
fp = fp or (piece.get("image_url") or {}).get("url")
|
||||||
if piece.get("type") == "audio":
|
if piece.get("type") == "audio":
|
||||||
@@ -95,19 +104,19 @@ def _serialize_content(content: Any, *, meta: dict[str, Any] | None = None, role
|
|||||||
continue
|
continue
|
||||||
ext = os.path.splitext(fp)[1].lower()
|
ext = os.path.splitext(fp)[1].lower()
|
||||||
if ext in IMAGE_FILE_EXTENSIONS:
|
if ext in IMAGE_FILE_EXTENSIONS:
|
||||||
parts.append({"type": "image", "url": data_url, "path": fp, "name": os.path.basename(fp)})
|
parts.append({"type": "image", "url": data_url, "path": fp, "name": display_name or os.path.basename(fp)})
|
||||||
elif ext in _AUDIO_EXT:
|
elif ext in _AUDIO_EXT:
|
||||||
audio_meta = meta.get("audio") if isinstance(meta.get("audio"), dict) else {}
|
audio_meta = meta.get("audio") if isinstance(meta.get("audio"), dict) else {}
|
||||||
parts.append({
|
parts.append({
|
||||||
"type": "audio",
|
"type": "audio",
|
||||||
"url": data_url,
|
"url": data_url,
|
||||||
"path": fp,
|
"path": fp,
|
||||||
"name": os.path.basename(fp),
|
"name": display_name or os.path.basename(fp),
|
||||||
"autoplay": bool(meta.get("autoplay") or audio_meta.get("autoplay")) if role == "assistant" else False,
|
"autoplay": bool(meta.get("autoplay") or audio_meta.get("autoplay")) if role == "assistant" else False,
|
||||||
"duration_seconds": audio_meta.get("duration_seconds"),
|
"duration_seconds": audio_meta.get("duration_seconds"),
|
||||||
})
|
})
|
||||||
else:
|
else:
|
||||||
parts.append({"type": "file", "url": data_url, "path": fp, "name": os.path.basename(fp)})
|
parts.append({"type": "file", "url": data_url, "path": fp, "name": display_name or os.path.basename(fp)})
|
||||||
return parts
|
return parts
|
||||||
|
|
||||||
|
|
||||||
@@ -244,6 +253,7 @@ def _materialize_uploads_to_temp(
|
|||||||
"""将上传文件落盘到临时目录,返回 ``(query_dict, tmp_root)``;调用方须在 finally 中删除 ``tmp_root``。"""
|
"""将上传文件落盘到临时目录,返回 ``(query_dict, tmp_root)``;调用方须在 finally 中删除 ``tmp_root``。"""
|
||||||
tmp_root = tempfile.mkdtemp(prefix="web_mm_")
|
tmp_root = tempfile.mkdtemp(prefix="web_mm_")
|
||||||
file_paths: list[str] = []
|
file_paths: list[str] = []
|
||||||
|
file_names: list[str] = []
|
||||||
for uf in images or []:
|
for uf in images or []:
|
||||||
if uf is None or not uf.filename:
|
if uf is None or not uf.filename:
|
||||||
continue
|
continue
|
||||||
@@ -252,6 +262,7 @@ def _materialize_uploads_to_temp(
|
|||||||
with open(dest, "wb") as f:
|
with open(dest, "wb") as f:
|
||||||
shutil.copyfileobj(uf.file, f)
|
shutil.copyfileobj(uf.file, f)
|
||||||
file_paths.append(dest)
|
file_paths.append(dest)
|
||||||
|
file_names.append(uf.filename)
|
||||||
|
|
||||||
for uf in files or []:
|
for uf in files or []:
|
||||||
if uf is None or not uf.filename:
|
if uf is None or not uf.filename:
|
||||||
@@ -261,6 +272,7 @@ def _materialize_uploads_to_temp(
|
|||||||
with open(dest, "wb") as f:
|
with open(dest, "wb") as f:
|
||||||
shutil.copyfileobj(uf.file, f)
|
shutil.copyfileobj(uf.file, f)
|
||||||
file_paths.append(dest)
|
file_paths.append(dest)
|
||||||
|
file_names.append(uf.filename)
|
||||||
|
|
||||||
if audio is not None and audio.filename:
|
if audio is not None and audio.filename:
|
||||||
ext = Path(audio.filename).suffix or ".bin"
|
ext = Path(audio.filename).suffix or ".bin"
|
||||||
@@ -268,8 +280,9 @@ def _materialize_uploads_to_temp(
|
|||||||
with open(dest, "wb") as f:
|
with open(dest, "wb") as f:
|
||||||
shutil.copyfileobj(audio.file, f)
|
shutil.copyfileobj(audio.file, f)
|
||||||
file_paths.append(dest)
|
file_paths.append(dest)
|
||||||
|
file_names.append(audio.filename)
|
||||||
|
|
||||||
query = {"text": (text or "").strip(), "files": file_paths}
|
query = {"text": (text or "").strip(), "files": file_paths, "file_names": file_names}
|
||||||
return query, tmp_root
|
return query, tmp_root
|
||||||
|
|
||||||
|
|
||||||
@@ -417,4 +430,4 @@ def serve_css() -> FileResponse:
|
|||||||
|
|
||||||
@app.get("/app.js")
|
@app.get("/app.js")
|
||||||
def serve_js() -> FileResponse:
|
def serve_js() -> FileResponse:
|
||||||
return FileResponse(_WEB_DIR / "app.js")
|
return FileResponse(_WEB_DIR / "app.js")
|
||||||
Reference in new issue
Block a user