Spaces:
Sleeping
Sleeping
add ui text settings to federation module
Browse files- blossomtune_gradio/federation.py +22 -49
blossomtune_gradio/federation.py
CHANGED
|
@@ -6,6 +6,7 @@ from datetime import datetime
|
|
| 6 |
|
| 7 |
from blossomtune_gradio import config as cfg
|
| 8 |
from blossomtune_gradio import mail
|
|
|
|
| 9 |
|
| 10 |
|
| 11 |
def generate_participant_id(length=6):
|
|
@@ -24,7 +25,6 @@ def check_participant_status(pid_to_check: str, email: str, activation_code: str
|
|
| 24 |
"""Handles a participant's request to join, activate, or check status."""
|
| 25 |
with sqlite3.connect(cfg.DB_PATH) as conn:
|
| 26 |
cursor = conn.cursor()
|
| 27 |
-
# NOTE: Assumes DB schema is updated with `is_activated` and `activation_code`
|
| 28 |
if activation_code:
|
| 29 |
cursor.execute(
|
| 30 |
"SELECT participant_id, status, partition_id, is_activated, activation_code FROM requests WHERE hf_handle = ? AND email = ? AND activation_code = ?",
|
|
@@ -44,22 +44,16 @@ def check_participant_status(pid_to_check: str, email: str, activation_code: str
|
|
| 44 |
# Case 1: New user registration
|
| 45 |
if result is None:
|
| 46 |
if activation_code:
|
| 47 |
-
return (
|
| 48 |
-
False,
|
| 49 |
-
"### ❌ Activation code is not valid, or participant hasn't subscribed yet.",
|
| 50 |
-
)
|
| 51 |
if not email or "@" not in email:
|
| 52 |
-
return False,
|
| 53 |
|
| 54 |
with sqlite3.connect(cfg.DB_PATH) as conn:
|
| 55 |
cursor = conn.cursor()
|
| 56 |
cursor.execute("SELECT COUNT(*) FROM requests WHERE status = 'approved'")
|
| 57 |
approved_count = cursor.fetchone()[0]
|
| 58 |
if approved_count >= cfg.MAX_NUM_NODES:
|
| 59 |
-
return (
|
| 60 |
-
False,
|
| 61 |
-
"### ❌ Federation Full\n**We're sorry, but we cannot accept new participants at this time.**",
|
| 62 |
-
)
|
| 63 |
|
| 64 |
participant_id = generate_participant_id()
|
| 65 |
new_activation_code = generate_activation_code()
|
|
@@ -78,10 +72,7 @@ def check_participant_status(pid_to_check: str, email: str, activation_code: str
|
|
| 78 |
0,
|
| 79 |
),
|
| 80 |
)
|
| 81 |
-
return (
|
| 82 |
-
True,
|
| 83 |
-
"✅ **Registration Submitted!** Please check your email for an activation code to complete your request.",
|
| 84 |
-
)
|
| 85 |
else:
|
| 86 |
return (False, message)
|
| 87 |
|
|
@@ -96,21 +87,12 @@ def check_participant_status(pid_to_check: str, email: str, activation_code: str
|
|
| 96 |
"UPDATE requests SET is_activated = 1 WHERE hf_handle = ?",
|
| 97 |
(pid_to_check,),
|
| 98 |
)
|
| 99 |
-
return (
|
| 100 |
-
True,
|
| 101 |
-
"✅ **Activation Successful!** Your request is now pending review by an administrator.",
|
| 102 |
-
)
|
| 103 |
else:
|
| 104 |
-
return (
|
| 105 |
-
False,
|
| 106 |
-
"### ❌ Activation code is not valid, or participant hasn't subscribed yet.",
|
| 107 |
-
)
|
| 108 |
else:
|
| 109 |
if not activation_code:
|
| 110 |
-
(
|
| 111 |
-
False,
|
| 112 |
-
"⏳ **Missing Activation Code.** Please check your email and enter the activation code you received.",
|
| 113 |
-
)
|
| 114 |
|
| 115 |
# Case 3: Activated user is checking their status
|
| 116 |
if status == "approved":
|
|
@@ -120,30 +102,21 @@ def check_participant_status(pid_to_check: str, email: str, activation_code: str
|
|
| 120 |
else f"{cfg.SPACE_ID.split('/')[1]}-{cfg.SPACE_ID.split('/')[0]}.hf.space"
|
| 121 |
)
|
| 122 |
superlink_hostname = cfg.SUPERLINK_HOST or hostname
|
| 123 |
-
|
| 124 |
-
connection_string =
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
*Example `flower-supernode` command:*
|
| 133 |
-
```bash
|
| 134 |
-
flower-supernode --superlink {hostname}:9092 --insecure --node-config "partition-id={partition_id} num-partitions={num_partitions}"
|
| 135 |
-
```
|
| 136 |
-
"""
|
| 137 |
return True, connection_string
|
| 138 |
elif status == "pending":
|
| 139 |
-
return (
|
| 140 |
-
False,
|
| 141 |
-
"### ⏳ Pending\nYour request has been activated and is awaiting administrator review.",
|
| 142 |
-
)
|
| 143 |
else: # Denied
|
| 144 |
return (
|
| 145 |
False,
|
| 146 |
-
|
| 147 |
)
|
| 148 |
|
| 149 |
|
|
@@ -159,7 +132,6 @@ def manage_request(participant_id: str, partition_id: str, action: str):
|
|
| 159 |
p_id_int = int(partition_id)
|
| 160 |
with sqlite3.connect(cfg.DB_PATH) as conn:
|
| 161 |
cursor = conn.cursor()
|
| 162 |
-
# Check if participant is activated before approval
|
| 163 |
cursor.execute(
|
| 164 |
"SELECT is_activated FROM requests WHERE participant_id = ?",
|
| 165 |
(participant_id,),
|
|
@@ -168,10 +140,9 @@ def manage_request(participant_id: str, partition_id: str, action: str):
|
|
| 168 |
if not is_activated_res or not is_activated_res[0]:
|
| 169 |
return (
|
| 170 |
False,
|
| 171 |
-
|
| 172 |
)
|
| 173 |
|
| 174 |
-
# Check if the partition ID is already in use by an approved participant
|
| 175 |
cursor.execute(
|
| 176 |
"SELECT 1 FROM requests WHERE partition_id = ? AND status = 'approved'",
|
| 177 |
(p_id_int,),
|
|
@@ -179,7 +150,9 @@ def manage_request(participant_id: str, partition_id: str, action: str):
|
|
| 179 |
if cursor.fetchone():
|
| 180 |
return (
|
| 181 |
False,
|
| 182 |
-
|
|
|
|
|
|
|
| 183 |
)
|
| 184 |
|
| 185 |
conn.execute(
|
|
|
|
| 6 |
|
| 7 |
from blossomtune_gradio import config as cfg
|
| 8 |
from blossomtune_gradio import mail
|
| 9 |
+
from blossomtune_gradio.settings import settings
|
| 10 |
|
| 11 |
|
| 12 |
def generate_participant_id(length=6):
|
|
|
|
| 25 |
"""Handles a participant's request to join, activate, or check status."""
|
| 26 |
with sqlite3.connect(cfg.DB_PATH) as conn:
|
| 27 |
cursor = conn.cursor()
|
|
|
|
| 28 |
if activation_code:
|
| 29 |
cursor.execute(
|
| 30 |
"SELECT participant_id, status, partition_id, is_activated, activation_code FROM requests WHERE hf_handle = ? AND email = ? AND activation_code = ?",
|
|
|
|
| 44 |
# Case 1: New user registration
|
| 45 |
if result is None:
|
| 46 |
if activation_code:
|
| 47 |
+
return (False, settings.get_text("activation_invalid_md"))
|
|
|
|
|
|
|
|
|
|
| 48 |
if not email or "@" not in email:
|
| 49 |
+
return (False, settings.get_text("invalid_email_md"))
|
| 50 |
|
| 51 |
with sqlite3.connect(cfg.DB_PATH) as conn:
|
| 52 |
cursor = conn.cursor()
|
| 53 |
cursor.execute("SELECT COUNT(*) FROM requests WHERE status = 'approved'")
|
| 54 |
approved_count = cursor.fetchone()[0]
|
| 55 |
if approved_count >= cfg.MAX_NUM_NODES:
|
| 56 |
+
return (False, settings.get_text("federation_full_md"))
|
|
|
|
|
|
|
|
|
|
| 57 |
|
| 58 |
participant_id = generate_participant_id()
|
| 59 |
new_activation_code = generate_activation_code()
|
|
|
|
| 72 |
0,
|
| 73 |
),
|
| 74 |
)
|
| 75 |
+
return (True, settings.get_text("registration_submitted_md"))
|
|
|
|
|
|
|
|
|
|
| 76 |
else:
|
| 77 |
return (False, message)
|
| 78 |
|
|
|
|
| 87 |
"UPDATE requests SET is_activated = 1 WHERE hf_handle = ?",
|
| 88 |
(pid_to_check,),
|
| 89 |
)
|
| 90 |
+
return (True, settings.get_text("activation_successful_md"))
|
|
|
|
|
|
|
|
|
|
| 91 |
else:
|
| 92 |
+
return (False, settings.get_text("activation_invalid_md"))
|
|
|
|
|
|
|
|
|
|
| 93 |
else:
|
| 94 |
if not activation_code:
|
| 95 |
+
return (False, settings.get_text("missing_activation_code_md"))
|
|
|
|
|
|
|
|
|
|
| 96 |
|
| 97 |
# Case 3: Activated user is checking their status
|
| 98 |
if status == "approved":
|
|
|
|
| 102 |
else f"{cfg.SPACE_ID.split('/')[1]}-{cfg.SPACE_ID.split('/')[0]}.hf.space"
|
| 103 |
)
|
| 104 |
superlink_hostname = cfg.SUPERLINK_HOST or hostname
|
| 105 |
+
|
| 106 |
+
connection_string = settings.get_text(
|
| 107 |
+
"status_approved_md",
|
| 108 |
+
participant_id=participant_id,
|
| 109 |
+
partition_id=partition_id,
|
| 110 |
+
superlink_hostname=superlink_hostname,
|
| 111 |
+
num_partitions=num_partitions,
|
| 112 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 113 |
return True, connection_string
|
| 114 |
elif status == "pending":
|
| 115 |
+
return (False, settings.get_text("status_pending_md"))
|
|
|
|
|
|
|
|
|
|
| 116 |
else: # Denied
|
| 117 |
return (
|
| 118 |
False,
|
| 119 |
+
settings.get_text("status_denied_md", participant_id=participant_id),
|
| 120 |
)
|
| 121 |
|
| 122 |
|
|
|
|
| 132 |
p_id_int = int(partition_id)
|
| 133 |
with sqlite3.connect(cfg.DB_PATH) as conn:
|
| 134 |
cursor = conn.cursor()
|
|
|
|
| 135 |
cursor.execute(
|
| 136 |
"SELECT is_activated FROM requests WHERE participant_id = ?",
|
| 137 |
(participant_id,),
|
|
|
|
| 140 |
if not is_activated_res or not is_activated_res[0]:
|
| 141 |
return (
|
| 142 |
False,
|
| 143 |
+
settings.get_text("participant_not_activated_warning_md"),
|
| 144 |
)
|
| 145 |
|
|
|
|
| 146 |
cursor.execute(
|
| 147 |
"SELECT 1 FROM requests WHERE partition_id = ? AND status = 'approved'",
|
| 148 |
(p_id_int,),
|
|
|
|
| 150 |
if cursor.fetchone():
|
| 151 |
return (
|
| 152 |
False,
|
| 153 |
+
settings.get_text(
|
| 154 |
+
"partition_in_use_warning_md", partition_id=p_id_int
|
| 155 |
+
),
|
| 156 |
)
|
| 157 |
|
| 158 |
conn.execute(
|