Beispiel #1
0
def new_reserved_lab():

    json = request.get_json(force=True)

    if not validate(
            json,
            'fullName',
            'department',
            'labNumber',
            'deviceQuantity',
            'date',
            'block',
    ):
        abort(400)

    full_name = json['fullName']
    department = json['department']
    lab_number = json['labNumber']
    device_quantity = json['deviceQuantity']
    date = json['date']
    block = json['block']

    reserved_lab = ReservedLabModel(
        full_name=full_name,
        department=department,
        lab_number=lab_number,
        device_quantity=device_quantity,
        date=date,
        block=block,
    )

    reserved_lab.push()

    return ''
Beispiel #2
0
def delete_lab():
    json = request.get_json(force=True)

    if not validate(json, 'labNumber'):
        abort(403)

    LabModel.delete_one(json['labNumber'])

    return ''
Beispiel #3
0
def delete_cart():
    json = request.get_json(force=True)

    if not validate(json, 'cartNumber'):
        abort(403)

    CartModel.delete_one(json['cartNumber'])

    return ''
Beispiel #4
0
def delete_reserved_lab():
    json = request.get_json(force=True)

    if not validate(json, 'labNumber', 'date', 'block'):
        abort(403)

    ReservedLabModel.delete_one(json['labNumber'], json['date'], json['block'])

    return ''
Beispiel #5
0
def delete_teacher():
    json = request.get_json(force=True)

    if not validate(json, 'firstName', 'lastName', 'department'):
        abort(403)

    TeacherModel.delete_one(json['firstName'], json['lastName'],
                            json['department'])

    return ''
Beispiel #6
0
def auth():
    a = request.get_json()
    if validate(a) == True:
        if check(a) == True:
            new_ticket = db_manager.ticket_number()
            return

        else:
            return make_error_message("1")
    else:
        return make_error_message("2")
Beispiel #7
0
def new_lab():

    json = request.get_json(force=True)

    if not validate(json, 'labNumber', 'deviceQuantity'):
        abort(400)

    lab_number = json['labNumber']
    device_quantity = json['deviceQuantity']

    lab = LabModel(lab_number=lab_number, device_quantity=device_quantity)

    lab.push()
    return ''
Beispiel #8
0
def new_cart():

    json = request.get_json(force=True)

    if not validate(json, 'cartNumber', 'type', 'deviceQuantity'):
        abort(400)

    cart_number = json['cartNumber']
    device_type = json['type']
    device_quantity = json['deviceQuantity']
    cart = CartModel(cart_number=cart_number,
                     device_type=device_type,
                     device_quantity=device_quantity)
    cart.push()
    return ''
Beispiel #9
0
def new_teacher():

    json = request.get_json(force=True)

    if not validate(json, 'firstName', 'lastName', 'department'):
        abort(400)

    first_name = json['firstName']
    last_name = json['lastName']
    department = json['department']

    teacher = LabModel(first_name=first_name,
                       last_name=last_name,
                       department=department)

    teacher.push()
    return ''
Beispiel #10
0
def validate():
    data = json.loads(request.data)
    results = utilities.validate(data)
    if results:
        row = results[0]

        if(row[3] == data["password"]):
            # do session stuff
            session.clear()
            if(data["role"] == "student"):
                session['s_id'] = row[0]
            else:
                session['p_id'] = row[0]
            session['name'] = row[1]
            return "Correct"
        else:
            # wrong password, tell user
            session.clear()
            return "Wrong"
    # If we still reach here, it means that the user is not a registered one
    return "Missing"
Beispiel #11
0
def new_reserved_cart():

    json = request.get_json(force=True)

    if not validate(
            json,
            'fullName',
            'department',
            'cartNumber',
            'deviceType',
            'deviceQuantity',
            'date',
            'block',
    ):
        abort(400)

    full_name = json['fullName']
    department = json['department']
    cart_number = json['cartNumber']
    device_type = json['deviceType']
    device_quantity = json['deviceQuantity']
    date = json['date']
    block = json['block']

    reserved_cart = ReservedCartModel(
        full_name=full_name,
        department=department,
        cart_number=cart_number,
        device_type=device_type,
        device_quantity=device_quantity,
        date=date,
        block=block,
    )

    reserved_cart.push()

    return ''
Beispiel #12
0
def main(model_path, model_name, log_path):

    torch.manual_seed(0)

    use_cuda = torch.cuda.is_available()
    device = torch.device("cuda" if use_cuda else "cpu")

    num_gpu = torch.cuda.device_count()
    print("There are {} gups.".format(num_gpu))

    kwargs = {'num_workers': 1, 'pin_memory': True} if use_cuda else {}

    if not os.path.isdir("../data"):
        os.makedirs("../data")

    if not os.path.isdir(model_path):
        os.makedirs(model_path)

    if not os.path.isfile(os.path.join(model_path, 'params.json')):
        with open('./params.json') as json_file:
            params = json.load(json_file)
    else:
        with open(os.path.join(model_path, 'params.json')) as json_file:
            params = json.load(json_file)

    data_params = params['data']
    train_params = params['train']

    dataset = LJSPEECH_MEL(
        '../data',
        params['data'],
        url='https://data.keithito.com/data/speech/LJSpeech-1.1.tar.bz2',
        download=True)

    train_dataset, test_dataset = torch.utils.data.random_split(
        dataset, [12000, 1100])

    train_loader = data.DataLoader(
        dataset=train_dataset,
        batch_size=train_params['batch_size'],
        shuffle=True,
        collate_fn=lambda x: data_mel_processing(x, params['data']),
        **kwargs)

    rand_sampler = torch.utils.data.RandomSampler(test_dataset,
                                                  num_samples=100,
                                                  replacement=True)
    val_loader = data.DataLoader(
        dataset=test_dataset,
        batch_size=1,
        shuffle=False,
        sampler=rand_sampler,
        collate_fn=lambda x: data_mel_processing(x, params['data']),
        **kwargs)

    model = Model(params, device).to(device)

    # if num_gpu > 1:
    #     model = nn.DataParallel(model)

    criteriate = TTSLoss(device).to(device)
    # print(model)
    # for name, param in model.named_parameters():
    #     if param.requires_grad:
    #         print("parameter {} is {}".format(name, param.shape))
    print('Num Model Parameters',
          sum([param.nelement() for param in model.parameters()]))
    epochs = train_params['epochs']
    writer = get_writer(model_path, log_path)
    optimizer = torch.optim.Adam(model.parameters(),
                                 lr=train_params['lr'],
                                 betas=(0.9, 0.98),
                                 eps=1e-09)

    iteration = 0
    model_file = os.path.join(model_path, 'model.pt')
    if os.path.isfile(model_file):
        file = torch.load(model_file)
        iteration = file['iteration']
        optimizer.load_state_dict(file['optimizer'])
        model.load_state_dict(file['model_dict'])
        best_valid_loss = validate(model, criteriate, device, val_loader,
                                   iteration, writer, train_params)
    else:
        best_valid_loss = float("inf")

    for epoch in range(1, epochs + 1):
        iteration = train(model, criteriate, device, train_loader, optimizer,
                          iteration, train_params, writer)
        test_loss = validate(model, criteriate, device, val_loader, iteration,
                             writer, train_params)
        if test_loss < best_valid_loss:
            print("The validation loss is improved by {}, new model is saving".
                  format(best_valid_loss - test_loss))
            best_valid_loss = test_loss
            save_model(model, optimizer, iteration, params, model_path,
                       model_name)

        if epoch % 10 == 0:
            temp_model_path = os.path.join(model_path, str(iteration))
            os.makedirs(temp_model_path)
            temp_model_name = 'model' + str(iteration) + '.pt'
            save_model(model, optimizer, iteration, params, temp_model_path,
                       temp_model_name)