{ "cells": [ { "cell_type": "code", "source": [ "!git clone https://huggingface.co/datasets/muellerzr/RAG-accelerate" ], "metadata": { "id": "L6pZLIWYSsSq" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "%cd RAG-accelerate" ], "metadata": { "id": "Ntgk_YKiSyIq", "outputId": "14d9ffc4-0b42-4283-adeb-4b981ad2ee57", "colab": { "base_uri": "https://localhost:8080/" } }, "execution_count": 4, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "/content/RAG-accelerate\n" ] } ] }, { "cell_type": "code", "source": [ "import re" ], "metadata": { "id": "ERy5RO58TBcu" }, "execution_count": 222, "outputs": [] }, { "cell_type": "code", "source": [ "def process_file(content, filename):\n", " # Declare constants\n", " func = ''\n", " tab = ''\n", " brackets = {'(':0, '[':0, '{':0}\n", " close = {')':'(', ']':'[', '}':'{'}\n", " string=''\n", " tab_f=''\n", " c1=''\n", " c2=''\n", " multiline=False\n", " check=False\n", " in_class = False\n", " in_function = False\n", " decorator = None\n", " class_name = \"\"\n", " func_list=[]\n", " for line in content.split('\\n'):\n", " tab = re.findall(r'^\\s*',line)[0]\n", " if re.findall(r'^\\s*@', line) and not func:\n", " decorator = line\n", " if (len(line.lstrip(\" \")) - len(line)) == 0:\n", " in_class = False\n", " if (re.findall(r'^\\s*def ', line) or re.findall(r'^\\s*class ', line)) and not string and not multiline:\n", " if re.findall(r'^\\s*class ', line):\n", " in_class = True\n", " class_name = re.findall(r'class\\s+(\\w+)\\s*[\\(:]', line)[0]\n", " func += line + '\\n'\n", " tab_f = tab\n", " check=True\n", " if func:\n", " if not check:\n", " if sum(brackets.values()) == 0 and not string and not multiline:\n", " if len(tab) <= len(tab_f):\n", " # Append the metadata\n", " metadata = f'File: {filename}'\n", " if in_class:\n", " metadata += f'\\nClass: {class_name}'\n", " if decorator is not None:\n", " func = f'{decorator}\\n{func}'\n", " decorator = None\n", " func = f'{metadata}\\nSource:\\n{func}'\n", " func_list.append(func)\n", " func=''\n", " c1=''\n", " c2=''\n", " continue\n", " func += line + '\\n'\n", " check = False\n", " for c0 in line:\n", " if c0 == '#' and not string and not multiline:\n", " break\n", " if c1 != '\\\\':\n", " if c0 in ['\"', \"'\"]:\n", " if c2 == c1 == c0 == '\"' and string != \"'\":\n", " multiline = not multiline\n", " string = ''\n", " continue\n", " if not multiline:\n", " if c0 in string:\n", " string = ''\n", " else:\n", " if not string:\n", " string = c0\n", " if not string and not multiline:\n", " if c0 in brackets:\n", " brackets[c0] += 1\n", " if c0 in close:\n", " b = close[c0]\n", " brackets[b] -= 1\n", " c2=c1\n", " c1=c0\n", " return func_list" ], "metadata": { "id": "yTaa5oufYwVj" }, "execution_count": 223, "outputs": [] }, { "cell_type": "code", "source": [ "from pathlib import Path" ], "metadata": { "id": "SObDuDAPYj1u" }, "execution_count": 224, "outputs": [] }, { "cell_type": "code", "source": [ "src_path = Path(\"src\")" ], "metadata": { "id": "EBVypAmNYsnR" }, "execution_count": 225, "outputs": [] }, { "cell_type": "code", "source": [ "results = []\n", "for f in src_path.glob('**/*'):\n", " if f.is_file():\n", " content = f.read_text()\n", " output = process_file(content, f.name)\n", " results += output" ], "metadata": { "id": "J0c0eNPYcAoX" }, "execution_count": 226, "outputs": [] }, { "cell_type": "code", "source": [ "len(results)" ], "metadata": { "id": "nuZS65CzdAMm", "outputId": "2601f995-fce6-49d5-b2dd-71e8150746a1", "colab": { "base_uri": "https://localhost:8080/" } }, "execution_count": 231, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "536" ] }, "metadata": {}, "execution_count": 231 } ] }, { "cell_type": "code", "source": [ "import numpy as np" ], "metadata": { "id": "eFinZFPTf6Ac" }, "execution_count": 232, "outputs": [] }, { "cell_type": "code", "source": [ "arr = np.array(results)" ], "metadata": { "id": "Hg6FiWfAgQcA" }, "execution_count": 234, "outputs": [] }, { "cell_type": "code", "source": [ "np.save(\"processed_chunks.npy\", arr)" ], "metadata": { "id": "OVEMm3gsgRW-" }, "execution_count": 235, "outputs": [] }, { "cell_type": "code", "source": [], "metadata": { "id": "4f3fivFAgxmg" }, "execution_count": null, "outputs": [] } ], "metadata": { "colab": { "name": "scratchpad", "provenance": [] }, "kernelspec": { "display_name": "Python 3", "name": "python3" } }, "nbformat": 4, "nbformat_minor": 0 }