Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -16,6 +16,7 @@ import datetime
|
|
| 16 |
import logging
|
| 17 |
import asyncio
|
| 18 |
import threading
|
|
|
|
| 19 |
file_lock = threading.Lock()
|
| 20 |
#logging.basicConfig(level=logging.DEBUG)
|
| 21 |
|
|
@@ -276,98 +277,131 @@ def upload_file_to_repo(
|
|
| 276 |
|
| 277 |
print(f"Uploaded {local_file_path} -> {repo_id}/{path_in_repo} [repo_type={repo_type}]")
|
| 278 |
|
| 279 |
-
def save_partial_data_to_excel(new_row, user_state):
|
| 280 |
-
|
| 281 |
-
|
| 282 |
-
|
| 283 |
|
| 284 |
-
|
| 285 |
-
|
| 286 |
-
|
| 287 |
|
| 288 |
-
|
| 289 |
-
|
| 290 |
-
|
| 291 |
-
|
| 292 |
-
|
| 293 |
-
|
| 294 |
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
|
| 298 |
|
| 299 |
-
|
| 300 |
|
| 301 |
-
|
| 302 |
-
|
| 303 |
-
|
| 304 |
-
|
| 305 |
-
|
| 306 |
-
|
| 307 |
-
|
| 308 |
-
|
| 309 |
-
|
| 310 |
-
|
| 311 |
-
|
| 312 |
-
|
| 313 |
-
|
| 314 |
|
| 315 |
-
def finalize_and_save_to_space(user_state):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 316 |
hf_token = os.getenv("write_token")
|
| 317 |
-
|
| 318 |
-
|
| 319 |
-
|
|
|
|
|
|
|
|
|
|
| 320 |
|
| 321 |
try:
|
| 322 |
with file_lock:
|
| 323 |
-
|
| 324 |
-
|
| 325 |
-
|
| 326 |
-
print("No partial_data.xlsx found, nothing to merge.")
|
| 327 |
-
df_partial = pd.DataFrame()
|
| 328 |
-
|
| 329 |
-
if os.path.exists(excel_file):
|
| 330 |
-
df_final = pd.read_excel(excel_file, engine="openpyxl")
|
| 331 |
else:
|
| 332 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 333 |
|
| 334 |
-
|
| 335 |
-
|
| 336 |
-
|
| 337 |
-
|
| 338 |
-
|
| 339 |
-
|
| 340 |
-
|
| 341 |
-
|
| 342 |
-
|
| 343 |
-
if hf_token and space_repo_id:
|
| 344 |
-
upload_file_to_repo(
|
| 345 |
-
local_file_path=excel_file,
|
| 346 |
-
repo_id=space_repo_id,
|
| 347 |
-
hf_token=hf_token,
|
| 348 |
-
path_in_repo="assessment_data.xlsx",
|
| 349 |
-
repo_type="dataset"
|
| 350 |
)
|
| 351 |
-
|
|
|
|
|
|
|
| 352 |
except Exception as e:
|
| 353 |
-
print(f"Error in
|
| 354 |
raise
|
| 355 |
|
| 356 |
-
# def save_to_pickle(standardized_data, user_state):
|
| 357 |
-
# existing_df = pd.read_pickle(pickle_file)
|
| 358 |
-
# # ._append is slightly different in newer pandas; you might use .concat instead:
|
| 359 |
-
# new_df = existing_df._append(pd.DataFrame([standardized_data]), ignore_index=True)
|
| 360 |
-
# new_df.to_pickle(pickle_file)
|
| 361 |
-
|
| 362 |
-
# # Upload after writing
|
| 363 |
-
# hf_token = os.getenv("write_token")
|
| 364 |
-
# repo_id = "mojakob/condition1"
|
| 365 |
-
# if hf_token and repo_id:
|
| 366 |
-
# upload_file_to_space(
|
| 367 |
-
# local_file_path=pickle_file,
|
| 368 |
-
# repo_id=repo_id,
|
| 369 |
-
# hf_token=hf_token
|
| 370 |
-
# )
|
| 371 |
|
| 372 |
# Processes usability questions
|
| 373 |
def process_feedback(ease, clarity, engagement, satisfaction, cognitive_load, user_state):
|
|
@@ -897,7 +931,7 @@ def new_chat_input(chat_history, user_state, user_input, *args, **kwargs):
|
|
| 897 |
elif user_state["usability_question_counter"] == 5:
|
| 898 |
user_state["usability_feedback"]["Additional Comments"] = user_input.strip()
|
| 899 |
user_state["stage"] = "completed"
|
| 900 |
-
|
| 901 |
'Prolific ID': user_state["prolific_id"],
|
| 902 |
'Age': user_state["standardized_data"].get("Age", None),
|
| 903 |
'Profession': user_state["standardized_data"].get("Profession", None),
|
|
@@ -937,7 +971,6 @@ def new_chat_input(chat_history, user_state, user_input, *args, **kwargs):
|
|
| 937 |
"Attention Check 1": user_state.get("Attention Check 1", None),
|
| 938 |
"Attention Check 2": user_state.get("Attention Check 2", None)
|
| 939 |
}, user_state)
|
| 940 |
-
finalize_and_save_to_space(user_state)
|
| 941 |
|
| 942 |
response = "Thank you for your feedback! Your responses have been saved. Please use the following link to submit your results: https://app.prolific.com/submissions/complete?cc=C1ECU6HA"
|
| 943 |
chat_history[-1] = (user_input, response)
|
|
@@ -1146,7 +1179,7 @@ def slider_input(chat_history, user_state, attribution_slider, *args, **kwargs):
|
|
| 1146 |
if user_state["vignette_counter"] > 12:
|
| 1147 |
print("All vignettes completed. Moving to usability feedback.")
|
| 1148 |
# save_to_pickle(user_state["standardized_data"])
|
| 1149 |
-
|
| 1150 |
'Prolific ID': user_state["prolific_id"],
|
| 1151 |
'Age': user_state["standardized_data"].get("Age", None),
|
| 1152 |
'Profession': user_state["standardized_data"].get("Profession", None),
|
|
@@ -1202,7 +1235,7 @@ def slider_input(chat_history, user_state, attribution_slider, *args, **kwargs):
|
|
| 1202 |
# Save data
|
| 1203 |
user_state["stage"] = "topic_choice"
|
| 1204 |
# save_to_pickle(user_state["standardized_data"])
|
| 1205 |
-
|
| 1206 |
'Prolific ID': user_state["prolific_id"],
|
| 1207 |
'Age': user_state["standardized_data"].get("Age", None),
|
| 1208 |
'Profession': user_state["standardized_data"].get("Profession", None),
|
|
@@ -1266,7 +1299,7 @@ def slider_input(chat_history, user_state, attribution_slider, *args, **kwargs):
|
|
| 1266 |
user_state["stage"] = "topic_choice"
|
| 1267 |
|
| 1268 |
# Save partial data
|
| 1269 |
-
|
| 1270 |
'Prolific ID': user_state["prolific_id"],
|
| 1271 |
'Age': user_state["standardized_data"].get("Age", None),
|
| 1272 |
'Profession': user_state["standardized_data"].get("Profession", None),
|
|
@@ -1551,8 +1584,12 @@ with gr.Blocks(theme= gr.themes.Glass(), css=css) as app:
|
|
| 1551 |
gr.update(visible=False), # keep page_2 hidden
|
| 1552 |
user_state
|
| 1553 |
)
|
| 1554 |
-
# 2) Store the PID
|
| 1555 |
user_state["prolific_id"] = pid
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1556 |
# 3) Hide page_1, show page_2
|
| 1557 |
return (
|
| 1558 |
f"Prolific ID {pid} recorded, now proceed to PHQ-9.",
|
|
|
|
| 16 |
import logging
|
| 17 |
import asyncio
|
| 18 |
import threading
|
| 19 |
+
import time
|
| 20 |
file_lock = threading.Lock()
|
| 21 |
#logging.basicConfig(level=logging.DEBUG)
|
| 22 |
|
|
|
|
| 277 |
|
| 278 |
print(f"Uploaded {local_file_path} -> {repo_id}/{path_in_repo} [repo_type={repo_type}]")
|
| 279 |
|
| 280 |
+
# def save_partial_data_to_excel(new_row, user_state):
|
| 281 |
+
# partial_excel_file = "partial_data.xlsx"
|
| 282 |
+
# dataset_repo_id = "mojakob/user_data"
|
| 283 |
+
# hf_token = os.getenv("write_token")
|
| 284 |
|
| 285 |
+
# # (Optional) add any additional fields, e.g., usability:
|
| 286 |
+
# usability_data = user_state.get("usability_feedback", {})
|
| 287 |
+
# new_row.update(usability_data)
|
| 288 |
|
| 289 |
+
# try:
|
| 290 |
+
# with file_lock:
|
| 291 |
+
# if not os.path.exists(partial_excel_file):
|
| 292 |
+
# df = pd.DataFrame(columns=new_row.keys())
|
| 293 |
+
# else:
|
| 294 |
+
# df = pd.read_excel(partial_excel_file, engine="openpyxl")
|
| 295 |
|
| 296 |
+
# # Use pd.concat instead of _append
|
| 297 |
+
# df = pd.concat([df, pd.DataFrame([new_row])], ignore_index=True)
|
| 298 |
+
# print("PARTIAL row being saved:", new_row)
|
| 299 |
|
| 300 |
+
# df.to_excel(partial_excel_file, index=False, engine="openpyxl")
|
| 301 |
|
| 302 |
+
# # Upload outside the lock (if desired)
|
| 303 |
+
# if hf_token and dataset_repo_id:
|
| 304 |
+
# upload_file_to_repo(
|
| 305 |
+
# local_file_path=partial_excel_file,
|
| 306 |
+
# repo_id=dataset_repo_id,
|
| 307 |
+
# hf_token=hf_token,
|
| 308 |
+
# path_in_repo="partial_data.xlsx",
|
| 309 |
+
# repo_type="dataset"
|
| 310 |
+
# )
|
| 311 |
+
|
| 312 |
+
# except Exception as e:
|
| 313 |
+
# print(f"Error in save_partial_data_to_excel: {e}")
|
| 314 |
+
# raise
|
| 315 |
|
| 316 |
+
# def finalize_and_save_to_space(user_state):
|
| 317 |
+
# hf_token = os.getenv("write_token")
|
| 318 |
+
# space_repo_id = "mojakob/user_data" # your space
|
| 319 |
+
# partial_excel_file = "partial_data.xlsx"
|
| 320 |
+
# excel_file = "assessment_data.xlsx"
|
| 321 |
+
|
| 322 |
+
# try:
|
| 323 |
+
# with file_lock:
|
| 324 |
+
# if os.path.exists(partial_excel_file):
|
| 325 |
+
# df_partial = pd.read_excel(partial_excel_file, engine="openpyxl")
|
| 326 |
+
# else:
|
| 327 |
+
# print("No partial_data.xlsx found, nothing to merge.")
|
| 328 |
+
# df_partial = pd.DataFrame()
|
| 329 |
+
|
| 330 |
+
# if os.path.exists(excel_file):
|
| 331 |
+
# df_final = pd.read_excel(excel_file, engine="openpyxl")
|
| 332 |
+
# else:
|
| 333 |
+
# df_final = pd.DataFrame(columns=df_partial.columns)
|
| 334 |
+
|
| 335 |
+
# if not df_partial.empty:
|
| 336 |
+
# df_merged = pd.concat([df_final, df_partial], ignore_index=True)
|
| 337 |
+
# df_merged.drop_duplicates(subset=["Prolific ID", "Vignette"], keep="last", inplace=True)
|
| 338 |
+
# else:
|
| 339 |
+
# df_merged = df_final
|
| 340 |
+
|
| 341 |
+
# df_merged.to_excel(excel_file, index=False, engine="openpyxl")
|
| 342 |
+
# print("Merged partial data into final file. New final row count:", len(df_merged))
|
| 343 |
+
|
| 344 |
+
# if hf_token and space_repo_id:
|
| 345 |
+
# upload_file_to_repo(
|
| 346 |
+
# local_file_path=excel_file,
|
| 347 |
+
# repo_id=space_repo_id,
|
| 348 |
+
# hf_token=hf_token,
|
| 349 |
+
# path_in_repo="assessment_data.xlsx",
|
| 350 |
+
# repo_type="dataset"
|
| 351 |
+
# )
|
| 352 |
+
|
| 353 |
+
# except Exception as e:
|
| 354 |
+
# print(f"Error in finalize_and_save_to_space: {e}")
|
| 355 |
+
# raise
|
| 356 |
+
|
| 357 |
+
def save_to_excel(new_row, user_state):
|
| 358 |
+
"""
|
| 359 |
+
Appends a single row (new_row) to the user's unique file
|
| 360 |
+
stored in user_state["excel_file"].
|
| 361 |
+
|
| 362 |
+
Then uploads that updated file to Hugging Face, preserving concurrency
|
| 363 |
+
by never touching a shared file.
|
| 364 |
+
"""
|
| 365 |
hf_token = os.getenv("write_token")
|
| 366 |
+
dataset_repo_id = "mojakob/user_data"
|
| 367 |
+
local_file = user_state.get("excel_file")
|
| 368 |
+
|
| 369 |
+
if not local_file:
|
| 370 |
+
print("Error: No user_state['excel_file'] found.")
|
| 371 |
+
return
|
| 372 |
|
| 373 |
try:
|
| 374 |
with file_lock:
|
| 375 |
+
# 1. If the unique user file doesn't exist yet, create a new one with appropriate columns
|
| 376 |
+
if not os.path.exists(local_file):
|
| 377 |
+
df = pd.DataFrame(columns=new_row.keys())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 378 |
else:
|
| 379 |
+
df = pd.read_excel(local_file, engine="openpyxl")
|
| 380 |
+
|
| 381 |
+
# 2. Append the new row
|
| 382 |
+
df = pd.concat([df, pd.DataFrame([new_row])], ignore_index=True)
|
| 383 |
+
print("SAVING row to unique user file:", new_row)
|
| 384 |
+
|
| 385 |
+
# 3. Save locally
|
| 386 |
+
df.to_excel(local_file, index=False, engine="openpyxl")
|
| 387 |
|
| 388 |
+
# 4. Upload to Hugging Face (outside the lock)
|
| 389 |
+
if hf_token and dataset_repo_id:
|
| 390 |
+
api = HfApi()
|
| 391 |
+
api.upload_file(
|
| 392 |
+
path_or_fileobj=local_file,
|
| 393 |
+
path_in_repo=local_file, # same name in the repo
|
| 394 |
+
repo_id=dataset_repo_id,
|
| 395 |
+
repo_type="dataset",
|
| 396 |
+
token=hf_token
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 397 |
)
|
| 398 |
+
print(f"Uploaded {local_file} to HF dataset.")
|
| 399 |
+
else:
|
| 400 |
+
print("No HF token or dataset repo provided, skipping HF upload.")
|
| 401 |
except Exception as e:
|
| 402 |
+
print(f"Error in save_to_excel: {e}")
|
| 403 |
raise
|
| 404 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 405 |
|
| 406 |
# Processes usability questions
|
| 407 |
def process_feedback(ease, clarity, engagement, satisfaction, cognitive_load, user_state):
|
|
|
|
| 931 |
elif user_state["usability_question_counter"] == 5:
|
| 932 |
user_state["usability_feedback"]["Additional Comments"] = user_input.strip()
|
| 933 |
user_state["stage"] = "completed"
|
| 934 |
+
save_to_excel({
|
| 935 |
'Prolific ID': user_state["prolific_id"],
|
| 936 |
'Age': user_state["standardized_data"].get("Age", None),
|
| 937 |
'Profession': user_state["standardized_data"].get("Profession", None),
|
|
|
|
| 971 |
"Attention Check 1": user_state.get("Attention Check 1", None),
|
| 972 |
"Attention Check 2": user_state.get("Attention Check 2", None)
|
| 973 |
}, user_state)
|
|
|
|
| 974 |
|
| 975 |
response = "Thank you for your feedback! Your responses have been saved. Please use the following link to submit your results: https://app.prolific.com/submissions/complete?cc=C1ECU6HA"
|
| 976 |
chat_history[-1] = (user_input, response)
|
|
|
|
| 1179 |
if user_state["vignette_counter"] > 12:
|
| 1180 |
print("All vignettes completed. Moving to usability feedback.")
|
| 1181 |
# save_to_pickle(user_state["standardized_data"])
|
| 1182 |
+
save_to_excel({
|
| 1183 |
'Prolific ID': user_state["prolific_id"],
|
| 1184 |
'Age': user_state["standardized_data"].get("Age", None),
|
| 1185 |
'Profession': user_state["standardized_data"].get("Profession", None),
|
|
|
|
| 1235 |
# Save data
|
| 1236 |
user_state["stage"] = "topic_choice"
|
| 1237 |
# save_to_pickle(user_state["standardized_data"])
|
| 1238 |
+
save_to_excel({
|
| 1239 |
'Prolific ID': user_state["prolific_id"],
|
| 1240 |
'Age': user_state["standardized_data"].get("Age", None),
|
| 1241 |
'Profession': user_state["standardized_data"].get("Profession", None),
|
|
|
|
| 1299 |
user_state["stage"] = "topic_choice"
|
| 1300 |
|
| 1301 |
# Save partial data
|
| 1302 |
+
save_to_excel({
|
| 1303 |
'Prolific ID': user_state["prolific_id"],
|
| 1304 |
'Age': user_state["standardized_data"].get("Age", None),
|
| 1305 |
'Profession': user_state["standardized_data"].get("Profession", None),
|
|
|
|
| 1584 |
gr.update(visible=False), # keep page_2 hidden
|
| 1585 |
user_state
|
| 1586 |
)
|
| 1587 |
+
# 2) Store the PID and create unique file name
|
| 1588 |
user_state["prolific_id"] = pid
|
| 1589 |
+
|
| 1590 |
+
timestamp = time.strftime("%Y%m%d_%H%M%S")
|
| 1591 |
+
user_state["excel_file"] = f"Cond1_{pid}_{timestamp}.xlsx"
|
| 1592 |
+
|
| 1593 |
# 3) Hide page_1, show page_2
|
| 1594 |
return (
|
| 1595 |
f"Prolific ID {pid} recorded, now proceed to PHQ-9.",
|