VyLala commited on
Commit
ba972a6
·
verified ·
1 Parent(s): ce7fb9c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -0
app.py CHANGED
@@ -290,6 +290,52 @@ with gr.Blocks() as interface:
290
  # "✅ Done",
291
  # "\n".join(log_lines)
292
  # )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
293
 
294
  def threaded_batch_runner(file=None, text="", email=""):
295
  print("📧 EMAIL RECEIVED:", repr(email))
@@ -343,6 +389,8 @@ with gr.Blocks() as interface:
343
  limited_acc = int(max_allowed-usage_count)
344
  # Step 1: Parse input
345
  accessions, error = extract_accessions_from_input(file, text)
 
 
346
  print("🧪 Accessions received:", accessions)
347
  if error:
348
  yield (
 
290
  # "✅ Done",
291
  # "\n".join(log_lines)
292
  # )
293
+
294
+ def log_submission_to_gsheet(email, samples):
295
+ from datetime import datetime
296
+ import json, os, gspread
297
+ from oauth2client.service_account import ServiceAccountCredentials
298
+ import uuid
299
+
300
+ timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
301
+ if not email.strip():
302
+ email = f"anonymous_{str(uuid.uuid4())[:8]}"
303
+
304
+ try:
305
+ creds_dict = json.loads(os.environ["GCP_CREDS_JSON"])
306
+ scope = ["https://spreadsheets.google.com/feeds", "https://www.googleapis.com/auth/drive"]
307
+ creds = ServiceAccountCredentials.from_json_keyfile_dict(creds_dict, scope)
308
+ client = gspread.authorize(creds)
309
+
310
+ sheet = client.open("user_usage_log")
311
+ worksheet = sheet.sheet1 # Main sheet
312
+
313
+ data = worksheet.get_all_values()
314
+ headers = data[0]
315
+ email_col = headers.index("email")
316
+ samples_col = headers.index("samples")
317
+ recent_time_col = headers.index("recent_time")
318
+
319
+ # Find the row to update
320
+ for i, row in enumerate(data[1:], start=2): # start=2 because header is row 1
321
+ if row[email_col].strip().lower() == email.strip().lower():
322
+ worksheet.update_cell(i, samples_col + 1, ", ".join(samples))
323
+ worksheet.update_cell(i, recent_time_col + 1, timestamp)
324
+ print(f"✅ Updated existing user row for: {email}")
325
+ return
326
+
327
+ # If email not found, append a new row
328
+ new_row = [""] * len(headers)
329
+ new_row[email_col] = email
330
+ new_row[samples_col] = ", ".join(samples)
331
+ new_row[recent_time_col] = timestamp
332
+ worksheet.append_row(new_row)
333
+ print(f"✅ Appended new user row for: {email}")
334
+
335
+ except Exception as e:
336
+ print(f"❌ Failed to log submission to Google Sheets: {e}")
337
+
338
+
339
 
340
  def threaded_batch_runner(file=None, text="", email=""):
341
  print("📧 EMAIL RECEIVED:", repr(email))
 
389
  limited_acc = int(max_allowed-usage_count)
390
  # Step 1: Parse input
391
  accessions, error = extract_accessions_from_input(file, text)
392
+ log_submission_to_gsheet(email, accessions)
393
+
394
  print("🧪 Accessions received:", accessions)
395
  if error:
396
  yield (