Spaces:
Runtime error
Runtime error
File size: 3,451 Bytes
5ec2fc9 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 | import json
import pandas as pd
from Data.database.sql_connector import engine
from src.similarity_model.preprocessing import preprocess_dataset
from src.similarity_model.embedding_engine import (
train_embedding_engine
)
def sync_projects():
projects_query = """
SELECT *
FROM Projects
WHERE Status IN (
'Completed',
'UnderReview',
'In_Progress'
)
"""
projects_df = pd.read_sql(
projects_query,
engine
)
existing_df = pd.read_sql(
"""
SELECT id
FROM PreProcessed_Projects
""",
engine
)
allowed_ids = set(
projects_df["Id"].tolist()
)
processed_ids = set(
existing_df["id"].tolist()
)
# ---------------------------------
# Remove projects no longer allowed
# ---------------------------------
ids_to_remove = (
processed_ids - allowed_ids
)
changed = False
if ids_to_remove:
ids_str = ",".join(
map(str, ids_to_remove)
)
with engine.begin() as conn:
conn.exec_driver_sql(
f"""
DELETE FROM PreProcessed_Projects
WHERE id IN ({ids_str})
"""
)
print(
f"Removed {len(ids_to_remove)} projects"
)
changed = True
# ---------------------------------
# Add new projects
# ---------------------------------
new_projects = projects_df[
~projects_df["Id"].isin(
processed_ids
)
].copy()
if len(new_projects) > 0:
print(
f"Found {len(new_projects)} new projects"
)
processed_df = preprocess_dataset(
new_projects
)
processed_df = processed_df[
[
"id",
"submittedat",
"project_title",
"studentnames",
"year",
"abstract",
"description",
"problemstatement",
"proposedsolution",
"objectives",
"full_content",
"clean_text",
"word_count",
"features"
]
]
processed_df = processed_df.rename(
columns={
"submittedat": "submitted_at",
"studentnames": "student_names",
"problemstatement": "problem_statement",
"proposedsolution": "proposed_solution"
}
)
processed_df["features"] = (
processed_df["features"]
.apply(json.dumps)
)
processed_df.to_sql(
"PreProcessed_Projects",
engine,
if_exists="append",
index=False
)
print(
f"Processed and inserted {len(processed_df)} projects"
)
changed = True
else:
print(
"No new projects found"
)
# ---------------------------------
# Rebuild FAISS only if changed
# ---------------------------------
if changed:
print(
"Updating embeddings..."
)
train_embedding_engine()
print(
"Embeddings updated"
)
else:
print(
"No changes detected"
)
if __name__ == "__main__":
sync_projects() |