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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +108 -49
app.py CHANGED
@@ -143,31 +143,59 @@ def process_json_file(file):
143
  json_data = json.load(f)
144
 
145
  print("File loaded successfully!")
146
-
147
- # Return both the status message and the JSON data
148
- return "File uploaded successfully!", json_data, gr.Radio.update(visible=True, interactive=True)
 
 
 
 
 
149
  except Exception as e:
150
- return f"Error: {str(e)}", None, gr.Radio.update(visible=False, interactive=False)
 
 
 
 
 
 
151
 
152
- def display_section(section_type, json_data):
153
- if not json_data or section_type not in json_data:
154
- return "Please upload a valid JSON file first"
155
-
156
- output_text = f"\n=== {section_type.upper().replace('_', ' ')} ===\n"
157
-
158
- questions = json_data[section_type]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
159
  for q_num, q_data in questions.items():
160
  output_text += f"\nQuestion {q_num.replace('question', '')}:\n"
161
  output_text += f"{q_data['question']}\n"
162
-
163
- # Handle multiple choice questions differently
164
- if 'options' in q_data:
165
- for opt_key, opt_val in q_data['options'].items():
166
- output_text += f"{opt_key}) {opt_val}\n"
167
-
168
  output_text += f"Answer: {q_data['answer']}\n"
169
  output_text += "-" * 50 + "\n"
170
-
171
  return output_text
172
 
173
  # Create Gradio interface
@@ -185,32 +213,75 @@ with gr.Blocks() as iface:
185
  upload_status = gr.Textbox(label="Upload Status")
186
 
187
  with gr.Row():
188
- radio_button = gr.Radio(
189
- choices=["multiple_choice_questions", "long_answer_questions", "short_answer_questions"],
190
- label="Select Question Type",
191
- visible=False, # Initially hidden
192
- interactive=False # Initially disabled
193
- )
194
-
195
- with gr.Row():
196
- section_output = gr.Textbox(
197
- label="Selected Questions",
198
- lines=20,
199
- max_lines=30
200
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
201
 
202
  # Handle file upload
203
  file_input.upload(
204
  fn=process_json_file,
205
  inputs=[file_input],
206
- outputs=[upload_status, json_state, radio_button]
 
 
 
 
 
 
207
  )
208
 
209
- # Handle radio button selection
210
- radio_button.change(
211
- fn=display_section,
212
- inputs=[radio_button, json_state],
213
- outputs=[section_output]
 
 
 
 
 
 
 
 
 
 
 
 
214
  )
215
 
216
  # Launch the interface
@@ -235,18 +306,6 @@ iface.launch()
235
 
236
 
237
 
238
-
239
-
240
-
241
-
242
-
243
-
244
-
245
-
246
-
247
-
248
-
249
-
250
 
251
 
252
 
 
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
 
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
 
306
 
307
 
308
 
 
 
 
 
 
 
 
 
 
 
 
 
309
 
310
 
311