Spaces:
Build error
Build error
Update app.py
Browse files
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 |
-
|
| 148 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 149 |
except Exception as e:
|
| 150 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 151 |
|
| 152 |
-
def
|
| 153 |
-
if not json_data or
|
| 154 |
-
return "
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 201 |
|
| 202 |
# Handle file upload
|
| 203 |
file_input.upload(
|
| 204 |
fn=process_json_file,
|
| 205 |
inputs=[file_input],
|
| 206 |
-
outputs=[
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 207 |
)
|
| 208 |
|
| 209 |
-
# Handle radio button
|
| 210 |
-
|
| 211 |
-
fn=
|
| 212 |
-
inputs=[
|
| 213 |
-
outputs=[
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
|