def test_ml_creation(self): """ Test to see if an ml model can be created and then if essays can be graded """ # Create 10 training essays that are scored problem_resource_uri = create_ml_problem_and_essays("train", 10) # Get the problem so that we can pass it to ml model generation engine problem = lookup_object(problem_resource_uri) problem_id = problem['id'] problem_model = Problem.objects.get(id=problem_id) # Create the ml model creator_success, message = ml_model_creation.handle_single_problem( problem_model) # Create some test essays and see if the model can score them essay_list = create_ml_essays_only("test", 10, problem_resource_uri) # Lookup the first essay and try to score it essay = lookup_object(essay_list[0]) essay_id = essay['id'] essay_model = Essay.objects.get(id=essay_id) # Try to score the essay grader_success, message = ml_grader.handle_single_essay(essay_model) self.assertEqual(creator_success, settings.FOUND_ML) self.assertEqual(grader_success, settings.FOUND_ML)
def test_ml_creation(self): """ Test to see if an ml model can be created and then if essays can be graded """ #Create 10 training essays that are scored problem_resource_uri = create_ml_problem_and_essays("train",10) #Get the problem so that we can pass it to ml model generation engine problem = lookup_object(problem_resource_uri) problem_id = problem['id'] problem_model = Problem.objects.get(id=problem_id) #Create the ml model creator_success, message = ml_model_creation.handle_single_problem(problem_model) #Create some test essays and see if the model can score them essay_list = create_ml_essays_only("test",10, problem_resource_uri) #Lookup the first essay and try to score it essay = lookup_object(essay_list[0]) essay_id = essay['id'] essay_model = Essay.objects.get(id=essay_id) #Try to score the essay grader_success, message = ml_grader.handle_single_essay(essay_model) self.assertEqual(creator_success, settings.FOUND_ML) self.assertEqual(grader_success, settings.FOUND_ML)
def grade_ml_essays(problem): """ Called by grade_ml. Handles a single grading task for a single essay. """ transaction.commit_unless_managed() lock_id = "celery-essay-grading-{0}".format(problem.id) acquire_lock = lambda: cache.add(lock_id, "true", settings.GRADING_CACHE_LOCK_TIME) release_lock = lambda: cache.delete(lock_id) if acquire_lock(): try: essays = Essay.objects.filter(problem=problem, has_been_ml_graded=False) #TODO: Grade essays in batches so ml model doesn't have to be loaded every single time (or cache the model files) for essay in essays: handle_single_essay(essay) finally: release_lock()
def grade_ml_single_essay(essay): """ Called by grade_ml. Handles a single grading task for a single essay. """ transaction.commit_unless_managed() handle_single_essay(essay)