Spaces:
Sleeping
Sleeping
Download FastAPI/app/utils/embedding.py from abadesalex/emb-rep: direct link, hf CLI and curl.
- Browser
- Download file 699 Bytes
-
https://huggingface.co/spaces/abadesalex/emb-rep/resolve/main/FastAPI/app/utils/embedding.py
- Command line
-
hf download hf://spaces/abadesalex/emb-rep/FastAPI/app/utils/embedding.py
-
curl -L -o embedding.py https://huggingface.co/spaces/abadesalex/emb-rep/resolve/main/FastAPI/app/utils/embedding.py
699 Bytes
| import os | |
| from fastapi import HTTPException | |
| import gensim.downloader as api | |
| # Ensure the environment variable is set correctly | |
| gensim_data_dir = os.getenv("GENSIM_DATA_DIR", "/home/user/gensim-data") | |
| # Load the GloVe model | |
| model = api.load("glove-wiki-gigaword-50") | |
| def get_embedding(word: str) -> list: | |
| min_val = -5.4593 | |
| max_val = 5.3101 | |
| global model | |
| try: | |
| embediing = model[word.lower()] | |
| # normalize vector min max to -1 to 1 | |
| embediing = (embediing - min_val) / (max_val - min_val) * 2 - 1 | |
| return embediing | |
| except KeyError: | |
| print("Word not in vocabulary") | |
| raise HTTPException(status_code=404, detail="Word not in vocabulary") | |