dx2102 commited on
Commit
3e53e47
1 Parent(s): d468aa0

Upload 4 files

Browse files
Files changed (5) hide show
  1. .gitattributes +1 -0
  2. Dockerfile +6 -0
  3. exported.txt +3 -0
  4. server.py +52 -0
  5. website.html +105 -0
.gitattributes CHANGED
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
 
 
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
36
+ exported.txt filter=lfs diff=lfs merge=lfs -text
Dockerfile ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ FROM python:3.9
2
+ WORKDIR /app
3
+ COPY --chown=user . /app
4
+
5
+ RUN pip install fastapi uvicorn
6
+ CMD ["uvicorn", "server:app", "--host", "0.0.0.0", "--port", "7860"]
exported.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:d9799396336c9763476c49cae9a3b02feceae9b76941e0baac3930bb52477036
3
+ size 3571992971
server.py ADDED
@@ -0,0 +1,52 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI, Query
2
+ from fastapi.responses import HTMLResponse, JSONResponse
3
+ from fastapi.staticfiles import StaticFiles
4
+ import json
5
+ from dataclasses import dataclass, asdict
6
+
7
+ '''
8
+
9
+ cd python/lean-expr-tree
10
+ uvicorn server:app --reload
11
+
12
+ '''
13
+
14
+ app = FastAPI()
15
+
16
+ # return website.html when accessing root
17
+ @app.get("/")
18
+ async def get_root():
19
+ return HTMLResponse(content=open('website.html', 'r').read())
20
+
21
+ @dataclass
22
+ class Decl:
23
+ file: str
24
+ name: str
25
+ typ: str
26
+ docs: str
27
+ ast: str
28
+
29
+ def parse_exported_file():
30
+ decls = []
31
+ print('Reading exported.txt')
32
+ lines = open('exported.txt', 'r').read() # 1000000)
33
+ print('Splitting exported.txt')
34
+ lines = lines.split('----ast----\n')[1:]
35
+ print('Parsing exported.txt')
36
+ for block in lines:
37
+ parts = block.split('--ast--\n')[:-1]
38
+ if len(parts) != 5:
39
+ continue
40
+ file, name, typ, docs, ast = parts
41
+ decls.append(Decl(file, name, typ, docs, ast))
42
+ print('Finished parsing exported.txt')
43
+ return decls
44
+
45
+ decls = parse_exported_file()
46
+
47
+ @app.get("/search")
48
+ async def search(query: str = Query(...)):
49
+ matched = [decl for decl in decls if query in decl.name]
50
+ matched = sorted(matched, key=lambda decl: decl.name)
51
+ matched_results = [asdict(decl) for decl in matched]
52
+ return JSONResponse(content=matched_results)
website.html ADDED
@@ -0,0 +1,105 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <!DOCTYPE html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="UTF-8">
5
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
+ <title>Syntax Tree Visualization</title>
7
+ <script src="https://cdn.tailwindcss.com"></script>
8
+ </head>
9
+ <body class="bg-gray-50 text-gray-900">
10
+ <div class="container mx-auto mt-10">
11
+ <h1 class="text-4xl font-bold mb-5">Syntax Tree Visualization</h1>
12
+ <form class="mb-5" onsubmit="event.preventDefault(); search();">
13
+ <input id="search-input" type="text" value="Lean" class="w-full p-2 border border-gray-300 rounded-md">
14
+ <button class="mt-2 bg-blue-500 text-white px-4 py-2 rounded-md">Search</button>
15
+ </form>
16
+ <div id="results" class="mb-5"></div>
17
+ <canvas id="tree-canvas" class="mx-auto border border-gray-300 rounded-md"></canvas>
18
+ </div>
19
+
20
+ <script>
21
+ async function search() {
22
+ console.log('search');
23
+ const query = document.getElementById('search-input').value;
24
+ const response = await fetch(`/search?query=${query}`);
25
+ const data = await response.json();
26
+ console.log(data);
27
+
28
+ const resultsDiv = document.getElementById('results');
29
+ resultsDiv.innerHTML = '';
30
+
31
+ data.forEach((result, index) => {
32
+ const button = document.createElement('button');
33
+ button.innerText = result.name;
34
+ button.className = 'block w-full text-left p-2 bg-gray-200 my-2 rounded-md';
35
+ if (data[index].ast !== 'expression tree does not exist\n') {
36
+ data[index].ast = JSON.parse(data[index].ast);
37
+ console.log(data[index].ast);
38
+ }
39
+ button.onclick = () => visualizeTree(data[index].ast);
40
+ resultsDiv.appendChild(button);
41
+ });
42
+ visualizeTree(data[0].ast);
43
+ }
44
+
45
+ const canvas = document.getElementById('tree-canvas');
46
+ const ctx = canvas.getContext('2d');
47
+ ctx.font = '100px Arial';
48
+
49
+ canvas.width = Math.floor(window.innerWidth * 0.8) * 2;
50
+ canvas.height = Math.floor(window.innerHeight * 0.8) * 2;
51
+ canvas.style.width = Math.floor(window.innerWidth * 0.8) + 'px';
52
+ canvas.style.height = Math.floor(window.innerHeight * 0.8) + 'px';
53
+
54
+ function visualizeTree(tree) {
55
+ ctx.fillStyle = 'white';
56
+ ctx.clearRect(0, 0, canvas.width, canvas.height);
57
+ const width = drawNode(tree, 10, 10);
58
+ if (width > canvas.width) {
59
+ canvas.width = width;
60
+ canvas.style.width = width / 2 + 'px';
61
+ }
62
+ // draw again
63
+ drawNode(tree, 10, 10);
64
+ }
65
+
66
+ const sizeX = 60;
67
+ const sizeY = 30;
68
+
69
+ function drawNode(node, x, y) {
70
+ // This is recursive. Returns the width of the node.
71
+
72
+ // if node is still an object, it is a non-leaf node
73
+ const isLeaf = typeof node !== 'object';
74
+ if (isLeaf) {
75
+ // draw leaf node simply as text
76
+ ctx.fillStyle = 'rgba(256, 0, 0, 0.1)';
77
+ ctx.fillRect(x, y, sizeX, sizeY);
78
+
79
+ ctx.fillStyle = 'black';
80
+ ctx.font = '20px Arial';
81
+ ctx.fillText(node, x, y + sizeY);
82
+ return sizeX;
83
+ }
84
+ // draw non-leaf node as a black square
85
+ ctx.fillStyle = 'rgba(0, 0, 0, 0.1)';
86
+ ctx.fillRect(x, y, sizeX, sizeY);
87
+
88
+ ctx.fillStyle = 'black';
89
+ ctx.font = '20px Arial';
90
+ ctx.fillText(node.kind, x, y + sizeY);
91
+
92
+ // draw children
93
+ const childY = y + sizeY;
94
+ let childX = x;
95
+ let totalWidth = 0;
96
+ node.children.forEach((child, index) => {
97
+ let width = drawNode(child, childX, childY);
98
+ childX += width;
99
+ totalWidth += width;
100
+ });
101
+ return totalWidth;
102
+ }
103
+ </script>
104
+ </body>
105
+ </html>