Spaces:
Sleeping
Sleeping
Deploy 2c79775 from hugging_face
Browse files- src/services/global_symbols.py +42 -15
src/services/global_symbols.py
CHANGED
|
@@ -13,13 +13,22 @@ Usage:
|
|
| 13 |
path = service.search_and_download("happy", "happy.png")
|
| 14 |
"""
|
| 15 |
|
|
|
|
| 16 |
import requests
|
| 17 |
from pathlib import Path
|
| 18 |
-
from typing import Optional, Union
|
| 19 |
from logging import getLogger
|
| 20 |
|
| 21 |
logger = getLogger(__name__)
|
| 22 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
class GlobalSymbolsService:
|
| 25 |
"""Query and download symbols from Global Symbols API."""
|
|
@@ -120,20 +129,38 @@ class GlobalSymbolsService:
|
|
| 120 |
Returns:
|
| 121 |
Path to the downloaded image, or None if no symbol found or download failed.
|
| 122 |
"""
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 137 |
|
| 138 |
|
| 139 |
def download_symbol(
|
|
|
|
| 13 |
path = service.search_and_download("happy", "happy.png")
|
| 14 |
"""
|
| 15 |
|
| 16 |
+
import re
|
| 17 |
import requests
|
| 18 |
from pathlib import Path
|
| 19 |
+
from typing import Iterator, Optional, Union
|
| 20 |
from logging import getLogger
|
| 21 |
|
| 22 |
logger = getLogger(__name__)
|
| 23 |
|
| 24 |
+
# Global Symbols does keyword matching, so the LLM's multi-word image prompts
|
| 25 |
+
# (e.g. "Winner trophy") usually miss. Drop these connective words when falling
|
| 26 |
+
# back to single-word queries.
|
| 27 |
+
_STOPWORDS = {
|
| 28 |
+
"a", "an", "and", "at", "by", "for", "from", "in", "is", "are", "of", "on",
|
| 29 |
+
"or", "the", "to", "with", "into", "over", "your", "their",
|
| 30 |
+
}
|
| 31 |
+
|
| 32 |
|
| 33 |
class GlobalSymbolsService:
|
| 34 |
"""Query and download symbols from Global Symbols API."""
|
|
|
|
| 129 |
Returns:
|
| 130 |
Path to the downloaded image, or None if no symbol found or download failed.
|
| 131 |
"""
|
| 132 |
+
# Try the full phrase first, then progressively simpler single-word
|
| 133 |
+
# queries, since keyword matching rarely hits on multi-word prompts.
|
| 134 |
+
for candidate in self._query_candidates(query):
|
| 135 |
+
results = self.search(candidate, symbolset=symbolset, language=language, limit=1)
|
| 136 |
+
if not results:
|
| 137 |
+
continue
|
| 138 |
+
|
| 139 |
+
image_url = (results[0].get("picto") or {}).get("image_url")
|
| 140 |
+
if not image_url:
|
| 141 |
+
continue
|
| 142 |
+
|
| 143 |
+
if candidate != query:
|
| 144 |
+
logger.info("Matched '%s' via fallback term '%s'", query, candidate)
|
| 145 |
+
return self.download_image(image_url, output_path)
|
| 146 |
+
|
| 147 |
+
logger.warning(f"No symbols found for query: '{query}' (including fallbacks)")
|
| 148 |
+
return None
|
| 149 |
+
|
| 150 |
+
@staticmethod
|
| 151 |
+
def _query_candidates(query: str) -> Iterator[str]:
|
| 152 |
+
"""Yield the full query, then each significant word (longest first)."""
|
| 153 |
+
query = (query or "").strip()
|
| 154 |
+
if not query:
|
| 155 |
+
return
|
| 156 |
+
yield query
|
| 157 |
+
|
| 158 |
+
seen = {query.lower()}
|
| 159 |
+
words = [w for w in re.findall(r"[A-Za-z]+", query) if len(w) > 2 and w.lower() not in _STOPWORDS]
|
| 160 |
+
for word in sorted(words, key=len, reverse=True):
|
| 161 |
+
if word.lower() not in seen:
|
| 162 |
+
seen.add(word.lower())
|
| 163 |
+
yield word
|
| 164 |
|
| 165 |
|
| 166 |
def download_symbol(
|