Ejemplo n.º 1
0
def info():
    session['token_info'], authorized = get_token(session)
    session.modified = True
    if not authorized:
        flash("Please Login with your Spotify Account")
        return redirect('/')

    sp = spotipy.Spotify(auth=session.get('token_info').get('access_token'))

    user = get_user(sp)
    songs = get_all_top_tracks(sp)
    artists = get_all_top_artists(sp)
    audio_features = get_audio_features(sp, songs['uri'])
    recommended_artists = get_recommended_artists(
        sp, artists['artists'][0]['url'][32:])
    recommended_songs = get_recommendations(sp, artists, songs['uri'],
                                            artists['genres'][0],
                                            audio_features['values'])
    return render_template('info.html',
                           songs=songs,
                           artists=artists,
                           similar=recommended_artists,
                           tracks=recommended_songs,
                           audio=audio_features,
                           user=user)
Ejemplo n.º 2
0
def home_agent(user):
    airline, flight_num, departure_airport, departure_city, departure_time, arrival_airport, arrival_city, \
    arrival_time, price, status, airplane_id = utils.retrieve_get_args_for_flight_query(request)
    airlines_and_cities = utils.get_all_airlines_and_cities(mysqltool=mt)

    if airline == flight_num == departure_airport == departure_city == departure_time == arrival_airport \
            == arrival_city == arrival_time == price == status == airplane_id is None:
        flight_list = utils.get_recommendations(mt, user=user, how_many=10)
        if len(flight_list) == 0:
            flight_list = utils.get_popular_flights(mysqltool=mt)
        flight_list = utils.flight_list_add_check_ticket_exists(
            mysqltool=mt, flight_list=flight_list)
        my_json = {
            "msg": "ok",
            "user": user[:-2],
            "role": "Agent",
            'searched': 0,
            "flights": utils.flight_list_to_json_list(flight_list)
        }
        my_json.update(airlines_and_cities)
        return render_template('home.html', data=my_json)

    departure_airport = utils.airport_city_to_airport_name_list(
        mt, session['user'], departure_city, departure_airport)
    arrival_airport = utils.airport_city_to_airport_name_list(
        mt, session['user'], arrival_city, arrival_airport)
    if departure_airport == False or arrival_airport == False:
        my_json = {
            "msg": "ok",
            "user": user[:-2],
            "role": "Agent",
            "flights": [],
            'searched': 0,
        }
        my_json.update(airlines_and_cities)
        return render_template('home.html', data=my_json)

    attribute = [
        'airline_name', 'flight_num', 'departure_airport', 'departure_time',
        'arrival_airport', 'arrival_time'
    ]
    value = [
        airline, flight_num, departure_airport, departure_time,
        arrival_airport, arrival_time
    ]
    result = mt.agent_query(user=session['user'],
                            table='flight',
                            attribute=attribute,
                            value=value)
    result = utils.flight_list_add_check_ticket_exists(mysqltool=mt,
                                                       flight_list=result)
    my_json = {
        "msg": "ok",
        'user': user[:-2],
        'role': 'Agent',
        'searched': 1,
        'flights': utils.flight_list_to_json_list(result)
    }
    my_json.update(airlines_and_cities)
    return render_template('home.html', data=my_json)
import utils
import pickle
import settings

token = utils.get_token(settings.CLIENT_ID, settings.CLIENT_SECRET)
genres = utils.get_genres(token)
discovered_artists = {}

print("Discovering artists...")
for genre in genres:
    res = utils.get_recommendations(seed_tracks=[],
                                    seed_artists=[],
                                    seed_genres=[genre],
                                    token=token)
    for track in res["tracks"]:
        artists = track["artists"]
        for artist in artists:
            if artist["id"] not in discovered_artists.keys():
                discovered_artists[artist["id"]] = {}
    print("genre: %s, total artists: %d" % (genre, len(discovered_artists)))

print("Enough discovered. Now will get info about them.")
artists = {}
buffer = []
it = 0
for key in discovered_artists.keys():
    it += 1
    if len(buffer) < 49:
        buffer.append(key)
    else:
        buffer.append(key)
    depth = int(raw_input("Enter the maximum depth of the tree (0 for no limit): "))
    
    start = timer()
    for index in xrange(len(users_db)):
        X_train, Y_train, X_test, Y_test = utils.extract_data(users_db[index], items_db, 70)
        dataset = {'X': X_train, 'Y': Y_train}
        
        classes = utils.get_classes(dataset)
        features = range(len(X_train[0]))

        root = DT(dataset, classes, features, 0, depth)
        Y_pred = utils.classify(root, X_test)

        for k in xrange(1, K+1):
            if k <= len(Y_test):
                top_K_indices = utils.get_recommendations(Y_pred, k)
                precision, recall = utils.compute_metrics(Y_pred, Y_test, top_K_indices)
                precision_dict[k].append(precision)
                recall_dict[k].append(recall)

        MAE = utils.calc_MAE(Y_pred, Y_test)
        RMSE = utils.calc_RMSE(Y_pred, Y_test)
        accu = utils.accuracy(Y_pred, Y_test)
        
        MAE_arr.append(MAE)
        RMSE_arr.append(RMSE)
        accuracy_arr.append(accu)
        print "User: {} - Test accuracy: {}".format(index+1, accu)

    print "\nTime taken: {} sec".format(timer() - start)
    print 'Average MAE: {}'.format(sum(MAE_arr)/float(len(MAE_arr)))
Ejemplo n.º 5
0
            keywords_similarity,
            overview_similarity,
            user_embedding_similarity,
        ]
        sim_weights = [
            cast_weight,
            director_weight,
            keywords_weight,
            overview_weight,
            user_embedding_weight,
        ]
        sim_mat = generate_weighted_similarity_matrix(arrays=sim_matrices,
                                                      weights=sim_weights)
        recommendations = get_recommendations(
            films=data,
            titles=liked_films,
            similarity_matrix=sim_mat,
            top_n=config.POSTERS_PER_ROW * config.NUM_POSTER_ROWS,
        )
        filtered_films = data[data["title"].isin(recommendations)]

        recommendation_order = CategoricalDtype(recommendations, ordered=True)
        filtered_films["title"] = filtered_films["title"].astype(
            recommendation_order)
        filtered_films = filtered_films.sort_values("title")

        display_film_posters(
            streamlit=st,
            data=filtered_films,
            num_rows=config.NUM_POSTER_ROWS,
            posters_per_row=config.POSTERS_PER_ROW,
        )