ShebMichel commited on
Commit
45dd9d8
·
verified ·
1 Parent(s): 6496a8b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +152 -53
app.py CHANGED
@@ -211,70 +211,152 @@ 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:
@@ -341,6 +423,23 @@ with gr.Blocks() as iface:
341
 
342
  # Launch the interface
343
  iface.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
344
  # # Create Gradio interface
345
  # with gr.Blocks() as iface:
346
  # gr.Markdown("# Exam Question Viewer")
 
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:
281
+ # return "Please upload a valid JSON file first"
282
+
283
+ # output_text = f"\n=== {section_type.upper().replace('_', ' ')} ===\n"
284
+
285
+ # questions = json_data[section_type]
286
+ # for q_num, q_data in questions.items():
287
+ # output_text += f"\nQuestion {q_num.replace('question', '')}:\n"
288
+ # output_text += f"{q_data['question']}\n"
289
 
290
+ # if 'options' in q_data:
291
+ # for opt_key, opt_val in q_data['options'].items():
292
+ # output_text += f"{opt_key}) {opt_val}\n"
293
 
294
+ # output_text += f"Answer: {q_data['answer']}\n"
295
+ # output_text += "-" * 50 + "\n"
296
+
297
+ # return output_text
298
+
299
+ # # Create Gradio interface
300
+ # with gr.Blocks() as iface:
301
+ # gr.Markdown("# Exam Question Viewer")
302
+
303
+ # # Store JSON data in state
304
+ # json_state = gr.State(None)
305
+
306
+ # with gr.Row():
307
+ # file_input = gr.File(
308
+ # label="Upload JSON Exam File",
309
+ # file_types=[".json"]
310
+ # )
311
+ # upload_status = gr.Textbox(label="Upload Status")
312
+
313
+ # with gr.Row():
314
+ # radio_button = gr.Radio(
315
+ # choices=["multiple_choice_questions", "long_answer_questions", "short_answer_questions"],
316
+ # label="Select Question Type",
317
+ # visible=False, # Initially hidden
318
+ # interactive=False # Initially disabled
319
+ # )
320
+
321
+ # with gr.Row():
322
+ # section_output = gr.Textbox(
323
+ # label="Selected Questions",
324
+ # lines=20,
325
+ # max_lines=30
326
+ # )
327
+
328
+ # # Handle file upload
329
+ # file_input.upload(
330
+ # fn=process_json_file,
331
+ # inputs=[file_input],
332
+ # outputs=[upload_status, json_state, radio_button]
333
+ # )
334
+
335
+ # # Handle radio button selection
336
+ # radio_button.change(
337
+ # fn=display_section,
338
+ # inputs=[radio_button, json_state],
339
+ # outputs=[section_output]
340
+ # )
341
+
342
+ # # Launch the interface
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:
 
423
 
424
  # Launch the interface
425
  iface.launch()
426
+
427
+
428
+
429
+
430
+
431
+
432
+
433
+
434
+
435
+
436
+
437
+
438
+
439
+
440
+
441
+
442
+
443
  # # Create Gradio interface
444
  # with gr.Blocks() as iface:
445
  # gr.Markdown("# Exam Question Viewer")