Spaces:
Running
Running
Update mtdna_backend.py
Browse files- mtdna_backend.py +83 -20
mtdna_backend.py
CHANGED
@@ -587,22 +587,42 @@ def hash_user_id(user_input):
|
|
587 |
# except Exception as e:
|
588 |
# print(f"⚠️ Failed to load user usage from Google Sheets: {e}")
|
589 |
# return {}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
590 |
def load_user_usage():
|
591 |
try:
|
592 |
-
|
593 |
-
|
|
|
|
|
594 |
|
595 |
-
|
596 |
-
|
597 |
-
return {} # not found, start fresh
|
598 |
|
599 |
-
|
600 |
-
|
601 |
-
|
602 |
-
|
|
|
|
|
603 |
|
604 |
except Exception as e:
|
605 |
-
print(f"⚠️ Failed to load
|
606 |
return {}
|
607 |
|
608 |
|
@@ -626,21 +646,57 @@ def load_user_usage():
|
|
626 |
# sheet.update(rows)
|
627 |
# except Exception as e:
|
628 |
# print(f"❌ Failed to save user usage to Google Sheets: {e}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
629 |
def save_user_usage(usage_dict):
|
630 |
try:
|
631 |
-
|
632 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
633 |
|
634 |
-
|
635 |
-
|
636 |
-
print("💾 Saving this usage dict:", usage_dict)
|
637 |
-
with open(tmp_path, "w") as f:
|
638 |
-
json.dump(usage_dict, f, indent=2)
|
639 |
|
640 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
641 |
|
642 |
except Exception as e:
|
643 |
-
print(f"❌ Failed to save
|
|
|
644 |
|
645 |
|
646 |
# def increment_usage(user_id, num_samples=1):
|
@@ -650,13 +706,20 @@ def save_user_usage(usage_dict):
|
|
650 |
# usage[user_id] += num_samples
|
651 |
# save_user_usage(usage)
|
652 |
# return usage[user_id]
|
653 |
-
def increment_usage(email: str, count: int):
|
|
|
|
|
|
|
|
|
|
|
|
|
654 |
usage = load_user_usage()
|
655 |
email_key = email.strip().lower()
|
656 |
usage[email_key] = usage.get(email_key, 0) + count
|
657 |
save_user_usage(usage)
|
658 |
return usage[email_key]
|
659 |
|
|
|
660 |
# run the batch
|
661 |
def summarize_batch(file=None, raw_text="", resume_file=None, user_email="",
|
662 |
stop_flag=None, output_file_path=None,
|
|
|
587 |
# except Exception as e:
|
588 |
# print(f"⚠️ Failed to load user usage from Google Sheets: {e}")
|
589 |
# return {}
|
590 |
+
# def load_user_usage():
|
591 |
+
# try:
|
592 |
+
# parent_id = pipeline.get_or_create_drive_folder("mtDNA-Location-Classifier")
|
593 |
+
# iterate3_id = pipeline.get_or_create_drive_folder("iterate3", parent_id=parent_id)
|
594 |
+
|
595 |
+
# found = pipeline.find_drive_file("user_usage_log.json", parent_id=iterate3_id)
|
596 |
+
# if not found:
|
597 |
+
# return {} # not found, start fresh
|
598 |
+
|
599 |
+
# #file_id = found[0]["id"]
|
600 |
+
# file_id = found
|
601 |
+
# content = pipeline.download_drive_file_content(file_id)
|
602 |
+
# return json.loads(content.strip()) if content.strip() else {}
|
603 |
+
|
604 |
+
# except Exception as e:
|
605 |
+
# print(f"⚠️ Failed to load user_usage_log.json from Google Drive: {e}")
|
606 |
+
# return {}
|
607 |
def load_user_usage():
|
608 |
try:
|
609 |
+
creds_dict = json.loads(os.environ["GCP_CREDS_JSON"])
|
610 |
+
scope = ['https://spreadsheets.google.com/feeds', 'https://www.googleapis.com/auth/drive']
|
611 |
+
creds = ServiceAccountCredentials.from_json_keyfile_dict(creds_dict, scope)
|
612 |
+
client = gspread.authorize(creds)
|
613 |
|
614 |
+
sheet = client.open("user_usage_log").sheet1
|
615 |
+
data = sheet.get_all_values()
|
|
|
616 |
|
617 |
+
if not data or len(data) < 2:
|
618 |
+
return {}
|
619 |
+
|
620 |
+
df = pd.DataFrame(data[1:], columns=data[0])
|
621 |
+
usage = {row["email"].strip().lower(): int(row["usage_count"]) for _, row in df.iterrows()}
|
622 |
+
return usage
|
623 |
|
624 |
except Exception as e:
|
625 |
+
print(f"⚠️ Failed to load user usage from Google Sheets: {e}")
|
626 |
return {}
|
627 |
|
628 |
|
|
|
646 |
# sheet.update(rows)
|
647 |
# except Exception as e:
|
648 |
# print(f"❌ Failed to save user usage to Google Sheets: {e}")
|
649 |
+
# def save_user_usage(usage_dict):
|
650 |
+
# try:
|
651 |
+
# parent_id = pipeline.get_or_create_drive_folder("mtDNA-Location-Classifier")
|
652 |
+
# iterate3_id = pipeline.get_or_create_drive_folder("iterate3", parent_id=parent_id)
|
653 |
+
|
654 |
+
# import tempfile
|
655 |
+
# tmp_path = os.path.join(tempfile.gettempdir(), "user_usage_log.json")
|
656 |
+
# print("💾 Saving this usage dict:", usage_dict)
|
657 |
+
# with open(tmp_path, "w") as f:
|
658 |
+
# json.dump(usage_dict, f, indent=2)
|
659 |
+
|
660 |
+
# pipeline.upload_file_to_drive(tmp_path, "user_usage_log.json", iterate3_id)
|
661 |
+
|
662 |
+
# except Exception as e:
|
663 |
+
# print(f"❌ Failed to save user_usage_log.json to Google Drive: {e}")
|
664 |
def save_user_usage(usage_dict):
|
665 |
try:
|
666 |
+
# Load credentials
|
667 |
+
creds_dict = json.loads(os.environ["GCP_CREDS_JSON"])
|
668 |
+
scope = ['https://spreadsheets.google.com/feeds', 'https://www.googleapis.com/auth/drive']
|
669 |
+
creds = ServiceAccountCredentials.from_json_keyfile_dict(creds_dict, scope)
|
670 |
+
client = gspread.authorize(creds)
|
671 |
+
|
672 |
+
# Open or create the sheet
|
673 |
+
spreadsheet = client.open("user_usage_log")
|
674 |
+
sheet = spreadsheet.sheet1
|
675 |
|
676 |
+
# Convert dict to DataFrame
|
677 |
+
df_new = pd.DataFrame(list(usage_dict.items()), columns=["email", "usage_count"])
|
|
|
|
|
|
|
678 |
|
679 |
+
# Load old data
|
680 |
+
existing_data = sheet.get_all_values()
|
681 |
+
if existing_data:
|
682 |
+
df_old = pd.DataFrame(existing_data[1:], columns=existing_data[0])
|
683 |
+
else:
|
684 |
+
df_old = pd.DataFrame(columns=["email", "usage_count"])
|
685 |
+
|
686 |
+
# Merge (replace old values with new)
|
687 |
+
df_old.set_index("email", inplace=True)
|
688 |
+
df_new.set_index("email", inplace=True)
|
689 |
+
df_old.update(df_new)
|
690 |
+
df_combined = df_old.reset_index()
|
691 |
+
|
692 |
+
# Write back
|
693 |
+
sheet.clear()
|
694 |
+
sheet.update([df_combined.columns.tolist()] + df_combined.values.tolist())
|
695 |
+
print("✅ Saved user usage to user_usage_log sheet.")
|
696 |
|
697 |
except Exception as e:
|
698 |
+
print(f"❌ Failed to save user usage to Google Sheets: {e}")
|
699 |
+
|
700 |
|
701 |
|
702 |
# def increment_usage(user_id, num_samples=1):
|
|
|
706 |
# usage[user_id] += num_samples
|
707 |
# save_user_usage(usage)
|
708 |
# return usage[user_id]
|
709 |
+
# def increment_usage(email: str, count: int):
|
710 |
+
# usage = load_user_usage()
|
711 |
+
# email_key = email.strip().lower()
|
712 |
+
# usage[email_key] = usage.get(email_key, 0) + count
|
713 |
+
# save_user_usage(usage)
|
714 |
+
# return usage[email_key]
|
715 |
+
def increment_usage(email: str, count: int = 1):
|
716 |
usage = load_user_usage()
|
717 |
email_key = email.strip().lower()
|
718 |
usage[email_key] = usage.get(email_key, 0) + count
|
719 |
save_user_usage(usage)
|
720 |
return usage[email_key]
|
721 |
|
722 |
+
|
723 |
# run the batch
|
724 |
def summarize_batch(file=None, raw_text="", resume_file=None, user_email="",
|
725 |
stop_flag=None, output_file_path=None,
|