def predict_label():
    _data = Data()
    labels = _data.labels
    if request.method == 'GET':
        data = _data.test_data
        image_data = get_image_data(data)
        prediction = predict_image(image_data)
        argmax = int(np.argmax(np.array(prediction)[0]))
        return jsonify({labels[argmax]: prediction[0][argmax]})

    elif request.method == 'POST':
        input_data = request.get_json()
        raw_data = input_data["image_data"]
        decoded = base64.b64decode(str(raw_data))
        io_bytes = io.BytesIO(decoded)
        data = Image.open(io_bytes)
        image_data = get_image_data(data)
        prediction = predict_image(image_data)
        argmax = int(np.argmax(np.array(prediction)[0]))
        job_id = data['job_id'] if 'job_id' in input_data.keys(
        ) else get_job_id()
        return jsonify({
            labels[argmax]: prediction[0][argmax],
            'job_id': job_id
        })
async def post_redirect(redirect_path: str, data: Data,
                        background_tasks: BackgroundTasks) -> Dict[str, Any]:
    data.data['job_id'] = get_job_id()
    logger.info(
        f'POST redirect abtest to: /{redirect_path} as {data.data["job_id"]} with group {data.ab_test}'
    )

    if data.ab_test.upper() in ServiceConfigurations.ab_test_group.keys():
        group_alias = ServiceConfigurations.ab_test_group[data.ab_test.upper()]
        customized_redirect_map = {
            group_alias:
            ServiceConfigurations.customized_redirect_map[group_alias]
        }
    else:
        customized_redirect_map = ServiceConfigurations.customized_redirect_map

    if ServiceConfigurations.enqueue:
        store_data_job._save_data_job(data.data, data.data['job_id'],
                                      background_tasks, True)
    async with aiohttp.ClientSession(timeout=aiohttp.ClientTimeout(
            total=2)) as session:
        tasks = [
            asyncio.ensure_future(
                _post_redirect(
                    session,
                    helpers.customized_redirect_builder(
                        k, v, redirect_path, customized_redirect_map),
                    data.data, k))
            for k, v in ServiceConfigurations.urls.items()
            if k in customized_redirect_map.keys()
        ]
        responses = await asyncio.gather(*tasks)
        logger.info(f'responses: {responses}')
        return responses
def predict():
    _data = Data()
    if request.method == 'GET':
        data = _data.test_data
        image_data = get_image_data(data)
        prediction = predict_image(image_data)
        return jsonify({'prediction': prediction})

    elif request.method == 'POST':
        input_data = request.get_json()
        raw_data = input_data["image_data"]
        decoded = base64.b64decode(str(raw_data))
        io_bytes = io.BytesIO(decoded)
        data = Image.open(io_bytes)
        image_data = get_image_data(data)
        prediction = predict_image(image_data)
        job_id = data['job_id'] if 'job_id' in input_data.keys(
        ) else get_job_id()
        return jsonify({'prediction': prediction, 'job_id': job_id})
def predict():
    _data = Data()
    if request.method == "GET":
        data = _data.test_data
        image_data = get_image_data(data)
        prediction = predict_image(image_data)
        return jsonify({"prediction": prediction})

    elif request.method == "POST":
        input_data = request.get_json()
        raw_data = input_data["image_data"]
        decoded = base64.b64decode(str(raw_data))
        io_bytes = io.BytesIO(decoded)
        data = Image.open(io_bytes)
        image_data = get_image_data(data)
        prediction = predict_image(image_data)
        job_id = data["job_id"] if "job_id" in input_data.keys(
        ) else get_job_id()
        return jsonify({"prediction": prediction, "job_id": job_id})
Beispiel #5
0
async def post_redirect(redirect_path: str, data: Data,
                        background_tasks: BackgroundTasks) -> Dict[str, Any]:
    data.data["job_id"] = get_job_id()
    logger.info(f'POST redirect to: /{redirect_path} as {data.data["job_id"]}')
    if ServiceConfigurations.enqueue:
        store_data_job._save_data_job(data.data, data.data["job_id"],
                                      background_tasks, True)
    async with aiohttp.ClientSession(timeout=aiohttp.ClientTimeout(
            total=2)) as session:
        tasks = [
            asyncio.ensure_future(
                _post_redirect(
                    session,
                    helpers.customized_redirect_builder(
                        k, v, redirect_path,
                        ServiceConfigurations.customized_redirect_map),
                    data.data, k))
            for k, v in ServiceConfigurations.urls.items()
        ]
        responses = await asyncio.gather(*tasks)
        logger.info(f"responses: {responses}")
        return responses
async def predict_label(data: Data,
                        background_tasks: BackgroundTasks = BackgroundTasks()):
    job_id = data.job_id if data.job_id is not None else get_job_id()
    return await _predict._predict_label(data, job_id, background_tasks)
Beispiel #7
0
async def predict(data: Data,
                  background_tasks: BackgroundTasks = BackgroundTasks()):
    job_id = data.job_id if data.job_id is not None else get_job_id()
    result = await _predict_image._predict(data, job_id, background_tasks)
    return result
Beispiel #8
0
async def predict_async(data: Data, background_tasks: BackgroundTasks):
    job_id = data.job_id if data.job_id is not None else get_job_id()
    return await _predict._predict_async_post(data, job_id, background_tasks)
Beispiel #9
0
async def predict(data: Data,
                  background_tasks: BackgroundTasks = BackgroundTasks()):
    job_id = data.job_id if data.job_id is not None else get_job_id()
    return await _predict_ab_test._predict(data, job_id, background_tasks,
                                           AB_TEST_GROUP)