ShebMichel commited on
Commit
369498a
·
verified ·
1 Parent(s): 562eb9e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +151 -8
app.py CHANGED
@@ -498,6 +498,118 @@ geomodel_llm = keras_nlp.models.CausalLM.from_preset("hf://ShebMichel/geobot_tea
498
  # # Launch the interface
499
  # iface.launch()
500
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
501
  import json
502
  import gradio as gr
503
 
@@ -515,17 +627,17 @@ def process_json_file(file):
515
  output_text = generate_output_text(loaded_data)
516
 
517
  print("\nProcessing complete!")
518
- # Enable radio buttons upon successful loading
519
- return output_text, gr.update(interactive=True), gr.update(interactive=True), gr.update(interactive=True), gr.update(interactive=True)
520
 
521
  except json.JSONDecodeError as e:
522
  error_msg = f"Error decoding JSON: {str(e)}"
523
  print(error_msg)
524
- return error_msg, gr.update(interactive=False), gr.update(interactive=False), gr.update(interactive=False), gr.update(interactive=False)
525
  except Exception as e:
526
  error_msg = f"Error processing file: {str(e)}"
527
  print(error_msg)
528
- return error_msg, gr.update(interactive=False), gr.update(interactive=False), gr.update(interactive=False), gr.update(interactive=False)
529
 
530
  def generate_output_text(data, section="ALL"):
531
  output_text = ""
@@ -569,6 +681,14 @@ def update_textbox_based_on_radio(selection):
569
  # Generate output based on the selected section
570
  return generate_output_text(loaded_data, section=selection)
571
 
 
 
 
 
 
 
 
 
572
  # Create Gradio interface
573
  with gr.Blocks() as iface:
574
  gr.Markdown("# Exam Question Viewer")
@@ -592,7 +712,7 @@ with gr.Blocks() as iface:
592
  label="Select Section to Display",
593
  interactive=False
594
  )
595
-
596
  # Update output_text when radio selection changes
597
  radio_section.change(
598
  fn=update_textbox_based_on_radio,
@@ -600,13 +720,36 @@ with gr.Blocks() as iface:
600
  outputs=[output_text]
601
  )
602
 
603
- # Process file input and enable radio buttons on successful load
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
604
  file_input.upload(
605
  fn=process_json_file,
606
  inputs=[file_input],
607
- outputs=[output_text, radio_section, radio_section, radio_section, radio_section]
608
  )
609
 
610
  # Launch the interface
611
  iface.launch()
612
-
 
498
  # # Launch the interface
499
  # iface.launch()
500
 
501
+ # import json
502
+ # import gradio as gr
503
+
504
+ # # Global variable to store loaded JSON data
505
+ # loaded_data = {}
506
+
507
+ # def process_json_file(file):
508
+ # global loaded_data
509
+ # try:
510
+ # # Read the JSON file
511
+ # with open(file.name, 'r') as f:
512
+ # loaded_data = json.load(f)
513
+
514
+ # # Generate the full output text
515
+ # output_text = generate_output_text(loaded_data)
516
+
517
+ # print("\nProcessing complete!")
518
+ # # Enable radio buttons upon successful loading
519
+ # return output_text, gr.update(interactive=True), gr.update(interactive=True), gr.update(interactive=True), gr.update(interactive=True)
520
+
521
+ # except json.JSONDecodeError as e:
522
+ # error_msg = f"Error decoding JSON: {str(e)}"
523
+ # print(error_msg)
524
+ # return error_msg, gr.update(interactive=False), gr.update(interactive=False), gr.update(interactive=False), gr.update(interactive=False)
525
+ # except Exception as e:
526
+ # error_msg = f"Error processing file: {str(e)}"
527
+ # print(error_msg)
528
+ # return error_msg, gr.update(interactive=False), gr.update(interactive=False), gr.update(interactive=False), gr.update(interactive=False)
529
+
530
+ # def generate_output_text(data, section="ALL"):
531
+ # output_text = ""
532
+
533
+ # if section == "ALL" or section == "header":
534
+ # if 'header' in data:
535
+ # output_text += "=== EXAM DETAILS ===\n"
536
+ # for key, value in data['header'].items():
537
+ # output_text += f"{key.replace('_', ' ').title()}: {value}\n"
538
+ # output_text += "\n"
539
+
540
+ # if section == "ALL" or section == "MULTIPLE CHOICE QUESTIONS":
541
+ # if 'multiple_choice_questions' in data:
542
+ # output_text += "=== MULTIPLE CHOICE QUESTIONS ===\n"
543
+ # for q_num, q_data in data['multiple_choice_questions'].items():
544
+ # output_text += f"\nQuestion {q_num.replace('question', '')}:\n"
545
+ # output_text += f"{q_data['question']}\n"
546
+ # for opt_key, opt_val in q_data['options'].items():
547
+ # output_text += f"{opt_key}) {opt_val}\n"
548
+ # output_text += f"Answer: {q_data['answer']}\n"
549
+
550
+ # if section == "ALL" or section == "SHORT ANSWER QUESTIONS":
551
+ # if 'short_answer_questions' in data:
552
+ # output_text += "\n=== SHORT ANSWER QUESTIONS ===\n"
553
+ # for q_num, q_data in data['short_answer_questions'].items():
554
+ # output_text += f"\nQuestion {q_num.replace('question', '')}:\n"
555
+ # output_text += f"{q_data['question']}\n"
556
+ # output_text += f"Answer: {q_data['answer']}\n"
557
+
558
+ # if section == "ALL" or section == "LONG ANSWER QUESTIONS":
559
+ # if 'long_answer_questions' in data:
560
+ # output_text += "\n=== LONG ANSWER QUESTIONS ===\n"
561
+ # for q_num, q_data in data['long_answer_questions'].items():
562
+ # output_text += f"\nQuestion {q_num.replace('question', '')}:\n"
563
+ # output_text += f"{q_data['question']}\n"
564
+ # output_text += f"Answer: {q_data['answer']}\n"
565
+
566
+ # return output_text
567
+
568
+ # def update_textbox_based_on_radio(selection):
569
+ # # Generate output based on the selected section
570
+ # return generate_output_text(loaded_data, section=selection)
571
+
572
+ # # Create Gradio interface
573
+ # with gr.Blocks() as iface:
574
+ # gr.Markdown("# Exam Question Viewer")
575
+
576
+ # with gr.Row():
577
+ # file_input = gr.File(
578
+ # label="Upload JSON Exam File",
579
+ # file_types=[".json"]
580
+ # )
581
+
582
+ # with gr.Row():
583
+ # output_text = gr.Textbox(
584
+ # label="Processed Questions",
585
+ # lines=20,
586
+ # max_lines=30
587
+ # )
588
+
589
+ # with gr.Row():
590
+ # radio_section = gr.Radio(
591
+ # ["MULTIPLE CHOICE QUESTIONS", "SHORT ANSWER QUESTIONS", "LONG ANSWER QUESTIONS", "ALL"],
592
+ # label="Select Section to Display",
593
+ # interactive=False
594
+ # )
595
+
596
+ # # Update output_text when radio selection changes
597
+ # radio_section.change(
598
+ # fn=update_textbox_based_on_radio,
599
+ # inputs=[radio_section],
600
+ # outputs=[output_text]
601
+ # )
602
+
603
+ # # Process file input and enable radio buttons on successful load
604
+ # file_input.upload(
605
+ # fn=process_json_file,
606
+ # inputs=[file_input],
607
+ # outputs=[output_text, radio_section, radio_section, radio_section, radio_section]
608
+ # )
609
+
610
+ # # Launch the interface
611
+ # iface.launch()
612
+
613
  import json
614
  import gradio as gr
615
 
 
627
  output_text = generate_output_text(loaded_data)
628
 
629
  print("\nProcessing complete!")
630
+ # Enable radio buttons and buttons upon successful loading
631
+ return output_text, gr.update(interactive=True), gr.update(interactive=True), gr.update(interactive=True), gr.update(interactive=True), gr.update(interactive=True), gr.update(interactive=True)
632
 
633
  except json.JSONDecodeError as e:
634
  error_msg = f"Error decoding JSON: {str(e)}"
635
  print(error_msg)
636
+ return error_msg, gr.update(interactive=False), gr.update(interactive=False), gr.update(interactive=False), gr.update(interactive=False), gr.update(interactive=False), gr.update(interactive=False)
637
  except Exception as e:
638
  error_msg = f"Error processing file: {str(e)}"
639
  print(error_msg)
640
+ return error_msg, gr.update(interactive=False), gr.update(interactive=False), gr.update(interactive=False), gr.update(interactive=False), gr.update(interactive=False), gr.update(interactive=False)
641
 
642
  def generate_output_text(data, section="ALL"):
643
  output_text = ""
 
681
  # Generate output based on the selected section
682
  return generate_output_text(loaded_data, section=selection)
683
 
684
+ def submit_question(question_content):
685
+ # Placeholder model processing
686
+ model_output = f"Model processed question: {question_content[:100]}..." # Only showing part for brevity
687
+ return model_output
688
+
689
+ def clear_output():
690
+ return ""
691
+
692
  # Create Gradio interface
693
  with gr.Blocks() as iface:
694
  gr.Markdown("# Exam Question Viewer")
 
712
  label="Select Section to Display",
713
  interactive=False
714
  )
715
+
716
  # Update output_text when radio selection changes
717
  radio_section.change(
718
  fn=update_textbox_based_on_radio,
 
720
  outputs=[output_text]
721
  )
722
 
723
+ # Submit button - Passes selected question to the model
724
+ with gr.Row():
725
+ submit_button = gr.Button("Submit", interactive=False)
726
+ clear_button = gr.Button("Clear", interactive=False)
727
+ output_button = gr.Button("Output", interactive=False)
728
+
729
+ # Define button actions
730
+ submit_button.click(
731
+ fn=submit_question,
732
+ inputs=[output_text],
733
+ outputs=[output_text]
734
+ )
735
+
736
+ clear_button.click(
737
+ fn=clear_output,
738
+ outputs=[output_text]
739
+ )
740
+
741
+ output_button.click(
742
+ fn=update_textbox_based_on_radio,
743
+ inputs=[radio_section],
744
+ outputs=[output_text]
745
+ )
746
+
747
+ # Process file input and enable radio buttons and buttons on successful load
748
  file_input.upload(
749
  fn=process_json_file,
750
  inputs=[file_input],
751
+ outputs=[output_text, radio_section, radio_section, radio_section, radio_section, submit_button, clear_button, output_button]
752
  )
753
 
754
  # Launch the interface
755
  iface.launch()