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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +162 -48
app.py CHANGED
@@ -288,60 +288,121 @@ geomodel_llm = keras_nlp.models.CausalLM.from_preset("hf://ShebMichel/geobot_tea
288
  # iface.launch()
289
 
290
 
291
- import json
292
- import gradio as gr
293
 
294
- def process_json_file(file):
295
- try:
296
- # Read and print the file content for debugging
297
- print("File received:", file.name)
298
 
299
- # Read the JSON file
300
- with open(file.name, 'r') as f:
301
- print("Reading file contents...")
302
- json_data = json.load(f)
303
 
304
- # Print the structure of the JSON data
305
- print("\nJSON Structure:")
306
- print("Available sections:", list(json_data.keys()))
307
 
308
- # Process each section
309
- output_text = ""
310
 
311
- # Process header if exists
312
- if 'header' in json_data:
313
- output_text += "=== EXAM DETAILS ===\n"
314
- for key, value in json_data['header'].items():
315
- output_text += f"{key.replace('_', ' ').title()}: {value}\n"
316
- output_text += "\n"
317
 
318
- # Process multiple choice questions
319
- if 'multiple_choice_questions' in json_data:
320
- output_text += "=== MULTIPLE CHOICE QUESTIONS ===\n"
321
- for q_num, q_data in json_data['multiple_choice_questions'].items():
322
- output_text += f"\nQuestion {q_num.replace('question', '')}:\n"
323
- output_text += f"{q_data['question']}\n"
324
- for opt_key, opt_val in q_data['options'].items():
325
- output_text += f"{opt_key}) {opt_val}\n"
326
- output_text += f"Answer: {q_data['answer']}\n"
327
 
328
- # Process short answer questions
329
- if 'short_answer_questions' in json_data:
330
- output_text += "\n=== SHORT ANSWER QUESTIONS ===\n"
331
- for q_num, q_data in json_data['short_answer_questions'].items():
332
- output_text += f"\nQuestion {q_num.replace('question', '')}:\n"
333
- output_text += f"{q_data['question']}\n"
334
- output_text += f"Answer: {q_data['answer']}\n"
335
 
336
- # Process long answer questions
337
- if 'long_answer_questions' in json_data:
338
- output_text += "\n=== LONG ANSWER QUESTIONS ===\n"
339
- for q_num, q_data in json_data['long_answer_questions'].items():
340
- output_text += f"\nQuestion {q_num.replace('question', '')}:\n"
341
- output_text += f"{q_data['question']}\n"
342
- output_text += f"Answer: {q_data['answer']}\n"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
343
 
344
  print("\nProcessing complete!")
 
345
  return output_text, gr.update(interactive=True), gr.update(interactive=True), gr.update(interactive=True)
346
 
347
  except json.JSONDecodeError as e:
@@ -353,6 +414,49 @@ def process_json_file(file):
353
  print(error_msg)
354
  return error_msg, gr.update(interactive=False), gr.update(interactive=False), gr.update(interactive=False)
355
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
356
  # Create Gradio interface
357
  with gr.Blocks() as iface:
358
  gr.Markdown("# Exam Question Viewer")
@@ -371,14 +475,24 @@ with gr.Blocks() as iface:
371
  )
372
 
373
  with gr.Row():
374
- radio_qcm = gr.Radio(["QCM"], label="Select QCM", interactive=False)
375
- radio_dtm = gr.Radio(["DTM"], label="Select DTM", interactive=False)
376
- radio_all = gr.Radio(["ALL"], label="Select All", interactive=False)
 
 
377
 
 
 
 
 
 
 
 
 
378
  file_input.upload(
379
  fn=process_json_file,
380
  inputs=[file_input],
381
- outputs=[output_text, radio_qcm, radio_dtm, radio_all]
382
  )
383
 
384
  # Launch the interface
 
288
  # iface.launch()
289
 
290
 
291
+ # import json
292
+ # import gradio as gr
293
 
294
+ # def process_json_file(file):
295
+ # try:
296
+ # # Read and print the file content for debugging
297
+ # print("File received:", file.name)
298
 
299
+ # # Read the JSON file
300
+ # with open(file.name, 'r') as f:
301
+ # print("Reading file contents...")
302
+ # json_data = json.load(f)
303
 
304
+ # # Print the structure of the JSON data
305
+ # print("\nJSON Structure:")
306
+ # print("Available sections:", list(json_data.keys()))
307
 
308
+ # # Process each section
309
+ # output_text = ""
310
 
311
+ # # Process header if exists
312
+ # if 'header' in json_data:
313
+ # output_text += "=== EXAM DETAILS ===\n"
314
+ # for key, value in json_data['header'].items():
315
+ # output_text += f"{key.replace('_', ' ').title()}: {value}\n"
316
+ # output_text += "\n"
317
 
318
+ # # Process multiple choice questions
319
+ # if 'multiple_choice_questions' in json_data:
320
+ # output_text += "=== MULTIPLE CHOICE QUESTIONS ===\n"
321
+ # for q_num, q_data in json_data['multiple_choice_questions'].items():
322
+ # output_text += f"\nQuestion {q_num.replace('question', '')}:\n"
323
+ # output_text += f"{q_data['question']}\n"
324
+ # for opt_key, opt_val in q_data['options'].items():
325
+ # output_text += f"{opt_key}) {opt_val}\n"
326
+ # output_text += f"Answer: {q_data['answer']}\n"
327
 
328
+ # # Process short answer questions
329
+ # if 'short_answer_questions' in json_data:
330
+ # output_text += "\n=== SHORT ANSWER QUESTIONS ===\n"
331
+ # for q_num, q_data in json_data['short_answer_questions'].items():
332
+ # output_text += f"\nQuestion {q_num.replace('question', '')}:\n"
333
+ # output_text += f"{q_data['question']}\n"
334
+ # output_text += f"Answer: {q_data['answer']}\n"
335
 
336
+ # # Process long answer questions
337
+ # if 'long_answer_questions' in json_data:
338
+ # output_text += "\n=== LONG ANSWER QUESTIONS ===\n"
339
+ # for q_num, q_data in json_data['long_answer_questions'].items():
340
+ # output_text += f"\nQuestion {q_num.replace('question', '')}:\n"
341
+ # output_text += f"{q_data['question']}\n"
342
+ # output_text += f"Answer: {q_data['answer']}\n"
343
+
344
+ # print("\nProcessing complete!")
345
+ # return output_text, gr.update(interactive=True), gr.update(interactive=True), gr.update(interactive=True)
346
+
347
+ # except json.JSONDecodeError as e:
348
+ # error_msg = f"Error decoding JSON: {str(e)}"
349
+ # print(error_msg)
350
+ # return error_msg, gr.update(interactive=False), gr.update(interactive=False), gr.update(interactive=False)
351
+ # except Exception as e:
352
+ # error_msg = f"Error processing file: {str(e)}"
353
+ # print(error_msg)
354
+ # return error_msg, gr.update(interactive=False), gr.update(interactive=False), gr.update(interactive=False)
355
+
356
+ # # Create Gradio interface
357
+ # with gr.Blocks() as iface:
358
+ # gr.Markdown("# Exam Question Viewer")
359
+
360
+ # with gr.Row():
361
+ # file_input = gr.File(
362
+ # label="Upload JSON Exam File",
363
+ # file_types=[".json"]
364
+ # )
365
+
366
+ # with gr.Row():
367
+ # output_text = gr.Textbox(
368
+ # label="Processed Questions",
369
+ # lines=20,
370
+ # max_lines=30
371
+ # )
372
+
373
+ # with gr.Row():
374
+ # radio_qcm = gr.Radio(["QCM"], label="Select QCM", interactive=False)
375
+ # radio_dtm = gr.Radio(["DTM"], label="Select DTM", interactive=False)
376
+ # radio_all = gr.Radio(["ALL"], label="Select All", interactive=False)
377
+
378
+ # file_input.upload(
379
+ # fn=process_json_file,
380
+ # inputs=[file_input],
381
+ # outputs=[output_text, radio_qcm, radio_dtm, radio_all]
382
+ # )
383
+
384
+ # # Launch the interface
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:
 
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")
 
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