def recommend_for_user(): result_df = pd.DataFrame() already_rated = [] recDict = {} recList = [] k = 10 for i in range(0, len(user_map)): client_id = user_map[i] user_idx = np.searchsorted(user_map, client_id) user_rated = [np.searchsorted(item_map, i) for i in already_rated] recommendations = generate_recommendations(user_idx, user_rated, row_factor, col_factor, k) article_recommendations = [int(item_map[i]) for i in recommendations] # article_recommendations = article_recommendations ## convert the array of item into list try: recDict = { 'clientId': client_id.decode("utf-8"), "rec": article_recommendations } except: recDict = {'clientId': client_id, "rec": article_recommendations} recList.append(recDict) # result = pd.DataFrame( {'clientId':client_id.decode("utf-8") , "recommendations" : article_recommendations }) # result_df = result_df.append( result ) if i % 10000 == 0: print(i) ## save the result file with open('trainer/result/nst_recommend_for_users.json', 'w') as f: json.dump(recList, f) ## upload the content to GCP bucket blob.upload_from_filename('trainer/result/nst_recommend_for_users.json')
def results(): if not 'name' in session or ('name' in session and session['name'] is None): return redirect(url_for('landing')) title = 'Market Change Prediction Results' text = str(request.args.get("text")) recs = generate_recommendations(text) form = ReturnForm() form_name = 'Return to Dashboard' if request.method == 'POST': return redirect(url_for('dashboard')) return render_template('results.html', title=title, form=form, form_name=form_name, text=text, recs=recs)
def results(): title = 'Based on your responses, we recommend' data = request.args recs = generate_recommendations(data) drug_1 = recs[0] # Score, name, description, id list drug_1 = (int(drug_1[0][0]), drug_1[1], get_desc(drug_1[1]), drug_1[0][1]) drug_2 = recs[1] drug_2 = (int(drug_2[0][0]), drug_2[1], get_desc(drug_2[1]), drug_2[0][1]) drug_3 = recs[2] drug_3 = (int(drug_3[0][0]), drug_3[1], get_desc(drug_3[1]), drug_3[0][1]) return render_template('results.html', title = title, drug_1=drug_1, drug_2=drug_2, drug_3=drug_3)
def results(): title = 'Based on your responses, we recommend:' data = request.args recs = generate_recommendations(data) drug_1 = recs[0][1] drug_2 = recs[1][1] drug_3 = recs[2][1] return render_template('results.html', title=title, drug_1=drug_1, drug_2=drug_2, drug_3=drug_3)
def main(args): model_dir = os.path.join(args['output_dir']) rec_path = os.path.join(args['output_dir']) pref_path = os.path.join(args['output_dir']) print("The args['output_dir'] = {}".format(args['output_dir'])) if model_dir.startswith('gs://'): print("gc path = {}".format( os.path.join(model_dir, 'model', 'recommendation_matrix.npy'))) rec_path = os.path.join(model_dir, 'model', 'recommendation_matrix.npy') pref_path = os.path.join(model_dir, 'model', 'Preference_matrix.npy') else: # model_dir here is the path for the latest recommendation matrix which was previously trained model_dir = (os.popen("cd './jobs' && ls -t | head -1").readlines() )[0].strip('\n') # Loading the matrix from the cmdOutput1 path rec_path = os.path.join('jobs', model_dir, 'model', 'recommendation_matrix.npy') pref_path = os.path.join('jobs', model_dir, 'model', 'Preference_matrix.npy') print("gc path 1 = {}".format(rec_path)) sess = tf.Session() with sess.as_default(): f_r = StringIO(file_io.read_file_to_string(rec_path)) print("gc f path = {}".format(f_r)) recommendation_numpy = tf.constant(np.load(f_r), name='recommendation_numpy') print("gc my_variable = {}".format(recommendation_numpy.eval())) f_p = StringIO(file_io.read_file_to_string(pref_path)) Preference_numpy = tf.constant(np.load(f_p), name='Preference_numpy') recommendation_matrix = recommendation_numpy.eval() preference_matrix = Preference_numpy.eval() print("preference_matrix= {}".format(preference_matrix)) with file_io.FileIO('gs://azg_bucket/data/test.json', mode='r') as f: data = json.load(f) users = data["user_id"] for user_id in users: recommendations = generate_recommendations( 100, user_id, recommendation_matrix, preference_matrix) print("Recommendations for user {} = {}".format( user_id, recommendations))
import numpy as np from model import generate_recommendations client_id = 10 already_rated = [293, 294, 1000000] k = 5 m_dir = '/home/parth27/bizzbytes/tensorflow-recommendation-wals/wals_ml_engine/jobs/wals_ml_local_20210116_103341/model' user_map = np.load(m_dir+"/user.npy") item_map = np.load(m_dir+"/item.npy") row_factor = np.load(m_dir+"/row.npy") col_factor = np.load(m_dir+"/col.npy") user_idx = np.searchsorted(user_map, client_id) user_rated = [np.searchsorted(item_map, i) for i in already_rated] print(len(item_map)) recommendations = generate_recommendations(942, user_rated, row_factor, col_factor, k) article_recommendations = [item_map[i] for i in recommendations] print(article_recommendations)