Download src/create-embedding.py from lpetrl/Test-API: direct link, hf CLI and curl.
- Browser
- Download file 628 Bytes
-
https://huggingface.co/spaces/lpetrl/Test-API/resolve/main/src/create-embedding.py
- Command line
-
hf download hf://spaces/lpetrl/Test-API/src/create-embedding.py
-
curl -L -o create-embedding.py https://huggingface.co/spaces/lpetrl/Test-API/resolve/main/src/create-embedding.py
628 Bytes
| import os | |
| import pandas as pd | |
| import lancedb | |
| from lancedb.embeddings import with_embeddings | |
| from sentence_transformers import SentenceTransformer | |
| from setting import CFG, AVAILABLE_WORDS | |
| df = pd.DataFrame(AVAILABLE_WORDS, columns=['word']) | |
| model = SentenceTransformer(CFG.model.name) | |
| data = with_embeddings( | |
| func=lambda texts: model.encode(texts), | |
| data=df, column="word", show_progress=True | |
| ) | |
| if not os.path.exists(CFG.db.lance_db_folder_path): | |
| os.makedirs(CFG.db.lance_db_folder_path) | |
| db = lancedb.connect(CFG.db.lance_db_folder_path) | |
| table = db.create_table(CFG.db.table_name, data) | |
| print("Table created") | |