| from __future__ import annotations |
|
|
| import os |
| from pathlib import Path |
| from typing import Any |
|
|
| import gradio as gr |
|
|
| from src.config import ALL_CATEGORIES_LABEL, FAVORITES_ZIP_PATH, PROJECT_ROOT |
|
|
|
|
| APP_TITLE = "Visual Search | CLIP-based Image Retrieval System" |
| APP_NAME = "Visual Search" |
| APP_SUBTITLE = "Search semantically similar images with a text description or a reference image." |
|
|
| CSS_PATH = PROJECT_ROOT / "assets" / "custom.css" |
| FAVORITE_MODAL_JS_PATH = PROJECT_ROOT / "assets" / "favorite_modal.js" |
| ZIP_PATH = FAVORITES_ZIP_PATH |
|
|
| DEFAULT_TOP_K = 6 |
| DEFAULT_CATEGORY_FILTER = ALL_CATEGORIES_LABEL |
| DEFAULT_MIN_SIMILARITY = 0.0 |
| MAX_TOP_K = 20 |
|
|
| TEXT_QUERY_EMPTY_PREVIEW = "Current query: no image description entered." |
| IMAGE_QUERY_EMPTY_PREVIEW = "Current query mode: image search\n\nImage uploaded: No" |
| NO_FAVORITES_STATUS = "No favorite images yet." |
|
|
| EXAMPLE_QUERIES = [ |
| "red apple", |
| "banana", |
| "milk bottle", |
| "green vegetables", |
| "bread", |
| "tomato", |
| "orange", |
| ] |
|
|
| THEME = gr.themes.Soft( |
| primary_hue="blue", |
| neutral_hue="slate", |
| radius_size="lg", |
| spacing_size="lg", |
| ) |
|
|
|
|
| def favorite_modal_head(js_path: Path = FAVORITE_MODAL_JS_PATH) -> str: |
| script = js_path.read_text(encoding="utf-8") |
| return f"<script>{script}</script>" |
|
|
|
|
| def gradio_launch_options() -> tuple[dict[str, Any], dict[str, Any]]: |
| blocks_kwargs: dict[str, Any] = {"title": APP_TITLE, "fill_width": True} |
| launch_kwargs: dict[str, Any] = {} |
| head = favorite_modal_head() |
| major_version = int(str(getattr(gr, "__version__", "0")).split(".", 1)[0]) |
| if major_version >= 6: |
| launch_kwargs.update({"theme": THEME, "css_paths": CSS_PATH, "head": head}) |
| else: |
| blocks_kwargs.update({"theme": THEME, "css_paths": CSS_PATH, "head": head}) |
| if os.getenv("SPACE_ID"): |
| launch_kwargs["server_name"] = "0.0.0.0" |
| try: |
| launch_kwargs["server_port"] = int(os.getenv("PORT", "7860")) |
| except ValueError: |
| launch_kwargs["server_port"] = 7860 |
| return blocks_kwargs, launch_kwargs |
|
|