import gradio as gr import json import bisect # Load questions and answers from JSON files with open('Verbal/questions.json', 'r') as q_file: questions = json.load(q_file) with open('Verbal/answers.json', 'r') as a_file: answers = json.load(a_file) with open('Verbal/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 def grade_test(*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 Gradio interface def create_interface(): inputs = [] for i, question in enumerate(questions): inputs.append(gr.Radio(choices=question['options'], label=question['question'])) return gr.Interface( fn=grade_test, inputs=inputs, outputs="text", live=False, title="Verbal Test", description="Answer the following questions and click 'Submit' to see your score." ) if __name__ == "__main__": interface = create_interface() interface.launch()