Spaces:
Sleeping
Sleeping
Update services/semantic.py
Browse files- services/semantic.py +76 -80
services/semantic.py
CHANGED
|
@@ -1,107 +1,103 @@
|
|
| 1 |
from __future__ import annotations
|
| 2 |
|
| 3 |
from dataclasses import dataclass
|
| 4 |
-
from
|
|
|
|
| 5 |
|
| 6 |
-
import
|
| 7 |
|
| 8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
|
| 11 |
@dataclass
|
| 12 |
-
class
|
| 13 |
term: str
|
| 14 |
score: float
|
| 15 |
|
| 16 |
|
| 17 |
class WordVectorFallback:
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
def __init__(self, model_name: str = "glove-wiki-gigaword-50", model_path: str = "", enable_download: bool = True):
|
| 24 |
-
self.model = None
|
| 25 |
self._kind = "disabled"
|
| 26 |
-
self.
|
| 27 |
-
self._load(model_name=model_name, model_path=model_path, enable_download=enable_download)
|
| 28 |
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
self.model = None
|
| 35 |
self._kind = "unavailable"
|
| 36 |
return
|
| 37 |
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
self.
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
self.model = None
|
| 60 |
self._kind = "disabled"
|
| 61 |
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
|
|
|
| 65 |
|
| 66 |
-
def
|
| 67 |
-
if not self.available:
|
| 68 |
-
return
|
| 69 |
-
|
| 70 |
-
normalized = normalize_text(phrase)
|
| 71 |
-
tokens = [singularize(t) for t in normalized.split()]
|
| 72 |
-
vectors = []
|
| 73 |
-
for token in tokens:
|
| 74 |
-
if token in self.model:
|
| 75 |
-
vectors.append(self.model[token])
|
| 76 |
-
|
| 77 |
-
if vectors:
|
| 78 |
-
return np.mean(np.stack(vectors), axis=0)
|
| 79 |
-
|
| 80 |
-
phrase_key = normalized.replace(" ", "_")
|
| 81 |
-
if phrase_key in self.model:
|
| 82 |
-
return self.model[phrase_key]
|
| 83 |
-
|
| 84 |
-
if normalized in self.model:
|
| 85 |
-
return self.model[normalized]
|
| 86 |
-
|
| 87 |
-
return None
|
| 88 |
|
| 89 |
-
|
| 90 |
-
if not
|
| 91 |
return []
|
| 92 |
|
| 93 |
-
|
| 94 |
-
|
|
|
|
| 95 |
return []
|
| 96 |
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
for
|
| 100 |
-
|
| 101 |
-
if
|
| 102 |
continue
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from __future__ import annotations
|
| 2 |
|
| 3 |
from dataclasses import dataclass
|
| 4 |
+
from pathlib import Path
|
| 5 |
+
from typing import List, Tuple
|
| 6 |
|
| 7 |
+
from .text_utils import normalize_text, singularize, ingredient_lookup_variants
|
| 8 |
|
| 9 |
+
try:
|
| 10 |
+
import gensim.downloader as api
|
| 11 |
+
except Exception: # pragma: no cover
|
| 12 |
+
api = None
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
DEFAULT_MODEL = "glove-wiki-gigaword-50"
|
| 16 |
|
| 17 |
|
| 18 |
@dataclass
|
| 19 |
+
class SemanticCandidate:
|
| 20 |
term: str
|
| 21 |
score: float
|
| 22 |
|
| 23 |
|
| 24 |
class WordVectorFallback:
|
| 25 |
+
def __init__(self, model_name: str = DEFAULT_MODEL, model_path: str | None = None, enable_download: bool = True):
|
| 26 |
+
self.model_name = model_name or DEFAULT_MODEL
|
| 27 |
+
self.model_path = model_path
|
| 28 |
+
self.enable_download = enable_download
|
| 29 |
+
self.available = False
|
|
|
|
|
|
|
| 30 |
self._kind = "disabled"
|
| 31 |
+
self._model = None
|
|
|
|
| 32 |
|
| 33 |
+
self._load()
|
| 34 |
+
|
| 35 |
+
def _load(self) -> None:
|
| 36 |
+
if api is None:
|
| 37 |
+
self.available = False
|
|
|
|
| 38 |
self._kind = "unavailable"
|
| 39 |
return
|
| 40 |
|
| 41 |
+
try:
|
| 42 |
+
if self.model_path:
|
| 43 |
+
path = Path(self.model_path)
|
| 44 |
+
if path.exists():
|
| 45 |
+
# Keep this permissive; local path loading is optional.
|
| 46 |
+
self._model = api.load(self.model_name)
|
| 47 |
+
elif self.enable_download:
|
| 48 |
+
self._model = api.load(self.model_name)
|
| 49 |
+
else:
|
| 50 |
+
self._model = None
|
| 51 |
+
else:
|
| 52 |
+
if self.enable_download:
|
| 53 |
+
self._model = api.load(self.model_name)
|
| 54 |
+
else:
|
| 55 |
+
self._model = None
|
| 56 |
+
|
| 57 |
+
self.available = self._model is not None
|
| 58 |
+
self._kind = "glove" if self.available else "disabled"
|
| 59 |
+
except Exception:
|
| 60 |
+
self._model = None
|
| 61 |
+
self.available = False
|
|
|
|
| 62 |
self._kind = "disabled"
|
| 63 |
|
| 64 |
+
def _normalize_candidate(self, term: str) -> str:
|
| 65 |
+
term = normalize_text(term)
|
| 66 |
+
term = singularize(term)
|
| 67 |
+
return term
|
| 68 |
|
| 69 |
+
def most_similar(self, ingredient: str, topn: int = 10) -> List[Tuple[str, float]]:
|
| 70 |
+
if not self.available or self._model is None:
|
| 71 |
+
return []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
|
| 73 |
+
query = self._normalize_candidate(ingredient)
|
| 74 |
+
if not query:
|
| 75 |
return []
|
| 76 |
|
| 77 |
+
try:
|
| 78 |
+
raw = self._model.most_similar(query, topn=max(topn, 10))
|
| 79 |
+
except Exception:
|
| 80 |
return []
|
| 81 |
|
| 82 |
+
out: List[Tuple[str, float]] = []
|
| 83 |
+
seen = set()
|
| 84 |
+
for term, score in raw:
|
| 85 |
+
term = self._normalize_candidate(term.replace("_", " "))
|
| 86 |
+
if not term or term in seen:
|
| 87 |
continue
|
| 88 |
+
seen.add(term)
|
| 89 |
+
|
| 90 |
+
# Keep only candidates with at least some lexical overlap or clear phrase family.
|
| 91 |
+
query_parts = set(query.split())
|
| 92 |
+
term_parts = set(term.split())
|
| 93 |
+
if query_parts and term_parts and not (query_parts & term_parts):
|
| 94 |
+
# Allow the last fallback word variant to pass through only if it looks like a food term.
|
| 95 |
+
variants = ingredient_lookup_variants(term)
|
| 96 |
+
if not variants:
|
| 97 |
+
continue
|
| 98 |
+
|
| 99 |
+
out.append((term, float(score)))
|
| 100 |
+
if len(out) >= topn:
|
| 101 |
+
break
|
| 102 |
+
|
| 103 |
+
return out
|