Miquel Farré
commited on
Commit
·
5fa7c4d
1
Parent(s):
1f8ef74
update
Browse files
app.py
CHANGED
@@ -10,40 +10,53 @@ from typing import Optional, Tuple
|
|
10 |
hf_api = HfApi(token=os.getenv("HF_TOKEN"))
|
11 |
DATASET_REPO = "HuggingFaceTB/smolvlm2-iphone-waitlist"
|
12 |
MAX_RETRIES = 3
|
13 |
-
|
14 |
def commit_signup(username: str, current_data: pd.DataFrame) -> Optional[str]:
|
15 |
"""Attempt to commit new signup atomically"""
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
'joined_at': datetime.utcnow().isoformat()
|
21 |
-
}])
|
22 |
-
updated_data = pd.concat([current_data, new_row], ignore_index=True)
|
23 |
-
|
24 |
-
# Save to temp file
|
25 |
-
with tempfile.NamedTemporaryFile(mode='w', suffix='.csv', delete=False) as tmp:
|
26 |
-
updated_data.to_csv(tmp.name, index=False)
|
27 |
|
28 |
-
#
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
)
|
33 |
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
41 |
)
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
47 |
|
48 |
def join_waitlist(
|
49 |
profile: gr.OAuthProfile | None,
|
@@ -61,11 +74,14 @@ def join_waitlist(
|
|
61 |
current_data = pd.read_csv(
|
62 |
f"https://huggingface.co/datasets/{DATASET_REPO}/raw/main/waitlist.csv"
|
63 |
)
|
|
|
|
|
|
|
64 |
except:
|
65 |
current_data = pd.DataFrame(columns=['userid', 'joined_at'])
|
66 |
|
67 |
# Check if user already registered
|
68 |
-
if profile.username in current_data['userid'].values:
|
69 |
return gr.update(
|
70 |
value="## 😎 You're already on the waitlist! We'll keep you updated.",
|
71 |
visible=True
|
@@ -75,6 +91,21 @@ def join_waitlist(
|
|
75 |
error = commit_signup(profile.username, current_data)
|
76 |
|
77 |
if error is None: # Success
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
78 |
return gr.update(
|
79 |
value="## 🎉 Thanks for joining the waitlist! We'll keep you updated on SmolVLM2 iPhone app.",
|
80 |
visible=True
|
@@ -98,25 +129,18 @@ def join_waitlist(
|
|
98 |
value="## 🫠 Sorry, something went wrong. Please try again later.",
|
99 |
visible=True
|
100 |
)
|
101 |
-
|
102 |
-
def update_ui(profile: gr.OAuthProfile | None) -> Tuple[gr.update, gr.update, gr.update]:
|
103 |
"""Update UI elements based on login status"""
|
104 |
if profile is None:
|
105 |
return (
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
),
|
110 |
-
gr.update(visible=False),
|
111 |
-
gr.update(visible=False)
|
112 |
)
|
113 |
return (
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
),
|
118 |
-
gr.update(visible=True),
|
119 |
-
gr.update(visible=False)
|
120 |
)
|
121 |
|
122 |
# Create the interface
|
|
|
10 |
hf_api = HfApi(token=os.getenv("HF_TOKEN"))
|
11 |
DATASET_REPO = "HuggingFaceTB/smolvlm2-iphone-waitlist"
|
12 |
MAX_RETRIES = 3
|
|
|
13 |
def commit_signup(username: str, current_data: pd.DataFrame) -> Optional[str]:
|
14 |
"""Attempt to commit new signup atomically"""
|
15 |
+
try:
|
16 |
+
# Verify current data has required columns
|
17 |
+
if 'userid' not in current_data.columns or 'joined_at' not in current_data.columns:
|
18 |
+
current_data = pd.DataFrame(columns=['userid', 'joined_at'])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
|
20 |
+
# Add new user with timestamp
|
21 |
+
new_row = pd.DataFrame([{
|
22 |
+
'userid': username,
|
23 |
+
'joined_at': datetime.utcnow().isoformat()
|
24 |
+
}])
|
25 |
|
26 |
+
# Ensure we're not duplicating any existing users
|
27 |
+
updated_data = pd.concat([
|
28 |
+
current_data[~current_data['userid'].isin([username])], # Keep all except current user
|
29 |
+
new_row
|
30 |
+
], ignore_index=True)
|
31 |
+
|
32 |
+
# Sort by joined_at to maintain chronological order
|
33 |
+
updated_data = updated_data.sort_values('joined_at').reset_index(drop=True)
|
34 |
+
|
35 |
+
# Save to temp file
|
36 |
+
with tempfile.NamedTemporaryFile(mode='w', suffix='.csv', delete=False) as tmp:
|
37 |
+
updated_data.to_csv(tmp.name, index=False)
|
38 |
+
|
39 |
+
# Create commit operation
|
40 |
+
operation = CommitOperationAdd(
|
41 |
+
path_in_repo="waitlist.csv",
|
42 |
+
path_or_fileobj=tmp.name
|
43 |
)
|
44 |
+
|
45 |
+
try:
|
46 |
+
# Try to create commit
|
47 |
+
create_commit(
|
48 |
+
repo_id=DATASET_REPO,
|
49 |
+
repo_type="dataset",
|
50 |
+
operations=[operation],
|
51 |
+
commit_message=f"Add user {username} to waitlist"
|
52 |
+
)
|
53 |
+
os.unlink(tmp.name)
|
54 |
+
return None # Success
|
55 |
+
except Exception as e:
|
56 |
+
os.unlink(tmp.name)
|
57 |
+
return str(e) # Return error message
|
58 |
+
except Exception as e:
|
59 |
+
return str(e)
|
60 |
|
61 |
def join_waitlist(
|
62 |
profile: gr.OAuthProfile | None,
|
|
|
74 |
current_data = pd.read_csv(
|
75 |
f"https://huggingface.co/datasets/{DATASET_REPO}/raw/main/waitlist.csv"
|
76 |
)
|
77 |
+
# Ensure data frame has required columns
|
78 |
+
if 'userid' not in current_data.columns or 'joined_at' not in current_data.columns:
|
79 |
+
current_data = pd.DataFrame(columns=['userid', 'joined_at'])
|
80 |
except:
|
81 |
current_data = pd.DataFrame(columns=['userid', 'joined_at'])
|
82 |
|
83 |
# Check if user already registered
|
84 |
+
if not current_data.empty and profile.username in current_data['userid'].values:
|
85 |
return gr.update(
|
86 |
value="## 😎 You're already on the waitlist! We'll keep you updated.",
|
87 |
visible=True
|
|
|
91 |
error = commit_signup(profile.username, current_data)
|
92 |
|
93 |
if error is None: # Success
|
94 |
+
# Verify the commit worked by reading back the data
|
95 |
+
try:
|
96 |
+
verification_data = pd.read_csv(
|
97 |
+
f"https://huggingface.co/datasets/{DATASET_REPO}/raw/main/waitlist.csv"
|
98 |
+
)
|
99 |
+
if profile.username not in verification_data['userid'].values:
|
100 |
+
if attempt < MAX_RETRIES - 1:
|
101 |
+
continue # Retry if verification failed
|
102 |
+
else:
|
103 |
+
raise Exception("Failed to verify signup")
|
104 |
+
except Exception as e:
|
105 |
+
if attempt < MAX_RETRIES - 1:
|
106 |
+
continue
|
107 |
+
raise e
|
108 |
+
|
109 |
return gr.update(
|
110 |
value="## 🎉 Thanks for joining the waitlist! We'll keep you updated on SmolVLM2 iPhone app.",
|
111 |
visible=True
|
|
|
129 |
value="## 🫠 Sorry, something went wrong. Please try again later.",
|
130 |
visible=True
|
131 |
)
|
132 |
+
def update_ui(profile: gr.OAuthProfile | None) -> Tuple[str, bool, bool]:
|
|
|
133 |
"""Update UI elements based on login status"""
|
134 |
if profile is None:
|
135 |
return (
|
136 |
+
"## Please sign in with your Hugging Face account to join the waitlist.", # welcome message
|
137 |
+
False, # hide join button
|
138 |
+
False, # hide status message
|
|
|
|
|
|
|
139 |
)
|
140 |
return (
|
141 |
+
f"## Welcome {profile.name} 👋 Click the button below 👇 to receive any updates related with the SmolVLM2 iPhone application", # welcome message
|
142 |
+
True, # show join button
|
143 |
+
True, # show status message
|
|
|
|
|
|
|
144 |
)
|
145 |
|
146 |
# Create the interface
|