Exemple #1
0
 async def delete(self, request):
     try:
         note = await Note.objects.get(id=request.path_params.get("id"))
         await note.delete()
         return UJSONResponse({"success": True})
     except NoMatch:
         return UJSONResponse({"error": "not found"}, status_code=404)
Exemple #2
0
async def homepage(request):
    global generate_count
    global sess

    if request.method == 'GET':
        params = request.query_params
    elif request.method == 'POST':
        params = await request.json()
    elif request.method == 'HEAD':
        return UJSONResponse({'text': ''},
                             headers=response_header)
    
    gpt2.load_gpt2(sess, run_name=params.get('run_name', ''))

    text = gpt2.generate(sess,
                         run_name=params.get('run_name', ''),
                         length=int(params.get('length', 1023)),
                         temperature=float(params.get('temperature', 0.7)),
                         top_k=int(params.get('top_k', 0)),
                         top_p=float(params.get('top_p', 0)),
                         prefix=params.get('prefix', '')[:500],
                         truncate=params.get('truncate', None),
                         include_prefix=str(params.get('include_prefix', True)).lower() == 'true',
                         return_as_list=True)[0]

    sess = gpt2.reset_session(sess)

    gc.collect()
    return UJSONResponse({'text': text},
                         headers=response_header)
Exemple #3
0
async def api_create_build(request: Request):
    env_var_dict = await request.json()
    try:
        create_build(env_var_dict)
        return UJSONResponse({"success": "true"})
    except Exception as e:
        return UJSONResponse({"success": "false", "error": str(e)})
Exemple #4
0
async def homepage(request):
    # global generate_count
    # global sess

    if request.method == 'GET':
        params = request.query_params
    elif request.method == 'POST':
        params = await request.json()
    elif request.method == 'HEAD':
        return UJSONResponse({'text': ''},
                             headers=response_header)

    text = interact_model(
        input_text=params.get('input_text', 'The quick brown fox'),
        model_name='124M',
        seed=None,
        nsamples=1,
        batch_size=1,
        length=None,
        temperature=float(params.get('temperature', 1.0)),
        top_k=int(params.get('top_k', 0)),
        top_p=float(params.get('top_p', 1)),
        models_dir='models',
    )

    gc.collect()
    return UJSONResponse({'text': text},
                         headers=response_header)
Exemple #5
0
async def api_create_checkpoint(request: Request):
    try:
        build = request.path_params["build"]
        target = request.path_params["target"]
        start_or_finish = request.path_params["start_or_finish"]

        if start_or_finish not in ["start", "finish"]:
            return UJSONResponse(
                {
                    "success":
                    "false",
                    "error":
                    "api must be called with /api/checkpoint/start or /api/checkpoint/finish",
                },
                status_code=400,
            )

        report_checkpoint(
            build,
            time(),
            start_or_finish,
            target,
            request.query_params.get("exit_code", None),
        )
        return UJSONResponse({"success": "true"})
    except Exception as e:
        return UJSONResponse({
            "success": "false",
            "error": str(e)
        },
                             status_code=500)
Exemple #6
0
async def homepage(request):
    global generate_count
    global sess

    if request.method == 'GET':
        params = request.query_params
    elif request.method == 'POST':
        params = await request.json()
    elif request.method == 'HEAD':
        return UJSONResponse({'text': ''}, headers=response_header)

    text = gpt2.generate(sess,
                         length=int(params.get('length', 1023)),
                         temperature=float(params.get('temperature', 0.7)),
                         top_k=int(params.get('top_k', 0)),
                         top_p=float(params.get('top_p', 0)),
                         prefix=params.get('prefix', '')[:500],
                         truncate=params.get('truncate', None),
                         include_prefix=str(params.get(
                             'include_prefix', True)).lower() == 'true',
                         return_as_list=True)[0]

    generate_count += 1
    if generate_count == 8:
        # Reload model to prevent Graph/Session from going OOM
        tf.reset_default_graph()
        sess.close()
        sess = gpt2.start_tf_sess(threads=1)
        gpt2.load_gpt2(sess)
        generate_count = 0

    gc.collect()
    return UJSONResponse({'text': text}, headers=response_header)
Exemple #7
0
    def run_parse(self, name):
        if not name:
            UJSONResponse(make_response(error="no name param found"),
                          status_code=400)
        self.logger.debug(f"name={name}")
        names, category = process.get_names(name)

        error = ""
        if not names:
            error = ""
        elif category is None:
            category = "Crowded"
            error = "This name appears to represent many people."
        elif category == process.CORP:
            error = (
                "The name appears to be from a corporation or other non-person entity."
            )

        return UJSONResponse(
            make_response(
                source=name,
                names=names,
                category=category,
                error=error,
                best=process.choose_name(names),
            ))
Exemple #8
0
async def start(request):
    k1 = request.path_params['k1']

    query = invites.select().where(invites.c.invite_code == k1)
    res = await database.fetch_one(query)

    if res:
        # even though it could be skipped, we want to ensure the invite code
        # is valid before revealing any information
        if res['is_used'] == False:
            return UJSONResponse({
                'callback': request.url_for("connect"),
                'k1': k1,
                'uri': config('NODE_URI'),
                'tag': 'channelRequest'
            })
        else:
            return UJSONResponse({
                'status': 'ERROR',
                'reason': 'this invite code has been used'
            })
    else:
        return UJSONResponse({
            'status': 'ERROR',
            'reason': 'this invite code is invalid'
        })
Exemple #9
0
async def delete_place(place_id: int, request: Request):
    status_code, payload, headers = await send_delete_people_by_place(place_id, request)

    if status_code == status.HTTP_204_NO_CONTENT:
        return UJSONResponse(status_code=status.HTTP_200_OK)
    else:
        return UJSONResponse(payload or {}, status_code=status_code, headers=headers)
Exemple #10
0
async def countries_detail(request):
    country_code = request.path_params["country_code"].upper()
    country = app.countries.get(country_code)
    if country:
        return UJSONResponse(country)
    else:
        return UJSONResponse({"message": "Country code not found."})
Exemple #11
0
async def geolocate_ip(request):
    r = app.geoip2.country(request.path_params["ip"])
    country = app.countries.get(r.country.iso_code)
    if country:
        return UJSONResponse(country)
    else:
        return UJSONResponse({"message": "Could not geocode request."})
Exemple #12
0
async def geolocate(request):
    country_code = request.headers.get("CF-IPCountry", "").upper()
    country = app.countries.get(country_code)
    if country:
        return UJSONResponse(country)
    else:
        return UJSONResponse({"message": "Could not geocode request."})
Exemple #13
0
async def homepage(request):

    if request.method == 'GET':
        params = request.query_params
    elif request.method == 'POST':
        params = await request.json()
    elif request.method == 'HEAD':
        return UJSONResponse({'text': ''}, headers=response_header)

    # Validate request token
    if REQUEST_TOKEN and params.get('token') != REQUEST_TOKEN:
        return UJSONResponse({'text': 'Incorrect request token.'},
                             headers=response_header)

    while True:
        # You can adapt this block for any text-generation method.
        text = gpt2.generate(sess,
                             length=300,
                             temperature=uniform(0.7, 1.0),
                             top_k=40,
                             return_as_list=True)[0]

        if (len(text) > 0 and len(text) <= 280
                and '<|startoftext|>' not in text):
            break

    api.update_status(text)

    return UJSONResponse({'text': 'Tweet successful!'},
                         headers=response_header)
Exemple #14
0
async def homepage(request):

    if request.method == 'GET':
        params = request.query_params
    elif request.method == 'POST':
        params = await request.json()
    elif request.method == 'HEAD':
        return UJSONResponse({'text': ''}, headers=response_header)

    # Validate request token
    if REQUEST_TOKEN and params.get('token') != REQUEST_TOKEN:
        return UJSONResponse({'text': 'Incorrect request token.'},
                             headers=response_header)

    db = create_engine(engine.url.URL(
        drivername='postgres+pg8000',
        username=DB_USER,
        password=DB_PASS,
        database=DB_NAME,
        query={
            'unix_sock': '/cloudsql/{}/.s.PGSQL.5432'.format(CONNECTION_NAME)
        }),
                       pool_size=1)

    metadata = MetaData()
    tweets = Table('tweets', metadata, autoload=True, autoload_with=db)

    auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
    auth.set_access_token(ACCESS_KEY, ACCESS_SECRET)

    api = tweepy.API(auth)

    with db.connect() as conn:
        q = (select([tweets]).where(tweets.columns.account == ACCOUNT).where(
            tweets.columns.tweet_timestamp == None).order_by(
                func.random()).limit(1))
        tweet = conn.execute(q).fetchone()

        if tweet is None:
            return UJSONResponse({'text': f"No more tweets left!"},
                                 headers=response_header)

        t = api.update_status(tweet.tweet)

        # Save record of tweet to prevent reuse
        t_timestamp = time.strftime("%Y-%m-%d %H:%M:%S+00", time.gmtime())
        t_url = f"https://twitter.com/{t.user.screen_name}/status/{t.id_str}"

        q_update = (tweets.update().where(
            tweets.columns.id == tweet.id).values(tweet_timestamp=t_timestamp,
                                                  tweet_url=t_url))

        conn.execute(q_update)

    db.dispose()

    return UJSONResponse({'text': f"Tweet posted! {t_url}"},
                         headers=response_header)
async def login(request):
    user = await request.state.get_user()
    credentials = await request.json()
    if not await user.authenticate(**credentials):
        return UJSONResponse({"message": "Bad username or password"}, 400)
    if not user["is_active"]:
        return UJSONResponse({"message": "User isn't active"}, 400)
    user.login()
    return UJSONResponse({})
async def change_password(request):
    data = await request.json()
    user = await request.state.get_user()
    if not user:
        return UJSONResponse({"message": "Log in first"}, 400)
    user.set_password(data["password"])
    await user.save(["password"])
    user.logout()
    return UJSONResponse({})
Exemple #17
0
    async def post(self, request):
        data = await request.json()

        async with get_conn() as conn:
            try:
                data = await Users(conn).change_password(
                    request.user, data['password'])
            except ApiException as e:
                return UJSONResponse({'error': str(e)}, status_code=400)

            return UJSONResponse({})
Exemple #18
0
async def api_get_checkpoint(request: Request):
    build = request.path_params["build"]
    target = request.path_params["target"]
    if build == "*" and target == "*":
        return UJSONResponse(
            {
                "success": "false",
                "error": "must specify at least one of build or target",
            },
            status_code=400,
        )
    return UJSONResponse(get_checkpoint_by_build_and_target(build, target))
Exemple #19
0
async def post_me(req):
    """Test POST route
    """
    try:
        # convert the body to json
        data = await req.json()
        return UJSONResponse({'msg': f'Hello {data["name"]}!'})
    except JSONDecodeError:
        return UJSONResponse({'msg': 'You posted nothing!!'})
    except KeyError:
        return UJSONResponse({'err': 'Please provide your name'},
                             status_code=400)
Exemple #20
0
async def connect(request):
    lnd_rpc = lnd_grpc.Client(
        network=config('LND_NETWORK', default='mainnet'),
        grpc_host=config('LND_GRPC_HOST', default='localhost'),
        grpc_port=config('LND_GRPC_PORT', default='10009'))

    try:
        req = ChannelOpenRequest(**request.query_params)

        try:
            query = invites.select().where(invites.c.invite_code == req.k1)
            res = await database.fetch_one(query)

            if res:
                if res['is_used'] == False:
                    next(lnd_rpc.open_channel(
                        node_pubkey=req.remoteid,
                        private=1 if config('LND_FORCE_PRIVATE', cast=bool, default=False) else req.private,  # noqa
                        local_funding_amount=res['funding_amount'],
                        push_sat=res['push_amount'],
                        spend_unconfirmed=config('LND_SPEND_UNCONFIRMED', cast=bool, default=True),  # noqa
                        sat_per_byte=config('LND_FEE_RATE', cast=int, default=None)))  # noqa

                    q = invites.update().where(invites.c.invite_code == req.k1) \
                        .values(
                            is_used=True,
                            node_id=req.remoteid.hex(),
                            used_at=datetime.utcnow())
                    await database.execute(q)

                    return UJSONResponse({'status': 'OK'})
                else:
                    return UJSONResponse({
                        'status': 'ERROR',
                        'reason': 'this invite code has been used'
                    })
            else:
                return UJSONResponse({
                    'status': 'ERROR',
                    'reason': 'this invite code is invalid'
                })
        except _MultiThreadedRendezvous as e:
            return UJSONResponse({
                'status': 'ERROR',
                'reason': e.details()
            })

    except ValidationError as e:
        return UJSONResponse({
            'status': 'ERROR',
            'reason': 'invalid parameter(s) provided'
        })
Exemple #21
0
async def status(request):
    """
    Checks status of task and return result
    when ready
    """
    task_id = request.path_params.get("task_id")
    result = tasks.get(task_id)

    if result is not None and result["status"] is "Pending":
        return UJSONResponse(result)

    tasks.pop(task_id, None)
    return UJSONResponse(result)
Exemple #22
0
async def health_check():
    status = True
    response = dict()
    db_status, _ = db_healthcheck()
    broker_status, _ = broker_healthcheck()
    status = status and db_status and broker_status
    response["status"] = status
    if status:
        return UJSONResponse(response, status_code=200)
    else:
        response['errors'] = dict()
        # Add errors here
        return UJSONResponse(response, status_code=503)
Exemple #23
0
async def homepage(request):
    global generate_count
    global sess

    if request.method == 'GET':
        params = request.query_params
    elif request.method == 'POST':
        params = await request.json()
    elif request.method == 'HEAD':
        return UJSONResponse({'text': ''}, headers=response_header)

    # Validate request token
    if REQUEST_TOKEN and params.get('token') != REQUEST_TOKEN:
        return UJSONResponse({'text': 'Incorrect request token.'},
                             headers=response_header)

    def generate_text():
        generated_text = gpt2.generate(
            sess,
            length=int(params.get('length', 280)),
            temperature=float(params.get('temperature', 0.9)),
            top_p=float(params.get('top_p', 0.7)),
            truncate=params.get('truncate', '<|endoftext|>'),
            prefix=params.get('prompt', '<|startoftext|>')[:100],
            include_prefix=str(params.get('include_prefix',
                                          False)).lower() == 'true',
            return_as_list=True,
            nsamples=int(params.get('count', 1)),
            batch_size=int(params.get('count', 1)))[0]
        return generated_text

    text = generate_text()
    text = text.replace('<|startoftext|>', '')
    text = text.replace('<|endoftext|>', '')

    # Retry once if returned text is empty (may happen as millzbot isn't that good with prompts)
    text = generate_text() if len(text) <= 2 else text
    text = "I literally actually couldn't come up with something to say" if len(
        text) == 0 else text

    generate_count += 1
    if generate_count == 9:
        # Reload model to prevent Graph/Session from going OOM
        tf.reset_default_graph()
        sess.close()
        sess = gpt2.start_tf_sess(threads=1)
        gpt2.load_gpt2(sess)
        generate_count = 0

    gc.collect()
    return UJSONResponse({'text': text}, headers=response_header)
Exemple #24
0
async def aggregation(request) -> UJSONResponse:
    """
    responses:
      200:
        description: A hello world response
        examples:
          { status: "ok", result: { hello: "world" }}
    """
    result = ''
    status = 'error'
    query = request.path_params['query']
    query = '' if query in ['\"\"', '\'\''] else query
    print('`%s`' % query)
    aggregation_name = request.path_params['aggregation_name']
    if not aggregation_name in ['top_tags', 'tweet_count', 'top_twitters']:
        return UJSONResponse({
            'aggregation': aggregation_name,
            'result': result,
            'status': status
        })
    if aggregation_name == 'top_tags':
        with MongoConnection('twitter',
                             os.environ.get('MONGO_HOST', 'localhost'),
                             int(os.environ.get('MONGO_PORT', 27017))):
            result = str(
                get_top_hashtags(query=query,
                                 limit=int(
                                     request.query_params.get('limit', 10))))
            status = 'ok'
    if aggregation_name == 'tweet_count':
        with MongoConnection('twitter',
                             os.environ.get('MONGO_HOST', 'localhost'),
                             int(os.environ.get('MONGO_PORT', 27017))):
            result = str(get_tweet_count(query=query))
            status = 'ok'
    if aggregation_name == 'top_twitters':
        with MongoConnection('twitter',
                             os.environ.get('MONGO_HOST', 'localhost'),
                             int(os.environ.get('MONGO_PORT', 27017))):
            result = str(
                get_top_twitters(query=query,
                                 limit=int(
                                     request.query_params.get('limit', 10))))
            status = 'ok'
    return UJSONResponse({
        'aggregation': aggregation_name,
        'result': result,
        'status': status
    })
Exemple #25
0
async def verify_magic(data: models.Magic = Body(...)):
    user = await security.authenticate_user_magic(data.email, data.secret)
    if not user:
        raise HTTPException(status_code=HTTP_400_BAD_REQUEST,
                            detail="Invalid Link")
    access_token_expires = datetime.timedelta(
        minutes=ACCESS_TOKEN_EXPIRE_MINUTES)
    access_token = security.create_access_token(
        data={"sub": user.email}, expires_delta=access_token_expires)
    response = UJSONResponse({"status": "authenticated"})
    response.set_cookie(oauth2_scheme.token_name,
                        access_token,
                        httponly=True,
                        secure=secure_cookies)
    return response
Exemple #26
0
async def create_user(req):
    """Creates a new user
    """
    try:
        data = await req.json()  # get request body as json (dict)

        # construct UPDATE query and execute asynchronously
        query = User.insert().values(name=data['name'])
        await db.execute(query)

        return UJSONResponse(
            {'msg': f"Created new user with name {data['name']}"})
    except (JSONDecodeError, KeyError):
        # handle client error
        return UJSONResponse({'err': 'Please supply a name'}, status_code=400)
Exemple #27
0
async def complete_horses(request):
    """
    Auto-completes the horse names as the user enters a name.
    This route isn't accessed directly by the user, but is
    used by JavaScript to auto-complete for horses.

    :param request: The HTTP request
    """

    # Get the query
    q: str = request.query_params.get("q", None)
    if q is None:
        # User didn't give a query
        return UJSONResponse({"error": "You didn't specify a query."},
                             status_code=400)

    # Get the limit of horses
    try:
        limit: int = int(request.query_params.get("limit", 10))
    except ValueError:
        # Default limit of 10 horses
        limit = 10

    # Clamp the limit (minimum 1 horse, maximum 100 horse)
    limit = max(1, min(100, limit))

    # Make the query
    query = (select([horses.c.name, horses.c.age]).where(
        horses.c.name.ilike(q + "%")).order_by(horses.c.name).limit(limit))

    # Fetch it all
    result = []
    async for row in database.iterate(query):
        # Fetch the first word of the horse name and age
        name_prefix = row[0].split(" ", 1)[0]
        age = DIGITS.match(row[1])

        # If the horse has an invalid age value in the database, skip it
        if age is None:
            print("WARNING, skipping", row[0], "because of invalid age")
            continue

        result.append(
            ["{},{}".format(name_prefix, age.group(0)), row[0],
             age.group(0)])

    # Return a response
    return UJSONResponse(result)
Exemple #28
0
async def get_multi_runs_events(request):
    event_kind = request.path_params["event_kind"]
    force = to_bool(request.query_params.get("force"), handle_none=True)
    if event_kind not in V1ArtifactKind.allowable_values:
        raise HTTPException(
            detail="received an unrecognisable event {}.".format(event_kind),
            status_code=status.HTTP_400_BAD_REQUEST,
        )
    run_uuids = request.query_params["runs"]
    event_names = request.query_params["names"]
    orient = request.query_params.get("orient")
    orient = orient or V1Events.ORIENT_DICT
    event_names = {e
                   for e in event_names.split(",")
                   if e} if event_names else set([])
    run_uuids = {e
                 for e in run_uuids.split(",") if e} if run_uuids else set([])
    events = await get_archived_operations_events(
        run_uuids=run_uuids,
        event_kind=event_kind,
        event_names=event_names,
        orient=orient,
        check_cache=not force,
    )
    return UJSONResponse({"data": events})
Exemple #29
0
async def answers_user(request):
    id = request.path_params["id"]
    results = await User.get(username=id)
    result = await Answer.all().filter(ans_user_id=results.id)
    # Serialize the queryset
    answers = answers_schema.dump(result)
    return UJSONResponse({"answers": answers})
Exemple #30
0
async def single_database_query(request):
    row_id = randint(1, 10000)

    async with connection_pool.acquire() as connection:
        number = await connection.fetchval(READ_ROW_SQL, row_id)

    return UJSONResponse({'id': row_id, 'randomNumber': number})