Spaces:
Runtime error
Runtime error
upload app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import json
|
| 3 |
+
import bisect
|
| 4 |
+
|
| 5 |
+
# Load questions and answers from JSON files
|
| 6 |
+
with open('numeric/questions.json', 'r') as q_file:
|
| 7 |
+
questions = json.load(q_file)
|
| 8 |
+
|
| 9 |
+
with open('numeric/answers.json', 'r') as a_file:
|
| 10 |
+
answers = json.load(a_file)
|
| 11 |
+
|
| 12 |
+
with open('numeric/distribution.json', 'r') as file:
|
| 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])
|
| 18 |
+
|
| 19 |
+
# Separate the correct answers and percentiles into two lists
|
| 20 |
+
scores = [item[0] for item in distribution]
|
| 21 |
+
percentiles = [item[1] for item in distribution]
|
| 22 |
+
|
| 23 |
+
# Find the index where the given correct_answers should be inserted
|
| 24 |
+
index = bisect.bisect_left(scores, correct_answers)
|
| 25 |
+
|
| 26 |
+
# If the exact score is found in the distribution
|
| 27 |
+
if index < len(scores) and scores[index] == correct_answers:
|
| 28 |
+
return percentiles[index]
|
| 29 |
+
|
| 30 |
+
# If the score is beyond the highest score in the distribution
|
| 31 |
+
if index == len(scores):
|
| 32 |
+
return 100.0
|
| 33 |
+
|
| 34 |
+
# If the score is between two values in the distribution, interpolate
|
| 35 |
+
if index > 0:
|
| 36 |
+
lower_score, lower_percentile = scores[index-1], percentiles[index-1]
|
| 37 |
+
upper_score, upper_percentile = scores[index], percentiles[index]
|
| 38 |
+
|
| 39 |
+
# Linear interpolation
|
| 40 |
+
slope = (upper_percentile - lower_percentile) / (upper_score - lower_score)
|
| 41 |
+
interpolated_percentile = lower_percentile + slope * (correct_answers - lower_score)
|
| 42 |
+
|
| 43 |
+
return round(interpolated_percentile, 2)
|
| 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 |
+
|
| 53 |
+
if any(response is None for response in responses):
|
| 54 |
+
indices = [index for index, value in enumerate(responses) if value is None]
|
| 55 |
+
indices = [value + 1 for value in indices]
|
| 56 |
+
raise gr.Error(f"No se puede quedar ningun valor en 0. Preguntas sin respuesta:{indices}")
|
| 57 |
+
|
| 58 |
+
for i, response in enumerate(responses):
|
| 59 |
+
response_parsed = response.split('.')[0]
|
| 60 |
+
if response_parsed == answers[str(i)]:
|
| 61 |
+
score += 1
|
| 62 |
+
percentile = calculate_percentile(score, distribution)
|
| 63 |
+
return f"Your score: {percentile}: correct answers: {score}"
|
| 64 |
+
|
| 65 |
+
# Create Gradio interface
|
| 66 |
+
demo = gr.Blocks(title="Numerical Test")
|
| 67 |
+
|
| 68 |
+
with demo:
|
| 69 |
+
inputs = []
|
| 70 |
+
for i, question in enumerate(questions):
|
| 71 |
+
inputs.append(gr.Radio(choices=question['options'], label=question['question']))
|
| 72 |
+
|
| 73 |
+
|
| 74 |
+
gr.Image(value="numeric/questions39.png", show_label=False, height=700, width=350)
|
| 75 |
+
|
| 76 |
+
inputs.append(gr.Radio(choices=["A. 8", "B. 4", "C. 2", "D. 1", "E. Ninguna de ellas"],
|
| 77 |
+
label="39. Si introduce el número 8, ¿cuántas veces pasará por elpaso 4 para llegar al final?"))
|
| 78 |
+
inputs.append(gr.Radio(choices=["A. 6", "B. 5", "C. 3", "D. 2", "E. Ninguna de ellas"],
|
| 79 |
+
label="40. Si introduce el número 3, ¿Cuál será el número cuando llegue al final?"))
|
| 80 |
+
|
| 81 |
+
output = gr.Textbox()
|
| 82 |
+
gr.Button("Submit").click(grade_test, inputs=inputs, outputs=output)
|
| 83 |
+
|
| 84 |
+
if __name__ == "__main__":
|
| 85 |
+
demo.launch()
|