Miquel Farré commited on
Commit
6f5edf8
·
1 Parent(s): c993b98
Files changed (1) hide show
  1. app.py +56 -51
app.py CHANGED
@@ -13,62 +13,67 @@ MAX_RETRIES = 3
13
 
14
  def commit_signup(username: str) -> Optional[str]:
15
  """Attempt to commit new signup atomically, always reading latest data before commit"""
16
- # try:
17
- # Always get the latest data right before committing
18
  try:
19
- file_content = hf_api.hf_hub_download(
20
- repo_id=DATASET_REPO,
21
- repo_type="dataset",
22
- filename="waitlist.csv",
23
- token=os.getenv("HF_TOKEN")
24
- )
25
- current_data = pd.read_csv(file_content)
26
- if 'userid' not in current_data.columns or 'joined_at' not in current_data.columns:
27
- current_data = pd.DataFrame(columns=['userid', 'joined_at'])
28
- except Exception as e:
29
- print(f"First error {str(e)}")
30
- current_data = pd.DataFrame(columns=['userid', 'joined_at'])
31
-
32
- # If user already exists in the latest data, don't add them again
33
- if username in current_data['userid'].values:
34
- return None
35
-
36
- # Add new user with timestamp
37
- new_row = pd.DataFrame([{
38
- 'userid': username,
39
- 'joined_at': datetime.utcnow().isoformat()
40
- }])
41
-
42
- # Combine with latest data
43
- updated_data = pd.concat([current_data, new_row], ignore_index=True)
44
-
45
- # Save to temp file
46
- with tempfile.NamedTemporaryFile(mode='w', suffix='.csv', delete=False) as tmp:
47
- updated_data.to_csv(tmp.name, index=False)
48
-
49
- try:
50
- operation = CommitOperationAdd(
51
- path_in_repo="waitlist.csv",
52
- path_or_fileobj=tmp.name
53
- )
54
- except Exception as e:
55
- print(f"CommitOperationAdd {str(e)}")
56
-
57
  try:
58
- create_commit( repo_id=DATASET_REPO,
 
59
  repo_type="dataset",
60
- operations=[operation],
61
- commit_message=f"Add user {username} to waitlist",
62
- token=os.getenv("HF_TOKEN"),
63
  )
64
- os.unlink(tmp.name)
65
- return None
 
66
  except Exception as e:
67
- print(f"Second error {str(e)}")
68
- os.unlink(tmp.name)
69
- return str(e)
70
- # except Exception as e:
71
- # return str(e)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
72
 
73
  def join_waitlist(
74
  profile: gr.OAuthProfile | None,
 
13
 
14
  def commit_signup(username: str) -> Optional[str]:
15
  """Attempt to commit new signup atomically, always reading latest data before commit"""
 
 
16
  try:
17
+ # Always get the latest data right before committing
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
  try:
19
+ file_content = hf_api.hf_hub_download(
20
+ repo_id=DATASET_REPO,
21
  repo_type="dataset",
22
+ filename="waitlist.csv",
23
+ token=os.getenv("HF_TOKEN")
 
24
  )
25
+ current_data = pd.read_csv(file_content)
26
+ if 'userid' not in current_data.columns or 'joined_at' not in current_data.columns:
27
+ current_data = pd.DataFrame(columns=['userid', 'joined_at'])
28
  except Exception as e:
29
+ print(f"First error {str(e)}")
30
+ current_data = pd.DataFrame(columns=['userid', 'joined_at'])
31
+ print("we make it here")
32
+ # If user already exists in the latest data, don't add them again
33
+ if username in current_data['userid'].values:
34
+ return None
35
+
36
+ # Add new user with timestamp
37
+ new_row = pd.DataFrame([{
38
+ 'userid': username,
39
+ 'joined_at': datetime.utcnow().isoformat()
40
+ }])
41
+ print("new row")
42
+
43
+ # Combine with latest data
44
+ updated_data = pd.concat([current_data, new_row], ignore_index=True)
45
+ print("updated data")
46
+
47
+ # Save to temp file
48
+ with tempfile.NamedTemporaryFile(mode='w', suffix='.csv', delete=False) as tmp:
49
+ updated_data.to_csv(tmp.name, index=False)
50
+
51
+ try:
52
+ operation = CommitOperationAdd(
53
+ path_in_repo="waitlist.csv",
54
+ path_or_fileobj=tmp.name
55
+ )
56
+ except Exception as e:
57
+ print(f"CommitOperationAdd {str(e)}")
58
+ print("commit operation")
59
+
60
+ try:
61
+ create_commit(
62
+ repo_id=DATASET_REPO,
63
+ repo_type="dataset",
64
+ operations=[operation],
65
+ commit_message=f"Add user {username} to waitlist",
66
+ token=os.getenv("HF_TOKEN"),
67
+ )
68
+ print("commit created")
69
+ os.unlink(tmp.name)
70
+ return None
71
+ except Exception as e:
72
+ print(f"Second error {str(e)}")
73
+ os.unlink(tmp.name)
74
+ return str(e)
75
+ except Exception as e:
76
+ return str(e)
77
 
78
  def join_waitlist(
79
  profile: gr.OAuthProfile | None,