def get_all_predicts(request,rid, perm,disabled4reader,count): print "in _predict.get_all_predicts, rid=",rid," user="******"error":"data not found"},status=404) #use "like" _icontains to get predict and ensemble_predict if count is None or int(count) <=0: predictions = Document.objects.all().filter(Q(file_type__icontains="predict"), train_id=rid).order_by('-id') else: predictions = Document.objects.all().filter(Q(file_type__icontains="predict"), train_id=rid).order_by('-id')[:int(count)] #serializer = PredictSerializer(predictions, many=True) #return Response(serializer.data) return Response(ml_serializers.pred2json(predictions))
def get_pred(rid, perm,disabled4reader): print 'in get_pred, rid=', rid predictions = Document.objects.all().filter(file_type__icontains="predict", id=rid) #print "t=",type(predictions),",predictions=",predictions # check if dataset doc accessible train_id=predictions[0].train_id ds_doc =_list.get_ds_doc(train_id, perm) if not ds_doc: return Response({"error":"data not found"},status=404) #serializer = PredictSerializer(predictions, many=True) #return Response(serializer.data) return Response(ml_serializers.pred2json(predictions))
def get_predict(request, rid, hash, document, perm, disabled4reader): print "in _predict.get_predict, hash=", hash, " user="******"predict", train_id=rid, filename=hash) #slz = PredictSerializer(doc, many=True) #return Response(slz.data) return Response(ml_serializers.pred2json(doc)) if request.method == 'POST': print "in POST, list=", request.POST.get('list') hash_list = [] if hash == 'list': hash_str = request.POST.get('list') if hash_str: hash_str = hash_str.lower() hash_list = hash_str.split(',') # get unique items hash_list = set(hash_list) else: hash_list.append(hash) host = request.POST.get('host') host = "" if host is None else host port = request.POST.get('port') port = "" if port is None else port db = request.POST.get('db') db = "" if db is None else db tbl = request.POST.get('tbl') tbl = "" if tbl is None else tbl usr = request.POST.get('usr') usr = "" if usr is None else usr pwd = request.POST.get('pwd') pwd = "" if pwd is None else pwd pred_doc = [] # forloop to predict each item for a_hash in hash_list: # create newdoc newdoc = Document() newdoc.filename = a_hash newdoc.submitted_by = request.user.username newdoc.acl_list = perm newdoc.train_id = str(rid) newdoc.file_type = "predict" newdoc.db_host = host newdoc.db_db = db newdoc.db_port = port newdoc.db_tbl = tbl newdoc.save() cid = newdoc.id upload_fname = a_hash print "before predict_hash *************** " #ret=_result.predict_hash(document, newdoc, a_hash, tlabel="", action_type='hash_predict' ret = predict_hash(document, newdoc, a_hash, tlabel="", action_type='hash_predict', host=host, port=port, db=db, tbl=tbl, usr=usr, pwd=pwd) #ret={'rid':rid,"the_post":request.POST.get('the_post'),"username":request.user.username} print 'in POST: ret=', ret retdoc = Document.objects.get(id=cid) pred_doc.append(retdoc) return Response(ml_serializers.pred2json(pred_doc)) else: ret = {"error": "no support"} return Response(json.dumps(ret), status=404)
def get_post_predict(request, rid, hash, perm, disabled4reader): print "in get_post_predict, hash=", hash, " user="******"not found!" #ret={"error":"data model not found!"} return Response({"error": "dataset not found"}, status=404) if hash: hash = hash.lower() if request.method == 'GET': print "In GET: rid=", rid, ",hash=", hash # by prediction if hash.isdigit(): doc = Document.objects.all().filter(file_type="predict", train_id=rid, id=hash) else: doc = Document.objects.all().filter(file_type="predict", train_id=rid, filename=hash) print "doc=", doc # get by md5/filename if len(doc) > 0: #slz = PredictSerializer(doc, many=True) #return Response(slz.data) return Response(ml_serializers.pred2json(doc)) return Response({"error": "prediction not found"}, status=404) action_type = 'hash_predict' if request.method == 'POST': print "in POST, list=", request.POST.get('list') verbose = request.POST.get('verbose') verbose = "0" if verbose is None else verbose print "verbose=", verbose host = request.POST.get('host') host = "" if host is None else host port = request.POST.get('port') port = "" if port is None else port db = request.POST.get('db') db = "" if db is None else db tbl = request.POST.get('tbl') tbl = "" if tbl is None else tbl usr = request.POST.get('usr') usr = "" if usr is None else usr pwd = request.POST.get('pwd') pwd = "" if pwd is None else pwd model_filename = request.POST.get('model_filename') model_filename = "" if model_filename is None else model_filename keep_flag = request.POST.get('keep_flag') keep_flag = "0" if keep_flag is None else keep_flag predict_list = [] pred_doc = [] # for offline massive prediction if hash == 'list_offline': hash_list = request.POST.get('list') feat_threshold = request.POST.get('feat_threshold') pred_doc = _predict.predict_massive(document, hash_list, host=host, port=port, db=db, tbl=tbl, usr=usr, pwd=pwd, model_filename=model_filename, keep_flag=keep_flag, feat_threshold=feat_threshold) return Response(pred_doc) # for ONE hash list elif 'list' in hash: hash_str = request.POST.get('list') if hash_str: hash_str = hash_str.lower() predict_list = hash_str.split(',') # get unique items predict_list = set(predict_list) # upload raw data for prediction elif 'raw' in hash: form = DocumentForm(request.POST, request.FILES) if form.is_valid(): print "in API upload predict" newdoc = Document(docfile=request.FILES['docfile']) newdoc.filename = request.FILES['docfile'] if document.file_type == 'ensemble': # upload binary for ensemble predict action_type = 'ensemble_predict' print "for ensemble predict..." else: action_type = 'upload_predict' predict_list.append(newdoc.filename) print "newdoc.filename=", newdoc.filename else: print "Form is invalid!" return Response({"Error": "invalid form"}, status=404) # upload binary for sandbox execution & predict elif hash == 'exec': form = DocumentForm(request.POST, request.FILES) if form.is_valid(): print "in API upload for execution & wait:" exe_type = request.POST.get('_file_type') # handle by _predict; key field is "_file_type" if exe_type is None: print "required field not found!" return Response({"Error": "required field not found."}, status=404) else: return _predict.predict(request, rid, cid=None, msg_id=None, perm=perm, disabled4reader=disabled4reader) else: print "Form is wrong!!" return Response({"Error": "invalid form"}, status=404) else: predict_list.append(hash) # TBD need to check upload count here? for p_item in predict_list: # create newdoc if action_type == "hash_predict": newdoc = Document() newdoc.filename = p_item newdoc.submitted_by = request.user.username newdoc.acl_list = perm newdoc.train_id = str(rid) if action_type == "ensemble": newdoc.file_type = "ensemble_predict" else: newdoc.file_type = "predict" newdoc.ml_n_gram = document.ml_n_gram newdoc.ml_opts = document.ml_opts newdoc.ml_lib = document.ml_lib newdoc.db_host = host newdoc.db_db = db newdoc.db_port = port newdoc.db_tbl = tbl newdoc.save() cid = newdoc.id #upload_fname=p_item #print "before predict_hash *************** " ret = _predict.predict_hash(document, newdoc, p_item, tlabel="", action_type=action_type, host=host, port=port, db=db, tbl=tbl, usr=usr, pwd=pwd, verbose=verbose) #print 'in POST: ret=', ret pred_doc.append(ret) return Response(pred_doc) else: return Response({"error": "data not found"}, status=404)