Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,16 +2,18 @@ import gradio as gr
|
|
| 2 |
import json
|
| 3 |
import bisect
|
| 4 |
|
| 5 |
-
|
| 6 |
-
|
|
|
|
| 7 |
questions = json.load(q_file)
|
| 8 |
|
| 9 |
-
with open('
|
| 10 |
answers = json.load(a_file)
|
| 11 |
|
| 12 |
-
with open('
|
| 13 |
distribution_data = json.load(file)
|
| 14 |
|
|
|
|
| 15 |
def calculate_percentile(correct_answers, distribution):
|
| 16 |
# Sort the distribution by the number of correct answers
|
| 17 |
distribution.sort(key=lambda x: x[0])
|
|
@@ -44,9 +46,8 @@ def calculate_percentile(correct_answers, distribution):
|
|
| 44 |
|
| 45 |
# If the score is below the lowest score in the distribution
|
| 46 |
return 0.0
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
def grade_test(*responses):
|
| 50 |
score = 0
|
| 51 |
distribution = [(item['correct_answers'], item['percentile']) for item in distribution_data]
|
| 52 |
|
|
@@ -62,21 +63,34 @@ def grade_test(*responses):
|
|
| 62 |
percentile = calculate_percentile(score, distribution)
|
| 63 |
return f"Your score: {percentile}: correct answers: {score}"
|
| 64 |
|
| 65 |
-
# Create Gradio interface
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
|
| 71 |
-
|
| 72 |
-
fn=grade_test,
|
| 73 |
-
inputs=inputs,
|
| 74 |
-
outputs="text",
|
| 75 |
-
live=False,
|
| 76 |
-
title="Verbal Test",
|
| 77 |
-
description="Answer the following questions and click 'Submit' to see your score."
|
| 78 |
-
)
|
| 79 |
|
|
|
|
| 80 |
if __name__ == "__main__":
|
| 81 |
-
|
| 82 |
-
|
|
|
|
| 2 |
import json
|
| 3 |
import bisect
|
| 4 |
|
| 5 |
+
|
| 6 |
+
# Define the questions with a single image for options
|
| 7 |
+
with open('mechanical/questions.json', 'r') as q_file:
|
| 8 |
questions = json.load(q_file)
|
| 9 |
|
| 10 |
+
with open('mechanical/answers.json', 'r') as a_file:
|
| 11 |
answers = json.load(a_file)
|
| 12 |
|
| 13 |
+
with open('mechanical/distribution.json', 'r') as file:
|
| 14 |
distribution_data = json.load(file)
|
| 15 |
|
| 16 |
+
|
| 17 |
def calculate_percentile(correct_answers, distribution):
|
| 18 |
# Sort the distribution by the number of correct answers
|
| 19 |
distribution.sort(key=lambda x: x[0])
|
|
|
|
| 46 |
|
| 47 |
# If the score is below the lowest score in the distribution
|
| 48 |
return 0.0
|
| 49 |
+
# Function to evaluate the answer
|
| 50 |
+
def evaluate_answer(*responses):
|
|
|
|
| 51 |
score = 0
|
| 52 |
distribution = [(item['correct_answers'], item['percentile']) for item in distribution_data]
|
| 53 |
|
|
|
|
| 63 |
percentile = calculate_percentile(score, distribution)
|
| 64 |
return f"Your score: {percentile}: correct answers: {score}"
|
| 65 |
|
| 66 |
+
# Create a Gradio interface
|
| 67 |
+
|
| 68 |
+
with gr.Blocks() as demo:
|
| 69 |
+
results = []
|
| 70 |
+
for question in questions:
|
| 71 |
+
with gr.Row():
|
| 72 |
+
with gr.Column():
|
| 73 |
+
gr.Image(type="filepath",
|
| 74 |
+
value=question["image"],
|
| 75 |
+
show_download_button=False,
|
| 76 |
+
mirror_webcam=False,
|
| 77 |
+
sources=['clipboard'],
|
| 78 |
+
interactive=False)
|
| 79 |
+
with gr.Column():
|
| 80 |
+
if len(question['options']) == 0:
|
| 81 |
+
options = ["A", "B", "C"]
|
| 82 |
+
else:
|
| 83 |
+
options = question['options']
|
| 84 |
+
|
| 85 |
+
radio = gr.Radio(choices=options,
|
| 86 |
+
label=question['question'])
|
| 87 |
+
results.append(radio)
|
| 88 |
+
submit = gr.Button("Submit")
|
| 89 |
+
output = gr.Textbox(label="Results")
|
| 90 |
|
| 91 |
+
submit.click(evaluate_answer, inputs=results, outputs=output)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
|
| 93 |
+
# Launch the interface
|
| 94 |
if __name__ == "__main__":
|
| 95 |
+
#app = create_interface()
|
| 96 |
+
demo.launch()
|