atz21 commited on
Commit
610d28c
Β·
verified Β·
1 Parent(s): 1696b58

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +135 -63
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
- # State to store triplets
684
- triplets_state = gr.State([])
 
 
 
 
 
 
685
 
686
- # Dynamic triplet container
687
- triplets_container = gr.Column()
 
 
 
 
 
 
 
 
688
 
689
- # Add triplet button
690
  with gr.Row():
691
- add_triplet_btn = gr.Button("βž• Add New Triplet", variant="secondary")
692
- start_grading_btn = gr.Button("πŸš€ Start Mass Grading", variant="primary", visible=False)
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 create_triplet_row(triplet_index):
709
- """Create a new triplet upload row."""
710
- with gr.Row() as row:
711
- with gr.Column(scale=2):
712
- qp_file = gr.File(label=f"πŸ“„ QP {triplet_index + 1}", file_types=[".pdf"])
713
- with gr.Column(scale=2):
714
- ms_file = gr.File(label=f"πŸ“‹ MS {triplet_index + 1}", file_types=[".pdf"])
715
- with gr.Column(scale=2):
716
- ans_file = gr.File(label=f"πŸ“ AS {triplet_index + 1}", file_types=[".pdf"])
717
- with gr.Column(scale=1):
718
- imprint_check = gr.Checkbox(label="βœ… Imprint", value=False)
719
- with gr.Column(scale=1):
720
- remove_btn = gr.Button("πŸ—‘οΈ", variant="stop", size="sm")
721
-
722
- return row, qp_file, ms_file, ans_file, imprint_check, remove_btn
 
 
 
 
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, gr.update(visible=len(current_triplets) > 0)
 
 
 
 
 
 
734
 
735
  def update_status_table(triplets):
736
  """Update the status table with current triplets."""
737
- if not triplets:
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
- add_triplet_btn.click(
805
- fn=add_triplet,
 
 
 
 
806
  inputs=[triplets_state],
807
- outputs=[triplets_state, start_grading_btn]
 
 
 
 
 
 
808
  ).then(
809
  fn=update_status_table,
810
  inputs=[triplets_state],
811
  outputs=[status_table]
812
  )
813
 
814
- # File upload handlers to update triplets state
815
- def create_file_update_handler(triplet_idx, file_type):
816
- def update_triplet_file(file_obj, current_triplets):
817
- if triplet_idx < len(current_triplets):
818
- current_triplets[triplet_idx][file_type] = file_obj.name if file_obj else None
819
- return current_triplets
820
- return update_triplet_file
 
 
821
 
822
- def create_imprint_update_handler(triplet_idx):
823
- def update_triplet_imprint(imprint_val, current_triplets):
824
- if triplet_idx < len(current_triplets):
825
- current_triplets[triplet_idx]["imprint"] = imprint_val
826
- return current_triplets
827
- return update_triplet_imprint
828
 
829
- # Dynamic UI management
830
  @gr.render(inputs=[triplets_state])
831
- def render_triplets(triplets):
832
- if not triplets:
833
  return
834
 
835
- for i in range(len(triplets)):
836
- row, qp_file, ms_file, ans_file, imprint_check, remove_btn = create_triplet_row(i)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
837
 
838
- # Set up file upload handlers
 
 
 
 
 
 
 
 
 
 
 
 
839
  qp_file.change(
840
- fn=create_file_update_handler(i, "qp_file"),
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=create_file_update_handler(i, "ms_file"),
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=create_file_update_handler(i, "ans_file"),
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=create_imprint_update_handler(i),
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],