barcegab/spatial_test
0
1import gradio as gr2import json3import bisect4import time5import threading6 7# Define the questions with a single image for options8with open('spatial/questions.json', 'r') as q_file:9 questions = json.load(q_file)10 11with open('spatial/answers.json', 'r') as a_file:12 answers = json.load(a_file)13 14with open('spatial/distribution.json', 'r') as file:15 distribution_data = json.load(file)16 17def calculate_percentile(correct_answers, distribution):18 # Sort the distribution by the number of correct answers19 distribution.sort(key=lambda x: x[0])20 21 # Separate the correct answers and percentiles into two lists22 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 inserted26 index = bisect.bisect_left(scores, correct_answers)27 28 # If the exact score is found in the distribution29 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 distribution33 if index == len(scores):34 return 100.035 36 # If the score is between two values in the distribution, interpolate37 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 interpolation42 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 distribution48 return 0.049# Function to evaluate the answer50def evaluate_answer(*responses):51 score = 052 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 += 163 percentile = calculate_percentile(score, distribution)64 return f"Your score: {percentile}: correct answers: {score}"65 66# Create a Gradio interface67 68with 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 interface97if __name__ == "__main__":98 #app = create_interface()99 demo.launch()100 