Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -4,8 +4,7 @@ from parser import parse_python_code
|
|
4 |
import os
|
5 |
import json
|
6 |
import io
|
7 |
-
import
|
8 |
-
from database import init_db, populate_sample_db
|
9 |
|
10 |
app = Flask(__name__)
|
11 |
|
@@ -14,50 +13,6 @@ def reconstruct_code(parts):
|
|
14 |
sorted_parts = sorted(parts, key=lambda p: p['location'][0])
|
15 |
return ''.join(part['source'] for part in sorted_parts)
|
16 |
|
17 |
-
def is_subsequence(subseq, seq):
|
18 |
-
"""Check if subseq is a subsequence of seq."""
|
19 |
-
it = iter(seq)
|
20 |
-
return all(item in it for item in subseq)
|
21 |
-
|
22 |
-
def query_programs(operations):
|
23 |
-
"""Query the database for programs matching the operations sequence."""
|
24 |
-
conn = sqlite3.connect('python_programs.db')
|
25 |
-
c = conn.cursor()
|
26 |
-
c.execute("SELECT id, code, sequence, vectors FROM programs")
|
27 |
-
results = []
|
28 |
-
for row in c.fetchall():
|
29 |
-
program_id, code, sequence_str, vectors_str = row
|
30 |
-
sequence = sequence_str.split(',')
|
31 |
-
vectors = eval(vectors_str) # Convert string back to list (use JSON in production)
|
32 |
-
if is_subsequence(operations, sequence):
|
33 |
-
# Compute similarity (simple average vector for now)
|
34 |
-
program_vector = sum(vectors, []) / len(vectors) if vectors else [0, 0, 0, 0, 0, 0]
|
35 |
-
query_vector = sum([create_vector(op, 0, (1, 1), 100, []) for op in operations], []) / len(operations) if operations else [0, 0, 0, 0, 0, 0]
|
36 |
-
similarity = cosine_similarity([program_vector], [query_vector])[0][0] if program_vector and query_vector else 0
|
37 |
-
results.append({'id': program_id, 'code': code, 'similarity': similarity})
|
38 |
-
conn.close()
|
39 |
-
return sorted(results, key=lambda x: x['similarity'], reverse=True)[:5] # Top 5 matches
|
40 |
-
|
41 |
-
from sklearn.metrics.pairwise import cosine_similarity
|
42 |
-
import numpy as np
|
43 |
-
|
44 |
-
def create_vector(category, level, location, total_lines, parent_path):
|
45 |
-
"""Helper to create a vector for query (matches parser's create_vector)."""
|
46 |
-
category_map = {
|
47 |
-
'import': 1, 'function': 2, 'async_function': 3, 'class': 4,
|
48 |
-
'if': 5, 'while': 6, 'for': 7, 'try': 8, 'expression': 9, 'spacer': 10,
|
49 |
-
'other': 11, 'elif': 12, 'else': 13, 'except': 14, 'finally': 15, 'return': 16,
|
50 |
-
'assigned_variable': 17, 'input_variable': 18, 'returned_variable': 19
|
51 |
-
}
|
52 |
-
category_id = category_map.get(category, 0)
|
53 |
-
start_line, end_line = location
|
54 |
-
span = (end_line - start_line + 1) / total_lines
|
55 |
-
center_pos = ((start_line + end_line) / 2) / total_lines
|
56 |
-
parent_depth = len(parent_path)
|
57 |
-
parent_weight = sum(category_map.get(parent.split('[')[0].lower(), 0) * (1 / (i + 1))
|
58 |
-
for i, parent in enumerate(parent_path)) / max(1, len(category_map))
|
59 |
-
return [category_id, level, center_pos, span, parent_depth, parent_weight]
|
60 |
-
|
61 |
@app.route('/', methods=['GET', 'POST'])
|
62 |
def index():
|
63 |
if request.method == 'POST':
|
@@ -77,10 +32,10 @@ def index():
|
|
77 |
with open(file_path, 'r') as f:
|
78 |
code_input = f.read()
|
79 |
parts, sequence = parse_python_code(code_input)
|
80 |
-
# Store in
|
81 |
vectors = [part['vector'] for part in parts]
|
82 |
-
|
83 |
-
store_program(code_input, sequence, vectors)
|
84 |
elif 'code' in request.form and request.form['code'].strip():
|
85 |
code_input = request.form['code']
|
86 |
filename = request.form.get('filename', 'unnamed.py') or 'unnamed.py'
|
@@ -88,12 +43,13 @@ def index():
|
|
88 |
filename += '.py'
|
89 |
parts, sequence = parse_python_code(code_input)
|
90 |
vectors = [part['vector'] for part in parts]
|
91 |
-
|
92 |
-
store_program(code_input, sequence, vectors)
|
93 |
elif 'query_ops' in request.form and request.form['query_ops'].strip():
|
94 |
# Handle query for operations
|
95 |
operations = [op.strip() for op in request.form['query_ops'].split(',')]
|
96 |
-
|
|
|
97 |
return render_template(
|
98 |
'results_partial.html',
|
99 |
parts=None,
|
@@ -117,8 +73,8 @@ def index():
|
|
117 |
return 'No file, code, or query provided', 400
|
118 |
|
119 |
# Initial page load
|
120 |
-
|
121 |
-
populate_sample_db() # Populate with sample data
|
122 |
return render_template('index.html', parts=None, filename=None, reconstructed_code=None, code_input=None, query_results=None)
|
123 |
|
124 |
@app.route('/export_json', methods=['POST'])
|
|
|
4 |
import os
|
5 |
import json
|
6 |
import io
|
7 |
+
from database import init_chromadb, populate_sample_db, store_program, query_programs
|
|
|
8 |
|
9 |
app = Flask(__name__)
|
10 |
|
|
|
13 |
sorted_parts = sorted(parts, key=lambda p: p['location'][0])
|
14 |
return ''.join(part['source'] for part in sorted_parts)
|
15 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
@app.route('/', methods=['GET', 'POST'])
|
17 |
def index():
|
18 |
if request.method == 'POST':
|
|
|
32 |
with open(file_path, 'r') as f:
|
33 |
code_input = f.read()
|
34 |
parts, sequence = parse_python_code(code_input)
|
35 |
+
# Store in ChromaDB
|
36 |
vectors = [part['vector'] for part in parts]
|
37 |
+
client = init_chromadb()
|
38 |
+
store_program(client, code_input, sequence, vectors)
|
39 |
elif 'code' in request.form and request.form['code'].strip():
|
40 |
code_input = request.form['code']
|
41 |
filename = request.form.get('filename', 'unnamed.py') or 'unnamed.py'
|
|
|
43 |
filename += '.py'
|
44 |
parts, sequence = parse_python_code(code_input)
|
45 |
vectors = [part['vector'] for part in parts]
|
46 |
+
client = init_chromadb()
|
47 |
+
store_program(client, code_input, sequence, vectors)
|
48 |
elif 'query_ops' in request.form and request.form['query_ops'].strip():
|
49 |
# Handle query for operations
|
50 |
operations = [op.strip() for op in request.form['query_ops'].split(',')]
|
51 |
+
client = init_chromadb()
|
52 |
+
query_results = query_programs(client, operations)
|
53 |
return render_template(
|
54 |
'results_partial.html',
|
55 |
parts=None,
|
|
|
73 |
return 'No file, code, or query provided', 400
|
74 |
|
75 |
# Initial page load
|
76 |
+
client = init_chromadb() # Ensure ChromaDB is initialized
|
77 |
+
populate_sample_db(client) # Populate with sample data
|
78 |
return render_template('index.html', parts=None, filename=None, reconstructed_code=None, code_input=None, query_results=None)
|
79 |
|
80 |
@app.route('/export_json', methods=['POST'])
|