def by_news(): for (code, ndata) in get_news().iteritems(): row = normalize.row({ "news": ndata }) print ml.predict(row)
def signin(): uname = request.args.get('uname') pw = request.args.get('pw') redisPWByte = r.get(uname) if redisPWByte is None: return 'UserName is not exists' redisPWStr = redisPWByte.decode(encoding='utf-8') replaced_pw = pw.replace('_', ' ') resPW = ml.predict(replaced_pw, vectorizer, clf) if resPW != redisPWStr: return 'Your PassWord is wrong' registSentenceByte = r.get(uname + 'RegistSentence') registSentenceStr = registSentenceByte.decode(encoding='utf-8') if pw == registSentenceStr: return 'You should use not same pass word, which you registered' concatResUname = resPW.tostring() + uname.encode('utf-8') hash = hashlib.sha256(b'uname' + concatResUname).hexdigest() return hash
def add_user(): global name global phone_number global gender global twitter_id global email global password name=request.form.get('username') email=request.form.get('useremail') password=request.form.get('userpassword') phone_number=request.form.get('userphone') gender=request.form.get('gender') twitter_id=request.form.get('twitter_id') cursor.execute("""INSERT INTO `users` (`user_id`,`name`,`email`,`password`,`phone_number`,`gender`,`twitter_id`) VALUES (NULL,'{}','{}','{}','{}','{}','{}')""".format(name,email,password,phone_number,gender,twitter_id)) conn.commit() cursor.execute("""SELECT * FROM `users` WHERE `email` LIKE '{}' AND `password` LIKE '{}' """.format(email,password)) user=cursor.fetchone() twitter_id = user[6] a=api.twitter_api(twitter_id) #print(a) res=ml.predict(a) per_map={0:'ENFJ', 1:'ENFP', 2:'ENTJ', 3:'ENTP', 4:'ESFJ', 5:'ESFP', 6:'ESTJ', 7:'ESTP', 8:'INFJ', 9:'INFP', 10:'INTJ', 11:'INTP', 12:'ISFJ', 13:'ISFP', 14:'ISTJ', 15:'ISTP'} personality_index =per_map[res] print(personality_index) cursor.execute("""SELECT * FROM `users` WHERE `email` LIKE '{}' AND `password` LIKE '{}' """.format(email,password)) user=cursor.fetchone() cursor.execute("""UPDATE `users` set personality_index='{}' WHERE email='{}' """.format(personality_index,email)) conn.commit() #flash('account created') return redirect('/login')
def predict(): data = request.get_json() prediction, onoff = model.predict(data) result = {'onoff_probability': float(prediction), 'onoff': bool(onoff)} return jsonify(result)
def show_predict(self): self.screen.fill(st.BACKGROUND_COLOR) pred_rbf, pred_lin = ml.predict() pred_rbf = "RBF: " + str(pred_rbf) pred_lin = "Linear: " + str(pred_lin) text = self.font.render(pred_rbf, 1, (104, 14, 216)) self.screen.blit(text, (self.get_start_x_y(pred_rbf)[0], 200)) text = self.font.render(pred_lin, 1, (10, 190, 100)) self.screen.blit(text, (self.get_start_x_y(pred_lin)[0], 400)) pygame.display.update() self.wait()
def get_content(parameters, thing): result = [] count = 0 # if (thing == "music"): # genre = [ml_db.random_val("genre") for i in range(7)] # artist [ml_db.random_val("artist") for i in range(7)] # count = 0 # for music in genre: # for singer in artist: # if (predict(parameters, [music, singer]) >= .5): # count++ # result.append() if (thing == "img"): numbers = [] tags = [] for i in range(10): t1 = [] t2 = [] for s in range(len(parameters) - 1): temp = ml_db.random_val("img") t1.append(temp[0]) t2.append(temp[1]) numbers.append(t1) tags.append(t2) for i in range(10): if (ml.predict(parameters, numbers[i]) >= .5): result.append(tags[i]) count += 1 if (count >= 2): break data = [] for i in range(5 - len(result)): #assures that we get 5 images temp = ml_db.random_val("img") t = api.getty(temp[1]) # ap = " ".join(str(x) for x in ap) # print "ap: ", ap data.append(t) # sleep(1) for i in range(len(result)): t = api.getty(" ".join(str(x) for x in result[i])) # ap = " ".join(str(x) for x in ap) # print "ap:", ap data.append(t) print data #with open('j.json', 'w') as jf: # json.dump(data, jf) #print j # dwrap = {'data':data} j = json.dumps(data) return j
def signUp(): uname = request.args.get('uname') pw = request.args.get('pw') redisPW = r.get(uname) if redisPW: return 'Already SignUp' replaced_pw = pw.replace('_', ' ') res = ml.predict(replaced_pw, vectorizer, clf) r.set(uname, res) r.set(uname + 'RegistSentence', pw) r.expire(uname, 60) return 'SignUp succeeded'
def main(): speakers = data.get_speakers() number_classes = len(speakers) print("speakers", speakers) # train(number_classes) # return model = ml.make_model(number_classes) model.load('classifier') stream = audio.Stream() while True: input('press enter to record!!!') buff = stream.record(1.5) sample = audio.stream_to_ints(buff) label, conf = ml.predict(model, speakers, sample) print("predicted : result = %s confidence = %.2f" % (label, conf))
def upload_file(): if request.method == 'POST': file = request.files.get('file') if file and save_file(file): prediction = predict(path_for_file(file)) if prediction.subject: return redirect( url_for('uploaded_file', subject=prediction.subject, filename=file.filename, breed=prediction.breed)) else: return redirect(url_for('error')) else: return redirect(request.url) return render_template('index.html')
def user_detail(user_id): """Show info about user.""" user = User.query.get(user_id) skillto = db.session.query(Skill.skill_name).join(UserSkill).filter(UserSkill.skill_direction=='to', UserSkill.user_id==user_id).scalar() skillfrom = db.session.query(Skill.skill_name).join(UserSkill).filter(UserSkill.skill_direction=='from', UserSkill.user_id==user_id).scalar() ml_skill_to,ml_skill_from = ml.predict(db.session.query(User.user_occupation_id).filter(User.user_id==session['user_id']).first()) t = np.asscalar(np.int16(ml_skill_to)) f = np.asscalar(np.int16(ml_skill_from)) pred_skill_to, pred_skill_from = db.session.query(Skill.skill_name).filter((Skill.skill_id==t)|(Skill.skill_id==f)).all() print "PRED FROM",pred_skill_from print "PRED TO",pred_skill_to return render_template("user_profile.html", user=user, map_key_api=map_key, skill_from=skillfrom, skill_to=skillto, pred_skill_to=pred_skill_to[0],pred_skill_from=pred_skill_from[0])
async def predict_rossmann_sales(payload: Payload): """ This endpoint accepts a POST request. The format of the data it accepts is presented in the Payload data model. Upon receiving JSON data, it inputs the data to a model. The output prediction of the model is returned as a JSON response Authored by: Renzo Benemerito input: JSON, data model -> Payload output: JSON format { sales: int } DOCS URL: /docs#/default/predict_sales_predict_post """ if payload.Open == 0: # Handling for store closed result_set = {"sales": 0.0} else: data = pre_process(payload) sales = float(round(predict(data), 3)) result_set = {"sales": sales} return result_set
def prediction(request): if request.method == "POST": data = request.POST.copy() print(data) try: c1 = Customer.objects.filter(key=request.POST['key'])[0] except Exception as e: print(e) return JsonResponse({"message": "Invalid API Key", "status": -1}) if c1.hit<=c1.max_hit: if request.FILES.get("image"): image = request.FILES["image"].read() output = predict(image) output['status'] = 1 output['message'] = "API hitted successfully" c1.hit+=1 c1.save() return JsonResponse(output) return JsonResponse({"message": "Please Upload an Image", "status": 0}) return JsonResponse({"message": "Sorry your free API quota is over", "status": -2})
def predict(): if request.method == 'POST': # check if the post request has the file part if 'file' not in request.files: return {"message": "no file selected"} file = request.files['file'] # if user does not select file, browser also # submit an empty part without filename if file.filename == '': return {"message": "no file selected"} if file and allowed_file(file.filename): df = pd.read_csv(file) df["slow_dispatch"] = np.where(df["velocity"].astype('int64') >= 30, 1, 0) df = df.dropna() df = df.drop(["slow_dispatch", "velocity"], axis=1) data = ml.encode(df) preds = ml.predict(data) preds = np.array(preds) preds_flat = preds.flatten() x = [str(x) for x in preds_flat] return json.dumps(x) return '''
from ml import train, save, load, predict #model = train('deep_fonts/data.csv') #save(model,'test_model') model = load('test_model') nothing = 0 predict(model, 'test.jpg', nothing)
import ml import nlp import pickle import re if __name__ == "__main__": results = pickle.load(open('-twitter-trained-log87.pickle', 'rb')) classifier = results[0][2][1][0] # best logistic dvp = pickle.load(open('-twitter-dvp87.pickle', 'rb')) while (True): print("Enter some text:") s = input() pre = ml.predict([s], classifier, dvp, nlp.cleanTokensTwitter) for p, pp in zip(pre['prediction'], pre['prediction_probabilities']): print('\tSarcastic' if p else '\tNon-sarcastic') print('\t' + str(pp[1] * 100) + '%' if p else '\t' + str(pp[0] * 100) + '%') print()
def predict(): req = request.get_json() image = req['image'] prediction = ml.predict(image) res = make_response(jsonify({'JSON': prediction})) return res
def predictHello(): text = "ok, that went well", res = ml.predict(*text, vectorizer, clf) return res
def main(): folders = os.listdir("wav") X = [] y = [] Xtest = [] ytest = [] nspeakers = 5 #feature extraction for i in range(nspeakers): folder = folders[i] files = [ f for f in glob.glob("wav/" + folder + "/" + "**/*.wav", recursive=True) ] sztraining = int(len(files) * 0.6) for fid in range(sztraining): sample_rate, signal = wav.read(files[fid]) mfcc = MFCC.main(signal, sample_rate) for j in range(len(mfcc)): X.append([]) for k in range(len(mfcc[j])): X[-1].append(mfcc[j][k]) y.append(i) for fid in range(sztraining, len(files)): sample_rate, signal = wav.read(files[fid]) mfcc = MFCC.main(signal, sample_rate) for j in range(len(mfcc)): Xtest.append([]) for k in range(len(mfcc[j])): Xtest[-1].append(mfcc[j][k]) ytest.append(i) y = np.array(y) X = np.array(X) ytest = np.array(ytest) Xtest = np.array(Xtest) input_layer_size = 390 hidden_layer_size = 200 num_labels = nspeakers lmbda = 1 initial_theta1 = ml.randInitializeWeights(input_layer_size, hidden_layer_size) initial_theta2 = ml.randInitializeWeights(hidden_layer_size, num_labels) nn_initial_params = np.hstack( (initial_theta1.ravel(order='F'), initial_theta2.ravel(order='F'))) print( ml.nnCostFunc(nn_initial_params, input_layer_size, hidden_layer_size, num_labels, X, y, lmbda)) theta_opt = opt.fmin_cg(maxiter=50, f=ml.nnCostFunc, x0=nn_initial_params, fprime=ml.nnGrad, args=(input_layer_size, hidden_layer_size, num_labels, X, y.flatten(), lmbda)) theta1_opt = np.reshape( theta_opt[:hidden_layer_size * (input_layer_size + 1)], (hidden_layer_size, input_layer_size + 1), 'F') theta2_opt = np.reshape( theta_opt[hidden_layer_size * (input_layer_size + 1):], (num_labels, hidden_layer_size + 1), 'F') pred = ml.predict(theta1_opt, theta2_opt, Xtest, ytest) print(np.mean(pred == ytest.flatten()) * 100) savemat('ml.mat', { 'theta1': theta1_opt, 'theta2': theta2_opt })
@app.route('/error', methods=['GET', 'POST']) def error(): return render_template('error.html') @app.route('/<subject>/<filename>/<breed>') def uploaded_file(subject, filename, breed): return render_template('results.html', subject=subject, filename=filename, breed=breed) # Warning # Without manually calling tf related stuff on main thread # tf crashes on requests handled by werkzeug, not sure how it's related print(predict('../my_images/2.jpg')) print(detect_dog('../my_images/2.jpg')) print(detect_face('../my_images/2.jpg')) # End if __name__ == "__main__": #decide what port to run the app in port = int(os.environ.get('PORT', 5000)) #run the app locally on the given port app.run(host='0.0.0.0', port=port) #optional if we want to run in debugging mode #app.run(debug=True)
import hardware import dataProcessing import ml # Start recording (2 second chunk..) chunk = hardware.recordData('/dev/cu.usbserial-DM02582X') # Data Processing pipeline (2 second chunk..) chunk = dataProcessing.process(chunk) # ML Model return 1 or 0 prediction = ml.predict(chunk, './ml_model.pt') print('Prediction:', prediction)
def upload_file(): photo = request.files['file'] result = predict(photo) return '{}'.format(result)
def auth(): stream = audio.Stream() buff = stream.record(1.5) sample = audio.stream_to_ints(buff) label, conf = ml.predict(model, speakers, sample) return label
def do_POST(self): rootdir = os.getcwd() try: print(rootdir + self.path) path = self.path.split("?", 1)[0] # handle 'result' endpoint if path == '/result': # JSON string payloadString = self.rfile.read( int(self.headers['Content-Length'])) # Python dictionary payload = json.loads(payloadString) duration = int(payload['trip_seconds']) miles = float(payload['trip_miles']) pickup_latitude = float(payload['pickup_latitude']) pickup_lat = convert_coordinate(pickup_latitude) pickup_longitude = float(payload['pickup_longitude']) pickup_lon = convert_coordinate(pickup_longitude) dropoff_latitude = float(payload['dropoff_latitude']) dropoff_lat = convert_coordinate(dropoff_latitude) dropoff_longitude = float(payload['dropoff_longitude']) dropoff_lon = convert_coordinate(dropoff_longitude) time = str(payload['time']) converted_time = convert_time(time) date = str(payload['date']) converted_month, converted_day = convert_date(date) self.send_response(200) self.send_header("Access-Control-Allow-Origin", "*") self.send_header("Access-Control-Allow-Methods", "*") self.send_header("Access-Control-Allow-Headers", "*") self.send_header("Content-Type", "application/json") self.end_headers() # call the prediction function in ml.py prediction, distances, fares = ml.predict( duration, miles, pickup_lat, pickup_lon, dropoff_lat, dropoff_lon, converted_time, converted_month, converted_day) accuracy = ml.get_accuracy(converted_month) # make a dictionary from the result resultObj = { "fare": prediction, "accuracy": accuracy, "distances": np.array(distances).tolist(), "fares": np.array(fares).tolist() } # convert dictionary to JSON string resultString = json.dumps(resultObj) self.wfile.write(resultString.encode('utf-8')) else: self.send_error(404, 'endpoint not supported') except IOError: self.send_error(404, 'endpoint not found')
def _loop_worker(self): """The main loop for performing real time analysis. Takes items from an analysis queue sequentially, forms mne epochs, and either uses the data for real time training or to predict the letter that was mind-typed. Structure is adapted from rteeg.rteeg.analysis._loop_worker. """ sleep_time = 0.01 # Time to sleep between queries. while not self._kill_signal.is_set(): # when items exist in the marker analysis queue if not self.m_stream.analyze.empty(): print('Began analyzing data...') trial_num, ts, marker_end = self.m_stream.remove_analysis() self.data_duration = trial_num * self.event_time + self.analysis_time tmp = np.array(self.eeg_stream.data) # get analysis_time seconds of data (in terms of the end_index) after the event end_index = int((np.abs(tmp[:, -1] - ts)).argmin() + self.analysis_time / (1 / self.eeg_stream.info['sfreq'])) # ensure there is enough eeg data before analyzing; wait if there isn't while len(self.eeg_stream.data) < end_index: time.sleep(sleep_time) # Make an MNE epoch from channels 0-3 (EEG), decim = keep every nth sample epochs, identities, targets = self.eeg_stream.make_epochs( marker_stream=self.m_stream, end_index=end_index, marker_end=marker_end, trial_num=trial_num, data_duration=self.data_duration, picks=[0, 1, 2, 3], tmin=0.0, tmax=1, decim=3) # get input to classifier print('Formatting data for classifier...') data = np.array(epochs.get_data()) # since the sample frequency is 220 Hz/3 = 73.33 Hz, indexes 8 and 55 is approximately 0.100 - 0.750 s data = data[:, :, 8:56] print('size of classifier-input: {}'.format(data.shape)) print('size of identities: {}'.format(identities.shape)) print('size of targets: {}'.format(targets.shape)) # If training classifier, send data to classifier with ground truth targets if self.train: self.train_number += data.shape[0] if self.train_number < self.train_epochs: self.train_data.extend(data) self.train_targets.extend(targets) else: print('Training ml classifier with {} epochs'.format( self.train_number)) package = zip(self.train_targets, self.train_data) if self.get_test: ml.save_test_data(self.test_path, package) print("test set created!") self._kill_signal.set() else: i, t = ml.create_input_target(package) classifier = ml.ml_classifier(i, t) print("Finished training.") ml.save(self.classifier_path, classifier) self.train_number = 0 # Get accuracy of classifier based on test set score = classifier.score(self.inputs_test, self.targets_test) print('Test Set Accuracy: {}%'.format(score * 100)) # else do a prediction else: classifier = ml.load(self.classifier_path) i, t = ml.create_input_target(zip(targets, data)) prediction = ml.predict(i, classifier) intermediate = 0 for index, item in enumerate(prediction): # To account for the fact that every marker is associated with 4 channels, average the output # of each channel (or apply specific weights to each channel, to possibly implement in future). # Predictions for a single event based on 4 channels is appended to a list. if (index + 1) % 4 == 0: intermediate += item / 4 self.predictions.append(intermediate) intermediate = 0 else: intermediate += item / 4 #TODO: create method to return a predictions with events time.sleep(sleep_time)
# Get the selected option option = int(input("\nSelect an option: ")) # Get/Update the parameters parameters = configuration.getParameters(configuration_file=sys.argv[1]) # Extract the discriminating k-mers if option == 1: print("\nCASTOR-KRFE: extraction mode\n") krfe.extract(parameters) # Fit a model using a set of k-mers elif option == 2: print("\nCASTOR-KRFE: training mode\n") ml.fit(parameters) # Predict a set of sequences elif option == 3: print("\nCASTOR-KRFE: testing mode\n") ml.predict(parameters) # Analyzer the identified k-mers elif option == 4: #start = time.time() print("\nCASTOR-KRFE: motif analyzer mode\n") Results = analyzer.identifyPerfectMatch(parameters) Results = analyzer.identifyVariations(Results, parameters) Results = analyzer.extractRelatedInformation(Results, parameters) # Quit the program elif option == 5: print("Program exit") sys.exit(0) # If the mode specified is not valid else: print("The specified option is not valid")