otacilio-psf commited on
Commit
6b03385
1 Parent(s): 26a6dc3

Upload recipe-short-embeddings-gpu.ipynb

Browse files
Files changed (1) hide show
  1. recipe-short-embeddings-gpu.ipynb +1 -0
recipe-short-embeddings-gpu.ipynb ADDED
@@ -0,0 +1 @@
 
 
1
+ {"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.14","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"nvidiaTeslaT4","dataSources":[],"dockerImageVersionId":30762,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"!pip install uv","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!uv pip install onnxruntime-gpu -i https://aiinfra.pkgs.visualstudio.com/PublicPackages/_packaging/onnxruntime-cuda-12/pypi/simple/ --system --quiet\n!uv pip install fastembed-gpu huggingface_hub --system --quiet","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from fastembed import TextEmbedding, SparseTextEmbedding,SparseEmbedding\nfrom kaggle_secrets import UserSecretsClient\nfrom datasets import Dataset\nfrom typing import List\nimport huggingface_hub\nimport polars as pl\nimport numpy as np\nimport tqdm\nimport ast\nimport os","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"user_secrets = UserSecretsClient()\nhf_toke_write = user_secrets.get_secret(\"hf_toke_write\")\nos.environ[\"HF_TOKEN\"] = hf_toke_write","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"huggingface_hub.login(hf_toke_write)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def process_data_structure(row):\n doc_template = \"\"\"\nTitle: {title}\nIngredients:\\n{ingredients}\nDirections:\\n{directions}\n\"\"\"\n new_row = list()\n\n for value in row:\n if isinstance(value, str):\n if value.startswith('[') and value.endswith(']'):\n string_list_value = ast.literal_eval(value)\n list_value= [item.strip() for item in string_list_value]\n new_row.append(list_value)\n else:\n new_row.append(value.strip())\n else:\n new_row.append(value)\n\n new_row[2] = \"\\n\".join(new_row[2])\n new_row[3] = \"\\n\".join(new_row[3])\n\n new_row.append({\"title\": new_row[1],\"NER\": new_row[6]})\n new_row.append(doc_template.format(title=new_row[1],ingredients=new_row[2],directions=new_row[3]).strip())\n\n return tuple(new_row)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = TextEmbedding(model_name=\"sentence-transformers/all-MiniLM-L6-v2\", providers=[\"CUDAExecutionProvider\"])\nsparse_model = SparseTextEmbedding(model_name=\"Qdrant/bm42-all-minilm-l6-v2-attentions\", providers=[\"CUDAExecutionProvider\"])","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = (\n pl.read_parquet('hf://datasets/rk404/recipe_short/final_recipes.parquet')\n .map_rows(process_data_structure)\n .rename({\n \"column_0\": \"id\",\n \"column_1\": \"title\",\n \"column_2\": \"ingredients\",\n \"column_3\": \"directions\",\n \"column_4\": \"link\",\n \"column_5\": \"source\",\n \"column_6\": \"NER\",\n \"column_7\": \"metadata\",\n \"column_8\": \"document\"\n })\n)\n\ndata = df.to_dict(as_series=False)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"embeddings_list: List[np.array] = list(model.embed(tqdm.tqdm(data['document']), batch_size=100))","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sparse_embeddings_list: List[SparseEmbedding] = list(sparse_model.embed(tqdm.tqdm(data['document']), batch_size=100))","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data['all-MiniLM-L6-v2'] = embeddings_list","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data['bm42-all-minilm-l6-v2-attentions'] = [i.as_object() for i in sparse_embeddings_list]","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ds = Dataset.from_dict(data)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ds.push_to_hub(\"otacilio-psf/recipe_short_dense_and_sparse_embeddings\")","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}