File size: 3,822 Bytes
a08dd7a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import gradio as gr
import json
import bisect
import time
import threading

# Define the questions with a single image for options
with open('spatial/questions.json', 'r') as q_file:
    questions = json.load(q_file)

with open('spatial/answers.json', 'r') as a_file:
    answers = json.load(a_file)

with open('spatial/distribution.json', 'r') as file:
    distribution_data = json.load(file)

def calculate_percentile(correct_answers, distribution):
    # Sort the distribution by the number of correct answers
    distribution.sort(key=lambda x: x[0])
    
    # Separate the correct answers and percentiles into two lists
    scores = [item[0] for item in distribution]
    percentiles = [item[1] for item in distribution]
    
    # Find the index where the given correct_answers should be inserted
    index = bisect.bisect_left(scores, correct_answers)
    
    # If the exact score is found in the distribution
    if index < len(scores) and scores[index] == correct_answers:
        return percentiles[index]
    
    # If the score is beyond the highest score in the distribution
    if index == len(scores):
        return 100.0
    
    # If the score is between two values in the distribution, interpolate
    if index > 0:
        lower_score, lower_percentile = scores[index-1], percentiles[index-1]
        upper_score, upper_percentile = scores[index], percentiles[index]
        
        # Linear interpolation
        slope = (upper_percentile - lower_percentile) / (upper_score - lower_score)
        interpolated_percentile = lower_percentile + slope * (correct_answers - lower_score)
        
        return round(interpolated_percentile, 2)
    
    # If the score is below the lowest score in the distribution
    return 0.0
# Function to evaluate the answer
def evaluate_answer(*responses):
    score = 0
    distribution = [(item['correct_answers'], item['percentile']) for item in distribution_data]

    if any(response is None for response in responses):
        indices = [index for index, value in enumerate(responses) if value is None]
        indices = [value + 1  for value in indices]
        raise gr.Error(f"No se puede quedar ningun valor en 0. Preguntas sin respuesta:{indices}")
    
    for i, response in enumerate(responses):
        response_parsed = response.split('.')[0]
        if response_parsed == answers[str(i)]:
            score += 1
    percentile = calculate_percentile(score, distribution)
    return f"Your score: {percentile}: correct answers: {score}"

# Create a Gradio interface

with gr.Blocks() as demo:
    results = []
    for question in questions:
        with gr.Row():
            with gr.Column():
                gr.Image(type="filepath", 
                             label=f"Question {questions.index(question) + 1}", 
                             value=question["question"],
                             show_download_button=False,
                             mirror_webcam=False,
                             sources=['clipboard'],
                             interactive=False)
            with gr.Column():
                gr.Image(type="filepath", label="Options", 
                                value=question["options"],
                                mirror_webcam=False,
                                show_download_button=False,
                                sources=['clipboard'],
                                interactive=False)
            radio = gr.Radio(choices=["A", "B", "C", "D"], 
                            label="Choose an option")
            results.append(radio)
    submit = gr.Button("Submit")
    output = gr.Textbox(label="Results")
    
    submit.click(evaluate_answer, inputs=results, outputs=output)
    

# Launch the interface
if __name__ == "__main__":
    #app = create_interface()
    demo.launch()