ShebMichel commited on
Commit
17f1f33
·
verified ·
1 Parent(s): 45dd9d8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +134 -134
app.py CHANGED
@@ -211,70 +211,70 @@ def generate_output(input_text):
211
 
212
 
213
 
214
- # import json
215
- # import gradio as gr
216
 
217
- # def process_json_file(file):
218
- # try:
219
- # # Read and print the file content for debugging
220
- # print("File received:", file.name)
221
 
222
- # # Read the JSON file
223
- # with open(file.name, 'r') as f:
224
- # print("Reading file contents...")
225
- # json_data = json.load(f)
226
 
227
- # # Print the structure of the JSON data
228
- # print("\nJSON Structure:")
229
- # print("Available sections:", list(json_data.keys()))
230
 
231
- # # Process each section
232
- # output_text = ""
233
 
234
- # # Process header if exists
235
- # if 'header' in json_data:
236
- # output_text += "=== EXAM DETAILS ===\n"
237
- # for key, value in json_data['header'].items():
238
- # output_text += f"{key.replace('_', ' ').title()}: {value}\n"
239
- # output_text += "\n"
240
 
241
- # # Process multiple choice questions
242
- # if 'multiple_choice_questions' in json_data:
243
- # output_text += "=== MULTIPLE CHOICE QUESTIONS ===\n"
244
- # for q_num, q_data in json_data['multiple_choice_questions'].items():
245
- # output_text += f"\nQuestion {q_num.replace('question', '')}:\n"
246
- # output_text += f"{q_data['question']}\n"
247
- # for opt_key, opt_val in q_data['options'].items():
248
- # output_text += f"{opt_key}) {opt_val}\n"
249
- # output_text += f"Answer: {q_data['answer']}\n"
250
 
251
- # # Process short answer questions
252
- # if 'short_answer_questions' in json_data:
253
- # output_text += "\n=== SHORT ANSWER QUESTIONS ===\n"
254
- # for q_num, q_data in json_data['short_answer_questions'].items():
255
- # output_text += f"\nQuestion {q_num.replace('question', '')}:\n"
256
- # output_text += f"{q_data['question']}\n"
257
- # output_text += f"Answer: {q_data['answer']}\n"
258
 
259
- # # Process long answer questions
260
- # if 'long_answer_questions' in json_data:
261
- # output_text += "\n=== LONG ANSWER QUESTIONS ===\n"
262
- # for q_num, q_data in json_data['long_answer_questions'].items():
263
- # output_text += f"\nQuestion {q_num.replace('question', '')}:\n"
264
- # output_text += f"{q_data['question']}\n"
265
- # output_text += f"Answer: {q_data['answer']}\n"
266
 
267
- # print("\nProcessing complete!")
268
- # return output_text
269
 
270
- # except json.JSONDecodeError as e:
271
- # error_msg = f"Error decoding JSON: {str(e)}"
272
- # print(error_msg)
273
- # return error_msg
274
- # except Exception as e:
275
- # error_msg = f"Error processing file: {str(e)}"
276
- # print(error_msg)
277
- # return error_msg
278
 
279
  # def display_section(section_type, json_data):
280
  # if not json_data or section_type not in json_data:
@@ -343,86 +343,86 @@ def generate_output(input_text):
343
  # iface.launch()
344
 
345
 
346
- import json
347
- import gradio as gr
348
 
349
- def process_json_file(file):
350
- try:
351
- print("File received:", file.name)
352
 
353
- with open(file.name, 'r') as f:
354
- json_data = json.load(f)
355
 
356
- print("File loaded successfully!")
357
- return "File uploaded successfully!", json_data, gr.Radio.update(visible=True, interactive=True)
358
- except Exception as e:
359
- return f"Error: {str(e)}", None, gr.Radio.update(visible=False, interactive=False)
360
 
361
- def display_section(section_type, json_data):
362
- if not json_data or section_type not in json_data:
363
- return "Please upload a valid JSON file first"
364
 
365
- output_text = f"\n=== {section_type.upper().replace('_', ' ')} ===\n"
366
 
367
- questions = json_data[section_type]
368
- for q_num, q_data in questions.items():
369
- output_text += f"\nQuestion {q_num.replace('question', '')}:\n"
370
- output_text += f"{q_data['question']}\n"
371
 
372
- if 'options' in q_data:
373
- for opt_key, opt_val in q_data['options'].items():
374
- output_text += f"{opt_key}) {opt_val}\n"
375
 
376
- output_text += f"Answer: {q_data['answer']}\n"
377
- output_text += "-" * 50 + "\n"
378
 
379
- return output_text
380
 
381
  # Create Gradio interface
382
- with gr.Blocks() as iface:
383
- gr.Markdown("# Exam Question Viewer")
384
 
385
- # Store JSON data in state
386
- json_state = gr.State(None)
387
 
388
- with gr.Row():
389
- file_input = gr.File(
390
- label="Upload JSON Exam File",
391
- file_types=[".json"]
392
- )
393
- upload_status = gr.Textbox(label="Upload Status")
394
 
395
- with gr.Row():
396
- radio_button = gr.Radio(
397
- choices=["multiple_choice_questions", "long_answer_questions", "short_answer_questions"],
398
- label="Select Question Type",
399
- visible=False, # Initially hidden
400
- interactive=False # Initially disabled
401
- )
402
 
403
- with gr.Row():
404
- section_output = gr.Textbox(
405
- label="Selected Questions",
406
- lines=20,
407
- max_lines=30
408
- )
409
 
410
- # Handle file upload
411
- file_input.upload(
412
- fn=process_json_file,
413
- inputs=[file_input],
414
- outputs=[upload_status, json_state, radio_button]
415
- )
416
 
417
- # Handle radio button selection
418
- radio_button.change(
419
- fn=display_section,
420
- inputs=[radio_button, json_state],
421
- outputs=[section_output]
422
- )
423
 
424
  # Launch the interface
425
- iface.launch()
426
 
427
 
428
 
@@ -441,27 +441,27 @@ iface.launch()
441
 
442
 
443
  # # Create Gradio interface
444
- # with gr.Blocks() as iface:
445
- # gr.Markdown("# Exam Question Viewer")
446
 
447
- # with gr.Row():
448
- # file_input = gr.File(
449
- # label="Upload JSON Exam File",
450
- # file_types=[".json"]
451
- # )
452
 
453
- # with gr.Row():
454
- # output_text = gr.Textbox(
455
- # label="Processed Questions",
456
- # lines=20,
457
- # max_lines=30
458
- # )
459
 
460
- # file_input.upload(
461
- # fn=process_json_file,
462
- # inputs=[file_input],
463
- # outputs=[output_text]
464
- # )
465
 
466
  # with gr.Row():
467
  # radio_button = gr.Radio(
@@ -493,7 +493,7 @@ iface.launch()
493
 
494
 
495
  # # Launch the interface
496
- # iface.launch()
497
 
498
 
499
 
 
211
 
212
 
213
 
214
+ import json
215
+ import gradio as gr
216
 
217
+ def process_json_file(file):
218
+ try:
219
+ # Read and print the file content for debugging
220
+ print("File received:", file.name)
221
 
222
+ # Read the JSON file
223
+ with open(file.name, 'r') as f:
224
+ print("Reading file contents...")
225
+ json_data = json.load(f)
226
 
227
+ # Print the structure of the JSON data
228
+ print("\nJSON Structure:")
229
+ print("Available sections:", list(json_data.keys()))
230
 
231
+ # Process each section
232
+ output_text = ""
233
 
234
+ # Process header if exists
235
+ if 'header' in json_data:
236
+ output_text += "=== EXAM DETAILS ===\n"
237
+ for key, value in json_data['header'].items():
238
+ output_text += f"{key.replace('_', ' ').title()}: {value}\n"
239
+ output_text += "\n"
240
 
241
+ # Process multiple choice questions
242
+ if 'multiple_choice_questions' in json_data:
243
+ output_text += "=== MULTIPLE CHOICE QUESTIONS ===\n"
244
+ for q_num, q_data in json_data['multiple_choice_questions'].items():
245
+ output_text += f"\nQuestion {q_num.replace('question', '')}:\n"
246
+ output_text += f"{q_data['question']}\n"
247
+ for opt_key, opt_val in q_data['options'].items():
248
+ output_text += f"{opt_key}) {opt_val}\n"
249
+ output_text += f"Answer: {q_data['answer']}\n"
250
 
251
+ # Process short answer questions
252
+ if 'short_answer_questions' in json_data:
253
+ output_text += "\n=== SHORT ANSWER QUESTIONS ===\n"
254
+ for q_num, q_data in json_data['short_answer_questions'].items():
255
+ output_text += f"\nQuestion {q_num.replace('question', '')}:\n"
256
+ output_text += f"{q_data['question']}\n"
257
+ output_text += f"Answer: {q_data['answer']}\n"
258
 
259
+ # Process long answer questions
260
+ if 'long_answer_questions' in json_data:
261
+ output_text += "\n=== LONG ANSWER QUESTIONS ===\n"
262
+ for q_num, q_data in json_data['long_answer_questions'].items():
263
+ output_text += f"\nQuestion {q_num.replace('question', '')}:\n"
264
+ output_text += f"{q_data['question']}\n"
265
+ output_text += f"Answer: {q_data['answer']}\n"
266
 
267
+ print("\nProcessing complete!")
268
+ return output_text
269
 
270
+ except json.JSONDecodeError as e:
271
+ error_msg = f"Error decoding JSON: {str(e)}"
272
+ print(error_msg)
273
+ return error_msg
274
+ except Exception as e:
275
+ error_msg = f"Error processing file: {str(e)}"
276
+ print(error_msg)
277
+ return error_msg
278
 
279
  # def display_section(section_type, json_data):
280
  # if not json_data or section_type not in json_data:
 
343
  # iface.launch()
344
 
345
 
346
+ # import json
347
+ # import gradio as gr
348
 
349
+ # def process_json_file(file):
350
+ # try:
351
+ # print("File received:", file.name)
352
 
353
+ # with open(file.name, 'r') as f:
354
+ # json_data = json.load(f)
355
 
356
+ # print("File loaded successfully!")
357
+ # return "File uploaded successfully!", json_data, gr.Radio.update(visible=True, interactive=True)
358
+ # except Exception as e:
359
+ # return f"Error: {str(e)}", None, gr.Radio.update(visible=False, interactive=False)
360
 
361
+ # def display_section(section_type, json_data):
362
+ # if not json_data or section_type not in json_data:
363
+ # return "Please upload a valid JSON file first"
364
 
365
+ # output_text = f"\n=== {section_type.upper().replace('_', ' ')} ===\n"
366
 
367
+ # questions = json_data[section_type]
368
+ # for q_num, q_data in questions.items():
369
+ # output_text += f"\nQuestion {q_num.replace('question', '')}:\n"
370
+ # output_text += f"{q_data['question']}\n"
371
 
372
+ # if 'options' in q_data:
373
+ # for opt_key, opt_val in q_data['options'].items():
374
+ # output_text += f"{opt_key}) {opt_val}\n"
375
 
376
+ # output_text += f"Answer: {q_data['answer']}\n"
377
+ # output_text += "-" * 50 + "\n"
378
 
379
+ # return output_text
380
 
381
  # Create Gradio interface
382
+ # with gr.Blocks() as iface:
383
+ # gr.Markdown("# Exam Question Viewer")
384
 
385
+ # # Store JSON data in state
386
+ # json_state = gr.State(None)
387
 
388
+ # with gr.Row():
389
+ # file_input = gr.File(
390
+ # label="Upload JSON Exam File",
391
+ # file_types=[".json"]
392
+ # )
393
+ # upload_status = gr.Textbox(label="Upload Status")
394
 
395
+ # with gr.Row():
396
+ # radio_button = gr.Radio(
397
+ # choices=["multiple_choice_questions", "long_answer_questions", "short_answer_questions"],
398
+ # label="Select Question Type",
399
+ # visible=False, # Initially hidden
400
+ # interactive=False # Initially disabled
401
+ # )
402
 
403
+ # with gr.Row():
404
+ # section_output = gr.Textbox(
405
+ # label="Selected Questions",
406
+ # lines=20,
407
+ # max_lines=30
408
+ # )
409
 
410
+ # # Handle file upload
411
+ # file_input.upload(
412
+ # fn=process_json_file,
413
+ # inputs=[file_input],
414
+ # outputs=[upload_status, json_state, radio_button]
415
+ # )
416
 
417
+ # # Handle radio button selection
418
+ # radio_button.change(
419
+ # fn=display_section,
420
+ # inputs=[radio_button, json_state],
421
+ # outputs=[section_output]
422
+ # )
423
 
424
  # Launch the interface
425
+ # iface.launch()
426
 
427
 
428
 
 
441
 
442
 
443
  # # Create Gradio interface
444
+ with gr.Blocks() as iface:
445
+ gr.Markdown("# Exam Question Viewer")
446
 
447
+ with gr.Row():
448
+ file_input = gr.File(
449
+ label="Upload JSON Exam File",
450
+ file_types=[".json"]
451
+ )
452
 
453
+ with gr.Row():
454
+ output_text = gr.Textbox(
455
+ label="Processed Questions",
456
+ lines=20,
457
+ max_lines=30
458
+ )
459
 
460
+ file_input.upload(
461
+ fn=process_json_file,
462
+ inputs=[file_input],
463
+ outputs=[output_text]
464
+ )
465
 
466
  # with gr.Row():
467
  # radio_button = gr.Radio(
 
493
 
494
 
495
  # # Launch the interface
496
+ iface.launch()
497
 
498
 
499