Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -157,122 +157,237 @@ with gr.Blocks() as interface:
|
|
157 |
# gr.update(value=final_log)
|
158 |
# )
|
159 |
|
160 |
-
def threaded_batch_runner(file=None, text="", email=""):
|
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 |
-
|
245 |
-
|
246 |
-
|
247 |
-
|
248 |
-
|
249 |
-
|
250 |
-
|
251 |
-
|
252 |
-
|
253 |
-
|
254 |
-
|
255 |
-
|
256 |
-
|
257 |
-
|
258 |
-
|
259 |
|
260 |
-
|
261 |
-
|
262 |
-
|
263 |
-
|
264 |
-
|
265 |
-
|
266 |
-
|
267 |
-
|
268 |
-
|
269 |
-
|
270 |
-
|
271 |
-
|
272 |
-
|
273 |
-
|
274 |
-
|
275 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
276 |
|
277 |
# def threaded_batch_runner(file=None, text="", email=""):
|
278 |
# global_stop_flag.value = False
|
|
|
157 |
# gr.update(value=final_log)
|
158 |
# )
|
159 |
|
160 |
+
# def threaded_batch_runner(file=None, text="", email=""):
|
161 |
+
# print("π§ EMAIL RECEIVED:", email)
|
162 |
+
# import tempfile
|
163 |
+
# from mtdna_backend import (
|
164 |
+
# extract_accessions_from_input,
|
165 |
+
# summarize_results,
|
166 |
+
# save_to_excel,
|
167 |
+
# hash_user_id,
|
168 |
+
# increment_usage,
|
169 |
+
# )
|
170 |
+
# import os
|
171 |
+
|
172 |
+
# global_stop_flag.value = False # reset stop flag
|
173 |
+
|
174 |
+
# tmp_dir = tempfile.mkdtemp()
|
175 |
+
# output_file_path = os.path.join(tmp_dir, "batch_output_live.xlsx")
|
176 |
+
# limited_acc = 50 + (10 if email.strip() else 0)
|
177 |
+
|
178 |
+
# # Step 1: Parse input
|
179 |
+
# accessions, error = extract_accessions_from_input(file, text)
|
180 |
+
# print(accessions)
|
181 |
+
# if error:
|
182 |
+
# yield (
|
183 |
+
# "", # output_table
|
184 |
+
# gr.update(visible=False), # results_group
|
185 |
+
# gr.update(visible=False), # download_file
|
186 |
+
# "", # usage_display
|
187 |
+
# "β Error", # status
|
188 |
+
# str(error) # progress_box
|
189 |
+
# )
|
190 |
+
# return
|
191 |
+
|
192 |
+
# total = len(accessions)
|
193 |
+
# if total > limited_acc:
|
194 |
+
# accessions = accessions[:limited_acc]
|
195 |
+
# warning = f"β οΈ Only processing first {limited_acc} accessions."
|
196 |
+
# else:
|
197 |
+
# warning = f"β
All {total} accessions will be processed."
|
198 |
+
|
199 |
+
# all_rows = []
|
200 |
+
# processed_accessions = 0 # β
tracks how many accessions were processed
|
201 |
+
# email_tracked = False
|
202 |
+
# log_lines = []
|
203 |
+
|
204 |
+
# # Step 2: Loop through accessions
|
205 |
+
# for i, acc in enumerate(accessions):
|
206 |
+
# if global_stop_flag.value:
|
207 |
+
# log_lines.append(f"π Stopped at {acc} ({i+1}/{total})")
|
208 |
+
# usage_text = ""
|
209 |
+
# if email.strip() and not email_tracked:
|
210 |
+
# # user_hash = hash_user_id(email)
|
211 |
+
# # usage_count = increment_usage(user_hash, len(all_rows))
|
212 |
+
# print("print(processed_accessions at stop) ",processed_accessions)
|
213 |
+
# usage_count = increment_usage(email, processed_accessions)
|
214 |
+
# email_tracked = True
|
215 |
+
# usage_text = f"**{usage_count}** samples used by this email. Ten more samples are added first (you now have 60 limited accessions), then wait we will contact you via this email."
|
216 |
+
# else:
|
217 |
+
# usage_text = f"The limited accession is 50. The user has used {processed_accessions}, and only {50-processed_accessions} left."
|
218 |
+
# yield (
|
219 |
+
# make_html_table(all_rows),
|
220 |
+
# gr.update(visible=True),
|
221 |
+
# gr.update(value=output_file_path, visible=True),
|
222 |
+
# gr.update(value=usage_text, visible=True),
|
223 |
+
# "π Stopped",
|
224 |
+
# "\n".join(log_lines)
|
225 |
+
# )
|
226 |
+
# return
|
227 |
+
|
228 |
+
# log_lines.append(f"[{i+1}/{total}] Processing {acc}")
|
229 |
+
# yield (
|
230 |
+
# make_html_table(all_rows),
|
231 |
+
# gr.update(visible=True),
|
232 |
+
# gr.update(visible=False),
|
233 |
+
# "",
|
234 |
+
# "β³ Processing...",
|
235 |
+
# "\n".join(log_lines)
|
236 |
+
# )
|
237 |
+
|
238 |
+
# try:
|
239 |
+
# print(acc)
|
240 |
+
# rows = summarize_results(acc)
|
241 |
+
# all_rows.extend(rows)
|
242 |
+
# processed_accessions += 1 # β
count only successful accessions
|
243 |
+
# save_to_excel(all_rows, "", "", output_file_path, is_resume=False)
|
244 |
+
# log_lines.append(f"β
Processed {acc} ({i+1}/{total})")
|
245 |
+
# except Exception as e:
|
246 |
+
# log_lines.append(f"β Failed to process {acc}: {e}")
|
247 |
+
|
248 |
+
# yield (
|
249 |
+
# make_html_table(all_rows),
|
250 |
+
# gr.update(visible=True),
|
251 |
+
# gr.update(visible=False),
|
252 |
+
# "",
|
253 |
+
# "β³ Processing...",
|
254 |
+
# "\n".join(log_lines)
|
255 |
+
# )
|
256 |
+
|
257 |
+
# # Final update
|
258 |
+
# usage_text = ""
|
259 |
|
260 |
+
# if email.strip() and not email_tracked:
|
261 |
+
# # user_hash = hash_user_id(email)
|
262 |
+
# # usage_count = increment_usage(user_hash, len(all_rows))
|
263 |
+
# print("print(processed_accessions final) ",processed_accessions)
|
264 |
+
# usage_count = increment_usage(email, processed_accessions)
|
265 |
+
# usage_text = f"**{usage_count}** samples used by this email. Ten more samples are added first (you now have 60 limited accessions), then wait we will contact you via this email."
|
266 |
+
# elif not email.strip():
|
267 |
+
# usage_text = f"The limited accession is 50. The user has used {processed_accessions}, and only {50-processed_accessions} left."
|
268 |
+
# yield (
|
269 |
+
# make_html_table(all_rows),
|
270 |
+
# gr.update(visible=True),
|
271 |
+
# gr.update(value=output_file_path, visible=True),
|
272 |
+
# gr.update(value=usage_text, visible=True),
|
273 |
+
# "β
Done",
|
274 |
+
# "\n".join(log_lines)
|
275 |
+
# )
|
276 |
+
def threaded_batch_runner(file=None, text="", email=""):
|
277 |
+
print("π§ EMAIL RECEIVED:", repr(email))
|
278 |
+
import tempfile
|
279 |
+
from mtdna_backend import (
|
280 |
+
extract_accessions_from_input,
|
281 |
+
summarize_results,
|
282 |
+
save_to_excel,
|
283 |
+
increment_usage,
|
284 |
+
)
|
285 |
+
import os
|
286 |
+
|
287 |
+
global_stop_flag.value = False # reset stop flag
|
288 |
+
|
289 |
+
tmp_dir = tempfile.mkdtemp()
|
290 |
+
output_file_path = os.path.join(tmp_dir, "batch_output_live.xlsx")
|
291 |
+
limited_acc = 50 + (10 if email.strip() else 0)
|
292 |
+
|
293 |
+
# Step 1: Parse input
|
294 |
+
accessions, error = extract_accessions_from_input(file, text)
|
295 |
+
print("π§ͺ Accessions received:", accessions)
|
296 |
+
if error:
|
297 |
+
yield (
|
298 |
+
"", # output_table
|
299 |
+
gr.update(visible=False), # results_group
|
300 |
+
gr.update(visible=False), # download_file
|
301 |
+
"", # usage_display
|
302 |
+
"β Error", # status
|
303 |
+
str(error) # progress_box
|
304 |
+
)
|
305 |
+
return
|
306 |
+
|
307 |
+
total = len(accessions)
|
308 |
+
if total > limited_acc:
|
309 |
+
accessions = accessions[:limited_acc]
|
310 |
+
warning = f"β οΈ Only processing first {limited_acc} accessions."
|
311 |
+
else:
|
312 |
+
warning = f"β
All {total} accessions will be processed."
|
313 |
+
|
314 |
+
all_rows = []
|
315 |
+
processed_accessions = 0 # β
track successful accessions
|
316 |
+
email_tracked = False
|
317 |
+
log_lines = []
|
318 |
+
|
319 |
+
# Step 2: Loop through accessions
|
320 |
+
for i, acc in enumerate(accessions):
|
321 |
+
if global_stop_flag.value:
|
322 |
+
log_lines.append(f"π Stopped at {acc} ({i+1}/{total})")
|
323 |
+
usage_text = ""
|
324 |
+
|
325 |
+
if email.strip() and not email_tracked:
|
326 |
+
print(f"π§ͺ increment_usage at STOP: {email=} {processed_accessions=}")
|
327 |
+
usage_count = increment_usage(email, processed_accessions)
|
328 |
+
email_tracked = True
|
329 |
+
usage_text = f"**{usage_count}** samples used by this email. Ten more samples are added first (you now have 60 limited accessions), then wait we will contact you via this email."
|
330 |
+
else:
|
331 |
+
usage_text = f"The limited accession is 50. The user has used {processed_accessions}, and only {50 - processed_accessions} left."
|
332 |
+
|
333 |
+
yield (
|
334 |
+
make_html_table(all_rows),
|
335 |
+
gr.update(visible=True),
|
336 |
+
gr.update(value=output_file_path, visible=True),
|
337 |
+
gr.update(value=usage_text, visible=True),
|
338 |
+
"π Stopped",
|
339 |
+
"\n".join(log_lines)
|
340 |
+
)
|
341 |
+
return
|
342 |
+
|
343 |
+
log_lines.append(f"[{i+1}/{total}] Processing {acc}")
|
344 |
+
yield (
|
345 |
+
make_html_table(all_rows),
|
346 |
+
gr.update(visible=True),
|
347 |
+
gr.update(visible=False),
|
348 |
+
"",
|
349 |
+
"β³ Processing...",
|
350 |
+
"\n".join(log_lines)
|
351 |
+
)
|
352 |
+
|
353 |
+
try:
|
354 |
+
print("π Processing accession:", acc)
|
355 |
+
rows = summarize_results(acc)
|
356 |
+
all_rows.extend(rows)
|
357 |
+
processed_accessions += 1 # β
only count success
|
358 |
+
save_to_excel(all_rows, "", "", output_file_path, is_resume=False)
|
359 |
+
log_lines.append(f"β
Processed {acc} ({i+1}/{total})")
|
360 |
+
except Exception as e:
|
361 |
+
log_lines.append(f"β Failed to process {acc}: {e}")
|
362 |
+
|
363 |
+
yield (
|
364 |
+
make_html_table(all_rows),
|
365 |
+
gr.update(visible=True),
|
366 |
+
gr.update(visible=False),
|
367 |
+
"",
|
368 |
+
"β³ Processing...",
|
369 |
+
"\n".join(log_lines)
|
370 |
+
)
|
371 |
+
|
372 |
+
# Step 3: Final usage update
|
373 |
+
usage_text = ""
|
374 |
+
if email.strip() and not email_tracked:
|
375 |
+
print(f"π§ͺ increment_usage at END: {email=} {processed_accessions=}")
|
376 |
+
usage_count = increment_usage(email, processed_accessions)
|
377 |
+
email_tracked = True
|
378 |
+
usage_text = f"**{usage_count}** samples used by this email. Ten more samples are added first (you now have 60 limited accessions), then wait we will contact you via this email."
|
379 |
+
elif not email.strip():
|
380 |
+
usage_text = f"The limited accession is 50. The user has used {processed_accessions}, and only {50 - processed_accessions} left."
|
381 |
+
|
382 |
+
yield (
|
383 |
+
make_html_table(all_rows),
|
384 |
+
gr.update(visible=True),
|
385 |
+
gr.update(value=output_file_path, visible=True),
|
386 |
+
gr.update(value=usage_text, visible=True),
|
387 |
+
"β
Done",
|
388 |
+
"\n".join(log_lines)
|
389 |
+
)
|
390 |
+
|
391 |
|
392 |
# def threaded_batch_runner(file=None, text="", email=""):
|
393 |
# global_stop_flag.value = False
|