-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: example text embedding functions
- Loading branch information
Showing
2 changed files
with
52 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import pandas as pd | ||
import torch | ||
from pandas import Series | ||
from sentence_transformers import SentenceTransformer | ||
from transformers import pipeline | ||
|
||
|
||
def huggingface_embedding(text_series: pd.Series, model_name: str) -> pd.DataFrame: | ||
"""Embeds the text data using a huggingface model. To use this | ||
in timeseriesflattener, you need to write a wrapper with your desired model name.""" | ||
extractor = pipeline(model=model_name, task="feature-extraction") | ||
embeddings = extractor(text_series.to_list(), return_tensors=True) | ||
embeddings = [torch.mean(embedding, dim=1).squeeze() for embedding in embeddings] | ||
return pd.DataFrame(embeddings).astype(float) | ||
|
||
|
||
def sentence_transformers_embedding( | ||
text_series: pd.Series, model_name: str | ||
) -> pd.DataFrame: | ||
"""Embeds the text data using a sentence-transformers model. To use this | ||
in timeseriesflattener, you need to write a wrapper with your desired model name.""" | ||
model = SentenceTransformer(model_name) | ||
embeddings = model.encode(text_series.to_list()) | ||
return pd.DataFrame(embeddings) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters