Spaces:
Sleeping
Sleeping
File size: 14,752 Bytes
bdace02 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 | {
"cells": [
{
"cell_type": "code",
"execution_count": 9,
"id": "a600d7fc",
"metadata": {},
"outputs": [],
"source": [
"import json \n",
"with open('metadata.jsonl', 'r') as f: \n",
" json_list = list(f)\n",
"\n",
"json_QA = []\n",
"for json_str in json_list: \n",
" json_data = json.loads(json_str)\n",
" json_QA.append(json_data)"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "fa5d8eb8",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"==================================================\n",
"Task ID: 7b5377b0-3f38-4103-8ad2-90fe89864c04\n",
"Question: Find the value of x to the nearest tenth: Lx = (d/dx * (A * x-squared)) + 4-thousand'n'ninety-7 minus C\n",
"Where L is the last two digits of the year of the Venezuelan Declaration of Independence,\n",
"A is the number of colors in the TikTok logo as of July 2023, excluding black and white,\n",
"and C is the height of the average woman in the Philippines according to a July 2023 Business Insider article, rounded to the nearest whole centimeter\n",
"Level: 2\n",
"Final Answer: 563.9\n",
"Annotator Metadata: \n",
" βββ Steps: \n",
" β βββ 1. Googled Venezuelan Declaration of Independence, found it to be in 1811, thus L = 11\n",
" β βββ 2. Googled TikTok logo, found 4 colors, 2 of which are black and white, so A = 2\n",
" β βββ 3. Googled average height of woman in Philippines, found it to be 149.6cm, so C = 150\n",
" β βββ 4. Deciphered formula to mean 11x = (d/dx(2x^2)) + 4097 - 150\n",
" β βββ 5. Used simple calculus and algebra to solve the equation\n",
" βββ Number of steps: 5\n",
" βββ How long did this take?: 40 minutes\n",
" βββ Tools:\n",
" β βββ 1. A web browser\n",
" β βββ 2. A search engine\n",
" β βββ 3. A calculator\n",
" βββ Number of tools: 3\n",
"==================================================\n"
]
}
],
"source": [
"import random\n",
"random_samples = random.sample(json_QA, 1)\n",
"for sample in random_samples:\n",
" print(\"=\" * 50)\n",
" print(f\"Task ID: {sample['task_id']}\")\n",
" print(f\"Question: {sample['Question']}\")\n",
" print(f\"Level: {sample['Level']}\")\n",
" print(f\"Final Answer: {sample['Final answer']}\")\n",
" print(f\"Annotator Metadata: \")\n",
" print(f\" βββ Steps: \")\n",
" for step in sample['Annotator Metadata']['Steps'].split('\\n'):\n",
" print(f\" β βββ {step}\")\n",
" print(f\" βββ Number of steps: {sample['Annotator Metadata']['Number of steps']}\")\n",
" print(f\" βββ How long did this take?: {sample['Annotator Metadata']['How long did this take?']}\")\n",
" print(f\" βββ Tools:\")\n",
" for tool in sample['Annotator Metadata']['Tools'].split('\\n'):\n",
" print(f\" β βββ {tool}\")\n",
" print(f\" βββ Number of tools: {sample['Annotator Metadata']['Number of tools']}\")\n",
"print(\"=\" * 50)"
]
},
{
"cell_type": "code",
"execution_count": 20,
"id": "05076516",
"metadata": {},
"outputs": [
{
"ename": "ModuleNotFoundError",
"evalue": "No module named 'supabase.client'",
"output_type": "error",
"traceback": [
"\u001b[31m---------------------------------------------------------------------------\u001b[39m",
"\u001b[31mModuleNotFoundError\u001b[39m Traceback (most recent call last)",
"\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[20]\u001b[39m\u001b[32m, line 5\u001b[39m\n\u001b[32m 3\u001b[39m \u001b[38;5;28;01mfrom\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[34;01mlangchain_huggingface\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;28;01mimport\u001b[39;00m HuggingFaceEmbeddings\n\u001b[32m 4\u001b[39m \u001b[38;5;28;01mfrom\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[34;01mlangchain_community\u001b[39;00m\u001b[34;01m.\u001b[39;00m\u001b[34;01mvectorstores\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;28;01mimport\u001b[39;00m SupabaseVectorStore\n\u001b[32m----> \u001b[39m\u001b[32m5\u001b[39m \u001b[38;5;28;01mfrom\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[34;01msupabase\u001b[39;00m\u001b[34;01m.\u001b[39;00m\u001b[34;01mclient\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;28;01mimport\u001b[39;00m create_client, Client\n\u001b[32m 7\u001b[39m os.environ[\u001b[33m\"\u001b[39m\u001b[33mSUPABASE_URL\u001b[39m\u001b[33m\"\u001b[39m] = \u001b[33m\"\u001b[39m\u001b[33mhttps://hkjtumxnumguevndjfcj.supabase.co\u001b[39m\u001b[33m\"\u001b[39m\n\u001b[32m 8\u001b[39m os.environ[\u001b[33m\"\u001b[39m\u001b[33mSUPABASE_SERVICE_ROLE_KEY\u001b[39m\u001b[33m\"\u001b[39m] = \u001b[33m\"\u001b[39m\u001b[33meyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6ImhranR1bXhudW1ndWV2bmRqZmNqIiwicm9sZSI6ImFub24iLCJpYXQiOjE3NDk1NjgxNTksImV4cCI6MjA2NTE0NDE1OX0.6qtTbevyd7l38ogJjecgDu0BIlUyDJpJ0Gqdogbt3DM\u001b[39m\u001b[33m\"\u001b[39m\n",
"\u001b[31mModuleNotFoundError\u001b[39m: No module named 'supabase.client'"
]
}
],
"source": [
"import os\n",
"from dotenv import load_dotenv\n",
"from langchain_huggingface import HuggingFaceEmbeddings\n",
"from langchain_community.vectorstores import SupabaseVectorStore\n",
"from supabase.client import create_client, Client\n",
"\n",
"os.environ[\"SUPABASE_URL\"] = \"https://hkjtumxnumguevndjfcj.supabase.co\"\n",
"os.environ[\"SUPABASE_SERVICE_ROLE_KEY\"] = \"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6ImhranR1bXhudW1ndWV2bmRqZmNqIiwicm9sZSI6ImFub24iLCJpYXQiOjE3NDk1NjgxNTksImV4cCI6MjA2NTE0NDE1OX0.6qtTbevyd7l38ogJjecgDu0BIlUyDJpJ0Gqdogbt3DM\"\n",
"\n",
"load_dotenv()\n",
"embeddings = HuggingFaceEmbeddings(model_name=\"sentence-transformers/all-mpnet-base-v2\") # dim=768\n",
"\n",
"supabase_url = os.environ.get(\"SUPABASE_URL\")\n",
"supabase_key = os.environ.get(\"SUPABASE_SERVICE_ROLE_KEY\")\n",
"supabase: Client = create_client(supabase_url, supabase_key)"
]
},
{
"cell_type": "code",
"execution_count": 20,
"id": "aa1402e3",
"metadata": {},
"outputs": [],
"source": [
"from langchain.schema import Document\n",
"docs = []\n",
"cnt = 0 \n",
"for sample in json_QA:\n",
" content = f\"Question : {sample['Question']}\\n\\nFinal answer : {sample['Final answer']}\"\n",
" doc = {\n",
" \"id\" : cnt,\n",
" \"content\" : content,\n",
" \"metadata\" : {\n",
" \"source\" : sample['task_id']\n",
" },\n",
" \"embedding\" : embeddings.embed_query(content),\n",
" }\n",
" docs.append(doc)\n",
" cnt += 1\n",
"\n",
"# upload the documents to the vector database\n",
"try:\n",
" response = (\n",
" supabase.table(\"documents2\")\n",
" .insert(docs)\n",
" .execute()\n",
" )\n",
"except Exception as exception:\n",
" print(\"Error inserting data into Supabase:\", exception)\n",
"\n",
"# # Save the documents (a list of dict) into a csv file, and manually upload it to Supabase\n",
"# import pandas as pd\n",
"# df = pd.DataFrame(docs)\n",
"# df.to_csv('supabase_docs.csv',index=False)"
]
},
{
"cell_type": "code",
"execution_count": 41,
"id": "9aa7eb5e",
"metadata": {},
"outputs": [],
"source": [
"# add items to vector database\n",
"vector_store = SupabaseVectorStore(\n",
" client=supabase,\n",
" embedding= embeddings,\n",
" table_name=\"documents2\",\n",
" query_name=\"match_documents_2\",\n",
")\n",
"retriever = vector_store.as_retriever()"
]
},
{
"cell_type": "code",
"execution_count": 42,
"id": "9eecafd1",
"metadata": {},
"outputs": [],
"source": [
"query = \"On June 6, 2023, an article by Carolyn Collins Petersen was published in Universe Today. This article mentions a team that produced a paper about their observations, linked at the bottom of the article. Find this paper. Under what NASA award number was the work performed by R. G. Arendt supported by?\"\n",
"# matched_docs = vector_store.similarity_search(query, k=2)\n",
"docs = retriever.invoke(query)"
]
},
{
"cell_type": "code",
"execution_count": 43,
"id": "ff917840",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Document(metadata={'source': '840bfca7-4f7b-481a-8794-c560c340185d'}, page_content='Question : On June 6, 2023, an article by Carolyn Collins Petersen was published in Universe Today. This article mentions a team that produced a paper about their observations, linked at the bottom of the article. Find this paper. Under what NASA award number was the work performed by R. G. Arendt supported by?\\n\\nFinal answer : 80GSFC21M0002')"
]
},
"execution_count": 43,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"docs[0]"
]
},
{
"cell_type": "code",
"execution_count": 44,
"id": "01c8f337",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"List of tools used in all samples:\n",
"Total number of tools used: 83\n",
" βββ web browser: 107\n",
" βββ image recognition tools (to identify and parse a figure with three axes): 1\n",
" βββ search engine: 101\n",
" βββ calculator: 34\n",
" βββ unlambda compiler (optional): 1\n",
" βββ a web browser.: 2\n",
" βββ a search engine.: 2\n",
" βββ a calculator.: 1\n",
" βββ microsoft excel: 5\n",
" βββ google search: 1\n",
" βββ ne: 9\n",
" βββ pdf access: 7\n",
" βββ file handling: 2\n",
" βββ python: 3\n",
" βββ image recognition tools: 12\n",
" βββ jsonld file access: 1\n",
" βββ video parsing: 1\n",
" βββ python compiler: 1\n",
" βββ video recognition tools: 3\n",
" βββ pdf viewer: 7\n",
" βββ microsoft excel / google sheets: 3\n",
" βββ word document access: 1\n",
" βββ tool to extract text from images: 1\n",
" βββ a word reversal tool / script: 1\n",
" βββ counter: 1\n",
" βββ excel: 3\n",
" βββ image recognition: 5\n",
" βββ color recognition: 3\n",
" βββ excel file access: 3\n",
" βββ xml file access: 1\n",
" βββ access to the internet archive, web.archive.org: 1\n",
" βββ text processing/diff tool: 1\n",
" βββ gif parsing tools: 1\n",
" βββ a web browser: 7\n",
" βββ a search engine: 7\n",
" βββ a speech-to-text tool: 2\n",
" βββ code/data analysis tools: 1\n",
" βββ audio capability: 2\n",
" βββ pdf reader: 1\n",
" βββ markdown: 1\n",
" βββ a calculator: 5\n",
" βββ access to wikipedia: 3\n",
" βββ image recognition/ocr: 3\n",
" βββ google translate access: 1\n",
" βββ ocr: 4\n",
" βββ bass note data: 1\n",
" βββ text editor: 1\n",
" βββ xlsx file access: 1\n",
" βββ powerpoint viewer: 1\n",
" βββ csv file access: 1\n",
" βββ calculator (or use excel): 1\n",
" βββ computer algebra system: 1\n",
" βββ video processing software: 1\n",
" βββ audio processing software: 1\n",
" βββ computer vision: 1\n",
" βββ google maps: 1\n",
" βββ access to excel files: 1\n",
" βββ calculator (or ability to count): 1\n",
" βββ a file interface: 3\n",
" βββ a python ide: 1\n",
" βββ spreadsheet editor: 1\n",
" βββ tools required: 1\n",
" βββ b browser: 1\n",
" βββ image recognition and processing tools: 1\n",
" βββ computer vision or ocr: 1\n",
" βββ c++ compiler: 1\n",
" βββ access to google maps: 1\n",
" βββ youtube player: 1\n",
" βββ natural language processor: 1\n",
" βββ graph interaction tools: 1\n",
" βββ bablyonian cuniform -> arabic legend: 1\n",
" βββ access to youtube: 1\n",
" βββ image search tools: 1\n",
" βββ calculator or counting function: 1\n",
" βββ a speech-to-text audio processing tool: 1\n",
" βββ access to academic journal websites: 1\n",
" βββ pdf reader/extracter: 1\n",
" βββ rubik's cube model: 1\n",
" βββ wikipedia: 1\n",
" βββ video capability: 1\n",
" βββ image processing tools: 1\n",
" βββ age recognition software: 1\n",
" βββ youtube: 1\n"
]
}
],
"source": [
"# list of the tools used in all the samples\n",
"from collections import Counter, OrderedDict\n",
"\n",
"tools = []\n",
"for sample in json_QA:\n",
" for tool in sample['Annotator Metadata']['Tools'].split('\\n'):\n",
" tool = tool[2:].strip().lower()\n",
" if tool.startswith(\"(\"):\n",
" tool = tool[11:].strip()\n",
" tools.append(tool)\n",
"tools_counter = OrderedDict(Counter(tools))\n",
"print(\"List of tools used in all samples:\")\n",
"print(\"Total number of tools used:\", len(tools_counter))\n",
"for tool, count in tools_counter.items():\n",
" print(f\" βββ {tool}: {count}\")"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.13.3"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
|