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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +208 -800
app.py CHANGED
@@ -132,846 +132,254 @@ geomodel_llm = keras_nlp.models.CausalLM.from_preset("hf://ShebMichel/geobot_tea
132
  # iface.launch()
133
 
134
 
135
- import json
136
- import gradio as gr
137
-
138
- def process_json_file(file):
139
- try:
140
- print("File received:", file.name)
141
-
142
- with open(file.name, 'r') as f:
143
- json_data = json.load(f)
144
-
145
- print("File loaded successfully!")
146
- return (
147
- "File uploaded successfully!",
148
- json_data,
149
- # Update all radio buttons to be visible and interactive
150
- gr.Radio.update(visible=True, interactive=True),
151
- gr.Radio.update(visible=True, interactive=True),
152
- gr.Radio.update(visible=True, interactive=True)
153
- )
154
- except Exception as e:
155
- return (
156
- f"Error: {str(e)}",
157
- None,
158
- gr.Radio.update(visible=False, interactive=False),
159
- gr.Radio.update(visible=False, interactive=False),
160
- gr.Radio.update(visible=False, interactive=False)
161
- )
162
-
163
- def display_mcq(choice, json_data):
164
- if not json_data or not choice:
165
- return ""
166
- output_text = "\n=== MULTIPLE CHOICE QUESTIONS ===\n"
167
- questions = json_data['multiple_choice_questions']
168
- for q_num, q_data in questions.items():
169
- output_text += f"\nQuestion {q_num.replace('question', '')}:\n"
170
- output_text += f"{q_data['question']}\n"
171
- for opt_key, opt_val in q_data['options'].items():
172
- output_text += f"{opt_key}) {opt_val}\n"
173
- output_text += f"Answer: {q_data['answer']}\n"
174
- output_text += "-" * 50 + "\n"
175
- return output_text
176
-
177
- def display_short(choice, json_data):
178
- if not json_data or not choice:
179
- return ""
180
- output_text = "\n=== SHORT ANSWER QUESTIONS ===\n"
181
- questions = json_data['short_answer_questions']
182
- for q_num, q_data in questions.items():
183
- output_text += f"\nQuestion {q_num.replace('question', '')}:\n"
184
- output_text += f"{q_data['question']}\n"
185
- output_text += f"Answer: {q_data['answer']}\n"
186
- output_text += "-" * 50 + "\n"
187
- return output_text
188
-
189
- def display_long(choice, json_data):
190
- if not json_data or not choice:
191
- return ""
192
- output_text = "\n=== LONG ANSWER QUESTIONS ===\n"
193
- questions = json_data['long_answer_questions']
194
- for q_num, q_data in questions.items():
195
- output_text += f"\nQuestion {q_num.replace('question', '')}:\n"
196
- output_text += f"{q_data['question']}\n"
197
- output_text += f"Answer: {q_data['answer']}\n"
198
- output_text += "-" * 50 + "\n"
199
- return output_text
200
-
201
- # Create Gradio interface
202
- with gr.Blocks() as iface:
203
- gr.Markdown("# Exam Question Viewer")
204
-
205
- # Store JSON data in state
206
- json_state = gr.State(None)
207
-
208
- with gr.Row():
209
- file_input = gr.File(
210
- label="Upload JSON Exam File",
211
- file_types=[".json"]
212
- )
213
- upload_status = gr.Textbox(label="Upload Status")
214
-
215
- with gr.Row():
216
- with gr.Column():
217
- mcq_radio = gr.Radio(
218
- choices=["Show Multiple Choice Questions"],
219
- label="Multiple Choice Questions",
220
- visible=False,
221
- interactive=False
222
- )
223
- mcq_output = gr.Textbox(
224
- label="Multiple Choice Questions",
225
- lines=10,
226
- visible=True
227
- )
228
-
229
- with gr.Column():
230
- short_radio = gr.Radio(
231
- choices=["Show Short Answer Questions"],
232
- label="Short Answer Questions",
233
- visible=False,
234
- interactive=False
235
- )
236
- short_output = gr.Textbox(
237
- label="Short Answer Questions",
238
- lines=10,
239
- visible=True
240
- )
241
-
242
- with gr.Column():
243
- long_radio = gr.Radio(
244
- choices=["Show Long Answer Questions"],
245
- label="Long Answer Questions",
246
- visible=False,
247
- interactive=False
248
- )
249
- long_output = gr.Textbox(
250
- label="Long Answer Questions",
251
- lines=10,
252
- visible=True
253
- )
254
-
255
- # Handle file upload
256
- file_input.upload(
257
- fn=process_json_file,
258
- inputs=[file_input],
259
- outputs=[
260
- upload_status,
261
- json_state,
262
- mcq_radio,
263
- short_radio,
264
- long_radio
265
- ]
266
- )
267
-
268
- # Handle radio button selections
269
- mcq_radio.change(
270
- fn=display_mcq,
271
- inputs=[mcq_radio, json_state],
272
- outputs=[mcq_output]
273
- )
274
-
275
- short_radio.change(
276
- fn=display_short,
277
- inputs=[short_radio, json_state],
278
- outputs=[short_output]
279
- )
280
-
281
- long_radio.change(
282
- fn=display_long,
283
- inputs=[long_radio, json_state],
284
- outputs=[long_output]
285
- )
286
-
287
- # Launch the interface
288
- iface.launch()
289
-
290
-
291
-
292
-
293
-
294
-
295
-
296
-
297
-
298
-
299
-
300
-
301
-
302
-
303
-
304
-
305
-
306
-
307
-
308
-
309
-
310
-
311
-
312
-
313
-
314
-
315
-
316
-
317
-
318
-
319
- # def launch(input):
320
- # template = "Instruction:\n{instruction}\n\nResponse:\n{response}"
321
- # prompt = template.format(
322
- # instruction=input,
323
- # response="",
324
- # )
325
- # out = geomodel_llm.generate(prompt, max_length=1024)
326
- # ind = out.index('Response') + len('Response')+2
327
- # return out[ind:]
328
-
329
- # # Define the function to handle both text and file input
330
- # def analyze_response(text_input, file_input):
331
- # # Process text and file inputs as required
332
- # # For now, we'll just return a placeholder response
333
- # response = f"Received text: {text_input}\n"
334
- # if file_input is not None:
335
- # response += f"File uploaded: {file_input.name}"
336
- # else:
337
- # response += "No file uploaded."
338
- # return response
339
-
340
- # # import gradio as gr
341
- # import json
342
- # from transformers import pipeline
343
-
344
- # Load a simple text-generation model from Hugging Face
345
- #generator = pipeline("text-generation", model="gpt2")
346
-
347
- # Function to read the JSON file and parse sections
348
- # def handle_file_upload(file):
349
- # if file is None:
350
- # return "Please upload a JSON file.", "", gr.update(visible=False, interactive=False)
351
-
352
- # try:
353
- # with open(file.name, 'r', encoding='utf-8') as f:
354
- # data = json.load(f)
355
- # except Exception as e:
356
- # return f"Failed to read JSON file: {e}", "", gr.update(visible=False, interactive=False)
357
-
358
- # # Assuming JSON has keys "QCM", "long_answer", and "short_answer"
359
- # sections = {
360
- # "multiple_choice_questions": data.get("multiple_choice_questions", "QCM content not found in JSON."),
361
- # "long_answer_questions": data.get("long_answer_questions", "Long answer content not found in JSON."),
362
- # "short_answer_questions": data.get("short_answer_questions", "Short answer content not found in JSON.")
363
- # }
364
-
365
- # # Enable radio button after file upload with three options
366
- # return "File uploaded successfully!", sections, gr.update(visible=True, interactive=True)
367
-
368
- # Function to display selected section content in input box
369
- # def display_selected_content(selection, sections):
370
- # return sections.get(selection, "No content available")
371
-
372
- # # Function to generate model output
373
- # def generate_output(input_text):
374
- # # Use the model to generate a response based on input_text
375
- # result = 'I am trying hard now'#generator(input_text, max_length=50, num_return_sequences=1)
376
- # return result[0]["generated_text"]
377
-
378
- # Define the Gradio app
379
- # with gr.Blocks() as demo:
380
- # gr.Markdown("### Upload a JSON file and select a section (multiple_choice_questions, long_answer_questions, short_answer_questions")
381
-
382
- # # File upload and output elements
383
- # file_input = gr.File(label="Upload JSON file")
384
- # upload_output = gr.Textbox(label="Upload Status")
385
- # radio_button = gr.Radio(["multiple_choice_questions", "long_answer_questions", "short_answer_questions"], label="Select Section", interactive=False, visible=False)
386
- # # section_input = gr.Textbox(label="Selected Content", placeholder="Selected content will appear here.")
387
- # # submit_button = gr.Button("Submit")
388
- # # output_box = gr.Textbox(label="Model Output")
389
-
390
- # # # Handle file upload and enable radio button with section selection
391
- # # file_input.change(fn=handle_file_upload, inputs=file_input, outputs=[upload_output, radio_button, radio_button])
392
-
393
- # # # Update input box based on selected section
394
- # # radio_button.change(fn=display_selected_content, inputs=[radio_button, radio_button], outputs=section_input)
395
-
396
- # # # Generate model output on submit
397
- # # submit_button.click(fn=generate_output, inputs=section_input, outputs=output_box)
398
-
399
- # # Launch the app
400
- # #demo.launch()
401
-
402
-
403
-
404
- # import json
405
- # import gradio as gr
406
-
407
- # def handle_file_upload(file):
408
- # if file is None:
409
- # return "Please upload a file", None
410
-
411
- # try:
412
- # # Read the JSON file
413
- # json_data = json.load(file)
414
-
415
- # # Enable the radio button after successful upload
416
- # radio_button.visible = True
417
- # radio_button.interactive = True
418
-
419
- # # Store the data in a global variable or return it
420
- # return "File uploaded successfully!", json_data
421
- # except json.JSONDecodeError:
422
- # return "Error: Invalid JSON file", None
423
- # except Exception as e:
424
- # return f"Error: {str(e)}", None
425
-
426
- # def display_section(section_choice, json_data):
427
- # if json_data is None:
428
- # return "Please upload a JSON file first"
429
-
430
- # try:
431
- # # Get the selected section from the JSON data
432
- # selected_data = json_data[section_choice]
433
-
434
- # # Format the output
435
- # formatted_output = ""
436
- # for q_num, q_data in selected_data.items():
437
- # formatted_output += f"\nQuestion {q_num}:\n"
438
- # formatted_output += f"Q: {q_data['question']}\n"
439
-
440
- # # Handle different question types
441
- # if 'options' in q_data: # Multiple choice questions
442
- # for opt_key, opt_val in q_data['options'].items():
443
- # formatted_output += f"{opt_key}) {opt_val}\n"
444
-
445
- # formatted_output += f"Answer: {q_data['answer']}\n"
446
- # formatted_output += "-" * 50 + "\n"
447
-
448
- # return formatted_output
449
- # except Exception as e:
450
- # return f"Error processing section: {str(e)}"
451
-
452
- # # Create the Gradio interface
453
- # with gr.Blocks() as iface:
454
- # # Store JSON data in state
455
- # json_state = gr.State(None)
456
-
457
- # # File upload components
458
- # file_input = gr.File(label="Upload JSON file")
459
- # upload_output = gr.Textbox(label="Upload Status")
460
-
461
- # # Radio button for section selection
462
- # radio_button = gr.Radio(
463
- # ["multiple_choice_questions", "long_answer_questions", "short_answer_questions"],
464
- # label="Select Section",
465
- # interactive=False,
466
- # visible=False
467
- # )
468
-
469
- # # Output display
470
- # display_output = gr.Textbox(label="Questions", lines=10)
471
-
472
- # # Handle file upload
473
- # file_input.upload(
474
- # fn=handle_file_upload,
475
- # inputs=[file_input],
476
- # outputs=[upload_output, json_state]
477
- # )
478
-
479
- # # Handle section selection
480
- # radio_button.change(
481
- # fn=display_section,
482
- # inputs=[radio_button, json_state],
483
- # outputs=[display_output]
484
- # )
485
-
486
- # # Launch the interface
487
- #iface.launch()
488
-
489
-
490
-
491
  # import json
492
  # import gradio as gr
493
 
494
  # def process_json_file(file):
495
  # try:
496
- # # Read and print the file content for debugging
497
  # print("File received:", file.name)
498
 
499
- # # Read the JSON file
500
  # with open(file.name, 'r') as f:
501
- # print("Reading file contents...")
502
  # json_data = json.load(f)
503
 
504
- # # Print the structure of the JSON data
505
- # print("\nJSON Structure:")
506
- # print("Available sections:", list(json_data.keys()))
507
-
508
- # # Process each section
509
- # output_text = ""
510
-
511
- # # Process header if exists
512
- # if 'header' in json_data:
513
- # output_text += "=== EXAM DETAILS ===\n"
514
- # for key, value in json_data['header'].items():
515
- # output_text += f"{key.replace('_', ' ').title()}: {value}\n"
516
- # output_text += "\n"
517
-
518
- # # Process multiple choice questions
519
- # if 'multiple_choice_questions' in json_data:
520
- # output_text += "=== MULTIPLE CHOICE QUESTIONS ===\n"
521
- # for q_num, q_data in json_data['multiple_choice_questions'].items():
522
- # output_text += f"\nQuestion {q_num.replace('question', '')}:\n"
523
- # output_text += f"{q_data['question']}\n"
524
- # for opt_key, opt_val in q_data['options'].items():
525
- # output_text += f"{opt_key}) {opt_val}\n"
526
- # output_text += f"Answer: {q_data['answer']}\n"
527
-
528
- # # Process short answer questions
529
- # if 'short_answer_questions' in json_data:
530
- # output_text += "\n=== SHORT ANSWER QUESTIONS ===\n"
531
- # for q_num, q_data in json_data['short_answer_questions'].items():
532
- # output_text += f"\nQuestion {q_num.replace('question', '')}:\n"
533
- # output_text += f"{q_data['question']}\n"
534
- # output_text += f"Answer: {q_data['answer']}\n"
535
-
536
- # # Process long answer questions
537
- # if 'long_answer_questions' in json_data:
538
- # output_text += "\n=== LONG ANSWER QUESTIONS ===\n"
539
- # for q_num, q_data in json_data['long_answer_questions'].items():
540
- # output_text += f"\nQuestion {q_num.replace('question', '')}:\n"
541
- # output_text += f"{q_data['question']}\n"
542
- # output_text += f"Answer: {q_data['answer']}\n"
543
-
544
- # print("\nProcessing complete!")
545
- # return output_text
546
-
547
- # except json.JSONDecodeError as e:
548
- # error_msg = f"Error decoding JSON: {str(e)}"
549
- # print(error_msg)
550
- # return error_msg
551
  # except Exception as e:
552
- # error_msg = f"Error processing file: {str(e)}"
553
- # print(error_msg)
554
- # return error_msg
555
-
556
- # # def display_section(section_type, json_data):
557
- # # if not json_data or section_type not in json_data:
558
- # # return "Please upload a valid JSON file first"
559
-
560
- # # output_text = f"\n=== {section_type.upper().replace('_', ' ')} ===\n"
561
-
562
- # # questions = json_data[section_type]
563
- # # for q_num, q_data in questions.items():
564
- # # output_text += f"\nQuestion {q_num.replace('question', '')}:\n"
565
- # # output_text += f"{q_data['question']}\n"
566
-
567
- # # if 'options' in q_data:
568
- # # for opt_key, opt_val in q_data['options'].items():
569
- # # output_text += f"{opt_key}) {opt_val}\n"
570
-
571
- # # output_text += f"Answer: {q_data['answer']}\n"
572
- # # output_text += "-" * 50 + "\n"
573
-
574
- # # return output_text
575
-
576
- # # # Create Gradio interface
577
- # # with gr.Blocks() as iface:
578
- # # gr.Markdown("# Exam Question Viewer")
579
-
580
- # # # Store JSON data in state
581
- # # json_state = gr.State(None)
582
-
583
- # # with gr.Row():
584
- # # file_input = gr.File(
585
- # # label="Upload JSON Exam File",
586
- # # file_types=[".json"]
587
- # # )
588
- # # upload_status = gr.Textbox(label="Upload Status")
589
-
590
- # # with gr.Row():
591
- # # radio_button = gr.Radio(
592
- # # choices=["multiple_choice_questions", "long_answer_questions", "short_answer_questions"],
593
- # # label="Select Question Type",
594
- # # visible=False, # Initially hidden
595
- # # interactive=False # Initially disabled
596
- # # )
597
-
598
- # # with gr.Row():
599
- # # section_output = gr.Textbox(
600
- # # label="Selected Questions",
601
- # # lines=20,
602
- # # max_lines=30
603
- # # )
604
-
605
- # # # Handle file upload
606
- # # file_input.upload(
607
- # # fn=process_json_file,
608
- # # inputs=[file_input],
609
- # # outputs=[upload_status, json_state, radio_button]
610
- # # )
611
-
612
- # # # Handle radio button selection
613
- # # radio_button.change(
614
- # # fn=display_section,
615
- # # inputs=[radio_button, json_state],
616
- # # outputs=[section_output]
617
- # # )
618
-
619
- # # # Launch the interface
620
- # # iface.launch()
621
-
622
-
623
- # # import json
624
- # # import gradio as gr
625
-
626
- # # def process_json_file(file):
627
- # # try:
628
- # # print("File received:", file.name)
629
-
630
- # # with open(file.name, 'r') as f:
631
- # # json_data = json.load(f)
632
-
633
- # # print("File loaded successfully!")
634
- # # return "File uploaded successfully!", json_data, gr.Radio.update(visible=True, interactive=True)
635
- # # except Exception as e:
636
- # # return f"Error: {str(e)}", None, gr.Radio.update(visible=False, interactive=False)
637
 
638
- # # def display_section(section_type, json_data):
639
- # # if not json_data or section_type not in json_data:
640
- # # return "Please upload a valid JSON file first"
641
-
642
- # # output_text = f"\n=== {section_type.upper().replace('_', ' ')} ===\n"
643
-
644
- # # questions = json_data[section_type]
645
- # # for q_num, q_data in questions.items():
646
- # # output_text += f"\nQuestion {q_num.replace('question', '')}:\n"
647
- # # output_text += f"{q_data['question']}\n"
648
-
649
- # # if 'options' in q_data:
650
- # # for opt_key, opt_val in q_data['options'].items():
651
- # # output_text += f"{opt_key}) {opt_val}\n"
652
-
653
- # # output_text += f"Answer: {q_data['answer']}\n"
654
- # # output_text += "-" * 50 + "\n"
655
-
656
- # # return output_text
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
657
 
658
  # # Create Gradio interface
659
- # # with gr.Blocks() as iface:
660
- # # gr.Markdown("# Exam Question Viewer")
661
-
662
- # # # Store JSON data in state
663
- # # json_state = gr.State(None)
664
-
665
- # # with gr.Row():
666
- # # file_input = gr.File(
667
- # # label="Upload JSON Exam File",
668
- # # file_types=[".json"]
669
- # # )
670
- # # upload_status = gr.Textbox(label="Upload Status")
671
-
672
- # # with gr.Row():
673
- # # radio_button = gr.Radio(
674
- # # choices=["multiple_choice_questions", "long_answer_questions", "short_answer_questions"],
675
- # # label="Select Question Type",
676
- # # visible=False, # Initially hidden
677
- # # interactive=False # Initially disabled
678
- # # )
679
-
680
- # # with gr.Row():
681
- # # section_output = gr.Textbox(
682
- # # label="Selected Questions",
683
- # # lines=20,
684
- # # max_lines=30
685
- # # )
686
-
687
- # # # Handle file upload
688
- # # file_input.upload(
689
- # # fn=process_json_file,
690
- # # inputs=[file_input],
691
- # # outputs=[upload_status, json_state, radio_button]
692
- # # )
693
-
694
- # # # Handle radio button selection
695
- # # radio_button.change(
696
- # # fn=display_section,
697
- # # inputs=[radio_button, json_state],
698
- # # outputs=[section_output]
699
- # # )
700
-
701
- # # Launch the interface
702
- # # iface.launch()
703
-
704
-
705
-
706
-
707
-
708
-
709
-
710
-
711
-
712
-
713
-
714
-
715
-
716
-
717
-
718
-
719
-
720
- # # # Create Gradio interface
721
  # with gr.Blocks() as iface:
722
  # gr.Markdown("# Exam Question Viewer")
723
 
 
 
 
724
  # with gr.Row():
725
  # file_input = gr.File(
726
  # label="Upload JSON Exam File",
727
  # file_types=[".json"]
728
  # )
 
729
 
730
  # with gr.Row():
731
- # output_text = gr.Textbox(
732
- # label="Processed Questions",
733
- # lines=20,
734
- # max_lines=30
735
- # )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
736
 
 
737
  # file_input.upload(
738
  # fn=process_json_file,
739
  # inputs=[file_input],
740
- # outputs=[output_text]
 
 
 
 
 
 
741
  # )
742
-
743
- # # with gr.Row():
744
- # # radio_button = gr.Radio(
745
- # # choices=["multiple_choice_questions", "long_answer_questions", "short_answer_questions"],
746
- # # label="Select Question Type",
747
- # # visible=False, # Initially hidden
748
- # # interactive=False # Initially disabled
749
- # # )
750
-
751
- # # with gr.Row():
752
- # # section_output = gr.Textbox(
753
- # # label="Selected Questions",
754
- # # lines=20,
755
- # # max_lines=30
756
- # # )
757
- # # # Handle file upload
758
- # # file_input.upload(
759
- # # fn=process_json_file,
760
- # # inputs=[file_input],
761
- # # outputs=[upload_status, json_state, radio_button]
762
- # # )
763
-
764
- # # # Handle radio button selection
765
- # # radio_button.change(
766
- # # fn=display_section,
767
- # # inputs=[radio_button, json_state],
768
- # # outputs=[section_output]
769
- # # )
770
-
771
-
772
- # # # Launch the interface
773
- # iface.launch()
774
-
775
-
776
-
777
- # # # import gradio as gr
778
- # # # import json
779
- # # # import fitz # PyMuPDF for PDFs
780
- # # # from docx import Document
781
-
782
- # # # Functions to read different file types
783
- # # # def read_pdf(file):
784
- # # # text = ""
785
- # # # pdf = fitz.open(file)
786
- # # # for page in pdf:
787
- # # # text += page.get_text()
788
- # # # pdf.close()
789
- # # # return text
790
-
791
- # # def read_json(file):
792
- # # with open(file.name, 'r', encoding='utf-8') as f:
793
- # # data = json.load(f)
794
- # # return json.dumps(data, indent=4)
795
-
796
- # # # def read_docx(file):
797
- # # # doc = Document(file)
798
- # # # text = "\n".join([paragraph.text for paragraph in doc.paragraphs])
799
- # # # return text
800
-
801
- # # def parse_content(content):
802
- # # # Assuming QCM, short_answer_questions, and long_answer_questions are sections
803
- # # sections = {"QCM": "", "short_answer_questions": "", "long_answer_questions": ""}
804
- # # # Simple splitting based on section keywords (customize based on format)
805
- # # if "QCM" in content:
806
- # # sections["QCM"] = content.split("QCM")[1].split("short_answer_questions")[0].strip()
807
- # # if "short_answer_questions" in content:
808
- # # sections["short_answer_questions"] = content.split("short_answer_questions")[1].split("long_answer_questions")[0].strip()
809
- # # if "long_answer_questions" in content:
810
- # # sections["long_answer_questions"] = content.split("long_answer_questions")[1].strip()
811
- # # return sections
812
-
813
- # # def handle_file_upload(file):
814
- # # if file is None:
815
- # # return "Please upload a file.", "", gr.update(visible=False, interactive=False)
816
-
817
- # # file_type = file.name.split('.')[-1].lower()
818
- # # if file_type == 'pdf':
819
- # # content = read_pdf(file.name)
820
- # # elif file_type == 'json':
821
- # # content = read_json(file)
822
- # # elif file_type == 'docx':
823
- # # content = read_docx(file)
824
- # # else:
825
- # # return "Unsupported file format. Please upload a PDF, JSON, or DOCX file.", "", gr.update(visible=False, interactive=False)
826
-
827
- # # # Parse content into predefined sections
828
- # # sections = parse_content(content)
829
 
830
- # # # Display default section content
831
- # # default_display = sections["QCM"] # Default to QCM section if it exists
832
-
833
- # # # Enable radio button after file upload
834
- # # return "File uploaded successfully!", default_display, gr.update(visible=True, interactive=True, choices=["QCM", "short_answer_questions", "long_answer_questions"])
835
-
836
- # # def display_selected_content(selection, sections):
837
- # # # Return content for selected section
838
- # return sections.get(selection, "No content available")
839
-
840
- # # Define the Gradio app
841
- # with gr.Blocks() as demo:
842
- # gr.Markdown("### Upload a file and select a section (QCM, short_answer_questions, long_answer_questions)")
843
 
844
- # # File upload and output elements
845
- # file_input = gr.File(label="Upload a file (PDF, JSON, DOCX)")
846
- # upload_output = gr.Textbox(label="Upload Status")
847
- # radio_button = gr.Radio(["QCM", "short_answer_questions", "long_answer_questions"], label="Select Section", interactive=False, visible=False)
848
- # section_output = gr.Textbox(label="Selected Content")
849
 
850
- # # Handle file upload and enable radio button with section selection
851
- # file_input.change(fn=handle_file_upload, inputs=file_input, outputs=[upload_output, section_output, radio_button])
852
- # radio_button.change(fn=display_selected_content, inputs=[radio_button, radio_button], outputs=section_output)
853
-
854
- # # Launch the app
855
- # demo.launch()
856
-
857
-
858
 
859
- # Set up Gradio Interface
860
- # iface = gr.Interface(
861
- # #fn=chatbot,
862
- # inputs=[
863
- # gr.File(label="Upload a file (PDF, JSON, DOCX)"),
864
- # gr.Textbox(label="Your Message"),
865
- # "state" # Keeps chat history between turns
866
- # ],
867
- # outputs="chatbot",
868
- # live=True,
869
- # description="Drag and drop a file and start chatting with the bot based on its contents."
870
- # )
871
  # iface.launch()
872
- #title="👋 Hola-Hello-Bonjour-Mbote-你好 👋 <br><br> I am geobot-teacher the student marker! <br><br> I am here to analyse each question to determine whether the response qualifies as a pass or fail. <br><br> Try me :)",
873
- #description="Synthetic QA pairs (~1k) was finetuned on top of Gemma_2b_en.")
874
- # inputs="text"
875
-
876
- # iface = gr.Interface(launch,
877
- # inputs="text",
878
- # outputs="text",
879
- # css=css,
880
- # title="👋 Hi, I am geobot-teacher: The Student Marker 👋",
881
- # description="Hola/Hello/Bonjour/Mbote/你好 \n\n"
882
- # "I am here to analyse each question to determine whether the response qualifies as a pass or fail.\n\n"
883
- # "Try me :)",
884
- # )
885
-
886
- # import gradio as gr
887
- # import json
888
- # import fitz # PyMuPDF for PDFs
889
- # from docx import Document
890
-
891
- # Function to read PDF, JSON, or DOCX files and categorize content
892
- # def read_pdf(file):
893
- # text = ""
894
- # pdf = fitz.open(file)
895
- # for page in pdf:
896
- # text += page.get_text()
897
- # pdf.close()
898
- # return text
899
 
900
- # def read_json(file):
901
- # with open(file.name, 'r', encoding='utf-8') as f:
902
- # data = json.load(f)
903
- # return json.dumps(data, indent=4)
904
 
905
- # # def read_docx(file):
906
- # # doc = Document(file)
907
- # # text = "\n".join([paragraph.text for paragraph in doc.paragraphs])
908
- # # return text
909
-
910
- # def parse_content(content):
911
- # # Assuming header and multiple_choice_questions sections are clearly marked in the content
912
- # sections = {"header": "", "multiple_choice_questions": ""}
913
- # # Here, a simple splitting example (customize this based on your format)
914
- # if "header" in content:
915
- # sections["header"] = content.split("header")[1].split("header")[0].strip()
916
- # if "multiple_choice_questions" in content:
917
- # sections["multiple_choice_questions"] = content.split("multiple_choice_questions")[1].strip()
918
- # return sections
919
 
920
- # def handle_file_upload(file):
921
- # if file is None:
922
- # return "Please upload a file.", [], ""
923
-
924
- # file_type = file.name.split('.')[-1].lower()
925
- # if file_type == 'pdf':
926
- # content = read_pdf(file.name)
927
- # elif file_type == 'json':
928
- # content = read_json(file)
929
- # elif file_type == 'docx':
930
- # content = read_docx(file)
931
- # else:
932
- # return "Unsupported file format. Please upload a PDF, JSON, or DOCX file.", [], ""
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
933
 
934
- # # Parse content into sections
935
- # sections = parse_content(content)
936
- # options = list(sections.keys()) # Radio button options (e.g., QCM, DTM)
937
 
938
- # # Default to showing the first option if available
939
- # default_display = sections[options[0]] if options else "No content available"
 
 
 
940
 
941
- # return "File uploaded successfully!", options, default_display
942
-
943
- # def display_selected_content(selection, sections):
944
- # return sections.get(selection, "No content available")
945
-
946
- # # Define the Gradio app
947
- # with gr.Blocks() as demo:
948
- # gr.Markdown("### Upload a file and select a section (QCM, DTM)")
949
 
950
- # # File upload and output elements
951
- # file_input = gr.File(label="Upload a file (PDF, JSON, DOCX)")
952
- # upload_output = gr.Textbox(label="Upload Status")
953
- # radio_button = gr.Radio(label="Select Section", interactive=True)
954
- # section_output = gr.Textbox(label="Selected Content")
955
 
956
- # # Handle file upload and section selection
957
- # file_input.change(fn=handle_file_upload, inputs=file_input, outputs=[upload_output, radio_button, section_output])
958
- # radio_button.change(fn=display_selected_content, inputs=[radio_button, radio_button], outputs=section_output)
959
-
960
- # # Launch the app
961
- # demo.launch()
962
-
963
- # iface = gr.Interface(fn=launch,
964
- # inputs=[
965
- # gr.File(label="Upload a file (PDF, JSON, DOCX)"),gr.Textbox(label="Enter your response"),
966
- # gr.Radio(
967
- # ["QCM","short_answer_questions","long_answer_questions"])],
968
- # outputs="text",
969
- # css=css,
970
- # title="👋 Hi, I am geobot-teacher: The Student Marker 👋",
971
- # description="Hola/Hello/Bonjour/Mbote/你好 \n\n"
972
- # "I am here to analyse each question to determine whether the response qualifies as a pass or fail.\n\n"
973
- # "Try me :)",
974
- # )
975
-
976
- # iface.launch()
977
 
 
 
 
132
  # iface.launch()
133
 
134
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
135
  # import json
136
  # import gradio as gr
137
 
138
  # def process_json_file(file):
139
  # try:
 
140
  # print("File received:", file.name)
141
 
 
142
  # with open(file.name, 'r') as f:
 
143
  # json_data = json.load(f)
144
 
145
+ # print("File loaded successfully!")
146
+ # return (
147
+ # "File uploaded successfully!",
148
+ # json_data,
149
+ # # Update all radio buttons to be visible and interactive
150
+ # gr.Radio.update(visible=True, interactive=True),
151
+ # gr.Radio.update(visible=True, interactive=True),
152
+ # gr.Radio.update(visible=True, interactive=True)
153
+ # )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
154
  # except Exception as e:
155
+ # return (
156
+ # f"Error: {str(e)}",
157
+ # None,
158
+ # gr.Radio.update(visible=False, interactive=False),
159
+ # gr.Radio.update(visible=False, interactive=False),
160
+ # gr.Radio.update(visible=False, interactive=False)
161
+ # )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
162
 
163
+ # def display_mcq(choice, json_data):
164
+ # if not json_data or not choice:
165
+ # return ""
166
+ # output_text = "\n=== MULTIPLE CHOICE QUESTIONS ===\n"
167
+ # questions = json_data['multiple_choice_questions']
168
+ # for q_num, q_data in questions.items():
169
+ # output_text += f"\nQuestion {q_num.replace('question', '')}:\n"
170
+ # output_text += f"{q_data['question']}\n"
171
+ # for opt_key, opt_val in q_data['options'].items():
172
+ # output_text += f"{opt_key}) {opt_val}\n"
173
+ # output_text += f"Answer: {q_data['answer']}\n"
174
+ # output_text += "-" * 50 + "\n"
175
+ # return output_text
176
+
177
+ # def display_short(choice, json_data):
178
+ # if not json_data or not choice:
179
+ # return ""
180
+ # output_text = "\n=== SHORT ANSWER QUESTIONS ===\n"
181
+ # questions = json_data['short_answer_questions']
182
+ # for q_num, q_data in questions.items():
183
+ # output_text += f"\nQuestion {q_num.replace('question', '')}:\n"
184
+ # output_text += f"{q_data['question']}\n"
185
+ # output_text += f"Answer: {q_data['answer']}\n"
186
+ # output_text += "-" * 50 + "\n"
187
+ # return output_text
188
+
189
+ # def display_long(choice, json_data):
190
+ # if not json_data or not choice:
191
+ # return ""
192
+ # output_text = "\n=== LONG ANSWER QUESTIONS ===\n"
193
+ # questions = json_data['long_answer_questions']
194
+ # for q_num, q_data in questions.items():
195
+ # output_text += f"\nQuestion {q_num.replace('question', '')}:\n"
196
+ # output_text += f"{q_data['question']}\n"
197
+ # output_text += f"Answer: {q_data['answer']}\n"
198
+ # output_text += "-" * 50 + "\n"
199
+ # return output_text
200
 
201
  # # Create Gradio interface
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
202
  # with gr.Blocks() as iface:
203
  # gr.Markdown("# Exam Question Viewer")
204
 
205
+ # # Store JSON data in state
206
+ # json_state = gr.State(None)
207
+
208
  # with gr.Row():
209
  # file_input = gr.File(
210
  # label="Upload JSON Exam File",
211
  # file_types=[".json"]
212
  # )
213
+ # upload_status = gr.Textbox(label="Upload Status")
214
 
215
  # with gr.Row():
216
+ # with gr.Column():
217
+ # mcq_radio = gr.Radio(
218
+ # choices=["Show Multiple Choice Questions"],
219
+ # label="Multiple Choice Questions",
220
+ # visible=False,
221
+ # interactive=False
222
+ # )
223
+ # mcq_output = gr.Textbox(
224
+ # label="Multiple Choice Questions",
225
+ # lines=10,
226
+ # visible=True
227
+ # )
228
+
229
+ # with gr.Column():
230
+ # short_radio = gr.Radio(
231
+ # choices=["Show Short Answer Questions"],
232
+ # label="Short Answer Questions",
233
+ # visible=False,
234
+ # interactive=False
235
+ # )
236
+ # short_output = gr.Textbox(
237
+ # label="Short Answer Questions",
238
+ # lines=10,
239
+ # visible=True
240
+ # )
241
+
242
+ # with gr.Column():
243
+ # long_radio = gr.Radio(
244
+ # choices=["Show Long Answer Questions"],
245
+ # label="Long Answer Questions",
246
+ # visible=False,
247
+ # interactive=False
248
+ # )
249
+ # long_output = gr.Textbox(
250
+ # label="Long Answer Questions",
251
+ # lines=10,
252
+ # visible=True
253
+ # )
254
 
255
+ # # Handle file upload
256
  # file_input.upload(
257
  # fn=process_json_file,
258
  # inputs=[file_input],
259
+ # outputs=[
260
+ # upload_status,
261
+ # json_state,
262
+ # mcq_radio,
263
+ # short_radio,
264
+ # long_radio
265
+ # ]
266
  # )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
267
 
268
+ # # Handle radio button selections
269
+ # mcq_radio.change(
270
+ # fn=display_mcq,
271
+ # inputs=[mcq_radio, json_state],
272
+ # outputs=[mcq_output]
273
+ # )
 
 
 
 
 
 
 
274
 
275
+ # short_radio.change(
276
+ # fn=display_short,
277
+ # inputs=[short_radio, json_state],
278
+ # outputs=[short_output]
279
+ # )
280
 
281
+ # long_radio.change(
282
+ # fn=display_long,
283
+ # inputs=[long_radio, json_state],
284
+ # outputs=[long_output]
285
+ # )
 
 
 
286
 
287
+ # # 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()