Exemple #1
0
def json_example():
    # Validate the request body contains JSON
    if request.is_json:

        # Parse the JSON into a Python dictionary
        
        user_list = request.get_json()
        users={}
        for user in user_list["users"]: 
            users[user["id"]] = {
                "cuisine" : {"$has":user["cuisine"][0]},
                "payment" : {"$has":user["payment"]},
            }

        #res = make_response(jsonify(users), 200)
        #return res
        queries = [
            {
            "from": "ratings",
            "where": {
                "placeID": {"payment": pref["payment"]},
                "userID": {"cuisine": pref["cuisine"]}
            },
            "recommend": "placeID",
            "goal": {"rating": 2},
            "select":["$p", "placeID"],
            "limit": 100
            } for usr, pref in users.items()]
        res = [aito_api.recommend(client=client, query=query).to_json_serializable() for query in queries]
        results = {usr: res[idx]["hits"] for idx, usr in enumerate(users)}
        
        recom = pd.concat([pd.DataFrame(value).set_index("placeID") for key,value in results.items()], axis=1).dropna(thresh=2, axis=1)
        recom["Total"] = recom.fillna(0).mean(axis=1)
        #top_recom = list(recom.nlargest(10, ['Total']).index)
        top_recom = recom.nlargest(5,"Total")["Total"].to_dict()
        print(top_recom)

        queries = [
            {"from": "places",
            "where": {
                "placeID": place
                },
            "select": ["name", "cuisine","latitude", "longitude", "payment"]
            }
            for place in top_recom.keys()]
        responses = [aito_api.generic_query(client=client, query=query).to_json_serializable()["hits"][0] for query in queries]
        print(responses)
        responses = {value: responses[idx] for idx,value in enumerate(top_recom.values())}
        #responses = {prob: res[idx]["hits"] for idx, usr in enumerate(users)}
        res = make_response(jsonify(responses), 200)        
        return res

    else:

        # The request body wasn't JSON so return a 400 HTTP status code
        return "Request was not JSON", 400
Exemple #2
0
    def top_recommendation(self):
        AITO_INSTANCE_URL = 'https://junction-test.aito.app'
        AITO_API_KEY = '4yaBPf9Kmk9xHNW30jBop7ieEmWMz2eSpmKyWvBi'

        client = AitoClient(instance_url=AITO_INSTANCE_URL,
                            api_key=AITO_API_KEY)
        queries = self.request.query_params
        query_type = queries.get("type", "Mexican")

        limit = int(queries.get("limit", 0))
        user_id = queries.get("userID")

        wh = {
            "placeID.cuisine": query_type,
        }
        if user_id:
            like_places = list(
                map(
                    lambda t: {"placeID": t},
                    UserLike.objects.exclude(user_id=user_id).values_list(
                        "place__aito_id", flat=True)))
            if like_places:
                query = {
                    "from": "ratings",
                    "where": {
                        "$and": like_places
                    },
                }
                res = aito_api.generic_query(client=client, query=query)
                user_ids = py_.map(res["hits"], "userID")
                wh["userID"] = {"$and": user_ids}

        rec_query = {
            "from": "ratings",
            "where": wh,
            "recommend": "placeID",
            "goal": {
                "rating": 2
            },
        }
        if limit:
            rec_query['limit'] = int(limit)

        res = aito_api.recommend(client=client, query=rec_query)
        return res.json['hits']
Exemple #3
0
    def top_recommendation(self):
        AITO_INSTANCE_URL = 'https://junction-test.aito.app'
        AITO_API_KEY = '4yaBPf9Kmk9xHNW30jBop7ieEmWMz2eSpmKyWvBi'

        client = AitoClient(instance_url=AITO_INSTANCE_URL, api_key=AITO_API_KEY)
        queries = self.request.query_params
        query_type = queries.get("type", "Mexican")

        limit = int(queries.get("limit", 0))
        rec_query = {
            "from": "ratings",
            "where": {
                "placeID.cuisine": query_type,
            },
            "recommend": "placeID",
            "goal": {"rating": 2},
        }
        if limit:
            rec_query['limit'] = int(limit)

        res = aito_api.recommend(client=client, query=rec_query)
        return res.json['hits']
from aito.schema import AitoStringType, AitoTextType, AitoDelimiterAnalyzerSchema, AitoTableSchema, AitoColumnLinkSchema, AitoDatabaseSchema
from aito.client import AitoClient
import aito.api as aito_api

AITO_INSTANCE_URL = 'https://recommender.aito.app/'
AITO_API_KEY = '8l2T7nRxqy1kyydH7fiqf5H65TjHit5T6G8S1wFr'

client = AitoClient(instance_url=AITO_INSTANCE_URL, api_key=AITO_API_KEY)

rec_query = {
    "from": "ratings",
    "where": {
        "userID": {
            "cuisine": "Mexican", 
            "payment": "cash"
        }
    },
    "recommend": "placeID",
    "goal": {"rating": 2}
}

res = aito_api.recommend(client=client, query=rec_query)
print(res.top_recommendation)