Spaces:
Sleeping
Sleeping
Fredrik Sitje
commited on
Commit
·
cbf05d1
1
Parent(s):
47ec288
Make a bulk commit for all Unknown answers when a user is registered.
Browse files- src/streamlit_app.py +43 -4
src/streamlit_app.py
CHANGED
|
@@ -298,18 +298,57 @@ def auto_score_all_unknown_answers_for_new_user(username):
|
|
| 298 |
"""
|
| 299 |
Automatically score all Unknown answers for all categories when a new user is created.
|
| 300 |
This runs in the background during account creation.
|
|
|
|
| 301 |
"""
|
| 302 |
try:
|
| 303 |
# Get all unique term-category pairs
|
| 304 |
term_category_pairs = df[['term', 'category']].drop_duplicates().sort_values(['term', 'category']).values.tolist()
|
| 305 |
|
| 306 |
-
#
|
|
|
|
| 307 |
for term, category in term_category_pairs:
|
| 308 |
unknown_answers = auto_score_unknown_answers(username, term, category, df)
|
| 309 |
if unknown_answers:
|
| 310 |
-
#
|
| 311 |
-
|
| 312 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 313 |
|
| 314 |
return True
|
| 315 |
except Exception as e:
|
|
|
|
| 298 |
"""
|
| 299 |
Automatically score all Unknown answers for all categories when a new user is created.
|
| 300 |
This runs in the background during account creation.
|
| 301 |
+
Performs a single bulk commit for all Unknown answers.
|
| 302 |
"""
|
| 303 |
try:
|
| 304 |
# Get all unique term-category pairs
|
| 305 |
term_category_pairs = df[['term', 'category']].drop_duplicates().sort_values(['term', 'category']).values.tolist()
|
| 306 |
|
| 307 |
+
# Collect all Unknown answers from all categories
|
| 308 |
+
all_unknown_answers = []
|
| 309 |
for term, category in term_category_pairs:
|
| 310 |
unknown_answers = auto_score_unknown_answers(username, term, category, df)
|
| 311 |
if unknown_answers:
|
| 312 |
+
# Add term and category to each tuple for bulk update
|
| 313 |
+
for subcategory, question, answer, score in unknown_answers:
|
| 314 |
+
all_unknown_answers.append((term, category, subcategory, question, answer, score))
|
| 315 |
+
|
| 316 |
+
# If no Unknown answers found, return early
|
| 317 |
+
if not all_unknown_answers:
|
| 318 |
+
return True
|
| 319 |
+
|
| 320 |
+
# Load user dataframe once
|
| 321 |
+
user_df = load_user_data(username)
|
| 322 |
+
|
| 323 |
+
# Get current timestamp once for all updates
|
| 324 |
+
current_timestamp = pd.Timestamp.now()
|
| 325 |
+
|
| 326 |
+
# Update all Unknown answers in memory
|
| 327 |
+
updated_count = 0
|
| 328 |
+
for term, category, subcategory, question, answer, score in all_unknown_answers:
|
| 329 |
+
# Find the matching row
|
| 330 |
+
mask = (
|
| 331 |
+
(user_df['term'] == term) &
|
| 332 |
+
(user_df['category'] == category) &
|
| 333 |
+
(user_df['subcategory'] == subcategory) &
|
| 334 |
+
(user_df['question'] == question) &
|
| 335 |
+
(user_df['answer'] == answer)
|
| 336 |
+
)
|
| 337 |
+
|
| 338 |
+
if mask.any():
|
| 339 |
+
user_df.loc[mask, 'legal_accuracy_score'] = score
|
| 340 |
+
user_df.loc[mask, 'time_stamp'] = current_timestamp
|
| 341 |
+
updated_count += 1
|
| 342 |
+
else:
|
| 343 |
+
print(f"Warning: Could not find matching row for: {term}, {category}, {subcategory}, {question}")
|
| 344 |
+
|
| 345 |
+
if updated_count == 0:
|
| 346 |
+
print(f"Warning: No Unknown answers were updated for new user {username}")
|
| 347 |
+
return False
|
| 348 |
+
|
| 349 |
+
# Save once with a single commit message
|
| 350 |
+
commit_message = f"Auto-score all Unknown answers for new user {username}"
|
| 351 |
+
save_user_data(username, user_df, commit_message=commit_message)
|
| 352 |
|
| 353 |
return True
|
| 354 |
except Exception as e:
|