robinroy03 commited on
Commit
dc1da67
1 Parent(s): e28bb62

first commit

Browse files
Files changed (4) hide show
  1. .gitignore +294 -0
  2. Dockerfile +16 -0
  3. requirements.txt +45 -0
  4. server.py +137 -0
.gitignore ADDED
@@ -0,0 +1,294 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Byte-compiled / optimized / DLL files
2
+ __pycache__/
3
+ *.py[cod]
4
+ *$py.class
5
+
6
+ # C extensions
7
+ *.so
8
+
9
+ # Distribution / packaging
10
+ .Python
11
+ build/
12
+ develop-eggs/
13
+ dist/
14
+ downloads/
15
+ eggs/
16
+ .eggs/
17
+ lib/
18
+ lib64/
19
+ parts/
20
+ sdist/
21
+ var/
22
+ wheels/
23
+ share/python-wheels/
24
+ *.egg-info/
25
+ .installed.cfg
26
+ *.egg
27
+ MANIFEST
28
+
29
+ # PyInstaller
30
+ # Usually these files are written by a python script from a template
31
+ # before PyInstaller builds the exe, so as to inject date/other infos into it.
32
+ *.manifest
33
+ *.spec
34
+
35
+ # Installer logs
36
+ pip-log.txt
37
+ pip-delete-this-directory.txt
38
+
39
+ # Unit test / coverage reports
40
+ htmlcov/
41
+ .tox/
42
+ .nox/
43
+ .coverage
44
+ .coverage.*
45
+ .cache
46
+ nosetests.xml
47
+ coverage.xml
48
+ *.cover
49
+ *.py,cover
50
+ .hypothesis/
51
+ .pytest_cache/
52
+ cover/
53
+
54
+ # Translations
55
+ *.mo
56
+ *.pot
57
+
58
+ # Django stuff:
59
+ *.log
60
+ local_settings.py
61
+ db.sqlite3
62
+ db.sqlite3-journal
63
+
64
+ # Flask stuff:
65
+ instance/
66
+ .webassets-cache
67
+
68
+ # Scrapy stuff:
69
+ .scrapy
70
+
71
+ # Sphinx documentation
72
+ docs/_build/
73
+
74
+ # PyBuilder
75
+ .pybuilder/
76
+ target/
77
+
78
+ # Jupyter Notebook
79
+ .ipynb_checkpoints
80
+
81
+ # IPython
82
+ profile_default/
83
+ ipython_config.py
84
+
85
+ # pyenv
86
+ # For a library or package, you might want to ignore these files since the code is
87
+ # intended to run in multiple environments; otherwise, check them in:
88
+ # .python-version
89
+
90
+ # pipenv
91
+ # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
92
+ # However, in case of collaboration, if having platform-specific dependencies or dependencies
93
+ # having no cross-platform support, pipenv may install dependencies that don't work, or not
94
+ # install all needed dependencies.
95
+ #Pipfile.lock
96
+
97
+ # poetry
98
+ # Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
99
+ # This is especially recommended for binary packages to ensure reproducibility, and is more
100
+ # commonly ignored for libraries.
101
+ # https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
102
+ #poetry.lock
103
+
104
+ # pdm
105
+ # Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
106
+ #pdm.lock
107
+ # pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
108
+ # in version control.
109
+ # https://pdm.fming.dev/latest/usage/project/#working-with-version-control
110
+ .pdm.toml
111
+ .pdm-python
112
+ .pdm-build/
113
+
114
+ # PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
115
+ __pypackages__/
116
+
117
+ # Celery stuff
118
+ celerybeat-schedule
119
+ celerybeat.pid
120
+
121
+ # SageMath parsed files
122
+ *.sage.py
123
+
124
+ # Environments
125
+ .env
126
+ .venv
127
+ env/
128
+ venv/
129
+ ENV/
130
+ env.bak/
131
+ venv.bak/
132
+
133
+ # Spyder project settings
134
+ .spyderproject
135
+ .spyproject
136
+
137
+ # Rope project settings
138
+ .ropeproject
139
+
140
+ # mkdocs documentation
141
+ /site
142
+
143
+ # mypy
144
+ .mypy_cache/
145
+ .dmypy.json
146
+ dmypy.json
147
+
148
+ # Pyre type checker
149
+ .pyre/
150
+
151
+ # pytype static type analyzer
152
+ .pytype/
153
+
154
+ # Cython debug symbols
155
+ cython_debug/
156
+
157
+ # PyCharm
158
+ # JetBrains specific template is maintained in a separate JetBrains.gitignore that can
159
+ # be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
160
+ # and can be added to the global gitignore or merged into this file. For a more nuclear
161
+ # option (not recommended) you can uncomment the following to ignore the entire idea folder.
162
+ #.idea/
163
+
164
+
165
+ # Logs
166
+ logs
167
+ *.log
168
+ npm-debug.log*
169
+ yarn-debug.log*
170
+ yarn-error.log*
171
+ lerna-debug.log*
172
+ .pnpm-debug.log*
173
+
174
+ # Diagnostic reports (https://nodejs.org/api/report.html)
175
+ report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
176
+
177
+ # Runtime data
178
+ pids
179
+ *.pid
180
+ *.seed
181
+ *.pid.lock
182
+
183
+ # Directory for instrumented libs generated by jscoverage/JSCover
184
+ lib-cov
185
+
186
+ # Coverage directory used by tools like istanbul
187
+ coverage
188
+ *.lcov
189
+
190
+ # nyc test coverage
191
+ .nyc_output
192
+
193
+ # Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
194
+ .grunt
195
+
196
+ # Bower dependency directory (https://bower.io/)
197
+ bower_components
198
+
199
+ # node-waf configuration
200
+ .lock-wscript
201
+
202
+ # Compiled binary addons (https://nodejs.org/api/addons.html)
203
+ build/Release
204
+
205
+ # Dependency directories
206
+ node_modules/
207
+ jspm_packages/
208
+
209
+ # Snowpack dependency directory (https://snowpack.dev/)
210
+ web_modules/
211
+
212
+ # TypeScript cache
213
+ *.tsbuildinfo
214
+
215
+ # Optional npm cache directory
216
+ .npm
217
+
218
+ # Optional eslint cache
219
+ .eslintcache
220
+
221
+ # Optional stylelint cache
222
+ .stylelintcache
223
+
224
+ # Microbundle cache
225
+ .rpt2_cache/
226
+ .rts2_cache_cjs/
227
+ .rts2_cache_es/
228
+ .rts2_cache_umd/
229
+
230
+ # Optional REPL history
231
+ .node_repl_history
232
+
233
+ # Output of 'npm pack'
234
+ *.tgz
235
+
236
+ # Yarn Integrity file
237
+ .yarn-integrity
238
+
239
+ # dotenv environment variable files
240
+ .env
241
+ .env.development.local
242
+ .env.test.local
243
+ .env.production.local
244
+ .env.local
245
+
246
+ # parcel-bundler cache (https://parceljs.org/)
247
+ .cache
248
+ .parcel-cache
249
+
250
+ # Next.js build output
251
+ .next
252
+ out
253
+
254
+ # Nuxt.js build / generate output
255
+ .nuxt
256
+ dist
257
+
258
+ # Gatsby files
259
+ .cache/
260
+ # Comment in the public line in if your project uses Gatsby and not Next.js
261
+ # https://nextjs.org/blog/next-9-1#public-directory-support
262
+ # public
263
+
264
+ # vuepress build output
265
+ .vuepress/dist
266
+
267
+ # vuepress v2.x temp and cache directory
268
+ .temp
269
+ .cache
270
+
271
+ # Docusaurus cache and generated files
272
+ .docusaurus
273
+
274
+ # Serverless directories
275
+ .serverless/
276
+
277
+ # FuseBox cache
278
+ .fusebox/
279
+
280
+ # DynamoDB Local files
281
+ .dynamodb/
282
+
283
+ # TernJS port file
284
+ .tern-port
285
+
286
+ # Stores VSCode versions used for testing VSCode extensions
287
+ .vscode-test
288
+
289
+ # yarn v2
290
+ .yarn/cache
291
+ .yarn/unplugged
292
+ .yarn/build-state.yml
293
+ .yarn/install-state.gz
294
+ .pnp.*
Dockerfile ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM python:3
2
+
3
+ RUN useradd -m -u 1000 user
4
+ USER user
5
+ ENV HOME=/home/user \
6
+ PATH=/home/user/.local/bin:$PATH
7
+
8
+ COPY --chown=user . $HOME/roadmapapi
9
+
10
+ WORKDIR $HOME/roadmapapi
11
+
12
+ RUN mkdir $HOME/.cache
13
+
14
+ RUN pip install --no-cache-dir --upgrade -r requirements.txt
15
+
16
+ CMD ["gunicorn", "-w", "5", "-b", "0.0.0.0:7860","server:app"]
requirements.txt ADDED
@@ -0,0 +1,45 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ annotated-types==0.7.0
2
+ anyio==4.6.0
3
+ blinker==1.8.2
4
+ cachetools==5.5.0
5
+ certifi==2024.8.30
6
+ charset-normalizer==3.3.2
7
+ click==8.1.7
8
+ distro==1.9.0
9
+ Flask==3.0.3
10
+ Flask-Cors==5.0.0
11
+ google-ai-generativelanguage==0.6.10
12
+ google-api-core==2.20.0
13
+ google-api-python-client==2.147.0
14
+ google-auth==2.35.0
15
+ google-auth-httplib2==0.2.0
16
+ google-generativeai==0.8.2
17
+ googleapis-common-protos==1.65.0
18
+ groq==0.11.0
19
+ grpcio==1.66.2
20
+ grpcio-status==1.66.2
21
+ gunicorn==23.0.0
22
+ h11==0.14.0
23
+ httpcore==1.0.6
24
+ httplib2==0.22.0
25
+ httpx==0.27.2
26
+ idna==3.10
27
+ itsdangerous==2.2.0
28
+ Jinja2==3.1.4
29
+ MarkupSafe==2.1.5
30
+ packaging==24.1
31
+ proto-plus==1.24.0
32
+ protobuf==5.28.2
33
+ pyasn1==0.6.1
34
+ pyasn1_modules==0.4.1
35
+ pydantic==2.9.2
36
+ pydantic_core==2.23.4
37
+ pyparsing==3.1.4
38
+ requests==2.32.3
39
+ rsa==4.9
40
+ sniffio==1.3.1
41
+ tqdm==4.66.5
42
+ typing_extensions==4.12.2
43
+ uritemplate==4.1.1
44
+ urllib3==2.2.3
45
+ Werkzeug==3.0.4
server.py ADDED
@@ -0,0 +1,137 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import json
3
+
4
+ from flask import Flask, request
5
+ from flask_cors import CORS
6
+
7
+ import google.generativeai
8
+ google.generativeai.configure(
9
+ api_key=os.environ.get("GEMINI_API_KEY"))
10
+
11
+ app = Flask(__name__)
12
+ CORS(app)
13
+
14
+
15
+ def output_sanitizer(prompt: str) -> dict:
16
+ """
17
+ return dictionary from llm output
18
+ """
19
+ begin = prompt.find("{")
20
+ end = prompt.find("}")
21
+ output = prompt[begin: end + 1]
22
+
23
+ try:
24
+ output = json.loads(output)
25
+ except json.JSONDecodeError:
26
+ return {}
27
+
28
+ return output
29
+
30
+
31
+ def dict_style_changer(file: dict) -> dict:
32
+ """
33
+ parses it in such a way so that mermaid won't crash due to semicolons and such
34
+ """
35
+ """
36
+ {"1[a]" : ["2[b]", "3[c]"], "4[d]" : ["5[e]", "6[f]"]}
37
+ """
38
+ new = {}
39
+ count = 1
40
+
41
+ for k, v in file.items():
42
+ new_key = f"{count}[{k}]"
43
+ count += 1
44
+ temp = []
45
+
46
+ for i in v:
47
+ new_value = f"{count}[{i}]"
48
+ count += 1
49
+ temp.append(new_value)
50
+
51
+ new[new_key] = temp
52
+
53
+ return new
54
+
55
+
56
+ def dict_to_mermaid(file: dict) -> str:
57
+ """
58
+ returns a mermaid.js syntax to be rendered for the roadmap
59
+ """
60
+ file = dict_style_changer(file)
61
+ output = """
62
+ flowchart TB\n\t
63
+ """
64
+ arrow = " --> "
65
+ thick_arrow = " ==> "
66
+
67
+ for k, v in file.items():
68
+ output += f"subgraph {k}\n\t{arrow.join(v)}\n\tend\n\t"
69
+
70
+ output += thick_arrow.join(list(file.keys()))
71
+
72
+ return output
73
+
74
+
75
+ def get_completion(message: str, current_knowledge: str) -> str:
76
+ """
77
+ Returns a string with mermaid syntax.
78
+ """
79
+ sys_message = f"""
80
+ You are required to provide a learning roadmap for the given topic. The learner profile is provided. Use these data to give a very detailed learning roadmap. Use the output format shown below. Remember to optimize the roadmap as per the learners profile.
81
+
82
+ Return the output in JSON format.
83
+ Example:
84
+ {{
85
+ "<Topic>": [
86
+ "<sub topic 1>",
87
+ "<sub topic 2>"
88
+ ],
89
+ "<Topic>": [
90
+ "<sub topic 1>",
91
+ "<sub topic 2>"
92
+ ]
93
+ }}
94
+
95
+ Topic name: {message}
96
+ Learner profile: {current_knowledge}
97
+ """
98
+ llm_model = google.generativeai.GenerativeModel('gemini-1.5-pro')
99
+ chat_completion = llm_model.generate_content(sys_message)
100
+ print(chat_completion)
101
+ chat_completion_text = chat_completion.to_dict()['candidates'][0]['content']['parts'][0]['text']
102
+ chat_completion_text = chat_completion_text.replace("(", "")
103
+ chat_completion_text = chat_completion_text.replace(")", "")
104
+
105
+ print(dict_to_mermaid(output_sanitizer(chat_completion_text)))
106
+ return dict_to_mermaid(output_sanitizer(chat_completion_text))
107
+
108
+
109
+
110
+ @app.route("/gemini_output", methods=['POST'])
111
+ def gemini_output():
112
+ message = request.get_json()
113
+ print(message)
114
+ if message['prompt'] == "chess":
115
+ return {'response': """flowchart TB
116
+
117
+ subgraph 1[Fundamentals]
118
+ 2[Objective of the game] --> 3[Chessboard setup] --> 4[Piece movement King, Queen, Rook, Bishop, Knight, Pawn] --> 5[Special moves Castling, En Passant] --> 6[Check, Checkmate, and Stalemate]
119
+ end
120
+ subgraph 7[Basic Tactics]
121
+ 8[Piece value and development] --> 9[Controlling the center] --> 10[Forks] --> 11[Pins] --> 12[Skewers] --> 13[Discovered attacks] --> 14[Double attacks] --> 15[Traps]
122
+ end
123
+ subgraph 16[Openings]
124
+ 17[Principles of opening theory] --> 18[Common opening traps to avoid] --> 19[Learning a few standard openings e.g., Italian Game, Ruy Lopez, Sicilian Defense] --> 20[Understanding opening goals control of the center, piece development, king safety]
125
+ end
126
+ subgraph 21[Middlegame Strategy]
127
+ 22[Planning and evaluating positions] --> 23[Pawn structures and their weaknesses] --> 24[Identifying tactical opportunities] --> 25[Positional play improving piece placement, creating weaknesses] --> 26[Attacking and defending techniques]
128
+ end
129
+ subgraph 27[Endgames]
130
+ 28[Basic checkmates King and Queen, King and Rook] --> 29[Theoretical endgames King and Pawn vs King, Rook and Pawn vs Rook] --> 30[Key endgame principles opposition, triangulation, key squares] --> 31[Practice common endgame patterns]
131
+ end
132
+ subgraph 32[Beyond the Basics]
133
+ 33[Studying annotated master games] --> 34[Analyzing your own games] --> 35[Solving chess puzzles and tactics] --> 36[Participating in tournaments and online play] --> 37[Utilizing chess software and engines for analysis and training] --> 38[Exploring advanced concepts e.g., prophylaxis, positional sacrifice]
134
+ end
135
+ 1[Fundamentals] ==> 7[Basic Tactics] ==> 16[Openings] ==> 21[Middlegame Strategy] ==> 27[Endgames] ==> 32[Beyond the Basics]"""}
136
+ # return {"prompt": "hi " + message['prompt']}
137
+ return {"response": get_completion(message['prompt'], message['current_knowledge'])}