arshy commited on
Commit
0977c3c
·
1 Parent(s): febac62

first commit

Browse files
.gitignore ADDED
@@ -0,0 +1,165 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ # Byte-compiled / optimized / DLL files
3
+ __pycache__/
4
+ *.py[cod]
5
+ *$py.class
6
+
7
+ # C extensions
8
+ *.so
9
+
10
+ # Distribution / packaging
11
+ .Python
12
+ build/
13
+ develop-eggs/
14
+ dist/
15
+ downloads/
16
+ eggs/
17
+ .eggs/
18
+ lib/
19
+ lib64/
20
+ parts/
21
+ sdist/
22
+ var/
23
+ wheels/
24
+ share/python-wheels/
25
+ *.egg-info/
26
+ .installed.cfg
27
+ *.egg
28
+ MANIFEST
29
+
30
+ # PyInstaller
31
+ # Usually these files are written by a python script from a template
32
+ # before PyInstaller builds the exe, so as to inject date/other infos into it.
33
+ *.manifest
34
+ *.spec
35
+
36
+ # Installer logs
37
+ pip-log.txt
38
+ pip-delete-this-directory.txt
39
+
40
+ # Unit test / coverage reports
41
+ htmlcov/
42
+ .tox/
43
+ .nox/
44
+ .coverage
45
+ .coverage.*
46
+ .cache
47
+ nosetests.xml
48
+ coverage.xml
49
+ *.cover
50
+ *.py,cover
51
+ .hypothesis/
52
+ .pytest_cache/
53
+ cover/
54
+
55
+ # Translations
56
+ *.mo
57
+ *.pot
58
+
59
+ # Django stuff:
60
+ *.log
61
+ local_settings.py
62
+ db.sqlite3
63
+ db.sqlite3-journal
64
+
65
+ # Flask stuff:
66
+ instance/
67
+ .webassets-cache
68
+
69
+ # Scrapy stuff:
70
+ .scrapy
71
+
72
+ # Sphinx documentation
73
+ docs/_build/
74
+
75
+ # PyBuilder
76
+ .pybuilder/
77
+ target/
78
+
79
+ # Jupyter Notebook
80
+ .ipynb_checkpoints
81
+
82
+ # IPython
83
+ profile_default/
84
+ ipython_config.py
85
+
86
+ # pyenv
87
+ # For a library or package, you might want to ignore these files since the code is
88
+ # intended to run in multiple environments; otherwise, check them in:
89
+ # .python-version
90
+
91
+ # pipenv
92
+ # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
93
+ # However, in case of collaboration, if having platform-specific dependencies or dependencies
94
+ # having no cross-platform support, pipenv may install dependencies that don't work, or not
95
+ # install all needed dependencies.
96
+ #Pipfile.lock
97
+
98
+ # poetry
99
+ # Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
100
+ # This is especially recommended for binary packages to ensure reproducibility, and is more
101
+ # commonly ignored for libraries.
102
+ # https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
103
+ #poetry.lock
104
+
105
+ # pdm
106
+ # Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
107
+ #pdm.lock
108
+ # pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
109
+ # in version control.
110
+ # https://pdm.fming.dev/latest/usage/project/#working-with-version-control
111
+ .pdm.toml
112
+ .pdm-python
113
+ .pdm-build/
114
+
115
+ # PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
116
+ __pypackages__/
117
+
118
+ # Celery stuff
119
+ celerybeat-schedule
120
+ celerybeat.pid
121
+
122
+ # SageMath parsed files
123
+ *.sage.py
124
+
125
+ # Environments
126
+ .env
127
+ .venv
128
+ env/
129
+ venv/
130
+ ENV/
131
+ env.bak/
132
+ venv.bak/
133
+
134
+ # Spyder project settings
135
+ .spyderproject
136
+ .spyproject
137
+
138
+ # Rope project settings
139
+ .ropeproject
140
+
141
+ # mkdocs documentation
142
+ /site
143
+
144
+ # mypy
145
+ .mypy_cache/
146
+ .dmypy.json
147
+ dmypy.json
148
+
149
+ # Pyre type checker
150
+ .pyre/
151
+
152
+ # pytype static type analyzer
153
+ .pytype/
154
+
155
+ # Cython debug symbols
156
+ cython_debug/
157
+
158
+ # PyCharm
159
+ # JetBrains specific template is maintained in a separate JetBrains.gitignore that can
160
+ # be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
161
+ # and can be added to the global gitignore or merged into this file. For a more nuclear
162
+ # option (not recommended) you can uncomment the following to ignore the entire idea folder.
163
+ #.idea/
164
+
165
+ test.ipynb
README.md ADDED
File without changes
poetry.lock ADDED
The diff for this file is too large to render. See raw diff
 
pyproject.toml ADDED
@@ -0,0 +1,17 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ [tool.poetry]
2
+ name = "wikipedia_kb"
3
+ version = "0.1.0"
4
+ description = "Wikipedia Knowledge Base"
5
+ authors = ["Mohamed Arshath <marshath@gmail.com>"]
6
+ readme = "README.md"
7
+
8
+ [tool.poetry.dependencies]
9
+ python = ">=3.10,<=3.13"
10
+ naptha-sdk = {git = "https://github.com/NapthaAI/naptha-sdk.git"}
11
+ pydantic = "^2.9.2"
12
+ pandas = "^2.2.3"
13
+ pyarrow = "^18.1.0"
14
+
15
+ [build-system]
16
+ requires = ["poetry-core"]
17
+ build-backend = "poetry.core.masonry.api"
wikipedia_kb/configs/knowledge_base_deployment.json ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "name": "kb_deployment_1",
3
+ "module": {"name": "wikipedia_kb"},
4
+ "kb_node_url": "http://localhost:7001",
5
+ "kb_config": {
6
+ "table_name": "wikipedia_kb",
7
+ "query_col": "title",
8
+ "answer_col": "text",
9
+ "schema": {
10
+ "id": {"type": "INTEGER", "primary_key": true},
11
+ "url": {"type": "TEXT"},
12
+ "title": {"type": "TEXT"},
13
+ "text": {"type": "TEXT"}
14
+ }
15
+ }
16
+ }
wikipedia_kb/data/wikipedia_kb.parquet ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:3e31369bc377665291427b7a1398408b040d1a07852664e645af3c1e5170cdae
3
+ size 133886521
wikipedia_kb/run.py ADDED
@@ -0,0 +1,51 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import logging
2
+ import pandas as pd
3
+ from tqdm import tqdm
4
+ from typing import Dict, Any
5
+ from naptha_sdk.client.node import Node
6
+
7
+ logger = logging.getLogger(__name__)
8
+
9
+ async def run(kb_run: Dict[str, Any], *args, **kwargs):
10
+ """
11
+ Run the Wikipedia Knowledge Base deployment
12
+ Args:
13
+ kb_run: Knowledge base run configuration containing deployment details
14
+ """
15
+ logger.info(f"Running Wikipedia Knowledge Base with deployment: {kb_run}")
16
+
17
+ # Extract configuration
18
+ kb_config = kb_run.kb_deployment.kb_config
19
+ if not isinstance(kb_config, dict):
20
+ kb_config = kb_config.model_dump()
21
+ table_name = kb_config['table_name']
22
+ schema = kb_config['schema']
23
+
24
+ # Create a node client
25
+ node_client = Node(kb_run.kb_node_url)
26
+
27
+ # Read the parquet file
28
+ df = pd.read_parquet("./wikipedia_kb/data/wikipedia_kb.parquet")
29
+
30
+ try:
31
+ # Create the table
32
+ logger.info(f"Creating table {table_name}")
33
+ await node_client.create_dynamic_table(table_name, schema)
34
+
35
+ # Convert DataFrame rows to dictionaries and add them to the table
36
+ logger.info("Adding rows to table")
37
+ for _, row in tqdm(df.iterrows(), total=len(df)):
38
+ row_data = {
39
+ "id": int(row['id']),
40
+ "url": str(row['url']),
41
+ "title": str(row['title']),
42
+ "text": str(row['text'])
43
+ }
44
+
45
+ await node_client.add_dynamic_row(table_name, row_data)
46
+
47
+ logger.info(f"Successfully populated {table_name} table with {len(df)} rows")
48
+
49
+
50
+ except Exception as e:
51
+ raise