justaddcoffee commited on
Commit
b82f8d3
·
verified ·
1 Parent(s): 201f360

Upload index-monarch-kg.ipynb with huggingface_hub

Browse files
Files changed (1) hide show
  1. index-monarch-kg.ipynb +245 -0
index-monarch-kg.ipynb ADDED
@@ -0,0 +1,245 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "code",
5
+ "execution_count": null,
6
+ "outputs": [],
7
+ "source": [
8
+ "!pip install git+https://github.com/monarch-initiative/curate-gpt.git\n",
9
+ "!pip install huggingface_hub pyyaml pandas pyarrow"
10
+ ],
11
+ "metadata": {
12
+ "collapsed": false
13
+ },
14
+ "id": "6ccb0b14fb5a11a1"
15
+ },
16
+ {
17
+ "cell_type": "code",
18
+ "execution_count": 12,
19
+ "outputs": [],
20
+ "source": [
21
+ "# Import necessary libraries\n",
22
+ "from huggingface_hub import HfApi, create_repo\n",
23
+ "import yaml"
24
+ ],
25
+ "metadata": {
26
+ "collapsed": false,
27
+ "ExecuteTime": {
28
+ "end_time": "2024-08-02T11:22:16.789896Z",
29
+ "start_time": "2024-08-02T11:22:16.378435Z"
30
+ }
31
+ },
32
+ "id": "105b0e6972a9e087"
33
+ },
34
+ {
35
+ "cell_type": "code",
36
+ "execution_count": null,
37
+ "outputs": [],
38
+ "source": [
39
+ "!wget https://data.monarchinitiative.org/monarch-kg/latest/monarch-kg.tar.gz\n",
40
+ "!tar -xvzf monarch-kg.tar.gz"
41
+ ],
42
+ "metadata": {
43
+ "collapsed": false
44
+ },
45
+ "id": "fb9336dad1877366"
46
+ },
47
+ {
48
+ "cell_type": "code",
49
+ "execution_count": null,
50
+ "outputs": [],
51
+ "source": [
52
+ "!curategpt index -p stagedb -c monarch_kg -m openai: monarch-kg_nodes.tsv"
53
+ ],
54
+ "metadata": {
55
+ "collapsed": false
56
+ },
57
+ "id": "f47fce4b73e51127"
58
+ },
59
+ {
60
+ "cell_type": "code",
61
+ "execution_count": 10,
62
+ "outputs": [
63
+ {
64
+ "name": "stdout",
65
+ "output_type": "stream",
66
+ "text": [
67
+ "About to write to monarch_text_embeddings.parquet\n",
68
+ "Embeddings have been successfully exported to monarch_text_embeddings.parquet\n"
69
+ ]
70
+ }
71
+ ],
72
+ "source": [
73
+ "import os\n",
74
+ "import pandas as pd\n",
75
+ "from curate_gpt import ChromaDBAdapter\n",
76
+ "\n",
77
+ "def fetch_embeddings_from_chromadb(path, collection):\n",
78
+ " # Initialize the database adapter\n",
79
+ " db = ChromaDBAdapter(path)\n",
80
+ " \n",
81
+ " # Fetch embeddings from the specified collection using get\n",
82
+ " collection_obj = db.client.get_collection(name=collection)\n",
83
+ " # results = collection_obj.peek(include=[\"embeddings\"])\n",
84
+ " results = collection_obj.get(include=[\"embeddings\"])\n",
85
+ " \n",
86
+ " return results['embeddings']\n",
87
+ "\n",
88
+ "def export_embeddings_to_parquet(path, collection, output_file):\n",
89
+ " try:\n",
90
+ " # Fetch embeddings\n",
91
+ " embeddings = fetch_embeddings_from_chromadb(path, collection)\n",
92
+ " \n",
93
+ " # Convert embeddings to DataFrame\n",
94
+ " df_embeddings = pd.DataFrame(embeddings)\n",
95
+ " \n",
96
+ " # Debugging statement: confirm path before writing\n",
97
+ " print(f\"About to write to {output_file}\")\n",
98
+ " \n",
99
+ " # Export DataFrame to Parquet file\n",
100
+ " df_embeddings.to_parquet(output_file, engine='pyarrow')\n",
101
+ " \n",
102
+ " # Confirm file creation\n",
103
+ " if os.path.exists(output_file):\n",
104
+ " print(f\"Embeddings have been successfully exported to {output_file}\")\n",
105
+ " else:\n",
106
+ " print(f\"Failed to write file to {output_file}\")\n",
107
+ " except Exception as e:\n",
108
+ " print(f\"An error occurred: {e}\")\n",
109
+ "\n",
110
+ "# Example usage\n",
111
+ "path_to_chromadb = '../../stagedb'\n",
112
+ "collection_name = 'monarch_kg'\n",
113
+ "output_parquet_file = 'monarch_text_embeddings.parquet'\n",
114
+ "\n",
115
+ "export_embeddings_to_parquet(path_to_chromadb, collection_name, output_parquet_file)"
116
+ ],
117
+ "metadata": {
118
+ "collapsed": false,
119
+ "ExecuteTime": {
120
+ "end_time": "2024-08-02T03:55:35.337205Z",
121
+ "start_time": "2024-08-01T21:29:05.165170Z"
122
+ }
123
+ },
124
+ "id": "4c04eeafb792a7bd"
125
+ },
126
+ {
127
+ "cell_type": "code",
128
+ "execution_count": 13,
129
+ "outputs": [
130
+ {
131
+ "name": "stdout",
132
+ "output_type": "stream",
133
+ "text": [
134
+ "Metadata saved to ./metadata.yaml\n"
135
+ ]
136
+ }
137
+ ],
138
+ "source": [
139
+ "# Generate metadata in venomx format\n",
140
+ "metadata = {\n",
141
+ " 'description': 'Embeddings of the Monarch KG nodes, generated using curategpt and the nodes.tsv file from the Monarch KG version 2024-07-12',\n",
142
+ " 'model': {\n",
143
+ " 'name': 'text-embedding-ada-002'\n",
144
+ " },\n",
145
+ " 'dataset': {\n",
146
+ " 'name': 'Monarch KG 2024-07-12',\n",
147
+ " 'url': 'https://data.monarchinitiative.org/monarch-kg/2024-07-12/'\n",
148
+ " }\n",
149
+ "}\n",
150
+ "\n",
151
+ "# Save the metadata to a YAML file\n",
152
+ "metadata_file_path = './metadata.yaml'\n",
153
+ "with open(metadata_file_path, 'w') as f:\n",
154
+ " yaml.dump(metadata, f)\n",
155
+ "\n",
156
+ "print(f\"Metadata saved to {metadata_file_path}\")"
157
+ ],
158
+ "metadata": {
159
+ "collapsed": false,
160
+ "ExecuteTime": {
161
+ "end_time": "2024-08-02T11:22:21.170816Z",
162
+ "start_time": "2024-08-02T11:22:21.161180Z"
163
+ }
164
+ },
165
+ "id": "e4573dbb4c2cc72b"
166
+ },
167
+ {
168
+ "cell_type": "code",
169
+ "execution_count": null,
170
+ "outputs": [
171
+ {
172
+ "data": {
173
+ "text/plain": "monarch_text_embeddings.parquet: 0%| | 0.00/9.93G [00:00<?, ?B/s]",
174
+ "application/vnd.jupyter.widget-view+json": {
175
+ "version_major": 2,
176
+ "version_minor": 0,
177
+ "model_id": "0a53be0630394f5b913470726d32f526"
178
+ }
179
+ },
180
+ "metadata": {},
181
+ "output_type": "display_data"
182
+ }
183
+ ],
184
+ "source": [
185
+ "# Upload to Hugging Face\n",
186
+ "repo_id = \"biomedical-translator/monarch_kg_node_text_embeddings\"\n",
187
+ "create_repo(repo_id, repo_type=\"dataset\")\n",
188
+ "\n",
189
+ "this_notebook_path = \"index-monarch-kg.ipynb\"\n",
190
+ "\n",
191
+ "api = HfApi()\n",
192
+ "files_to_upload = [output_parquet_file, metadata_file_path, this_notebook_path]\n",
193
+ "\n",
194
+ "for file in files_to_upload:\n",
195
+ " api.upload_file(\n",
196
+ " path_or_fileobj=file,\n",
197
+ " path_in_repo=file,\n",
198
+ " repo_id=repo_id,\n",
199
+ " repo_type=\"dataset\"\n",
200
+ " )\n",
201
+ "\n",
202
+ "print(f\"Files uploaded to Hugging Face in repository: {repo_id}\")"
203
+ ],
204
+ "metadata": {
205
+ "collapsed": false,
206
+ "is_executing": true,
207
+ "ExecuteTime": {
208
+ "start_time": "2024-08-02T11:52:43.155295Z"
209
+ }
210
+ },
211
+ "id": "d3fcdcba15078167"
212
+ },
213
+ {
214
+ "cell_type": "code",
215
+ "execution_count": null,
216
+ "outputs": [],
217
+ "source": [],
218
+ "metadata": {
219
+ "collapsed": false
220
+ },
221
+ "id": "af213f49b772ace7"
222
+ }
223
+ ],
224
+ "metadata": {
225
+ "kernelspec": {
226
+ "display_name": "Python 3",
227
+ "language": "python",
228
+ "name": "python3"
229
+ },
230
+ "language_info": {
231
+ "codemirror_mode": {
232
+ "name": "ipython",
233
+ "version": 2
234
+ },
235
+ "file_extension": ".py",
236
+ "mimetype": "text/x-python",
237
+ "name": "python",
238
+ "nbconvert_exporter": "python",
239
+ "pygments_lexer": "ipython2",
240
+ "version": "2.7.6"
241
+ }
242
+ },
243
+ "nbformat": 4,
244
+ "nbformat_minor": 5
245
+ }