File size: 4,018 Bytes
fd5705b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import os
import gradio as gr
from huggingface_hub import HfApi, CommitOperationAdd, create_commit
import pandas as pd
from datetime import datetime
import tempfile
from typing import Optional

# Initialize Hugging Face client
hf_api = HfApi(token=os.getenv("HF_TOKEN"))
DATASET_REPO = "HuggingFaceTB/smolvlm2-iphone-waitlist"
MAX_RETRIES = 3

def commit_signup(username: str, email: str, current_data: pd.DataFrame) -> Optional[str]:
    """Attempt to commit new signup atomically"""
    
    # Add new user with timestamp
    new_row = pd.DataFrame([{
        'userid': username,
        'email': email,
        'joined_at': datetime.utcnow().isoformat()
    }])
    updated_data = pd.concat([current_data, new_row], ignore_index=True)
    
    # Save to temp file
    with tempfile.NamedTemporaryFile(mode='w', suffix='.csv', delete=False) as tmp:
        updated_data.to_csv(tmp.name, index=False)
        
        # Create commit operation
        operation = CommitOperationAdd(
            path_in_repo="waitlist.csv",
            path_or_fileobj=tmp.name,
            commit_message=f"Add user {username} to waitlist"
        )
        
        try:
            # Try to create commit
            create_commit(
                repo_id=DATASET_REPO,
                repo_type="dataset",
                operations=[operation],
                commit_message=f"Add user {username} to waitlist"
            )
            os.unlink(tmp.name)
            return None  # Success
        except Exception as e:
            os.unlink(tmp.name)
            return str(e)  # Return error message

def join_waitlist(
    token: gr.OAuthToken | None,
    profile: gr.OAuthProfile | None,
) -> str:
    """Add user to the SmolVLM2 iPhone waitlist with retry logic for concurrent updates"""
    
    if token is None or profile is None:
        gr.Warning("Please log in to Hugging Face first!")
        return None

    for attempt in range(MAX_RETRIES):
        try:
            # Get current waitlist state
            try:
                current_data = pd.read_csv(
                    f"https://huggingface.co/datasets/{DATASET_REPO}/raw/main/waitlist.csv"
                )
            except:
                current_data = pd.DataFrame(columns=['userid', 'email', 'joined_at'])

            # Check if user already registered
            if profile.username in current_data['userid'].values:
                return "You're already on the waitlist! We'll keep you updated."

            # Try to commit the update
            error = commit_signup(profile.username, profile.email, current_data)
            
            if error is None:  # Success
                return "Thanks for joining the waitlist! We'll keep you updated on SmolVLM2 iPhone app."
            
            # If we got a conflict error, retry
            if "Conflict" in str(error) and attempt < MAX_RETRIES - 1:
                continue
                
            # Other error or last attempt
            gr.Error(f"An error occurred: {str(error)}")
            return "Sorry, something went wrong. Please try again later."

        except Exception as e:
            if attempt == MAX_RETRIES - 1:  # Last attempt
                gr.Error(f"An error occurred: {str(e)}")
                return "Sorry, something went wrong. Please try again later."

# Create the interface
with gr.Blocks() as demo:
    gr.Markdown("""
    # Join SmolVLM2 iPhone Waitlist 📱
    Sign up to be notified when the SmolVLM2 iPhone app and source code are released.
    
    By joining the waitlist, you'll get:
    - Early access to the SmolVLM2 iPhone app
    - Updates on development progress
    - First look at the source code when available
    """)

    with gr.Row():
        gr.LoginButton()
        
    join_button = gr.Button("Join Waitlist", variant="primary")
    output = gr.Markdown(visible=False)

    join_button.click(
        fn=join_waitlist,
        outputs=output,
    )

if __name__ == "__main__":
    demo.launch(auth_required=True)