File size: 12,217 Bytes
10c89d7
 
715218e
10c89d7
ea2400d
36075c9
10c89d7
cdb68ce
 
 
 
36075c9
 
 
d2efda9
11dc894
d2efda9
11dc894
 
4a79184
 
 
 
 
 
 
 
 
 
11dc894
4a79184
11dc894
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2a6ea32
36075c9
 
 
 
 
 
 
 
 
10c89d7
 
 
4a79184
715218e
 
11dc894
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
715218e
36075c9
11dc894
10c89d7
11dc894
 
2b22ecd
11dc894
 
 
156531b
2b22ecd
11dc894
 
2b22ecd
11dc894
 
2b22ecd
11dc894
 
 
 
 
 
 
 
 
 
 
 
4a79184
 
11dc894
2b22ecd
d23fcf4
d06f8eb
717a260
715218e
 
 
520d961
f6ed65a
715218e
11dc894
715218e
 
 
 
 
 
26bec7a
11dc894
2b22ecd
715218e
11dc894
 
 
 
 
 
715218e
156531b
78acf2b
 
 
11dc894
 
78acf2b
 
11dc894
 
 
 
 
 
 
 
 
 
 
d06f8eb
 
ba78dd6
4ad726e
ba78dd6
d06f8eb
 
 
11dc894
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d06f8eb
94224bf
d06f8eb
11dc894
 
 
 
 
 
 
 
 
7fe69ad
 
11dc894
 
 
 
 
 
 
 
 
 
 
 
 
ba78dd6
11dc894
 
 
 
ba78dd6
 
11dc894
ba78dd6
11dc894
 
 
 
 
 
 
 
715218e
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
import gradio as gr
import pandas as pd
import sqlite3
import os
import datetime
import shutil

DATA_DIR = "data"
os.makedirs(DATA_DIR, exist_ok=True)

DB_PATH = os.path.join(DATA_DIR, "teamup.db")
BACKUP_DIR = os.path.join(DATA_DIR, "backup")
os.makedirs(BACKUP_DIR, exist_ok=True)

ADMIN_CODE = os.getenv("ADMIN_CODE", "")
PAGE_SIZE = 20

LANGUAGES = ["English", "French", "Spanish", "German", "Portuguese", "Chinese", "Arabic", "Hindi"]
ALL_COUNTRIES = sorted(set([
    "United States of America", "United Kingdom", "India", "France", "Germany", "Canada", "Australia", "Japan", "Brazil", "Mexico",
    "Afghanistan", "Albania", "Algeria", "Andorra", "Angola", "Argentina", "Armenia", "Austria", "Azerbaijan",
    "Bangladesh", "Belgium", "Bhutan", "Bolivia", "Bosnia and Herzegovina", "Botswana", "Bulgaria", "Cambodia",
    "Chile", "China", "Colombia", "Croatia", "Cuba", "Czech Republic", "Denmark", "Dominican Republic", "Ecuador",
    "Egypt", "Estonia", "Ethiopia", "Finland", "Greece", "Hungary", "Iceland", "Indonesia", "Iran", "Iraq", "Ireland",
    "Israel", "Italy", "Jordan", "Kenya", "Kuwait", "Latvia", "Lithuania", "Luxembourg", "Malaysia", "Malta", "Morocco",
    "Nepal", "Netherlands", "New Zealand", "Nigeria", "Norway", "Pakistan", "Peru", "Philippines", "Poland", "Portugal",
    "Qatar", "Romania", "Russia", "Saudi Arabia", "Serbia", "Singapore", "Slovakia", "Slovenia", "South Africa", "South Korea",
    "Spain", "Sri Lanka", "Sweden", "Switzerland", "Thailand", "Tunisia", "Turkey", "Ukraine", "United Arab Emirates", "Vietnam",
    "Zambia", "Zimbabwe"
]))

def init_db():
    with sqlite3.connect(DB_PATH) as conn:
        conn.execute("""
            CREATE TABLE IF NOT EXISTS teamup (
                id INTEGER PRIMARY KEY AUTOINCREMENT,
                name TEXT,
                discord TEXT UNIQUE,
                city TEXT,
                country TEXT,
                address TEXT,
                looking TEXT,
                onlinecheck TEXT,
                languages TEXT,
                laptop TEXT,
                robot TEXT,
                skills TEXT,
                describe3 TEXT,
                experience TEXT,
                idea TEXT,
                created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
            );
        """)
init_db()

def backup_db():
    timestamp = datetime.datetime.now().strftime('%Y%m%d_%H%M%S')
    backup_file = os.path.join(BACKUP_DIR, f"teamup_backup_{timestamp}.db")
    shutil.copy(DB_PATH, backup_file)

    backups = sorted(os.listdir(BACKUP_DIR))
    while len(backups) > 20:
        os.remove(os.path.join(BACKUP_DIR, backups.pop(0)))

def submit_profile(name, discord, city, country, address, looking, onlinecheck, languages, laptop, robot, skills, describe3, experience, idea):
    if not discord or not city or not country or not laptop or not robot:
        return "❌ Please fill in all required fields."

    lang_str = ", ".join(languages) if isinstance(languages, list) else languages

    with sqlite3.connect(DB_PATH) as conn:
        conn.execute("""
            INSERT INTO teamup (discord, name, city, country, address, looking, onlinecheck, languages, laptop, robot, skills, describe3, experience, idea)
            VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
            ON CONFLICT(discord) DO UPDATE SET
                name=excluded.name,
                city=excluded.city,
                country=excluded.country,
                address=excluded.address,
                looking=excluded.looking,
                onlinecheck=excluded.onlinecheck,
                languages=excluded.languages,
                laptop=excluded.laptop,
                robot=excluded.robot,
                skills=excluded.skills,
                describe3=excluded.describe3,
                experience=excluded.experience,
                idea=excluded.idea
        """, (discord, name, city.title(), country.title(), address, looking, onlinecheck, lang_str.lower(), laptop, robot, skills, describe3, experience, idea))

    backup_db()
    return "βœ… Profile saved!"

def filter_by_fields(selected_country, selected_city, selected_language, page):
    offset = (page - 1) * PAGE_SIZE

    query = "SELECT * FROM teamup"
    filters = []
    params = []

    if selected_country != "All":
        filters.append("country = ?")
        params.append(selected_country.title())
    if selected_city != "All":
        filters.append("city = ?")
        params.append(selected_city.title())
    if selected_language != "All":
        filters.append("languages LIKE ?")
        params.append(f"%{selected_language.lower()}%")

    if filters:
        query += " WHERE " + " AND ".join(filters)
    query += " ORDER BY created_at DESC LIMIT ? OFFSET ?"
    params.extend([PAGE_SIZE, offset])

    with sqlite3.connect(DB_PATH) as conn:
        df = pd.read_sql_query(query, conn, params=params)
        total_query = "SELECT COUNT(*) FROM teamup" + (" WHERE " + " AND ".join(filters) if filters else "")
        total_count = pd.read_sql_query(total_query, conn, params=params[:-2] if filters else []).iloc[0, 0]

    if df.empty:
        return "<p>No participants match your filters.</p>", page, total_count

    html = "<h3 style='margin-top:20px;'>πŸ” Find your matching Teammates & Register your team <a href='https://forms.gle/gJEMGD4CEA2emhD18' target='_blank'>here</a></h3>"
    html += "<table style='width:100%; border-collapse: collapse;'><tr>"
    headers = ["Discord", "Name", "City", "Country", "Looking", "How?", "Languages", "Laptop", "Robot", "Skills", "Description", "Experience", "Idea"]
    for col in headers:
        html += f"<th style='border:1px solid #ccc; padding:6px;'>{col}</th>"
    html += "</tr>"

    for _, row in df.iterrows():
        html += "<tr>"
        for col in ["discord", "name", "city", "country", "looking", "onlinecheck", "languages", "laptop", "robot", "skills", "describe3", "experience", "idea"]:
            val = row[col]
            if col == "discord":
                val = f"<a href='https://discord.com/users/{val}' target='_blank'>{val}</a>"
            html += f"<td style='border:1px solid #eee; padding:6px;'>{val}</td>"
        html += "</tr>"

    html += "</table>"
    return html, page, total_count

def update_city_filter(country):
    with sqlite3.connect(DB_PATH) as conn:
        if country == "All":
            result = conn.execute("SELECT DISTINCT city FROM teamup").fetchall()
        else:
            result = conn.execute("SELECT DISTINCT city FROM teamup WHERE country = ?", (country.title(),)).fetchall()
    cities = [r[0].title() for r in result if r[0]]
    return gr.update(choices=["All"] + sorted(cities), value="All")

def delete_by_discord(discord, code):
    if code != ADMIN_CODE:
        return "❌ Invalid admin code."
    with sqlite3.connect(DB_PATH) as conn:
        conn.execute("DELETE FROM teamup WHERE lower(discord) = ?", (discord.lower(),))
    return f"πŸ—‘οΈ Deleted user with Discord: {discord}"

def download_csv(code):
    if code != ADMIN_CODE:
        raise gr.Error("❌ Invalid admin code.")
    with sqlite3.connect(DB_PATH) as conn:
        df = pd.read_sql_query("SELECT * FROM teamup", conn)
    csv_path = os.path.join(DATA_DIR, "teamup_export.csv")
    df.to_csv(csv_path, index=False)
    return csv_path

def interface():
    with gr.Blocks(css=".gr-dropdown { max-height: 100px; overflow-y: auto; font-size: 12px; }") as demo:
        gr.Markdown("""
        # 🌍 LeRobot Worldwide Hackathon - Team-Up Dashboard

        Welcome to the Team-Up dashboard! Follow these steps:

        1. **Submit or update** your profile to help others find and contact you via Discord. Required fields are marked with `*`.
        2. **Use the filters** (country, city, language) to search for teammates from around the world.
        """)

        with gr.Row():
            with gr.Column():
                name = gr.Text(label="Name")
                discord = gr.Text(label="πŸ‘€ Discord Username *")
                city = gr.Text(label="πŸ“ City *")
                country = gr.Dropdown(label="🌍 Country *", choices=ALL_COUNTRIES, value="France")
                address = gr.Text(label="Address (optional)")
                looking = gr.Radio(["Yes", "No"], label="πŸ” Looking for a team?")
                onlinecheck = gr.Radio(["Participate Online", "Join a Local Hackathon"], label="πŸš€ I will...")
                languages = gr.CheckboxGroup(choices=LANGUAGES, label="Languages Spoken *")
                laptop = gr.Text(label="πŸ’» Laptop Setup *")
                robot = gr.Text(label="πŸ€– Robot Setup *")
                skills = gr.Text(label="🧠 Skills (comma separated)")
                describe3 = gr.Text(label="3 Words That Describe You")
                experience = gr.Dropdown(choices=["Beginner", "Intermediate", "Advanced"], label="Experience Level", value="Beginner")
                idea = gr.Textbox(label="Project Idea (optional)")
                submit_btn = gr.Button("Submit or Update βœ…")
                status = gr.Textbox(label="", interactive=False)

            with gr.Column():
                gr.Markdown("""
                🎯 **Use the filters below to find your ideal teammates:**
                """)
                country_filter = gr.Dropdown(label="Filter by Country", choices=["All"] + ALL_COUNTRIES, value="All")
                city_filter = gr.Dropdown(label="Filter by City", choices=["All"], value="All")
                language_filter = gr.Dropdown(label="Filter by Language", choices=["All"] + LANGUAGES, value="All")
                page_state = gr.Number(value=1, visible=False)
                table_html = gr.HTML(label="Matching Participants")
                prev_btn = gr.Button("⬅️ Previous Page")
                next_btn = gr.Button("➑️ Next Page")

        submit_btn.click(submit_profile, inputs=[name, discord, city, country, address, looking, onlinecheck, languages, laptop, robot, skills, describe3, experience, idea], outputs=[status])
        submit_btn.click(lambda: 1, outputs=[page_state])
        submit_btn.click(filter_by_fields, inputs=[country_filter, city_filter, language_filter, gr.State(1)], outputs=[table_html, page_state, gr.Number(visible=False)])

        country_filter.change(update_city_filter, inputs=[country_filter], outputs=[city_filter])

        for dropdown in [country_filter, city_filter, language_filter]:
            dropdown.change(lambda c, ci, l: (1,), inputs=[country_filter, city_filter, language_filter], outputs=[page_state])
            dropdown.change(filter_by_fields, inputs=[country_filter, city_filter, language_filter, page_state], outputs=[table_html, page_state, gr.Number(visible=False)])

        prev_btn.click(lambda p: max(p - 1, 1), inputs=[page_state], outputs=[page_state])
        prev_btn.click(filter_by_fields, inputs=[country_filter, city_filter, language_filter, page_state], outputs=[table_html, page_state, gr.Number(visible=False)])

        next_btn.click(lambda p: p + 1, inputs=[page_state], outputs=[page_state])
        next_btn.click(filter_by_fields, inputs=[country_filter, city_filter, language_filter, page_state], outputs=[table_html, page_state, gr.Number(visible=False)])

        del_btn = gr.Button("Delete Profile")
        admin_discord = gr.Text(label="Discord Username")
        admin_code = gr.Text(label="Admin Code", type="password")
        del_status = gr.Textbox(label="Status", interactive=False)
        del_btn.click(delete_by_discord, inputs=[admin_discord, admin_code], outputs=[del_status])
        del_btn.click(lambda: 1, outputs=[page_state])
        del_btn.click(filter_by_fields, inputs=[country_filter, city_filter, language_filter, gr.State(1)], outputs=[table_html, page_state, gr.Number(visible=False)])

        gr.Markdown("---\n### πŸ“… Admin Export CSV")
        export_code = gr.Text(label="Admin Code", type="password")
        download_btn = gr.Button("Generate and Download CSV")
        download_file = gr.File(label="CSV Export", interactive=False)
        download_btn.click(download_csv, inputs=[export_code], outputs=[download_file])

    return demo

demo = interface()
demo.launch()