Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -19,12 +19,6 @@ ORDERS_PATH = "orders.csv"
|
|
| 19 |
SENT_PATH = "sent.csv"
|
| 20 |
KEYS_PATH = "keys.csv"
|
| 21 |
|
| 22 |
-
# # Optional SMTP config (Space secrets)
|
| 23 |
-
# SMTP_HOST = os.getenv("SMTP_HOST")
|
| 24 |
-
# SMTP_PORT = int(os.getenv("SMTP_PORT", "587"))
|
| 25 |
-
# SMTP_USER = os.getenv("SMTP_USER")
|
| 26 |
-
# SMTP_PASS = os.getenv("SMTP_PASS")
|
| 27 |
-
# FROM_EMAIL = os.getenv("FROM_EMAIL", SMTP_USER or "[email protected]")
|
| 28 |
|
| 29 |
api = HfApi(token=HF_TOKEN)
|
| 30 |
_lock = threading.Lock()
|
|
@@ -149,39 +143,10 @@ def load_state(force: bool = False):
|
|
| 149 |
_cache["keys"] = keys
|
| 150 |
return orders, sent, keys
|
| 151 |
|
| 152 |
-
def send_key_email(to_email: str, keys: list, contact_person: str) -> None:
|
| 153 |
-
"""
|
| 154 |
-
Send keys to the customer via email.
|
| 155 |
-
"""
|
| 156 |
-
# if not (SMTP_HOST and SMTP_USER and SMTP_PASS):
|
| 157 |
-
# return
|
| 158 |
-
|
| 159 |
-
# import smtplib
|
| 160 |
-
# from email.message import EmailMessage
|
| 161 |
-
|
| 162 |
-
# msg = EmailMessage()
|
| 163 |
-
# msg["Subject"] = "Your Gradium Keys"
|
| 164 |
-
# msg["From"] = FROM_EMAIL
|
| 165 |
-
# msg["To"] = to_email
|
| 166 |
-
# keys_text = "\n".join([f"- {k}" for k in keys])
|
| 167 |
-
# msg.set_content(
|
| 168 |
-
# f"Hi {contact_person},\n\n"
|
| 169 |
-
# f"Here are your Gradium keys:\n\n"
|
| 170 |
-
# f"{keys_text}\n\n"
|
| 171 |
-
# f"Best regards,\nGradium Team\n"
|
| 172 |
-
# )
|
| 173 |
-
|
| 174 |
-
# with smtplib.SMTP(SMTP_HOST, SMTP_PORT) as s:
|
| 175 |
-
# s.starttls()
|
| 176 |
-
# s.login(SMTP_USER, SMTP_PASS)
|
| 177 |
-
# s.send_message(msg)
|
| 178 |
-
|
| 179 |
-
pass
|
| 180 |
-
|
| 181 |
# -------------------------
|
| 182 |
# Core API
|
| 183 |
# -------------------------
|
| 184 |
-
def
|
| 185 |
order_number: str,
|
| 186 |
request: Optional[gr.Request] = None,
|
| 187 |
) -> Tuple[str, str]:
|
|
@@ -238,24 +203,19 @@ def send_keys_for_order(
|
|
| 238 |
DATASET_REPO_ID,
|
| 239 |
SENT_PATH,
|
| 240 |
updated_sent_csv,
|
| 241 |
-
commit_message=f"Send keys for order
|
| 242 |
)
|
| 243 |
_upload_csv(
|
| 244 |
DATASET_REPO_ID,
|
| 245 |
KEYS_PATH,
|
| 246 |
updated_keys_csv,
|
| 247 |
-
commit_message=f"Mark keys as sent for order
|
| 248 |
)
|
| 249 |
|
| 250 |
# refresh cache immediately
|
| 251 |
_cache["ts"] = 0.0
|
| 252 |
|
| 253 |
-
|
| 254 |
-
try:
|
| 255 |
-
send_key_email(order["email_address"], keys_str.split(","), order["contact_person"])
|
| 256 |
-
return keys_str, f"Keys sent successfully to {order['email_address']}."
|
| 257 |
-
except Exception as e:
|
| 258 |
-
return keys_str, f"Keys allocated: {keys_str}. Email failed: {str(e)}"
|
| 259 |
|
| 260 |
# -------------------------
|
| 261 |
# UI
|
|
@@ -263,20 +223,21 @@ def send_keys_for_order(
|
|
| 263 |
with gr.Blocks() as demo:
|
| 264 |
gr.Markdown(
|
| 265 |
"## Gradium Key Distribution System\n\n"
|
| 266 |
-
"Enter an **order number** to
|
| 267 |
)
|
| 268 |
|
| 269 |
order_in = gr.Textbox(label="Order number", placeholder="e.g. 0000-1111")
|
| 270 |
-
|
| 271 |
-
keys_out = gr.Textbox(label="
|
| 272 |
status_out = gr.Textbox(label="Status", interactive=False)
|
| 273 |
|
| 274 |
-
|
| 275 |
-
fn=
|
| 276 |
inputs=[order_in],
|
| 277 |
outputs=[keys_out, status_out],
|
| 278 |
-
api_name="
|
| 279 |
)
|
| 280 |
|
| 281 |
demo.queue()
|
| 282 |
demo.launch()
|
|
|
|
|
|
| 19 |
SENT_PATH = "sent.csv"
|
| 20 |
KEYS_PATH = "keys.csv"
|
| 21 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
api = HfApi(token=HF_TOKEN)
|
| 24 |
_lock = threading.Lock()
|
|
|
|
| 143 |
_cache["keys"] = keys
|
| 144 |
return orders, sent, keys
|
| 145 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 146 |
# -------------------------
|
| 147 |
# Core API
|
| 148 |
# -------------------------
|
| 149 |
+
def claim_key(
|
| 150 |
order_number: str,
|
| 151 |
request: Optional[gr.Request] = None,
|
| 152 |
) -> Tuple[str, str]:
|
|
|
|
| 203 |
DATASET_REPO_ID,
|
| 204 |
SENT_PATH,
|
| 205 |
updated_sent_csv,
|
| 206 |
+
commit_message=f"Send keys for new order at {_utc_now_iso()}",
|
| 207 |
)
|
| 208 |
_upload_csv(
|
| 209 |
DATASET_REPO_ID,
|
| 210 |
KEYS_PATH,
|
| 211 |
updated_keys_csv,
|
| 212 |
+
commit_message=f"Mark keys as sent for new order at {_utc_now_iso()}",
|
| 213 |
)
|
| 214 |
|
| 215 |
# refresh cache immediately
|
| 216 |
_cache["ts"] = 0.0
|
| 217 |
|
| 218 |
+
return keys_str, f"Keys sent successfully."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 219 |
|
| 220 |
# -------------------------
|
| 221 |
# UI
|
|
|
|
| 223 |
with gr.Blocks() as demo:
|
| 224 |
gr.Markdown(
|
| 225 |
"## Gradium Key Distribution System\n\n"
|
| 226 |
+
"Enter an **order number** to claim your key.\n\n"
|
| 227 |
)
|
| 228 |
|
| 229 |
order_in = gr.Textbox(label="Order number", placeholder="e.g. 0000-1111")
|
| 230 |
+
claim_btn = gr.Button("Claim Key", variant="primary")
|
| 231 |
+
keys_out = gr.Textbox(label="Your Key(s)", interactive=False)
|
| 232 |
status_out = gr.Textbox(label="Status", interactive=False)
|
| 233 |
|
| 234 |
+
claim_btn.click(
|
| 235 |
+
fn=claim_key,
|
| 236 |
inputs=[order_in],
|
| 237 |
outputs=[keys_out, status_out],
|
| 238 |
+
api_name="claim",
|
| 239 |
)
|
| 240 |
|
| 241 |
demo.queue()
|
| 242 |
demo.launch()
|
| 243 |
+
|