Spaces:
Running
Running
from data_access import get_pg_sync_connection | |
conn = get_pg_sync_connection() | |
def create_eval_database(): | |
"""Create SQLite database with a proper relational structure.""" | |
# Connect to the database (creates it if it doesn't exist) | |
cursor = conn.cursor() | |
cursor.execute(''' | |
CREATE TABLE IF NOT EXISTS question_groups ( | |
id SERIAL PRIMARY KEY, | |
group_name TEXT NOT NULL, | |
description TEXT | |
); | |
''') | |
# Create questions table | |
cursor.execute(''' | |
CREATE TABLE IF NOT EXISTS questions ( | |
id SERIAL PRIMARY KEY, | |
question_text TEXT NOT NULL, | |
question_group_id INTEGER, | |
CONSTRAINT unique_question_text UNIQUE (question_text), | |
FOREIGN KEY (question_group_id) REFERENCES question_groups (id); | |
''') | |
cursor.execute(''' | |
CREATE TABLE IF NOT EXISTS source_finders ( | |
id SERIAL PRIMARY KEY, | |
source_finder_type TEXT NOT NULL, | |
description TEXT, | |
source_finder_version TEXT NOT NULL | |
); | |
''') | |
cursor.execute(''' | |
CREATE TABLE IF NOT EXISTS source_finder_runs ( | |
id SERIAL PRIMARY KEY, | |
run_id INTEGER NOT NULL, | |
source_finder_id INTEGER NOT NULL, | |
description TEXT, | |
FOREIGN KEY (source_finder_id) REFERENCES source_finders(id), | |
CONSTRAINT unique_source_per_run_id UNIQUE(run_id, source_finder_id) | |
); | |
''') | |
cursor.execute(''' | |
CREATE TABLE IF NOT EXISTS source_finder_run_question_metadata ( | |
id SERIAL PRIMARY KEY, | |
question_id INTEGER NOT NULL, | |
source_finder_run_id INTEGER NOT NULL, | |
metadata JSON, | |
FOREIGN KEY (source_finder_run_id) REFERENCES source_finder_runs(id),d4pnrdvjqlt9gg | |
FOREIGN KEY (question_id) REFERENCES questions(id), | |
CONSTRAINT unique_question_per_run_id UNIQUE(question_id, source_finder_run_id) | |
); | |
''') | |
# Create table for logging all sources from each run | |
cursor.execute(''' | |
CREATE TABLE IF NOT EXISTS source_run_results ( | |
id SERIAL PRIMARY KEY, | |
source_finder_run_id INTEGER NOT NULL, | |
run_timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP, | |
question_id INTEGER NOT NULL, | |
tractate TEXT NOT NULL, | |
folio TEXT NOT NULL, | |
sugya_id TEXT NOT NULL, | |
rank INTEGER NOT NULL, | |
reason TEXT, | |
FOREIGN KEY (source_finder_run_id) REFERENCES source_finder_runs(id), | |
FOREIGN KEY (question_id) REFERENCES questions(id), | |
CONSTRAINT constraint source_run_results_pk UNIQUE(source_finder_run_id, question_id, sugya_id) | |
); | |
''') | |
conn.commit() | |
conn.close() | |
def load_source_finders(): | |
cursor = conn.cursor() | |
for item in ["claude_sources", "keywords", "lenses"]: | |
cursor.execute("INSERT INTO source_finders (source_finder_type, source_finder_version) VALUES (%s, 1)", (item,)) | |
conn.commit() | |
if __name__ == '__main__': | |
# Create the database | |
create_eval_database() | |