Spaces:
Build error
Build error
Update app.py
Browse files210 unmodified lines21121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928062 unmodified lines34334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742812 unmodified lines44144244344444544644744844945045145245345445545645745845946046146246346446546646725 unmodified lines493494495496497498499210 unmodified lines# import json# import gradio as gr# def process_json_file(file):# try:# # Read and print the file content for debugging# print("File received:", file.name)# # Read the JSON file# with open(file.name, 'r') as f:# print("Reading file contents...")# json_data = json.load(f)# # Print the structure of the JSON data# print("\nJSON Structure:")# print("Available sections:", list(json_data.keys()))# # Process each section# output_text = ""# # Process header if exists# if 'header' in json_data:# output_text += "=== EXAM DETAILS ===\n"# for key, value in json_data['header'].items():# output_text += f"{key.replace('_', ' ').title()}: {value}\n"# output_text += "\n"# # Process multiple choice questions# if 'multiple_choice_questions' in json_data:# output_text += "=== MULTIPLE CHOICE QUESTIONS ===\n"# for q_num, q_data in json_data['multiple_choice_questions'].items():# output_text += f"\nQuestion {q_num.replace('question', '')}:\n"# output_text += f"{q_data['question']}\n"# for opt_key, opt_val in q_data['options'].items():# output_text += f"{opt_key}) {opt_val}\n"# output_text += f"Answer: {q_data['answer']}\n"# # Process short answer questions# if 'short_answer_questions' in json_data:# output_text += "\n=== SHORT ANSWER QUESTIONS ===\n"# for q_num, q_data in json_data['short_answer_questions'].items():# output_text += f"\nQuestion {q_num.replace('question', '')}:\n"# output_text += f"{q_data['question']}\n"# output_text += f"Answer: {q_data['answer']}\n"# # Process long answer questions# if 'long_answer_questions' in json_data:# output_text += "\n=== LONG ANSWER QUESTIONS ===\n"# for q_num, q_data in json_data['long_answer_questions'].items():# output_text += f"\nQuestion {q_num.replace('question', '')}:\n"# output_text += f"{q_data['question']}\n"# output_text += f"Answer: {q_data['answer']}\n"# print("\nProcessing complete!")# return output_text# except json.JSONDecodeError as e:# error_msg = f"Error decoding JSON: {str(e)}"# print(error_msg)# return error_msg# except Exception as e:# error_msg = f"Error processing file: {str(e)}"# print(error_msg)# return error_msg# def display_section(section_type, json_data):# if not json_data or section_type not in json_data:62 unmodified lines# iface.launch()import jsonimport gradio as grdef process_json_file(file):try:print("File received:", file.name)with open(file.name, 'r') as f:json_data = json.load(f)print("File loaded successfully!")return "File uploaded successfully!", json_data, gr.Radio.update(visible=True, interactive=True)except Exception as e:return f"Error: {str(e)}", None, gr.Radio.update(visible=False, interactive=False)def display_section(section_type, json_data):if not json_data or section_type not in json_data:return "Please upload a valid JSON file first"output_text = f"\n=== {section_type.upper().replace('_', ' ')} ===\n"questions = json_data[section_type]for q_num, q_data in questions.items():output_text += f"\nQuestion {q_num.replace('question', '')}:\n"output_text += f"{q_data['question']}\n"if 'options' in q_data:for opt_key, opt_val in q_data['options'].items():output_text += f"{opt_key}) {opt_val}\n"output_text += f"Answer: {q_data['answer']}\n"output_text += "-" * 50 + "\n"return output_text# Create Gradio interfacewith gr.Blocks() as iface:gr.Markdown("# Exam Question Viewer")# Store JSON data in statejson_state = gr.State(None)with gr.Row():file_input = gr.File(label="Upload JSON Exam File",file_types=[".json"])upload_status = gr.Textbox(label="Upload Status")with gr.Row():radio_button = gr.Radio(choices=["multiple_choice_questions", "long_answer_questions", "short_answer_questions"],label="Select Question Type",visible=False, # Initially hiddeninteractive=False # Initially disabled)with gr.Row():section_output = gr.Textbox(label="Selected Questions",lines=20,max_lines=30)# Handle file uploadfile_input.upload(fn=process_json_file,inputs=[file_input],outputs=[upload_status, json_state, radio_button])# Handle radio button selectionradio_button.change(fn=display_section,inputs=[radio_button, json_state],outputs=[section_output])# Launch the interfaceiface.launch()12 unmodified lines# # Create Gradio interface# with gr.Blocks() as iface:# gr.Markdown("# Exam Question Viewer")# with gr.Row():# file_input = gr.File(# label="Upload JSON Exam File",# file_types=[".json"]# )# with gr.Row():# output_text = gr.Textbox(# label="Processed Questions",# lines=20,# max_lines=30# )# file_input.upload(# fn=process_json_file,# inputs=[file_input],# outputs=[output_text]# )# with gr.Row():# radio_button = gr.Radio(25 unmodified lines# # Launch the interface# iface.launch()210 unmodified lines21121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928062 unmodified lines34334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742812 unmodified lines44144244344444544644744844945045145245345445545645745845946046146246346446546646725 unmodified lines493494495496497498499210 unmodified linesimport jsonimport gradio as grdef process_json_file(file):try:# Read and print the file content for debuggingprint("File received:", file.name)# Read the JSON filewith open(file.name, 'r') as f:print("Reading file contents...")json_data = json.load(f)# Print the structure of the JSON dataprint("\nJSON Structure:")print("Available sections:", list(json_data.keys()))# Process each sectionoutput_text = ""# Process header if existsif 'header' in json_data:output_text += "=== EXAM DETAILS ===\n"for key, value in json_data['header'].items():output_text += f"{key.replace('_', ' ').title()}: {value}\n"output_text += "\n"# Process multiple choice questionsif 'multiple_choice_questions' in json_data:output_text += "=== MULTIPLE CHOICE QUESTIONS ===\n"for q_num, q_data in json_data['multiple_choice_questions'].items():output_text += f"\nQuestion {q_num.replace('question', '')}:\n"output_text += f"{q_data['question']}\n"for opt_key, opt_val in q_data['options'].items():output_text += f"{opt_key}) {opt_val}\n"output_text += f"Answer: {q_data['answer']}\n"# Process short answer questionsif 'short_answer_questions' in json_data:output_text += "\n=== SHORT ANSWER QUESTIONS ===\n"for q_num, q_data in json_data['short_answer_questions'].items():output_text += f"\nQuestion {q_num.replace('question', '')}:\n"output_text += f"{q_data['question']}\n"output_text += f"Answer: {q_data['answer']}\n"# Process long answer questionsif 'long_answer_questions' in json_data:output_text += "\n=== LONG ANSWER QUESTIONS ===\n"for q_num, q_data in json_data['long_answer_questions'].items():output_text += f"\nQuestion {q_num.replace('question', '')}:\n"output_text += f"{q_data['question']}\n"output_text += f"Answer: {q_data['answer']}\n"print("\nProcessing complete!")return output_textexcept json.JSONDecodeError as e:error_msg = f"Error decoding JSON: {str(e)}"print(error_msg)return error_msgexcept Exception as e:error_msg = f"Error processing file: {str(e)}"print(error_msg)return error_msg# def display_section(section_type, json_data):# if not json_data or section_type not in json_data:62 unmodified lines# iface.launch()# import json# import gradio as gr# def process_json_file(file):# try:# print("File received:", file.name)# with open(file.name, 'r') as f:# json_data = json.load(f)# print("File loaded successfully!")# return "File uploaded successfully!", json_data, gr.Radio.update(visible=True, interactive=True)# except Exception as e:# return f"Error: {str(e)}", None, gr.Radio.update(visible=False, interactive=False)# def display_section(section_type, json_data):# if not json_data or section_type not in json_data:# return "Please upload a valid JSON file first"# output_text = f"\n=== {section_type.upper().replace('_', ' ')} ===\n"# questions = json_data[section_type]# for q_num, q_data in questions.items():# output_text += f"\nQuestion {q_num.replace('question', '')}:\n"# output_text += f"{q_data['question']}\n"# if 'options' in q_data:# for opt_key, opt_val in q_data['options'].items():# output_text += f"{opt_key}) {opt_val}\n"# output_text += f"Answer: {q_data['answer']}\n"# output_text += "-" * 50 + "\n"# return output_text# Create Gradio interface# with gr.Blocks() as iface:# gr.Markdown("# Exam Question Viewer")# # Store JSON data in state# json_state = gr.State(None)# with gr.Row():# file_input = gr.File(# label="Upload JSON Exam File",# file_types=[".json"]# )# upload_status = gr.Textbox(label="Upload Status")# with gr.Row():# radio_button = gr.Radio(# choices=["multiple_choice_questions", "long_answer_questions", "short_answer_questions"],# label="Select Question Type",# visible=False, # Initially hidden# interactive=False # Initially disabled# )# with gr.Row():# section_output = gr.Textbox(# label="Selected Questions",# lines=20,# max_lines=30# )# # Handle file upload# file_input.upload(# fn=process_json_file,# inputs=[file_input],# outputs=[upload_status, json_state, radio_button]# )# # Handle radio button selection# radio_button.change(# fn=display_section,# inputs=[radio_button, json_state],# outputs=[section_output]# )# Launch the interface# iface.launch()12 unmodified lines# # Create Gradio interfacewith gr.Blocks() as iface:gr.Markdown("# Exam Question Viewer")with gr.Row():file_input = gr.File(label="Upload JSON Exam File",file_types=[".json"])with gr.Row():output_text = gr.Textbox(label="Processed Questions",lines=20,max_lines=30)file_input.upload(fn=process_json_file,inputs=[file_input],outputs=[output_text])# with gr.Row():# radio_button = gr.Radio(25 unmodified lines# # Launch the interfaceiface.launch()
app.py+134-134