Spaces:
Running
Running
Update mtdna_backend.py
Browse files- mtdna_backend.py +57 -18
mtdna_backend.py
CHANGED
|
@@ -681,6 +681,50 @@ def load_user_usage():
|
|
| 681 |
|
| 682 |
# except Exception as e:
|
| 683 |
# print(f"❌ Failed to save user_usage_log.json to Google Drive: {e}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 684 |
def save_user_usage(usage_dict):
|
| 685 |
try:
|
| 686 |
creds_dict = json.loads(os.environ["GCP_CREDS_JSON"])
|
|
@@ -691,36 +735,30 @@ def save_user_usage(usage_dict):
|
|
| 691 |
spreadsheet = client.open("user_usage_log")
|
| 692 |
sheet = spreadsheet.sheet1
|
| 693 |
|
| 694 |
-
#
|
| 695 |
df_new = pd.DataFrame(list(usage_dict.items()), columns=["email", "usage_count"])
|
| 696 |
df_new["email"] = df_new["email"].str.strip().str.lower()
|
|
|
|
| 697 |
|
| 698 |
-
#
|
| 699 |
existing_data = sheet.get_all_values()
|
| 700 |
-
|
| 701 |
-
|
| 702 |
-
# Try to load old data
|
| 703 |
-
if existing_data and len(existing_data[0]) >= 1:
|
| 704 |
df_old = pd.DataFrame(existing_data[1:], columns=existing_data[0])
|
| 705 |
-
|
| 706 |
-
# Fix missing columns
|
| 707 |
-
if "email" not in df_old.columns:
|
| 708 |
-
df_old["email"] = ""
|
| 709 |
-
if "usage_count" not in df_old.columns:
|
| 710 |
-
df_old["usage_count"] = 0
|
| 711 |
-
|
| 712 |
df_old["email"] = df_old["email"].str.strip().str.lower()
|
| 713 |
df_old["usage_count"] = pd.to_numeric(df_old["usage_count"], errors="coerce").fillna(0).astype(int)
|
| 714 |
else:
|
| 715 |
df_old = pd.DataFrame(columns=["email", "usage_count"])
|
| 716 |
|
| 717 |
-
#
|
| 718 |
-
|
| 719 |
-
|
|
|
|
|
|
|
|
|
|
| 720 |
|
| 721 |
-
#
|
| 722 |
sheet.clear()
|
| 723 |
-
sheet.update([
|
| 724 |
print("✅ Saved user usage to user_usage_log sheet.")
|
| 725 |
|
| 726 |
except Exception as e:
|
|
@@ -728,6 +766,7 @@ def save_user_usage(usage_dict):
|
|
| 728 |
|
| 729 |
|
| 730 |
|
|
|
|
| 731 |
# def increment_usage(user_id, num_samples=1):
|
| 732 |
# usage = load_user_usage()
|
| 733 |
# if user_id not in usage:
|
|
|
|
| 681 |
|
| 682 |
# except Exception as e:
|
| 683 |
# print(f"❌ Failed to save user_usage_log.json to Google Drive: {e}")
|
| 684 |
+
# def save_user_usage(usage_dict):
|
| 685 |
+
# try:
|
| 686 |
+
# creds_dict = json.loads(os.environ["GCP_CREDS_JSON"])
|
| 687 |
+
# scope = ['https://spreadsheets.google.com/feeds', 'https://www.googleapis.com/auth/drive']
|
| 688 |
+
# creds = ServiceAccountCredentials.from_json_keyfile_dict(creds_dict, scope)
|
| 689 |
+
# client = gspread.authorize(creds)
|
| 690 |
+
|
| 691 |
+
# spreadsheet = client.open("user_usage_log")
|
| 692 |
+
# sheet = spreadsheet.sheet1
|
| 693 |
+
|
| 694 |
+
# # Step 1: Convert new usage to DataFrame
|
| 695 |
+
# df_new = pd.DataFrame(list(usage_dict.items()), columns=["email", "usage_count"])
|
| 696 |
+
# df_new["email"] = df_new["email"].str.strip().str.lower()
|
| 697 |
+
|
| 698 |
+
# # Step 2: Load existing data
|
| 699 |
+
# existing_data = sheet.get_all_values()
|
| 700 |
+
# print("🧪 Sheet existing_data:", existing_data)
|
| 701 |
+
|
| 702 |
+
# # Try to load old data
|
| 703 |
+
# if existing_data and len(existing_data[0]) >= 1:
|
| 704 |
+
# df_old = pd.DataFrame(existing_data[1:], columns=existing_data[0])
|
| 705 |
+
|
| 706 |
+
# # Fix missing columns
|
| 707 |
+
# if "email" not in df_old.columns:
|
| 708 |
+
# df_old["email"] = ""
|
| 709 |
+
# if "usage_count" not in df_old.columns:
|
| 710 |
+
# df_old["usage_count"] = 0
|
| 711 |
+
|
| 712 |
+
# df_old["email"] = df_old["email"].str.strip().str.lower()
|
| 713 |
+
# df_old["usage_count"] = pd.to_numeric(df_old["usage_count"], errors="coerce").fillna(0).astype(int)
|
| 714 |
+
# else:
|
| 715 |
+
# df_old = pd.DataFrame(columns=["email", "usage_count"])
|
| 716 |
+
|
| 717 |
+
# # Step 3: Merge
|
| 718 |
+
# df_combined = pd.concat([df_old, df_new], ignore_index=True)
|
| 719 |
+
# df_combined = df_combined.groupby("email", as_index=False).sum()
|
| 720 |
+
|
| 721 |
+
# # Step 4: Write back
|
| 722 |
+
# sheet.clear()
|
| 723 |
+
# sheet.update([df_combined.columns.tolist()] + df_combined.astype(str).values.tolist())
|
| 724 |
+
# print("✅ Saved user usage to user_usage_log sheet.")
|
| 725 |
+
|
| 726 |
+
# except Exception as e:
|
| 727 |
+
# print(f"❌ Failed to save user usage to Google Sheets: {e}")
|
| 728 |
def save_user_usage(usage_dict):
|
| 729 |
try:
|
| 730 |
creds_dict = json.loads(os.environ["GCP_CREDS_JSON"])
|
|
|
|
| 735 |
spreadsheet = client.open("user_usage_log")
|
| 736 |
sheet = spreadsheet.sheet1
|
| 737 |
|
| 738 |
+
# Build new df
|
| 739 |
df_new = pd.DataFrame(list(usage_dict.items()), columns=["email", "usage_count"])
|
| 740 |
df_new["email"] = df_new["email"].str.strip().str.lower()
|
| 741 |
+
df_new["usage_count"] = pd.to_numeric(df_new["usage_count"], errors="coerce").fillna(0).astype(int)
|
| 742 |
|
| 743 |
+
# Read existing data
|
| 744 |
existing_data = sheet.get_all_values()
|
| 745 |
+
if existing_data and len(existing_data[0]) >= 2:
|
|
|
|
|
|
|
|
|
|
| 746 |
df_old = pd.DataFrame(existing_data[1:], columns=existing_data[0])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 747 |
df_old["email"] = df_old["email"].str.strip().str.lower()
|
| 748 |
df_old["usage_count"] = pd.to_numeric(df_old["usage_count"], errors="coerce").fillna(0).astype(int)
|
| 749 |
else:
|
| 750 |
df_old = pd.DataFrame(columns=["email", "usage_count"])
|
| 751 |
|
| 752 |
+
# ✅ Overwrite specific emails only
|
| 753 |
+
df_old = df_old.set_index("email")
|
| 754 |
+
for email, count in usage_dict.items():
|
| 755 |
+
email = email.strip().lower()
|
| 756 |
+
df_old.loc[email, "usage_count"] = count
|
| 757 |
+
df_old = df_old.reset_index()
|
| 758 |
|
| 759 |
+
# Save
|
| 760 |
sheet.clear()
|
| 761 |
+
sheet.update([df_old.columns.tolist()] + df_old.astype(str).values.tolist())
|
| 762 |
print("✅ Saved user usage to user_usage_log sheet.")
|
| 763 |
|
| 764 |
except Exception as e:
|
|
|
|
| 766 |
|
| 767 |
|
| 768 |
|
| 769 |
+
|
| 770 |
# def increment_usage(user_id, num_samples=1):
|
| 771 |
# usage = load_user_usage()
|
| 772 |
# if user_id not in usage:
|