Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -680,16 +680,30 @@ with gr.Blocks(title="π AI Mass Grading System", theme=gr.themes.Soft()) as d
|
|
| 680 |
gr.Markdown("# π AI Mass Grading System")
|
| 681 |
gr.Markdown("Upload multiple sets of Question Papers, Markschemes, and Answer Sheets for batch processing.")
|
| 682 |
|
| 683 |
-
#
|
| 684 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 685 |
|
| 686 |
-
#
|
| 687 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 688 |
|
| 689 |
-
#
|
| 690 |
with gr.Row():
|
| 691 |
-
add_triplet_btn = gr.Button("β Add
|
| 692 |
-
start_grading_btn = gr.Button("π
|
| 693 |
|
| 694 |
# Status table
|
| 695 |
status_table = gr.Dataframe(
|
|
@@ -705,21 +719,25 @@ with gr.Blocks(title="π AI Mass Grading System", theme=gr.themes.Soft()) as d
|
|
| 705 |
gr.Markdown("## π₯ Download Results")
|
| 706 |
results_files = gr.Column()
|
| 707 |
|
| 708 |
-
def
|
| 709 |
-
"""
|
| 710 |
-
|
| 711 |
-
|
| 712 |
-
|
| 713 |
-
|
| 714 |
-
|
| 715 |
-
|
| 716 |
-
|
| 717 |
-
|
| 718 |
-
|
| 719 |
-
|
| 720 |
-
|
| 721 |
-
|
| 722 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 723 |
|
| 724 |
def add_triplet(current_triplets):
|
| 725 |
"""Add a new triplet to the list."""
|
|
@@ -730,11 +748,17 @@ with gr.Blocks(title="π AI Mass Grading System", theme=gr.themes.Soft()) as d
|
|
| 730 |
"imprint": False
|
| 731 |
}
|
| 732 |
current_triplets.append(new_triplet)
|
| 733 |
-
return current_triplets
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 734 |
|
| 735 |
def update_status_table(triplets):
|
| 736 |
"""Update the status table with current triplets."""
|
| 737 |
-
if
|
| 738 |
return gr.update(visible=False, value=[])
|
| 739 |
|
| 740 |
table_data = []
|
|
@@ -786,58 +810,84 @@ with gr.Blocks(title="π AI Mass Grading System", theme=gr.themes.Soft()) as d
|
|
| 786 |
# Update status
|
| 787 |
table_data[i][4] = result["status"]
|
| 788 |
|
| 789 |
-
# Create download links
|
| 790 |
-
download_components = []
|
| 791 |
-
for i, result in enumerate(results):
|
| 792 |
-
if result["grading_pdf"]:
|
| 793 |
-
download_components.append(
|
| 794 |
-
gr.File(value=result["grading_pdf"], label=f"π Set {i+1} - Grading PDF", visible=True)
|
| 795 |
-
)
|
| 796 |
-
if result["imprinted_pdf"]:
|
| 797 |
-
download_components.append(
|
| 798 |
-
gr.File(value=result["imprinted_pdf"], label=f"βοΈ Set {i+1} - Imprinted PDF", visible=True)
|
| 799 |
-
)
|
| 800 |
-
|
| 801 |
return gr.update(value=table_data), gr.update(visible=True)
|
| 802 |
|
| 803 |
-
# Event handlers
|
| 804 |
-
|
| 805 |
-
fn=
|
|
|
|
|
|
|
|
|
|
|
|
|
| 806 |
inputs=[triplets_state],
|
| 807 |
-
outputs=[
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 808 |
).then(
|
| 809 |
fn=update_status_table,
|
| 810 |
inputs=[triplets_state],
|
| 811 |
outputs=[status_table]
|
| 812 |
)
|
| 813 |
|
| 814 |
-
|
| 815 |
-
|
| 816 |
-
|
| 817 |
-
|
| 818 |
-
|
| 819 |
-
|
| 820 |
-
|
|
|
|
|
|
|
| 821 |
|
| 822 |
-
|
| 823 |
-
|
| 824 |
-
|
| 825 |
-
|
| 826 |
-
|
| 827 |
-
return update_triplet_imprint
|
| 828 |
|
| 829 |
-
# Dynamic UI management
|
| 830 |
@gr.render(inputs=[triplets_state])
|
| 831 |
-
def
|
| 832 |
-
if
|
| 833 |
return
|
| 834 |
|
| 835 |
-
for i in range(len(triplets)):
|
| 836 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 837 |
|
| 838 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 839 |
qp_file.change(
|
| 840 |
-
fn=
|
| 841 |
inputs=[qp_file, triplets_state],
|
| 842 |
outputs=[triplets_state]
|
| 843 |
).then(
|
|
@@ -847,7 +897,7 @@ with gr.Blocks(title="π AI Mass Grading System", theme=gr.themes.Soft()) as d
|
|
| 847 |
)
|
| 848 |
|
| 849 |
ms_file.change(
|
| 850 |
-
fn=
|
| 851 |
inputs=[ms_file, triplets_state],
|
| 852 |
outputs=[triplets_state]
|
| 853 |
).then(
|
|
@@ -857,7 +907,7 @@ with gr.Blocks(title="π AI Mass Grading System", theme=gr.themes.Soft()) as d
|
|
| 857 |
)
|
| 858 |
|
| 859 |
ans_file.change(
|
| 860 |
-
fn=
|
| 861 |
inputs=[ans_file, triplets_state],
|
| 862 |
outputs=[triplets_state]
|
| 863 |
).then(
|
|
@@ -867,11 +917,33 @@ with gr.Blocks(title="π AI Mass Grading System", theme=gr.themes.Soft()) as d
|
|
| 867 |
)
|
| 868 |
|
| 869 |
imprint_check.change(
|
| 870 |
-
fn=
|
| 871 |
inputs=[imprint_check, triplets_state],
|
| 872 |
outputs=[triplets_state]
|
| 873 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 874 |
|
|
|
|
| 875 |
start_grading_btn.click(
|
| 876 |
fn=start_mass_grading,
|
| 877 |
inputs=[triplets_state],
|
|
|
|
| 680 |
gr.Markdown("# π AI Mass Grading System")
|
| 681 |
gr.Markdown("Upload multiple sets of Question Papers, Markschemes, and Answer Sheets for batch processing.")
|
| 682 |
|
| 683 |
+
# Initialize with one triplet
|
| 684 |
+
initial_triplet = {
|
| 685 |
+
"qp_file": None,
|
| 686 |
+
"ms_file": None,
|
| 687 |
+
"ans_file": None,
|
| 688 |
+
"imprint": False
|
| 689 |
+
}
|
| 690 |
+
triplets_state = gr.State([initial_triplet])
|
| 691 |
|
| 692 |
+
# Initial triplet row (always visible)
|
| 693 |
+
with gr.Row():
|
| 694 |
+
qp_file_1 = gr.File(label="π Upload Question Paper (PDF)", file_types=[".pdf"])
|
| 695 |
+
ms_file_1 = gr.File(label="π Upload Markscheme (PDF)", file_types=[".pdf"])
|
| 696 |
+
ans_file_1 = gr.File(label="π Upload Student Answer Sheet (PDF)", file_types=[".pdf"])
|
| 697 |
+
|
| 698 |
+
imprint_check_1 = gr.Checkbox(label="β Imprint Marks on Student Answer Sheet", value=False)
|
| 699 |
+
|
| 700 |
+
# Dynamic container for additional triplets
|
| 701 |
+
additional_triplets = gr.Column()
|
| 702 |
|
| 703 |
+
# Buttons row
|
| 704 |
with gr.Row():
|
| 705 |
+
add_triplet_btn = gr.Button("β Add Another Set", variant="secondary")
|
| 706 |
+
start_grading_btn = gr.Button("π Run Pipeline", variant="primary")
|
| 707 |
|
| 708 |
# Status table
|
| 709 |
status_table = gr.Dataframe(
|
|
|
|
| 719 |
gr.Markdown("## π₯ Download Results")
|
| 720 |
results_files = gr.Column()
|
| 721 |
|
| 722 |
+
def update_first_triplet_qp(file_obj, current_triplets):
|
| 723 |
+
"""Update QP file for first triplet."""
|
| 724 |
+
current_triplets[0]["qp_file"] = file_obj.name if file_obj else None
|
| 725 |
+
return current_triplets
|
| 726 |
+
|
| 727 |
+
def update_first_triplet_ms(file_obj, current_triplets):
|
| 728 |
+
"""Update MS file for first triplet."""
|
| 729 |
+
current_triplets[0]["ms_file"] = file_obj.name if file_obj else None
|
| 730 |
+
return current_triplets
|
| 731 |
+
|
| 732 |
+
def update_first_triplet_ans(file_obj, current_triplets):
|
| 733 |
+
"""Update AS file for first triplet."""
|
| 734 |
+
current_triplets[0]["ans_file"] = file_obj.name if file_obj else None
|
| 735 |
+
return current_triplets
|
| 736 |
+
|
| 737 |
+
def update_first_triplet_imprint(imprint_val, current_triplets):
|
| 738 |
+
"""Update imprint setting for first triplet."""
|
| 739 |
+
current_triplets[0]["imprint"] = imprint_val
|
| 740 |
+
return current_triplets
|
| 741 |
|
| 742 |
def add_triplet(current_triplets):
|
| 743 |
"""Add a new triplet to the list."""
|
|
|
|
| 748 |
"imprint": False
|
| 749 |
}
|
| 750 |
current_triplets.append(new_triplet)
|
| 751 |
+
return current_triplets
|
| 752 |
+
|
| 753 |
+
def remove_triplet(triplet_idx, current_triplets):
|
| 754 |
+
"""Remove a triplet from the list."""
|
| 755 |
+
if triplet_idx < len(current_triplets) and len(current_triplets) > 1:
|
| 756 |
+
current_triplets.pop(triplet_idx)
|
| 757 |
+
return current_triplets
|
| 758 |
|
| 759 |
def update_status_table(triplets):
|
| 760 |
"""Update the status table with current triplets."""
|
| 761 |
+
if len(triplets) <= 1:
|
| 762 |
return gr.update(visible=False, value=[])
|
| 763 |
|
| 764 |
table_data = []
|
|
|
|
| 810 |
# Update status
|
| 811 |
table_data[i][4] = result["status"]
|
| 812 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 813 |
return gr.update(value=table_data), gr.update(visible=True)
|
| 814 |
|
| 815 |
+
# Event handlers for first triplet
|
| 816 |
+
qp_file_1.change(
|
| 817 |
+
fn=update_first_triplet_qp,
|
| 818 |
+
inputs=[qp_file_1, triplets_state],
|
| 819 |
+
outputs=[triplets_state]
|
| 820 |
+
).then(
|
| 821 |
+
fn=update_status_table,
|
| 822 |
inputs=[triplets_state],
|
| 823 |
+
outputs=[status_table]
|
| 824 |
+
)
|
| 825 |
+
|
| 826 |
+
ms_file_1.change(
|
| 827 |
+
fn=update_first_triplet_ms,
|
| 828 |
+
inputs=[ms_file_1, triplets_state],
|
| 829 |
+
outputs=[triplets_state]
|
| 830 |
).then(
|
| 831 |
fn=update_status_table,
|
| 832 |
inputs=[triplets_state],
|
| 833 |
outputs=[status_table]
|
| 834 |
)
|
| 835 |
|
| 836 |
+
ans_file_1.change(
|
| 837 |
+
fn=update_first_triplet_ans,
|
| 838 |
+
inputs=[ans_file_1, triplets_state],
|
| 839 |
+
outputs=[triplets_state]
|
| 840 |
+
).then(
|
| 841 |
+
fn=update_status_table,
|
| 842 |
+
inputs=[triplets_state],
|
| 843 |
+
outputs=[status_table]
|
| 844 |
+
)
|
| 845 |
|
| 846 |
+
imprint_check_1.change(
|
| 847 |
+
fn=update_first_triplet_imprint,
|
| 848 |
+
inputs=[imprint_check_1, triplets_state],
|
| 849 |
+
outputs=[triplets_state]
|
| 850 |
+
)
|
|
|
|
| 851 |
|
| 852 |
+
# Dynamic UI management for additional triplets
|
| 853 |
@gr.render(inputs=[triplets_state])
|
| 854 |
+
def render_additional_triplets(triplets):
|
| 855 |
+
if len(triplets) <= 1:
|
| 856 |
return
|
| 857 |
|
| 858 |
+
for i in range(1, len(triplets)): # Start from index 1 (skip first triplet)
|
| 859 |
+
with gr.Row():
|
| 860 |
+
qp_file = gr.File(label=f"π QP Set {i + 1}", file_types=[".pdf"])
|
| 861 |
+
ms_file = gr.File(label=f"π MS Set {i + 1}", file_types=[".pdf"])
|
| 862 |
+
ans_file = gr.File(label=f"π AS Set {i + 1}", file_types=[".pdf"])
|
| 863 |
+
|
| 864 |
+
with gr.Row():
|
| 865 |
+
imprint_check = gr.Checkbox(label=f"β Imprint Set {i + 1}", value=False)
|
| 866 |
+
remove_btn = gr.Button("ποΈ Remove", variant="stop", size="sm")
|
| 867 |
+
|
| 868 |
+
# File update handlers
|
| 869 |
+
def create_file_handler(idx, file_type):
|
| 870 |
+
def handler(file_obj, current_triplets):
|
| 871 |
+
if idx < len(current_triplets):
|
| 872 |
+
current_triplets[idx][file_type] = file_obj.name if file_obj else None
|
| 873 |
+
return current_triplets
|
| 874 |
+
return handler
|
| 875 |
|
| 876 |
+
def create_imprint_handler(idx):
|
| 877 |
+
def handler(imprint_val, current_triplets):
|
| 878 |
+
if idx < len(current_triplets):
|
| 879 |
+
current_triplets[idx]["imprint"] = imprint_val
|
| 880 |
+
return current_triplets
|
| 881 |
+
return handler
|
| 882 |
+
|
| 883 |
+
def create_remove_handler(idx):
|
| 884 |
+
def handler(current_triplets):
|
| 885 |
+
return remove_triplet(idx, current_triplets)
|
| 886 |
+
return handler
|
| 887 |
+
|
| 888 |
+
# Set up event handlers
|
| 889 |
qp_file.change(
|
| 890 |
+
fn=create_file_handler(i, "qp_file"),
|
| 891 |
inputs=[qp_file, triplets_state],
|
| 892 |
outputs=[triplets_state]
|
| 893 |
).then(
|
|
|
|
| 897 |
)
|
| 898 |
|
| 899 |
ms_file.change(
|
| 900 |
+
fn=create_file_handler(i, "ms_file"),
|
| 901 |
inputs=[ms_file, triplets_state],
|
| 902 |
outputs=[triplets_state]
|
| 903 |
).then(
|
|
|
|
| 907 |
)
|
| 908 |
|
| 909 |
ans_file.change(
|
| 910 |
+
fn=create_file_handler(i, "ans_file"),
|
| 911 |
inputs=[ans_file, triplets_state],
|
| 912 |
outputs=[triplets_state]
|
| 913 |
).then(
|
|
|
|
| 917 |
)
|
| 918 |
|
| 919 |
imprint_check.change(
|
| 920 |
+
fn=create_imprint_handler(i),
|
| 921 |
inputs=[imprint_check, triplets_state],
|
| 922 |
outputs=[triplets_state]
|
| 923 |
)
|
| 924 |
+
|
| 925 |
+
remove_btn.click(
|
| 926 |
+
fn=create_remove_handler(i),
|
| 927 |
+
inputs=[triplets_state],
|
| 928 |
+
outputs=[triplets_state]
|
| 929 |
+
).then(
|
| 930 |
+
fn=update_status_table,
|
| 931 |
+
inputs=[triplets_state],
|
| 932 |
+
outputs=[status_table]
|
| 933 |
+
)
|
| 934 |
+
|
| 935 |
+
# Add triplet button handler
|
| 936 |
+
add_triplet_btn.click(
|
| 937 |
+
fn=add_triplet,
|
| 938 |
+
inputs=[triplets_state],
|
| 939 |
+
outputs=[triplets_state]
|
| 940 |
+
).then(
|
| 941 |
+
fn=update_status_table,
|
| 942 |
+
inputs=[triplets_state],
|
| 943 |
+
outputs=[status_table]
|
| 944 |
+
)
|
| 945 |
|
| 946 |
+
# Start grading button handler
|
| 947 |
start_grading_btn.click(
|
| 948 |
fn=start_mass_grading,
|
| 949 |
inputs=[triplets_state],
|