Spaces:
Sleeping
Sleeping
Upload app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import json
|
| 3 |
+
import bisect
|
| 4 |
+
import time
|
| 5 |
+
import threading
|
| 6 |
+
|
| 7 |
+
# Define the questions with a single image for options
|
| 8 |
+
with open('spatial/questions.json', 'r') as q_file:
|
| 9 |
+
questions = json.load(q_file)
|
| 10 |
+
|
| 11 |
+
with open('spatial/answers.json', 'r') as a_file:
|
| 12 |
+
answers = json.load(a_file)
|
| 13 |
+
|
| 14 |
+
with open('spatial/distribution.json', 'r') as file:
|
| 15 |
+
distribution_data = json.load(file)
|
| 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])
|
| 20 |
+
|
| 21 |
+
# Separate the correct answers and percentiles into two lists
|
| 22 |
+
scores = [item[0] for item in distribution]
|
| 23 |
+
percentiles = [item[1] for item in distribution]
|
| 24 |
+
|
| 25 |
+
# Find the index where the given correct_answers should be inserted
|
| 26 |
+
index = bisect.bisect_left(scores, correct_answers)
|
| 27 |
+
|
| 28 |
+
# If the exact score is found in the distribution
|
| 29 |
+
if index < len(scores) and scores[index] == correct_answers:
|
| 30 |
+
return percentiles[index]
|
| 31 |
+
|
| 32 |
+
# If the score is beyond the highest score in the distribution
|
| 33 |
+
if index == len(scores):
|
| 34 |
+
return 100.0
|
| 35 |
+
|
| 36 |
+
# If the score is between two values in the distribution, interpolate
|
| 37 |
+
if index > 0:
|
| 38 |
+
lower_score, lower_percentile = scores[index-1], percentiles[index-1]
|
| 39 |
+
upper_score, upper_percentile = scores[index], percentiles[index]
|
| 40 |
+
|
| 41 |
+
# Linear interpolation
|
| 42 |
+
slope = (upper_percentile - lower_percentile) / (upper_score - lower_score)
|
| 43 |
+
interpolated_percentile = lower_percentile + slope * (correct_answers - lower_score)
|
| 44 |
+
|
| 45 |
+
return round(interpolated_percentile, 2)
|
| 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 |
+
|
| 54 |
+
if any(response is None for response in responses):
|
| 55 |
+
indices = [index for index, value in enumerate(responses) if value is None]
|
| 56 |
+
indices = [value + 1 for value in indices]
|
| 57 |
+
raise gr.Error(f"No se puede quedar ningun valor en 0. Preguntas sin respuesta:{indices}")
|
| 58 |
+
|
| 59 |
+
for i, response in enumerate(responses):
|
| 60 |
+
response_parsed = response.split('.')[0]
|
| 61 |
+
if response_parsed == answers[str(i)]:
|
| 62 |
+
score += 1
|
| 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 |
+
label=f"Question {questions.index(question) + 1}",
|
| 75 |
+
value=question["question"],
|
| 76 |
+
show_download_button=False,
|
| 77 |
+
mirror_webcam=False,
|
| 78 |
+
sources=['clipboard'],
|
| 79 |
+
interactive=False)
|
| 80 |
+
with gr.Column():
|
| 81 |
+
gr.Image(type="filepath", label="Options",
|
| 82 |
+
value=question["options"],
|
| 83 |
+
mirror_webcam=False,
|
| 84 |
+
show_download_button=False,
|
| 85 |
+
sources=['clipboard'],
|
| 86 |
+
interactive=False)
|
| 87 |
+
radio = gr.Radio(choices=["A", "B", "C", "D"],
|
| 88 |
+
label="Choose an option")
|
| 89 |
+
results.append(radio)
|
| 90 |
+
submit = gr.Button("Submit")
|
| 91 |
+
output = gr.Textbox(label="Results")
|
| 92 |
+
|
| 93 |
+
submit.click(evaluate_answer, inputs=results, outputs=output)
|
| 94 |
+
|
| 95 |
+
|
| 96 |
+
# Launch the interface
|
| 97 |
+
if __name__ == "__main__":
|
| 98 |
+
#app = create_interface()
|
| 99 |
+
demo.launch()
|