Spaces:
Build error
Build error
Create data_processing.py
Browse files- data_processing.py +90 -0
data_processing.py
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
import gradio as gr
|
| 3 |
+
|
| 4 |
+
def process_json_file(file):
|
| 5 |
+
try:
|
| 6 |
+
# Read and print the file content for debugging
|
| 7 |
+
print("File received:", file.name)
|
| 8 |
+
|
| 9 |
+
# Read the JSON file
|
| 10 |
+
with open(file.name, 'r') as f:
|
| 11 |
+
print("Reading file contents...")
|
| 12 |
+
json_data = json.load(f)
|
| 13 |
+
|
| 14 |
+
# Print the structure of the JSON data
|
| 15 |
+
print("\nJSON Structure:")
|
| 16 |
+
print("Available sections:", list(json_data.keys()))
|
| 17 |
+
|
| 18 |
+
# Process each section
|
| 19 |
+
output_text = ""
|
| 20 |
+
|
| 21 |
+
# Process header if exists
|
| 22 |
+
if 'header' in json_data:
|
| 23 |
+
output_text += "=== EXAM DETAILS ===\n"
|
| 24 |
+
for key, value in json_data['header'].items():
|
| 25 |
+
output_text += f"{key.replace('_', ' ').title()}: {value}\n"
|
| 26 |
+
output_text += "\n"
|
| 27 |
+
|
| 28 |
+
# Process multiple choice questions
|
| 29 |
+
if 'multiple_choice_questions' in json_data:
|
| 30 |
+
output_text += "=== MULTIPLE CHOICE QUESTIONS ===\n"
|
| 31 |
+
for q_num, q_data in json_data['multiple_choice_questions'].items():
|
| 32 |
+
output_text += f"\nQuestion {q_num.replace('question', '')}:\n"
|
| 33 |
+
output_text += f"{q_data['question']}\n"
|
| 34 |
+
for opt_key, opt_val in q_data['options'].items():
|
| 35 |
+
output_text += f"{opt_key}) {opt_val}\n"
|
| 36 |
+
output_text += f"Answer: {q_data['answer']}\n"
|
| 37 |
+
|
| 38 |
+
# Process short answer questions
|
| 39 |
+
if 'short_answer_questions' in json_data:
|
| 40 |
+
output_text += "\n=== SHORT ANSWER QUESTIONS ===\n"
|
| 41 |
+
for q_num, q_data in json_data['short_answer_questions'].items():
|
| 42 |
+
output_text += f"\nQuestion {q_num.replace('question', '')}:\n"
|
| 43 |
+
output_text += f"{q_data['question']}\n"
|
| 44 |
+
output_text += f"Answer: {q_data['answer']}\n"
|
| 45 |
+
|
| 46 |
+
# Process long answer questions
|
| 47 |
+
if 'long_answer_questions' in json_data:
|
| 48 |
+
output_text += "\n=== LONG ANSWER QUESTIONS ===\n"
|
| 49 |
+
for q_num, q_data in json_data['long_answer_questions'].items():
|
| 50 |
+
output_text += f"\nQuestion {q_num.replace('question', '')}:\n"
|
| 51 |
+
output_text += f"{q_data['question']}\n"
|
| 52 |
+
output_text += f"Answer: {q_data['answer']}\n"
|
| 53 |
+
|
| 54 |
+
print("\nProcessing complete!")
|
| 55 |
+
return output_text
|
| 56 |
+
|
| 57 |
+
except json.JSONDecodeError as e:
|
| 58 |
+
error_msg = f"Error decoding JSON: {str(e)}"
|
| 59 |
+
print(error_msg)
|
| 60 |
+
return error_msg
|
| 61 |
+
except Exception as e:
|
| 62 |
+
error_msg = f"Error processing file: {str(e)}"
|
| 63 |
+
print(error_msg)
|
| 64 |
+
return error_msg
|
| 65 |
+
|
| 66 |
+
# Create Gradio interface
|
| 67 |
+
with gr.Blocks() as iface:
|
| 68 |
+
gr.Markdown("# Exam Question Viewer")
|
| 69 |
+
|
| 70 |
+
with gr.Row():
|
| 71 |
+
file_input = gr.File(
|
| 72 |
+
label="Upload JSON Exam File",
|
| 73 |
+
file_types=[".json"]
|
| 74 |
+
)
|
| 75 |
+
|
| 76 |
+
with gr.Row():
|
| 77 |
+
output_text = gr.Textbox(
|
| 78 |
+
label="Processed Questions",
|
| 79 |
+
lines=20,
|
| 80 |
+
max_lines=30
|
| 81 |
+
)
|
| 82 |
+
|
| 83 |
+
file_input.upload(
|
| 84 |
+
fn=process_json_file,
|
| 85 |
+
inputs=[file_input],
|
| 86 |
+
outputs=[output_text]
|
| 87 |
+
)
|
| 88 |
+
|
| 89 |
+
# Launch the interface
|
| 90 |
+
iface.launch()
|