{ "cells": [ { "cell_type": "code", "execution_count": null, "outputs": [], "source": [ "!pip install git+https://github.com/monarch-initiative/curate-gpt.git\n", "!pip install huggingface_hub pyyaml pandas pyarrow" ], "metadata": { "collapsed": false }, "id": "6ccb0b14fb5a11a1" }, { "cell_type": "code", "execution_count": 12, "outputs": [], "source": [ "# Import necessary libraries\n", "from huggingface_hub import HfApi, create_repo\n", "import yaml" ], "metadata": { "collapsed": false, "ExecuteTime": { "end_time": "2024-08-02T11:22:16.789896Z", "start_time": "2024-08-02T11:22:16.378435Z" } }, "id": "105b0e6972a9e087" }, { "cell_type": "code", "execution_count": null, "outputs": [], "source": [ "!wget https://data.monarchinitiative.org/monarch-kg/latest/monarch-kg.tar.gz\n", "!tar -xvzf monarch-kg.tar.gz" ], "metadata": { "collapsed": false }, "id": "fb9336dad1877366" }, { "cell_type": "code", "execution_count": null, "outputs": [], "source": [ "!curategpt index -p stagedb -c monarch_kg -m openai: monarch-kg_nodes.tsv" ], "metadata": { "collapsed": false }, "id": "f47fce4b73e51127" }, { "cell_type": "code", "execution_count": 10, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "About to write to monarch_text_embeddings.parquet\n", "Embeddings have been successfully exported to monarch_text_embeddings.parquet\n" ] } ], "source": [ "import os\n", "import pandas as pd\n", "from curate_gpt import ChromaDBAdapter\n", "\n", "def fetch_embeddings_from_chromadb(path, collection):\n", " # Initialize the database adapter\n", " db = ChromaDBAdapter(path)\n", " \n", " # Fetch embeddings from the specified collection using get\n", " collection_obj = db.client.get_collection(name=collection)\n", " # results = collection_obj.peek(include=[\"embeddings\"])\n", " results = collection_obj.get(include=[\"embeddings\"])\n", " \n", " return results['embeddings']\n", "\n", "def export_embeddings_to_parquet(path, collection, output_file):\n", " try:\n", " # Fetch embeddings\n", " embeddings = fetch_embeddings_from_chromadb(path, collection)\n", " \n", " # Convert embeddings to DataFrame\n", " df_embeddings = pd.DataFrame(embeddings)\n", " \n", " # Debugging statement: confirm path before writing\n", " print(f\"About to write to {output_file}\")\n", " \n", " # Export DataFrame to Parquet file\n", " df_embeddings.to_parquet(output_file, engine='pyarrow')\n", " \n", " # Confirm file creation\n", " if os.path.exists(output_file):\n", " print(f\"Embeddings have been successfully exported to {output_file}\")\n", " else:\n", " print(f\"Failed to write file to {output_file}\")\n", " except Exception as e:\n", " print(f\"An error occurred: {e}\")\n", "\n", "# Example usage\n", "path_to_chromadb = '../../stagedb'\n", "collection_name = 'monarch_kg'\n", "output_parquet_file = 'monarch_text_embeddings.parquet'\n", "\n", "export_embeddings_to_parquet(path_to_chromadb, collection_name, output_parquet_file)" ], "metadata": { "collapsed": false, "ExecuteTime": { "end_time": "2024-08-02T03:55:35.337205Z", "start_time": "2024-08-01T21:29:05.165170Z" } }, "id": "4c04eeafb792a7bd" }, { "cell_type": "code", "execution_count": 13, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Metadata saved to ./metadata.yaml\n" ] } ], "source": [ "# Generate metadata in venomx format\n", "metadata = {\n", " 'description': 'Embeddings of the Monarch KG nodes, generated using curategpt and the nodes.tsv file from the Monarch KG version 2024-07-12',\n", " 'model': {\n", " 'name': 'text-embedding-ada-002'\n", " },\n", " 'dataset': {\n", " 'name': 'Monarch KG 2024-07-12',\n", " 'url': 'https://data.monarchinitiative.org/monarch-kg/2024-07-12/'\n", " }\n", "}\n", "\n", "# Save the metadata to a YAML file\n", "metadata_file_path = './metadata.yaml'\n", "with open(metadata_file_path, 'w') as f:\n", " yaml.dump(metadata, f)\n", "\n", "print(f\"Metadata saved to {metadata_file_path}\")" ], "metadata": { "collapsed": false, "ExecuteTime": { "end_time": "2024-08-02T11:22:21.170816Z", "start_time": "2024-08-02T11:22:21.161180Z" } }, "id": "e4573dbb4c2cc72b" }, { "cell_type": "code", "execution_count": null, "outputs": [ { "data": { "text/plain": "monarch_text_embeddings.parquet: 0%| | 0.00/9.93G [00:00