Persist upload OCR captions in gallery

This commit is contained in:
pewdiepie-archdaemon
2026-06-29 01:45:05 +00:00
parent 3b6d771be9
commit 402a2771b3
2 changed files with 39 additions and 0 deletions
+31
View File
@@ -29,6 +29,34 @@ from src.youtube_handler import (
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
def _sync_upload_vision_to_gallery(file_info: Dict[str, Any], owner: Optional[str], text: str) -> None:
file_hash = (file_info or {}).get("hash")
if not file_hash or not text:
return
try:
from core.database import GalleryImage, SessionLocal
db = SessionLocal()
try:
q = db.query(GalleryImage).filter(
GalleryImage.file_hash == file_hash,
GalleryImage.is_active == True, # noqa: E712
)
if owner:
q = q.filter(GalleryImage.owner == owner)
img = q.first()
if not img:
return
img.caption = text.strip()
db.commit()
except Exception:
db.rollback()
raise
finally:
db.close()
except Exception as e:
logger.warning("Failed to sync upload vision text to gallery: %s", e)
class ChatHandler: class ChatHandler:
"""Handles chat operations for both streaming and non-streaming endpoints.""" """Handles chat operations for both streaming and non-streaming endpoints."""
@@ -207,6 +235,7 @@ class ChatHandler:
_vtext = _vf.read().strip() _vtext = _vf.read().strip()
if _vtext: if _vtext:
enhanced_message += f"\n[User-corrected caption / OCR for this image — treat as authoritative]:\n{_vtext}" enhanced_message += f"\n[User-corrected caption / OCR for this image — treat as authoritative]:\n{_vtext}"
_sync_upload_vision_to_gallery(file_info, owner, _vtext)
_m = meta_by_id.get(att_id) _m = meta_by_id.get(att_id)
if _m is not None: if _m is not None:
_m["vision"] = _vtext _m["vision"] = _vtext
@@ -226,6 +255,7 @@ class ChatHandler:
cached_desc = _vf.read().strip() cached_desc = _vf.read().strip()
if cached_desc and not cached_desc.startswith("["): if cached_desc and not cached_desc.startswith("["):
vl_desc = cached_desc vl_desc = cached_desc
_sync_upload_vision_to_gallery(file_info, owner, vl_desc)
except Exception: except Exception:
vl_desc = None vl_desc = None
if not vl_desc: if not vl_desc:
@@ -237,6 +267,7 @@ class ChatHandler:
os.makedirs(os.path.join(UPLOAD_DIR, ".vision"), exist_ok=True) os.makedirs(os.path.join(UPLOAD_DIR, ".vision"), exist_ok=True)
with open(_vcache, "w", encoding="utf-8") as _vf: with open(_vcache, "w", encoding="utf-8") as _vf:
_vf.write(vl_desc) _vf.write(vl_desc)
_sync_upload_vision_to_gallery(file_info, owner, vl_desc)
except Exception: except Exception:
pass pass
enhanced_message = f"{enhanced_message}\n\n[Image: {file_info['name']}]\n{vl_desc}" enhanced_message = f"{enhanced_message}\n\n[Image: {file_info['name']}]\n{vl_desc}"
+8
View File
@@ -92,6 +92,13 @@ function _displayHistoryContent(content) {
].join('\n'); ].join('\n');
} }
function _stripUserVisionBlocks(text) {
return String(text || '').replace(
/\n*\[Image: ([^\]]+)\]\n[\s\S]*?(?=\n*\[Image: |\n*\[Image attached: |\n*=== File: |\n*\[PDF content\]:|$)/g,
''
).trim();
}
function _historyPageLimit() { function _historyPageLimit() {
return window.innerWidth <= 768 ? HISTORY_PAGE_LIMIT_MOBILE : HISTORY_PAGE_LIMIT_DESKTOP; return window.innerWidth <= 768 ? HISTORY_PAGE_LIMIT_MOBILE : HISTORY_PAGE_LIMIT_DESKTOP;
} }
@@ -114,6 +121,7 @@ function _renderHistoryMessage(msg, modelName) {
displayContent = ''; displayContent = '';
} }
if (msg.role === 'user') { if (msg.role === 'user') {
displayContent = _stripUserVisionBlocks(displayContent);
const trimmed = displayContent.trim(); const trimmed = displayContent.trim();
if ( if (
trimmed === 'Continue where you left off' || trimmed === 'Continue where you left off' ||