Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,6 +5,7 @@ import data_preprocess, model, pipeline
|
|
| 5 |
import os
|
| 6 |
import hashlib
|
| 7 |
import threading
|
|
|
|
| 8 |
# Gradio UI
|
| 9 |
#stop_flag = gr.State(value=False)
|
| 10 |
class StopFlag:
|
|
@@ -40,9 +41,6 @@ custom_css = """
|
|
| 40 |
}
|
| 41 |
"""
|
| 42 |
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
with gr.Blocks() as interface:
|
| 47 |
# with gr.Tab("CURIOUS ABOUT THIS PRODUCT?"):
|
| 48 |
# gr.HTML(value=pricing_html)
|
|
@@ -459,6 +457,28 @@ with gr.Blocks() as interface:
|
|
| 459 |
raise result
|
| 460 |
|
| 461 |
return False, None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 462 |
|
| 463 |
def threaded_batch_runner(file=None, text="", email=""):
|
| 464 |
print("π§ EMAIL RECEIVED:", repr(email))
|
|
@@ -608,186 +628,253 @@ with gr.Blocks() as interface:
|
|
| 608 |
# )
|
| 609 |
# return
|
| 610 |
|
| 611 |
-
|
| 612 |
-
|
| 613 |
-
for i, acc in enumerate(accessions):
|
| 614 |
-
if global_stop_flag.value:
|
| 615 |
-
log_lines.append(f"π Stopped at {acc} ({i+1}/{total})")
|
| 616 |
-
usage_text = ""
|
| 617 |
|
| 618 |
-
|
| 619 |
-
|
| 620 |
-
|
| 621 |
-
|
| 622 |
-
|
| 623 |
-
|
| 624 |
-
|
| 625 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 626 |
|
| 627 |
-
# yield (
|
| 628 |
-
# make_html_table(all_rows),
|
| 629 |
-
# gr.update(visible=True),
|
| 630 |
-
# #gr.update(value=output_file_path, visible=True),
|
| 631 |
-
# gr.update(value=output_file_path, visible=bool(output_file_path)),
|
| 632 |
-
# gr.update(value=usage_text, visible=True),
|
| 633 |
-
# "π Stopped",
|
| 634 |
-
# "\n".join(log_lines)
|
| 635 |
-
# )
|
| 636 |
yield (
|
| 637 |
make_html_table(all_rows),
|
| 638 |
-
gr.update(visible=True),
|
| 639 |
gr.update(value=output_file_path, visible=bool(output_file_path)), # download_file
|
| 640 |
-
gr.update(value=usage_text, visible=True),
|
| 641 |
-
"
|
| 642 |
"\n".join(log_lines),
|
| 643 |
-
gr.update(visible=False),
|
| 644 |
-
gr.update(visible=
|
| 645 |
-
gr.update(visible=True),
|
| 646 |
-
gr.update(visible=False),
|
| 647 |
-
gr.update(visible=False),
|
| 648 |
-
gr.update(value=
|
| 649 |
-
gr.update(visible=
|
| 650 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 651 |
|
| 652 |
-
|
| 653 |
|
| 654 |
-
|
| 655 |
-
|
| 656 |
-
|
| 657 |
-
|
| 658 |
-
|
| 659 |
-
|
| 660 |
-
|
| 661 |
-
|
| 662 |
-
|
| 663 |
-
|
| 664 |
-
|
| 665 |
-
|
| 666 |
-
|
| 667 |
-
|
| 668 |
-
|
| 669 |
-
|
| 670 |
-
|
| 671 |
-
|
| 672 |
-
|
| 673 |
-
|
| 674 |
-
|
| 675 |
-
|
| 676 |
-
|
| 677 |
-
|
| 678 |
-
|
| 679 |
|
| 680 |
|
| 681 |
-
|
| 682 |
-
|
| 683 |
-
|
| 684 |
-
|
| 685 |
-
|
| 686 |
-
|
| 687 |
-
|
| 688 |
-
|
| 689 |
-
|
| 690 |
-
|
| 691 |
-
|
| 692 |
-
|
| 693 |
|
| 694 |
-
|
| 695 |
-
|
| 696 |
-
|
| 697 |
-
|
| 698 |
-
|
| 699 |
-
|
| 700 |
|
| 701 |
-
|
| 702 |
-
|
| 703 |
-
|
| 704 |
-
|
| 705 |
-
|
| 706 |
-
|
| 707 |
-
|
| 708 |
-
|
| 709 |
-
|
| 710 |
-
|
| 711 |
-
|
| 712 |
-
|
| 713 |
-
|
| 714 |
-
|
| 715 |
-
|
| 716 |
-
|
| 717 |
|
| 718 |
-
|
| 719 |
-
|
| 720 |
-
|
| 721 |
-
|
| 722 |
-
|
| 723 |
-
|
| 724 |
-
|
| 725 |
|
| 726 |
-
|
| 727 |
-
|
| 728 |
-
|
| 729 |
-
|
| 730 |
-
|
| 731 |
-
|
| 732 |
-
|
| 733 |
-
|
| 734 |
-
|
| 735 |
-
|
| 736 |
-
|
| 737 |
-
|
| 738 |
-
|
| 739 |
-
|
| 740 |
-
|
| 741 |
-
|
| 742 |
-
|
| 743 |
-
|
| 744 |
-
|
| 745 |
-
|
| 746 |
-
|
| 747 |
-
|
| 748 |
-
|
| 749 |
-
|
| 750 |
-
|
| 751 |
-
|
| 752 |
|
| 753 |
-
|
| 754 |
|
| 755 |
-
|
| 756 |
-
|
| 757 |
-
|
| 758 |
-
|
| 759 |
-
|
| 760 |
-
|
| 761 |
-
|
| 762 |
-
|
| 763 |
-
|
| 764 |
-
|
| 765 |
-
|
| 766 |
|
| 767 |
-
|
| 768 |
-
|
| 769 |
-
|
| 770 |
-
|
| 771 |
-
|
| 772 |
-
|
| 773 |
-
|
| 774 |
-
|
| 775 |
-
|
| 776 |
-
|
| 777 |
-
|
| 778 |
-
|
| 779 |
-
|
| 780 |
-
|
| 781 |
-
|
| 782 |
-
|
| 783 |
-
|
| 784 |
-
|
| 785 |
-
|
| 786 |
-
|
| 787 |
-
|
| 788 |
-
|
| 789 |
-
|
| 790 |
-
|
|
|
|
| 791 |
|
| 792 |
# except Exception as e:
|
| 793 |
# log_lines.append(f"β Failed to process {acc}: {e}")
|
|
|
|
| 5 |
import os
|
| 6 |
import hashlib
|
| 7 |
import threading
|
| 8 |
+
|
| 9 |
# Gradio UI
|
| 10 |
#stop_flag = gr.State(value=False)
|
| 11 |
class StopFlag:
|
|
|
|
| 41 |
}
|
| 42 |
"""
|
| 43 |
|
|
|
|
|
|
|
|
|
|
| 44 |
with gr.Blocks() as interface:
|
| 45 |
# with gr.Tab("CURIOUS ABOUT THIS PRODUCT?"):
|
| 46 |
# gr.HTML(value=pricing_html)
|
|
|
|
| 457 |
raise result
|
| 458 |
|
| 459 |
return False, None
|
| 460 |
+
|
| 461 |
+
from multiprocessing import Pool, cpu_count
|
| 462 |
+
|
| 463 |
+
def process_accession_worker(args):
|
| 464 |
+
acc, stop_flag = args
|
| 465 |
+
if stop_flag.value: # Early exit if stop requested before starting
|
| 466 |
+
return None
|
| 467 |
+
try:
|
| 468 |
+
# You can still use run_with_timeout if you want per-accession timeout
|
| 469 |
+
success, rows = run_with_timeout(
|
| 470 |
+
summarize_results,
|
| 471 |
+
args=(acc,),
|
| 472 |
+
timeout=None, # or set max seconds per sample if needed
|
| 473 |
+
stop_value=stop_flag
|
| 474 |
+
)
|
| 475 |
+
if success:
|
| 476 |
+
return rows
|
| 477 |
+
else:
|
| 478 |
+
return None
|
| 479 |
+
except Exception as e:
|
| 480 |
+
print(f"β οΈ Error processing {acc}: {e}")
|
| 481 |
+
return None
|
| 482 |
|
| 483 |
def threaded_batch_runner(file=None, text="", email=""):
|
| 484 |
print("π§ EMAIL RECEIVED:", repr(email))
|
|
|
|
| 628 |
# )
|
| 629 |
# return
|
| 630 |
|
| 631 |
+
num_workers = min(cpu_count(), 4) # HF free tier usually has 2 CPUs
|
| 632 |
+
pool = Pool(processes=num_workers)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 633 |
|
| 634 |
+
try:
|
| 635 |
+
for result in pool.imap_unordered(
|
| 636 |
+
process_accession_worker, [(acc, global_stop_flag) for acc in accessions]
|
| 637 |
+
):
|
| 638 |
+
# if global_stop_flag.value:
|
| 639 |
+
# log_lines.append("π Stop requested β terminating workers...")
|
| 640 |
+
# pool.terminate()
|
| 641 |
+
# break
|
| 642 |
+
if global_stop_flag.value:
|
| 643 |
+
log_lines.append("π Stopped mid-batch")
|
| 644 |
+
usage_text = ""
|
| 645 |
+
if email.strip() and not email_tracked:
|
| 646 |
+
usage_count, max_allowed = increment_usage(email, processed_accessions)
|
| 647 |
+
email_tracked = True
|
| 648 |
+
usage_text = f"**{usage_count}**/{max_allowed} allowed samples used."
|
| 649 |
+
else:
|
| 650 |
+
usage_text = f"The limited accession is 30. The user has used {processed_accessions}, and only {30 - processed_accessions} left."
|
| 651 |
+
|
| 652 |
+
pool.terminate()
|
| 653 |
+
yield (
|
| 654 |
+
make_html_table(all_rows),
|
| 655 |
+
gr.update(visible=True),
|
| 656 |
+
gr.update(value=output_file_path, visible=bool(output_file_path)),
|
| 657 |
+
gr.update(value=usage_text, visible=True),
|
| 658 |
+
"π Stopped",
|
| 659 |
+
"\n".join(log_lines),
|
| 660 |
+
gr.update(visible=False), # run_button
|
| 661 |
+
gr.update(visible=False), # stop_button
|
| 662 |
+
gr.update(visible=True), # reset_button
|
| 663 |
+
gr.update(visible=False), # raw_text
|
| 664 |
+
gr.update(visible=False), # file_upload
|
| 665 |
+
gr.update(value=processed_info, visible=False),
|
| 666 |
+
gr.update(visible=True) # NPS modal
|
| 667 |
+
)
|
| 668 |
+
return
|
| 669 |
+
|
| 670 |
+
if result:
|
| 671 |
+
all_rows.extend(result)
|
| 672 |
+
processed_count += 1
|
| 673 |
+
log_lines.append(f"β
Processed {processed_count}/{total}")
|
| 674 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 675 |
yield (
|
| 676 |
make_html_table(all_rows),
|
| 677 |
+
gr.update(visible=True), # results_group
|
| 678 |
gr.update(value=output_file_path, visible=bool(output_file_path)), # download_file
|
| 679 |
+
gr.update(value=usage_text, visible=True), # usage_display
|
| 680 |
+
f"β³ Processed {processed_count}/{total}",
|
| 681 |
"\n".join(log_lines),
|
| 682 |
+
gr.update(visible=False), # run_button
|
| 683 |
+
gr.update(visible=True), # stop_button
|
| 684 |
+
gr.update(visible=True), # reset_button
|
| 685 |
+
gr.update(visible=False), # raw_text
|
| 686 |
+
gr.update(visible=False), # file_upload
|
| 687 |
+
gr.update(value="", visible=False), # processed_info
|
| 688 |
+
gr.update(visible=False) # NPS modal
|
| 689 |
)
|
| 690 |
+
|
| 691 |
+
finally:
|
| 692 |
+
pool.close()
|
| 693 |
+
pool.join()
|
| 694 |
+
|
| 695 |
+
if not global_stop_flag.value:
|
| 696 |
+
log_lines.append("β
All accessions processed")
|
| 697 |
+
|
| 698 |
+
# # Step 2: Loop through accessions
|
| 699 |
+
# for i, acc in enumerate(accessions):
|
| 700 |
+
# if global_stop_flag.value:
|
| 701 |
+
# log_lines.append(f"π Stopped at {acc} ({i+1}/{total})")
|
| 702 |
+
# usage_text = ""
|
| 703 |
+
|
| 704 |
+
# if email.strip() and not email_tracked:
|
| 705 |
+
# print(f"π§ͺ increment_usage at STOP: {email=} {processed_accessions=}")
|
| 706 |
+
# usage_count, max_allowed = increment_usage(email, processed_accessions)
|
| 707 |
+
# email_tracked = True
|
| 708 |
+
# usage_text = f"**{usage_count}**/{max_allowed} allowed samples used by this email."
|
| 709 |
+
# #Ten more samples are added first (you now have 60 limited accessions), then wait we will contact you via this email."
|
| 710 |
+
# else:
|
| 711 |
+
# usage_text = f"The limited accession is 30. The user has used {processed_accessions}, and only {30 - processed_accessions} left."
|
| 712 |
+
|
| 713 |
+
# # yield (
|
| 714 |
+
# # make_html_table(all_rows),
|
| 715 |
+
# # gr.update(visible=True),
|
| 716 |
+
# # #gr.update(value=output_file_path, visible=True),
|
| 717 |
+
# # gr.update(value=output_file_path, visible=bool(output_file_path)),
|
| 718 |
+
# # gr.update(value=usage_text, visible=True),
|
| 719 |
+
# # "π Stopped",
|
| 720 |
+
# # "\n".join(log_lines)
|
| 721 |
+
# # )
|
| 722 |
+
# yield (
|
| 723 |
+
# make_html_table(all_rows),
|
| 724 |
+
# gr.update(visible=True), # results_group
|
| 725 |
+
# gr.update(value=output_file_path, visible=bool(output_file_path)), # download_file
|
| 726 |
+
# gr.update(value=usage_text, visible=True), # usage_display
|
| 727 |
+
# "π Stopped", # "β
Done" or "π Stopped"
|
| 728 |
+
# "\n".join(log_lines),
|
| 729 |
+
# gr.update(visible=False), # run_button
|
| 730 |
+
# gr.update(visible=False), # stop_button
|
| 731 |
+
# gr.update(visible=True), # reset_button
|
| 732 |
+
# gr.update(visible=False), # raw_text
|
| 733 |
+
# gr.update(visible=False), # file_upload
|
| 734 |
+
# gr.update(value=processed_info, visible=False), # processed_info
|
| 735 |
+
# gr.update(visible=True) # NPS modal now visible
|
| 736 |
+
# )
|
| 737 |
|
| 738 |
+
# return
|
| 739 |
|
| 740 |
+
# log_lines.append(f"[{i+1}/{total}] Processing {acc}")
|
| 741 |
+
# # yield (
|
| 742 |
+
# # make_html_table(all_rows),
|
| 743 |
+
# # gr.update(visible=True),
|
| 744 |
+
# # gr.update(visible=False),
|
| 745 |
+
# # "",
|
| 746 |
+
# # "β³ Processing...",
|
| 747 |
+
# # "\n".join(log_lines)
|
| 748 |
+
# # )
|
| 749 |
+
# # Hide inputs, show processed_info at start
|
| 750 |
+
# yield (
|
| 751 |
+
# make_html_table(all_rows), # output_table
|
| 752 |
+
# gr.update(visible=True), # results_group
|
| 753 |
+
# gr.update(visible=False), # download_file
|
| 754 |
+
# "", # usage_display
|
| 755 |
+
# "β³ Processing...", # status
|
| 756 |
+
# "\n".join(log_lines), # progress_box
|
| 757 |
+
# gr.update(visible=False), # run_button
|
| 758 |
+
# gr.update(visible=True), # stop_button
|
| 759 |
+
# gr.update(visible=True), # reset_button
|
| 760 |
+
# gr.update(visible=False), # hide raw_text
|
| 761 |
+
# gr.update(visible=False), # hide file_upload
|
| 762 |
+
# gr.update(value=processed_info, visible=True), # processed_info
|
| 763 |
+
# gr.update(visible=False) # hide NPS modal at start
|
| 764 |
+
# )
|
| 765 |
|
| 766 |
|
| 767 |
+
# # try:
|
| 768 |
+
# # print("π Processing accession:", acc)
|
| 769 |
+
# # rows = summarize_results(acc)
|
| 770 |
+
# # all_rows.extend(rows)
|
| 771 |
+
# # processed_accessions += 1 # β
only count success
|
| 772 |
+
# # if email.strip():
|
| 773 |
+
# # save_to_excel(all_rows, "", "", output_file_path, is_resume=False)
|
| 774 |
+
# # log_lines.append(f"β
Processed {acc} ({i+1}/{total})")
|
| 775 |
+
# print("π Processing accession:", acc)
|
| 776 |
+
# # --- Before calling summarize_results ---
|
| 777 |
+
# samples_left = total - i # including current one
|
| 778 |
+
# estimated_seconds_left = samples_left * 100 # your observed average per sample
|
| 779 |
|
| 780 |
+
# log_lines.append(
|
| 781 |
+
# f"Running... usually ~100s per sample"
|
| 782 |
+
# )
|
| 783 |
+
# log_lines.append(
|
| 784 |
+
# f"β³ Estimated time left: ~{estimated_seconds_left} seconds ({samples_left} sample{'s' if samples_left > 1 else ''} remaining)"
|
| 785 |
+
# )
|
| 786 |
|
| 787 |
+
# # Yield update to UI before the heavy pipeline call
|
| 788 |
+
# yield (
|
| 789 |
+
# make_html_table(all_rows),
|
| 790 |
+
# gr.update(visible=True), # results_group
|
| 791 |
+
# gr.update(visible=False), # download_file
|
| 792 |
+
# "", # usage_display
|
| 793 |
+
# "β³ Processing...", # status
|
| 794 |
+
# "\n".join(log_lines), # progress_box
|
| 795 |
+
# gr.update(visible=False), # run_button
|
| 796 |
+
# gr.update(visible=True), # stop_button
|
| 797 |
+
# gr.update(visible=True), # reset_button
|
| 798 |
+
# gr.update(visible=False), # raw_text
|
| 799 |
+
# gr.update(visible=False), # file_upload
|
| 800 |
+
# gr.update(value=processed_info, visible=True), # processed_info
|
| 801 |
+
# gr.update(visible=False) # hide NPS modal
|
| 802 |
+
# )
|
| 803 |
|
| 804 |
+
# # Run summarize_results in a separate process with stop flag support
|
| 805 |
+
# success, rows = run_with_timeout(
|
| 806 |
+
# summarize_results,
|
| 807 |
+
# args=(acc,),
|
| 808 |
+
# timeout=None, # or set max seconds per sample if you want
|
| 809 |
+
# stop_value=global_stop_flag
|
| 810 |
+
# )
|
| 811 |
|
| 812 |
+
# # If stop was pressed during this accession
|
| 813 |
+
# if not success and global_stop_flag.value:
|
| 814 |
+
# log_lines.append(f"π Cancelled {acc} before completion")
|
| 815 |
+
# # yield (
|
| 816 |
+
# # make_html_table(all_rows),
|
| 817 |
+
# # gr.update(visible=True),
|
| 818 |
+
# # gr.update(visible=False),
|
| 819 |
+
# # "",
|
| 820 |
+
# # "π Stopped",
|
| 821 |
+
# # "\n".join(log_lines)
|
| 822 |
+
# # )
|
| 823 |
+
# yield (
|
| 824 |
+
# make_html_table(all_rows),
|
| 825 |
+
# gr.update(visible=True), # results_group
|
| 826 |
+
# gr.update(value=output_file_path, visible=bool(output_file_path)), # download_file
|
| 827 |
+
# gr.update(value=usage_text, visible=True), # usage_display
|
| 828 |
+
# "π Stopped", # "β
Done" or "π Stopped"
|
| 829 |
+
# "\n".join(log_lines),
|
| 830 |
+
# gr.update(visible=False), # run_button
|
| 831 |
+
# gr.update(visible=False), # stop_button
|
| 832 |
+
# gr.update(visible=True), # reset_button
|
| 833 |
+
# gr.update(visible=False), # raw_text
|
| 834 |
+
# gr.update(visible=False), # file_upload
|
| 835 |
+
# gr.update(value="", visible=False), # processed_info
|
| 836 |
+
# gr.update(visible=True) # NPS modal now visible
|
| 837 |
+
# )
|
| 838 |
|
| 839 |
+
# break # stop processing entirely
|
| 840 |
|
| 841 |
+
# # If it finished normally
|
| 842 |
+
# if success and rows:
|
| 843 |
+
# all_rows.extend(rows)
|
| 844 |
+
# processed_accessions += 1
|
| 845 |
+
# if email.strip():
|
| 846 |
+
# save_to_excel(all_rows, "", "", output_file_path, is_resume=False)
|
| 847 |
+
# log_lines.append(f"β
Processed {acc} ({i+1}/{total})")
|
| 848 |
+
# else:
|
| 849 |
+
# # If it failed due to timeout or other error
|
| 850 |
+
# if not global_stop_flag.value:
|
| 851 |
+
# log_lines.append(f"β οΈ Skipped {acc} due to timeout or error")
|
| 852 |
|
| 853 |
+
# # Always yield updated logs after each attempt
|
| 854 |
+
# # yield (
|
| 855 |
+
# # make_html_table(all_rows),
|
| 856 |
+
# # gr.update(visible=True),
|
| 857 |
+
# # gr.update(visible=False),
|
| 858 |
+
# # "",
|
| 859 |
+
# # "β³ Processing...",
|
| 860 |
+
# # "\n".join(log_lines)
|
| 861 |
+
# # )
|
| 862 |
+
# yield (
|
| 863 |
+
# make_html_table(all_rows), # output_table
|
| 864 |
+
# gr.update(visible=True), # results_group
|
| 865 |
+
# gr.update(visible=False), # download_file
|
| 866 |
+
# "", # usage_display
|
| 867 |
+
# "β³ Processing...", # status
|
| 868 |
+
# "\n".join(log_lines), # progress_box
|
| 869 |
+
# gr.update(visible=True), # run_button
|
| 870 |
+
# gr.update(visible=True), # stop_button
|
| 871 |
+
# gr.update(visible=True), # reset_button
|
| 872 |
+
# gr.update(visible=False), # hide raw_text
|
| 873 |
+
# gr.update(visible=False), # hide file_upload
|
| 874 |
+
# gr.update(value=processed_info, visible=True), # processed_info
|
| 875 |
+
# gr.update(visible=False) # hide NPS modal at start
|
| 876 |
+
# )
|
| 877 |
+
|
| 878 |
|
| 879 |
# except Exception as e:
|
| 880 |
# log_lines.append(f"β Failed to process {acc}: {e}")
|