Spaces:
Sleeping
Sleeping
| from __future__ import annotations | |
| from pathlib import Path | |
| from src.search_service import SearchResult | |
| from src.ui.constants import ( | |
| DEFAULT_CATEGORY_FILTER, | |
| DEFAULT_MIN_SIMILARITY, | |
| DEFAULT_TOP_K, | |
| IMAGE_QUERY_EMPTY_PREVIEW, | |
| TEXT_QUERY_EMPTY_PREVIEW, | |
| ) | |
| from src.ui.types import FavoriteState, GalleryItem, ResultState | |
| def initial_overview( | |
| top_k: int = DEFAULT_TOP_K, | |
| category_filter: str = DEFAULT_CATEGORY_FILTER, | |
| min_similarity: float = DEFAULT_MIN_SIMILARITY, | |
| ) -> str: | |
| return ( | |
| "### Results Overview\n\n" | |
| "Enter a description or upload a reference image, then click **Search**.\n\n" | |
| "- Current query: None\n" | |
| "- Returned results: 0 images" | |
| ) | |
| def error_overview(title: str, message: str) -> str: | |
| return f"### {title}\n\n{message}" | |
| def search_overview( | |
| *, | |
| current_query: str, | |
| returned_results: int, | |
| message: str | None = None, | |
| ) -> str: | |
| lines = [ | |
| "### Results Overview", | |
| "", | |
| f"- Current query: {current_query}", | |
| f"- Returned results: {returned_results} images", | |
| ] | |
| if message: | |
| lines.extend(["", message]) | |
| return "\n".join(lines) | |
| def result_to_state(result: SearchResult) -> ResultState: | |
| return { | |
| "id": result.id, | |
| "path": result.path, | |
| "filename": result.filename, | |
| "category": result.category, | |
| "score": result.score, | |
| "choice_label": f"{result.filename} | {result.category} | {result.score:.4f}", | |
| } | |
| def result_gallery(results: list[ResultState]) -> list[GalleryItem]: | |
| gallery: list[GalleryItem] = [] | |
| for result in results: | |
| caption = ( | |
| f"File: {result['filename']}\n" | |
| f"Category: {result['category']}\n" | |
| f"Similarity: {float(result['score']):.4f}" | |
| ) | |
| gallery.append((str(result["path"]), caption)) | |
| return gallery | |
| def favorite_gallery(favorites: list[FavoriteState] | None) -> list[GalleryItem]: | |
| gallery: list[GalleryItem] = [] | |
| for favorite in favorites or []: | |
| favorite_path = Path(str(favorite.get("favorite_path", ""))) | |
| if not favorite_path.exists(): | |
| continue | |
| caption = ( | |
| f"File: {favorite.get('filename', favorite_path.name)}\n" | |
| f"Category: {favorite.get('category', 'Unknown')}" | |
| ) | |
| gallery.append((str(favorite_path), caption)) | |
| return gallery | |
| def text_query_preview(text: str) -> str: | |
| text = (text or "").strip() | |
| if not text: | |
| return TEXT_QUERY_EMPTY_PREVIEW | |
| return f"Current query: {text}" | |
| def image_query_preview(image_present: bool) -> str: | |
| uploaded = "Yes" if image_present else "No" | |
| return f"Current query mode: image search\n\nImage uploaded: {uploaded}" | |