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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +138 -25
app.py CHANGED
@@ -385,6 +385,119 @@ geomodel_llm = keras_nlp.models.CausalLM.from_preset("hf://ShebMichel/geobot_tea
385
  # iface.launch()
386
 
387
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
388
  import json
389
  import gradio as gr
390
 
@@ -403,16 +516,16 @@ def process_json_file(file):
403
 
404
  print("\nProcessing complete!")
405
  # Enable radio buttons upon successful loading
406
- return output_text, gr.update(interactive=True), gr.update(interactive=True), gr.update(interactive=True)
407
 
408
  except json.JSONDecodeError as e:
409
  error_msg = f"Error decoding JSON: {str(e)}"
410
  print(error_msg)
411
- return error_msg, gr.update(interactive=False), gr.update(interactive=False), gr.update(interactive=False)
412
  except Exception as e:
413
  error_msg = f"Error processing file: {str(e)}"
414
  print(error_msg)
415
- return error_msg, gr.update(interactive=False), gr.update(interactive=False), gr.update(interactive=False)
416
 
417
  def generate_output_text(data, section="ALL"):
418
  output_text = ""
@@ -424,7 +537,7 @@ def generate_output_text(data, section="ALL"):
424
  output_text += f"{key.replace('_', ' ').title()}: {value}\n"
425
  output_text += "\n"
426
 
427
- if section == "ALL" or section == "QCM":
428
  if 'multiple_choice_questions' in data:
429
  output_text += "=== MULTIPLE CHOICE QUESTIONS ===\n"
430
  for q_num, q_data in data['multiple_choice_questions'].items():
@@ -434,22 +547,21 @@ def generate_output_text(data, section="ALL"):
434
  output_text += f"{opt_key}) {opt_val}\n"
435
  output_text += f"Answer: {q_data['answer']}\n"
436
 
437
- if section == "ALL" or section == "DTM":
438
- if 'short_answer_questions' in data or 'long_answer_questions' in data:
439
- output_text += "\n=== DETAILED QUESTIONS ===\n"
440
- if 'short_answer_questions' in data:
441
- output_text += "\n=== SHORT ANSWER QUESTIONS ===\n"
442
- for q_num, q_data in data['short_answer_questions'].items():
443
- output_text += f"\nQuestion {q_num.replace('question', '')}:\n"
444
- output_text += f"{q_data['question']}\n"
445
- output_text += f"Answer: {q_data['answer']}\n"
446
-
447
- if 'long_answer_questions' in data:
448
- output_text += "\n=== LONG ANSWER QUESTIONS ===\n"
449
- for q_num, q_data in data['long_answer_questions'].items():
450
- output_text += f"\nQuestion {q_num.replace('question', '')}:\n"
451
- output_text += f"{q_data['question']}\n"
452
- output_text += f"Answer: {q_data['answer']}\n"
453
 
454
  return output_text
455
 
@@ -475,16 +587,16 @@ with gr.Blocks() as iface:
475
  )
476
 
477
  with gr.Row():
478
- radio_qcm = gr.Radio(
479
- ["QCM", "DTM", "ALL"],
480
  label="Select Section to Display",
481
  interactive=False
482
  )
483
 
484
  # Update output_text when radio selection changes
485
- radio_qcm.change(
486
  fn=update_textbox_based_on_radio,
487
- inputs=[radio_qcm],
488
  outputs=[output_text]
489
  )
490
 
@@ -492,8 +604,9 @@ with gr.Blocks() as iface:
492
  file_input.upload(
493
  fn=process_json_file,
494
  inputs=[file_input],
495
- outputs=[output_text, radio_qcm, radio_qcm, radio_qcm]
496
  )
497
 
498
  # Launch the interface
499
  iface.launch()
 
 
385
  # iface.launch()
386
 
387
 
388
+ # import json
389
+ # import gradio as gr
390
+
391
+ # # Global variable to store loaded JSON data
392
+ # loaded_data = {}
393
+
394
+ # def process_json_file(file):
395
+ # global loaded_data
396
+ # try:
397
+ # # Read the JSON file
398
+ # with open(file.name, 'r') as f:
399
+ # loaded_data = json.load(f)
400
+
401
+ # # Generate the full output text
402
+ # output_text = generate_output_text(loaded_data)
403
+
404
+ # print("\nProcessing complete!")
405
+ # # Enable radio buttons upon successful loading
406
+ # return output_text, gr.update(interactive=True), gr.update(interactive=True), gr.update(interactive=True)
407
+
408
+ # except json.JSONDecodeError as e:
409
+ # error_msg = f"Error decoding JSON: {str(e)}"
410
+ # print(error_msg)
411
+ # return error_msg, gr.update(interactive=False), gr.update(interactive=False), gr.update(interactive=False)
412
+ # except Exception as e:
413
+ # error_msg = f"Error processing file: {str(e)}"
414
+ # print(error_msg)
415
+ # return error_msg, gr.update(interactive=False), gr.update(interactive=False), gr.update(interactive=False)
416
+
417
+ # def generate_output_text(data, section="ALL"):
418
+ # output_text = ""
419
+
420
+ # if section == "ALL" or section == "header":
421
+ # if 'header' in data:
422
+ # output_text += "=== EXAM DETAILS ===\n"
423
+ # for key, value in data['header'].items():
424
+ # output_text += f"{key.replace('_', ' ').title()}: {value}\n"
425
+ # output_text += "\n"
426
+
427
+ # if section == "ALL" or section == "QCM":
428
+ # if 'multiple_choice_questions' in data:
429
+ # output_text += "=== MULTIPLE CHOICE QUESTIONS ===\n"
430
+ # for q_num, q_data in data['multiple_choice_questions'].items():
431
+ # output_text += f"\nQuestion {q_num.replace('question', '')}:\n"
432
+ # output_text += f"{q_data['question']}\n"
433
+ # for opt_key, opt_val in q_data['options'].items():
434
+ # output_text += f"{opt_key}) {opt_val}\n"
435
+ # output_text += f"Answer: {q_data['answer']}\n"
436
+
437
+ # if section == "ALL" or section == "DTM":
438
+ # if 'short_answer_questions' in data or 'long_answer_questions' in data:
439
+ # output_text += "\n=== DETAILED QUESTIONS ===\n"
440
+ # if 'short_answer_questions' in data:
441
+ # output_text += "\n=== SHORT ANSWER QUESTIONS ===\n"
442
+ # for q_num, q_data in data['short_answer_questions'].items():
443
+ # output_text += f"\nQuestion {q_num.replace('question', '')}:\n"
444
+ # output_text += f"{q_data['question']}\n"
445
+ # output_text += f"Answer: {q_data['answer']}\n"
446
+
447
+ # if 'long_answer_questions' in data:
448
+ # output_text += "\n=== LONG ANSWER QUESTIONS ===\n"
449
+ # for q_num, q_data in data['long_answer_questions'].items():
450
+ # output_text += f"\nQuestion {q_num.replace('question', '')}:\n"
451
+ # output_text += f"{q_data['question']}\n"
452
+ # output_text += f"Answer: {q_data['answer']}\n"
453
+
454
+ # return output_text
455
+
456
+ # def update_textbox_based_on_radio(selection):
457
+ # # Generate output based on the selected section
458
+ # return generate_output_text(loaded_data, section=selection)
459
+
460
+ # # Create Gradio interface
461
+ # with gr.Blocks() as iface:
462
+ # gr.Markdown("# Exam Question Viewer")
463
+
464
+ # with gr.Row():
465
+ # file_input = gr.File(
466
+ # label="Upload JSON Exam File",
467
+ # file_types=[".json"]
468
+ # )
469
+
470
+ # with gr.Row():
471
+ # output_text = gr.Textbox(
472
+ # label="Processed Questions",
473
+ # lines=20,
474
+ # max_lines=30
475
+ # )
476
+
477
+ # with gr.Row():
478
+ # radio_qcm = gr.Radio(
479
+ # ["QCM", "DTM", "ALL"],
480
+ # label="Select Section to Display",
481
+ # interactive=False
482
+ # )
483
+
484
+ # # Update output_text when radio selection changes
485
+ # radio_qcm.change(
486
+ # fn=update_textbox_based_on_radio,
487
+ # inputs=[radio_qcm],
488
+ # outputs=[output_text]
489
+ # )
490
+
491
+ # # Process file input and enable radio buttons on successful load
492
+ # file_input.upload(
493
+ # fn=process_json_file,
494
+ # inputs=[file_input],
495
+ # outputs=[output_text, radio_qcm, radio_qcm, radio_qcm]
496
+ # )
497
+
498
+ # # Launch the interface
499
+ # iface.launch()
500
+
501
  import json
502
  import gradio as gr
503
 
 
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 = ""
 
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():
 
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
 
 
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
 
 
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
+