示例#1
0
    def handle(self, *args, **options):
        """
        Read from file
        """

        parser = SafeConfigParser()
        parser.read(args[0])

        print("Starting import...")
        print("Reading config from file {0}".format(args[0]))

        header_name = "importdata"

        prompt = parser.get(header_name, 'prompt')
        essay_file = parser.get(header_name, 'essay_file')
        essay_limit = int(parser.get(header_name, 'essay_limit'))
        name = parser.get(header_name, "name")
        add_score = parser.get(header_name, "add_grader_object") == "True"
        max_target_scores = json.loads(parser.get(header_name, "max_target_scores"))
        grader_type = parser.get(header_name, "grader_type")

        try:
            User.objects.create_user('vik', '*****@*****.**', 'vik')
        except:
            # User already exists, but doesn't matter to us
            pass

        user = User.objects.get(username='******')
        organization, created = Organization.objects.get_or_create(
            organization_name="edX"
        )

        course, created = Course.objects.get_or_create(
            course_name="edX101",
        )
        if created:
            course.organizations.add(organization)

        user.profile.organization = organization
        user.save()
        course.users.add(user)
        course.save()

        problem, created = Problem.objects.get_or_create(
            prompt=prompt,
            name=name,
        )
        problem.courses.add(course)
        problem.save()

        grades, text = [], []
        combined_raw = open(settings.REPO_PATH / essay_file).read()
        raw_lines = combined_raw.splitlines()
        for row in xrange(1, len(raw_lines)):
            line_split = raw_lines[row].strip().split("\t")
            text.append(line_split[0])
            grades.append(line_split[1:])

        max_scores = []
        for i in xrange(0, len(grades[0])):
            scores_at_point = [g[i] for g in grades]
            max_scores.append(max(scores_at_point))
        problem.max_target_scores = json.dumps(max_scores)
        problem.save()
        for i in range(0, min(essay_limit, len(text))):
            essay = Essay(
                problem=problem,
                user=user,
                essay_type="train",
                essay_text=text[i],
            )

            essay.save()
            score = EssayGrade(
                target_scores=json.dumps(grades[i]),
                feedback="",
                grader_type=grader_type,
                essay=essay,
                success=True,
            )
            score.save()

        print ("Successfully imported {0} essays using configuration in file {1}.".format(
            min(essay_limit, len(text)),
            args[0],
        ))
示例#2
0
def handle_single_essay(essay):
    #Needed to ensure that the DB is not wrapped in a transaction and pulls old data
    transaction.commit_unless_managed()

    #strip out unicode and other characters in student response
    #Needed, or grader may potentially fail
    #TODO: Handle unicode in student responses properly
    student_response = essay.essay_text.encode('ascii', 'ignore')

    #Gets both the max scores for each target and the number of targets
    target_max_scores = json.loads(essay.problem.max_target_scores)
    target_counts = len(target_max_scores)

    target_scores=[]
    for m in xrange(0,target_counts):
        #Gets latest model for a given problem and target
        success, created_model=ml_grading_util.get_latest_created_model(essay.problem,m)

        if not success:
            error_message = "Could not identify a valid created model!"
            log.error(error_message)
            results= RESULT_FAILURE_DICT
            formatted_feedback="error"
            return False, error_message

        #Create grader path from location in submission
        grader_path = os.path.join(settings.ML_MODEL_PATH,created_model.model_relative_path)

        #Indicates whether the model is stored locally or in the cloud
        model_stored_in_s3=created_model.model_stored_in_s3

        #Try to load the model file
        success, grader_data=load_model_file(created_model,use_full_path=False)
        if success:
            #Send to ML grading algorithm to be graded
            results = grade.grade(grader_data, student_response)
        else:
            results=RESULT_FAILURE_DICT

        #If the above fails, try using the full path in the created_model object
        if not results['success'] and not created_model.model_stored_in_s3:
            #Before, we used the relative path to load.  Possible that the full path may work
            grader_path=created_model.model_full_path
            try:
                success, grader_data=load_model_file(created_model,use_full_path=True)
                if success:
                    results = grade.grade(grader_data, student_response)
                else:
                    results=RESULT_FAILURE_DICT
            except:
                error_message="Could not find a valid model file."
                log.exception(error_message)
                results=RESULT_FAILURE_DICT

        if m==0:
            final_results=results
        if results['success'] == False:
            error_message = "Unsuccessful grading: {0}".format(results)
            log.exception(error_message)
            return False, error_message
        target_scores.append(int(results['score']))

    grader_dict = {
        'essay' : essay,
        'target_scores' : json.dumps(target_scores),
        'grader_type' : GraderTypes.machine,
        'feedback' : '',
        'annotated_text' : '',
        'premium_feedback_scores' : json.dumps([]),
        'success' :final_results['success'],
        'confidence' : final_results['confidence'],
        }

    # Create grader object in controller by posting back results
    essay_grade = EssayGrade(**grader_dict)
    essay_grade.save()
    #Update the essay so that it doesn't keep trying to re-grade
    essay.has_been_ml_graded = True
    essay.save()
    transaction.commit_unless_managed()
    return True, "Successfully scored!"
示例#3
0
    def handle(self, *args, **options):
        """
        Read from file
        """

        parser = SafeConfigParser()
        parser.read(args[0])


        print("Starting import...")
        print("Reading config from file {0}".format(args[0]))

        header_name = "importdata"

        prompt = parser.get(header_name, 'prompt')
        essay_file = parser.get(header_name, 'essay_file')
        essay_limit = int(parser.get(header_name, 'essay_limit'))
        name = parser.get(header_name, "name")
        add_score = parser.get(header_name, "add_grader_object") == "True"
        max_target_scores = json.loads(parser.get(header_name, "max_target_scores"))
        grader_type = parser.get(header_name, "grader_type")

        try:
            User.objects.create_user('vik', '*****@*****.**', 'vik')
        except:
            #User already exists, but doesn't matter to us
            pass

        user = User.objects.get(username='******')
        organization, created = Organization.objects.get_or_create(
            organization_name = "edX"
        )

        course, created = Course.objects.get_or_create(
            course_name = "edX101",
        )
        if created:
            course.organizations.add(organization)

        user.profile.organization = organization
        user.save()
        course.users.add(user)
        course.save()

        problem, created = Problem.objects.get_or_create(
            prompt = prompt,
            name = name,
        )
        problem.courses.add(course)
        problem.save()

        grades, text = [], []
        combined_raw = open(settings.REPO_PATH / essay_file).read()
        raw_lines = combined_raw.splitlines()
        for row in xrange(1, len(raw_lines)):
            line_split = raw_lines[row].strip().split("\t")
            text.append(line_split[0])
            grades.append(line_split[1:])

        max_scores = []
        for i in xrange(0,len(grades[0])):
            scores_at_point = [g[i] for g in grades]
            max_scores.append(max(scores_at_point))
        problem.max_target_scores = json.dumps(max_scores)
        problem.save()
        for i in range(0, min(essay_limit, len(text))):
            essay = Essay(
                problem = problem,
                user =user,
                essay_type = "train",
                essay_text = text[i],
            )

            essay.save()
            score = EssayGrade(
                target_scores=json.dumps(grades[i]),
                feedback="",
                grader_type = grader_type,
                essay = essay,
                success = True,
            )
            score.save()

        print ("Successfully imported {0} essays using configuration in file {1}.".format(
            min(essay_limit, len(text)),
            args[0],
        ))
示例#4
0
def handle_single_essay(essay):
    # Needed to ensure that the DB is not wrapped in a transaction and pulls old data
    transaction.commit()

    # strip out unicode and other characters in student response
    # Needed, or grader may potentially fail
    # TODO: Handle unicode in student responses properly
    student_response = essay.essay_text.encode('ascii', 'ignore')

    # Gets both the max scores for each target and the number of targets
    target_max_scores = json.loads(essay.problem.max_target_scores)
    target_counts = len(target_max_scores)

    target_scores = []
    for m in xrange(0, target_counts):
        # Gets latest model for a given problem and target
        success, created_model = ml_grading_util.get_latest_created_model(
            essay.problem, m)

        if not success:
            results = RESULT_FAILURE_DICT
            formatted_feedback = "error"
            transaction.commit()
            return False, formatted_feedback

        # Try to load the model file
        success, grader_data = load_model_file(created_model,
                                               use_full_path=False)
        if success:
            # Send to ML grading algorithm to be graded
            results = grade.grade(grader_data, student_response)
        else:
            results = RESULT_FAILURE_DICT

        # If the above fails, try using the full path in the created_model object
        if not results['success'] and not created_model.model_stored_in_s3:
            try:
                success, grader_data = load_model_file(created_model,
                                                       use_full_path=True)
                if success:
                    results = grade.grade(grader_data, student_response)
                else:
                    results = RESULT_FAILURE_DICT
            except:
                error_message = "Could not find a valid model file."
                log.exception(error_message)
                results = RESULT_FAILURE_DICT

        if m == 0:
            final_results = results
        if results['success'] == False:
            error_message = "Unsuccessful grading: {0}".format(results)
            log.exception(error_message)
            transaction.commit()
            return False, error_message
        target_scores.append(int(results['score']))

    grader_dict = {
        'essay': essay,
        'target_scores': json.dumps(target_scores),
        'grader_type': GraderTypes.machine,
        'feedback': '',
        'annotated_text': '',
        'premium_feedback_scores': json.dumps([]),
        'success': final_results['success'],
        'confidence': final_results['confidence'],
    }

    # Create grader object in controller by posting back results
    essay_grade = EssayGrade(**grader_dict)
    essay_grade.save()
    # Update the essay so that it doesn't keep trying to re-grade
    essay.has_been_ml_graded = True
    essay.save()
    # copy permissions from the essay to the essaygrade
    helpers.copy_permissions(essay, Essay, essay_grade, EssayGrade)
    transaction.commit()
    return True, "Successfully scored!"