Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -5,9 +5,9 @@ import os
|
|
5 |
import json
|
6 |
import io
|
7 |
import subprocess # To call process_hf_dataset.py
|
|
|
8 |
|
9 |
# User-configurable variables
|
10 |
-
DB_NAME = "python_programs" # ChromaDB collection name
|
11 |
UPLOAD_DIR = "./uploads" # Directory for uploads
|
12 |
|
13 |
app = Flask(__name__)
|
@@ -37,7 +37,6 @@ def index():
|
|
37 |
code_input = f.read()
|
38 |
parts, sequence = parse_python_code(code_input)
|
39 |
# Store in ChromaDB
|
40 |
-
from database import init_chromadb, store_program
|
41 |
client = init_chromadb()
|
42 |
vectors = [part['vector'] for part in parts]
|
43 |
store_program(client, code_input, sequence, vectors, DB_NAME)
|
@@ -48,13 +47,11 @@ def index():
|
|
48 |
filename += '.py'
|
49 |
parts, sequence = parse_python_code(code_input)
|
50 |
vectors = [part['vector'] for part in parts]
|
51 |
-
from database import init_chromadb, store_program
|
52 |
client = init_chromadb()
|
53 |
store_program(client, code_input, sequence, vectors, DB_NAME)
|
54 |
elif 'query_ops' in request.form and request.form['query_ops'].strip():
|
55 |
# Handle query for operations (category sequence)
|
56 |
operations = [op.strip() for op in request.form['query_ops'].split(',')]
|
57 |
-
from database import load_chromadb_from_hf, query_programs
|
58 |
client = load_chromadb_from_hf()
|
59 |
query_results = query_programs(client, operations, DB_NAME)
|
60 |
return render_template(
|
@@ -68,7 +65,6 @@ def index():
|
|
68 |
elif 'semantic_query' in request.form and request.form['semantic_query'].strip():
|
69 |
# Handle semantic query (natural language description)
|
70 |
semantic_query = request.form['semantic_query']
|
71 |
-
from database import load_chromadb_from_hf, query_programs
|
72 |
client = load_chromadb_from_hf()
|
73 |
query_results = query_programs(client, None, DB_NAME, semantic_query=semantic_query)
|
74 |
return render_template(
|
@@ -80,8 +76,17 @@ def index():
|
|
80 |
query_results=query_results
|
81 |
)
|
82 |
elif 'process_hf' in request.form:
|
83 |
-
# Trigger processing of Hugging Face dataset
|
84 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
85 |
subprocess.run(['python', 'process_hf_dataset.py'], check=True)
|
86 |
return render_template(
|
87 |
'results_partial.html',
|
@@ -90,10 +95,31 @@ def index():
|
|
90 |
reconstructed_code=None,
|
91 |
code_input=None,
|
92 |
query_results=None,
|
93 |
-
message="Hugging Face dataset processed and stored successfully."
|
94 |
)
|
95 |
except subprocess.CalledProcessError as e:
|
96 |
return f"Error processing Hugging Face dataset: {e}", 500
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
97 |
|
98 |
if parts:
|
99 |
indexed_parts = [{'index': i + 1, **part} for i, part in enumerate(parts)]
|
@@ -109,15 +135,12 @@ def index():
|
|
109 |
return 'No file, code, or query provided', 400
|
110 |
|
111 |
# Initial page load
|
112 |
-
from database import load_chromadb_from_hf
|
113 |
client = load_chromadb_from_hf()
|
114 |
# If no dataset exists locally, populate with samples
|
115 |
try:
|
116 |
if not client.list_collections()[0].name == DB_NAME:
|
117 |
-
from database import populate_sample_db
|
118 |
populate_sample_db(client)
|
119 |
except:
|
120 |
-
from database import populate_sample_db
|
121 |
populate_sample_db(client)
|
122 |
return render_template('index.html', parts=None, filename=None, reconstructed_code=None, code_input=None, query_results=None)
|
123 |
|
|
|
5 |
import json
|
6 |
import io
|
7 |
import subprocess # To call process_hf_dataset.py
|
8 |
+
from database import init_chromadb, populate_sample_db, store_program, query_programs, load_chromadb_from_hf, DB_NAME
|
9 |
|
10 |
# User-configurable variables
|
|
|
11 |
UPLOAD_DIR = "./uploads" # Directory for uploads
|
12 |
|
13 |
app = Flask(__name__)
|
|
|
37 |
code_input = f.read()
|
38 |
parts, sequence = parse_python_code(code_input)
|
39 |
# Store in ChromaDB
|
|
|
40 |
client = init_chromadb()
|
41 |
vectors = [part['vector'] for part in parts]
|
42 |
store_program(client, code_input, sequence, vectors, DB_NAME)
|
|
|
47 |
filename += '.py'
|
48 |
parts, sequence = parse_python_code(code_input)
|
49 |
vectors = [part['vector'] for part in parts]
|
|
|
50 |
client = init_chromadb()
|
51 |
store_program(client, code_input, sequence, vectors, DB_NAME)
|
52 |
elif 'query_ops' in request.form and request.form['query_ops'].strip():
|
53 |
# Handle query for operations (category sequence)
|
54 |
operations = [op.strip() for op in request.form['query_ops'].split(',')]
|
|
|
55 |
client = load_chromadb_from_hf()
|
56 |
query_results = query_programs(client, operations, DB_NAME)
|
57 |
return render_template(
|
|
|
65 |
elif 'semantic_query' in request.form and request.form['semantic_query'].strip():
|
66 |
# Handle semantic query (natural language description)
|
67 |
semantic_query = request.form['semantic_query']
|
|
|
68 |
client = load_chromadb_from_hf()
|
69 |
query_results = query_programs(client, None, DB_NAME, semantic_query=semantic_query)
|
70 |
return render_template(
|
|
|
76 |
query_results=query_results
|
77 |
)
|
78 |
elif 'process_hf' in request.form:
|
79 |
+
# Trigger processing of Hugging Face dataset with fresh database
|
80 |
try:
|
81 |
+
# Reset ChromaDB collection
|
82 |
+
client = init_chromadb()
|
83 |
+
try:
|
84 |
+
client.delete_collection(DB_NAME)
|
85 |
+
except:
|
86 |
+
pass # Collection may not exist
|
87 |
+
client.create_collection(DB_NAME)
|
88 |
+
|
89 |
+
# Process dataset
|
90 |
subprocess.run(['python', 'process_hf_dataset.py'], check=True)
|
91 |
return render_template(
|
92 |
'results_partial.html',
|
|
|
95 |
reconstructed_code=None,
|
96 |
code_input=None,
|
97 |
query_results=None,
|
98 |
+
message="Hugging Face dataset processed and stored successfully with fresh database."
|
99 |
)
|
100 |
except subprocess.CalledProcessError as e:
|
101 |
return f"Error processing Hugging Face dataset: {e}", 500
|
102 |
+
elif 'reset_db' in request.form:
|
103 |
+
# Reset ChromaDB collection
|
104 |
+
try:
|
105 |
+
client = init_chromadb()
|
106 |
+
try:
|
107 |
+
client.delete_collection(DB_NAME)
|
108 |
+
except:
|
109 |
+
pass # Collection may not exist
|
110 |
+
client.create_collection(DB_NAME)
|
111 |
+
populate_sample_db(client) # Optionally repopulate with samples
|
112 |
+
return render_template(
|
113 |
+
'results_partial.html',
|
114 |
+
parts=None,
|
115 |
+
filename="Database Reset",
|
116 |
+
reconstructed_code=None,
|
117 |
+
code_input=None,
|
118 |
+
query_results=None,
|
119 |
+
message="Database reset and repopulated with samples successfully."
|
120 |
+
)
|
121 |
+
except Exception as e:
|
122 |
+
return f"Error resetting database: {e}", 500
|
123 |
|
124 |
if parts:
|
125 |
indexed_parts = [{'index': i + 1, **part} for i, part in enumerate(parts)]
|
|
|
135 |
return 'No file, code, or query provided', 400
|
136 |
|
137 |
# Initial page load
|
|
|
138 |
client = load_chromadb_from_hf()
|
139 |
# If no dataset exists locally, populate with samples
|
140 |
try:
|
141 |
if not client.list_collections()[0].name == DB_NAME:
|
|
|
142 |
populate_sample_db(client)
|
143 |
except:
|
|
|
144 |
populate_sample_db(client)
|
145 |
return render_template('index.html', parts=None, filename=None, reconstructed_code=None, code_input=None, query_results=None)
|
146 |
|