def books_list(): # 获取关键字 keywords = request.args.get('keywords', None) if keywords: print("有keywords:") print(keywords) # 搜素书籍信息 data = Model().query('select * from wxapp.books where title like "' + keywords + '"') else: # 获取当前所有书籍信息,并返回json数据 data = Model().query('select * from wxapp.books') print(data) return jsonify(data)
def create_model(): form = Form(request.form) del form.delete if request.method == 'POST' and form.validate(): new_el = Model() for field in form: if field.type == "SubmitField": continue if field.data == "": continue setattr(new_el, field.name.lower(), field.data) db_session.add(new_el) try: db_session.commit() except (sqlalchemy.exc.IntegrityError, sqlalchemy.orm.exc.FlushError) as e: flash(str(e), "danger") return redirect(url_for(".view_all")) return render_template("form_view.html", title=title, form=form)
def edit_kbba(): if request.data: username = json.loads(request.data).get('username') alay_before = json.loads(request.data).get('alay_before') alay_after = json.loads(request.data).get('alay_after') baku_before = json.loads(request.data).get('baku_before') baku_after = json.loads(request.data).get('baku_after') try: if session['username'] == username: modelkbba = Model() result = modelkbba.edit_kbba(alay_after, baku_after, alay_before, baku_before) modelkbba.close_connection() return jsonify(result) else: return redirect(url_for('login')) except Exception as error: print(error) return redirect(url_for('login')) else: return redirect(url_for('login'))
def delete_keyword(): if request.data: username = json.loads(request.data).get('username') keyword = json.loads(request.data).get('keyword') try: if session['username'] == username: modeltwitter = Model() result = modeltwitter.delete_keyword_twitter(keyword) data = {} data.setdefault('succes', True) modeltwitter.close_connection() return jsonify(data) else: return redirect(url_for('login')) except Exception as error: print(error) return redirect(url_for('login')) else: return redirect(url_for('login'))
def test_add_model_for_existing_organism(self): organism_name = 'E coli' model_name = 'E coli - test' model_strain = 'MG16555' model_comments = 'just testing' organism = Organism(name=organism_name) db.session.add(organism) db.session.commit() self.assertEqual(organism.query.first().name, organism_name) self.assertEqual(organism.query.first().models.count(), 0) model = Model(name=model_name, organism_name=organism_name, strain=model_strain, comments=model_comments) db.session.add(model) organism.add_model(model) db.session.commit() self.assertEqual(organism.query.first().models.count(), 1) self.assertEqual(organism.query.first().models[0].name, model_name) self.assertEqual(model.query.first().name, model_name) self.assertEqual(model.query.first().strain, model_strain) self.assertEqual(model.query.first().comments, model_comments) self.assertEqual(model.query.first().organism.name, organism_name)
def test_add_model_and_create_organism(self): organism_name = 'E coli' model_name = 'E coli - test' model_strain = 'MG16555' model_comments = 'just testing' organism = Organism.query.filter_by(name=organism_name).first() if not organism: organism = Organism(name=organism_name) db.session.add(organism) model = Model(name=model_name, organism_name=organism_name, strain=model_strain, comments=model_comments) db.session.add(model) organism.add_model(model) db.session.commit() self.assertEqual(organism.query.first().name, organism_name) self.assertEqual(organism.models.first().name, model_name) self.assertEqual(model.query.first().name, model_name) self.assertEqual(model.query.first().strain, model_strain) self.assertEqual(model.query.first().comments, model_comments)
def register(): form = RegisterForm(request.form) msg = None if 'username' in session: return redirect(url_for('index_admin')) if request.method == 'GET': return render_template('admin/pages/register.html', form=form, msg=msg) if form.validate_on_submit(): username = request.form['username'] password = request.form['password'] modeluser = Model() account = modeluser.validate_account(username) if account: msg = "Username is exist. Please try other username." return render_template('admin/pages/register.html', form=form, msg=msg) else: modeluser.save_account(username, password) return redirect(url_for('login')) else: msg = 'Input error' return render_template('admin/pages/register.html', form=form, msg=msg)
def table_cluster(): if request.data: username = json.loads(request.data).get('username') try: if session['username'] == username: modelcluster = Model() result = modelcluster.select_cluster() data = {} data.setdefault('data', result) modelcluster.close_connection() return jsonify(data) else: return redirect(url_for('login')) except Exception as error: print(error) return redirect(url_for('login')) else: return redirect(url_for('login'))
def register(): # 获取 code code = request.args.get('code') # 向微信服务器发请求 获取 openid # GET wxurl = ''' https://api.weixin.qq.com/sns/jscode2session?appid={APPID}&secret={SECRET}&js_code={JSCODE}&grant_type=authorization_code '''.format(APPID=app.config['APPID'], SECRET=app.config['SECRET'], JSCODE=code) # 向微信服务器发起请求,获取openid req = urllib.request.urlopen(url=wxurl) # 获取接口响应的内容 content = req.read() resdata = json.loads(content.decode('utf-8')) # {'session_key': 'B4RnIIYNQNPa3+hCWexeYg==', # 'openid': 'oo-ye4riGgJLZzdLA6cZu-3f8o-E'} # 判断openid是否存在 print('-' * 50) print(resdata) print('-' * 50) sql = 'select * from users where openid="{}"'.format(resdata['openid']) fromres = Model().query(sql) if fromres: # 存在,则返回 return jsonify({'msg': '用户已经存在', 'code': fromres[0]['id']}) # 不存在,则添加 # 接收数据 userdata = json.loads(request.args.get('userinfo')) userdata['openid'] = resdata['openid'] # 设置注册时间 userdata['addtime'] = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") # 执行数据的添加 sql = ''' insert into users values( null,"{nikeName}","{avatarUrl}", "{username}","{bumen}","{phone}", "{openid}","{addtime}") '''.format(**userdata) # 执行添加,并获取最后的id userid = Model().exec(sql) # 返回json数据 return jsonify({'code': userid, 'msg': 'ok'})
def get_borrow(): # 获取用户id userid = request.args.get('userid') # 获取当前用户的借阅信息 sql = 'select bw.*,books.title,books.pic_url,books.author,books.price,books.publisher from borrow as bw inner join books on bw.book_isbn = books.isbn13 where bw.user_id = {user_id}'.format( user_id=userid) res = Model().query(sql) return jsonify(res)
def add_models(): model_list = [('E. coli - iteration 1', 'E. coli', 'MG16555'), ('E. coli - iteration 2', 'E. coli', 'MG16555')] for name, organism_name, strain in model_list: model = Model(name=name, organism_name=organism_name, strain=strain) db.session.add(model) db.session.commit()
def visualitation(): if 'username' not in session: return redirect(url_for('login')) modeltraining = Model() list_keyword = modeltraining.select_model_keyword() modeltraining.close_connection() return render_template('admin/pages/visualitation.html', keyword=list_keyword, username=session['username'])
def resolve(self, model): model.save(self.file_path) Model({ 'name': self.name, 'model_path': self.file_path, 'status': ModelStatus.ENABLE, 'user_id': self.user_id, 'module': model.__module__, 'method': model.__class__.__name__, 'project_id': self.project_id }).create() return model
def books_add(): # 判断当前的请求方式 if request.method == 'GET': # 显示添加的表单 return render_template('books/add.html') else: # 接受表单数据 data = request.form.to_dict() # print(data) # 判断是否上传了封面图 myfile = request.files.get('pic') # print(app.config['UP_DIR']) if myfile : # 执行文件的上传操作 Suffix = myfile.filename.split('.').pop() # 1.jpg filename = str(time.time())+str(random.randint(10000,99999))+'.'+Suffix myfile.save(app.config["UP_DIR"]+ filename) data['pic_url'] = filename else: # 判断是否右隐藏域传递了豆瓣的远程图片 imgurl = request.form.get('doubanimage',None) if not imgurl: return '<script>alert("请选择图书的封面图");history.back(-1);</script>' # 把远程的图片下载到本地服务器中 存储 Suffix = imgurl.split('.').pop() filename = str(time.time())+str(random.randint(10000,99999))+'.'+Suffix urllib.request.urlretrieve(imgurl,app.config["UP_DIR"]+filename) data['pic_url'] = filename data.pop('doubanimage') # return '' data['addtime'] = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") # 执行数据的添加 sql = ''' insert into books values( null,"{title}","{author}","{pic_url}" ,"{price}","{publisher}","{isbn13}","{summary}" ,"{catalog}","{pubdate}","{author_intro}" ,"{status}","{num}","{addtime}" ) '''.format(**data) res = Model().exec(sql) print(sql) print(res) if res : return '<script>alert("添加成功");location.href="'+url_for('admin.books_index')+'"</script>' return '<script>alert("添加失败");location.href="'+url_for('admin.books_add')+'"</script>'
def backgroundprocess_training(username, keyword, centroid, cluster, iteration, k=3): model_vocabulary = Model() total_tweets, data = model_vocabulary.select_data_training(keyword) list_id = [x['id'] for x in data] list_preprocessing = [x['tfidf'] for x in data] kmeans = KMeans(model_vocabulary) status = kmeans.fit(keyword, list_id, centroid, cluster, list_preprocessing, total_tweets, k, iteration) chache[username]['statustraining'] = status
def crawling(self, keyword, startdate, enddate, maxtweet): model_crawling = Model() if startdate and enddate and maxtweet > 0: tweetCriteria = got.manager.TweetCriteria().setQuerySearch( keyword).setSince(startdate).setUntil(enddate).setMaxTweets( maxtweet) elif startdate and enddate and maxtweet < 1: tweetCriteria = got.manager.TweetCriteria().setQuerySearch( keyword).setSince(startdate).setUntil(enddate) elif startdate and not enddate and maxtweet > 0: tweetCriteria = got.manager.TweetCriteria().setQuerySearch( keyword).setSince(startdate).setMaxTweets(maxtweet) elif enddate and not startdate and maxtweet > 0: tweetCriteria = got.manager.TweetCriteria().setQuerySearch( keyword).setUntil(startdate).setMaxTweets(maxtweet) elif startdate and not enddate and maxtweet < 1: tweetCriteria = got.manager.TweetCriteria().setQuerySearch( keyword).setUntil(startdate) elif enddate and not startdate and maxtweet < 1: tweetCriteria = got.manager.TweetCriteria().setQuerySearch( keyword).setUntil(startdate) else: print(keyword, startdate, enddate, maxtweet) tweetCriteria = got.manager.TweetCriteria().setQuerySearch(keyword) tweets = got.manager.TweetManager.getTweets(tweetCriteria) for tweet in tweets: content = tweet.text content = content.strip() tweetdate = tweet.date isnews = self.check_isnews(content) if not isnews: model_crawling.save_tweet(content, keyword, tweetdate)
def create_model(): user = get_user(request) try: model = Model(name=request.get_json()['name'], user=user) for f in request.get_json()['fields']: field = Field(name=f['name'], model=model, data_type=f['data_type']) field.parent_node = f['parent_node'] if 'parent_node' in f else None model.fields.append(field) db.session.add(model) db.session.commit() query = Model.query.get(model.id) return jsonify(ModelSchema().dump(query).data), 201 except sqlalchemy.exc.SQLAlchemyError as e: db.session.rollback() return jsonify({"error": str(e)}), 401
def training(): if 'username' not in session: return redirect(url_for('login')) if chache[session['username']]['statustraining'] == 0 and session[ 'process_training']: session['process_training'].remove(1) session.modified = True modellabel = Model() emoticon = modellabel.select_label() modellabel.close_connection() return render_template('admin/pages/training.html', username=session['username'], training=session['process_training'], emoticon=emoticon)
def add_random_models( user_id: int = None, project_id: int = None, count: int = 25 ) -> None: users = User.query.all() projects = Project.query.all() for i in range(count): name = get_random_name(separator=" ") number = randint(10, 50) # if given, assign the user selected_user_id = choice(users).id if user_id != None: selected_user_id = user_id selected_project_id = choice(projects).id # if given, assign the project if project_id != None: selected_project_id = project_id print( f"#{i+1} Adding model `{name}` to project_id {selected_project_id} with user_id {selected_user_id}" ) hyperparameters = get_random_dictionary(10, 20) parameters = get_random_dictionary(10, 100) metrics = get_random_dictionary(5, 15) new_model = Model( user_id=selected_user_id, project_id=selected_project_id, hyperparameters=hyperparameters, parameters=parameters, metrics=metrics, name=f"Randomized: {name}", path=uuid.uuid4(), dataset_name=f"Random dataset #{number}", dataset_description=DESCRIPTION, git_active_branch="develop", git_commit_hash="29ea5f511668248ea7ffe229c0f09992aaa6b382", private=False, ) db.session.add(new_model) db.session.commit()
def books_borrow(): # 接受数据 data = request.args.to_dict() # 先检测当前图书的数量 selsql = 'select num from books where isbn13 = ' + data['book_isbn'] selres = Model().query(selsql) if selres[0]['num'] <= 0: return jsonify({'msg': '当前图书已经被借阅,只能预约', 'error': 1}) data['addtime'] = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") data['status'] = 0 # 执行数据的创建 print('执行数据的创建') sql = 'insert into borrow values(null,"{book_isbn}",{user_id},"{addtime}",{status})'.format( **data) # # 修改图书的可借阅的数量 upsql = 'update books set num = num-1 where isbn13 = ' + data['book_isbn'] import pymysql # 打开数据库连接 db = pymysql.connect("localhost", "root", "", "wxapp") # 使用cursor()方法获取操作游标 cursor = db.cursor() # SQL 插入语句 try: # 执行sql语句 cursor.execute(sql) cursor.execute(upsql) # 提交到数据库执行 db.commit() resdata = {'msg': 'ok', 'error': 0} except: # 如果发生错误则回滚 db.rollback() resdata = {'msg': '执行错误', 'error': 2} # 关闭数据库连接 db.close() return jsonify(resdata)
def post(self) -> dict: parser = reqparse.RequestParser() parser.add_argument("name", type=str) parser.add_argument("dataset_name", type=str) parser.add_argument("dataset_description", type=str) parser.add_argument("project_id", type=int, required=True) parser.add_argument("user_id", type=int, required=True) parser.add_argument("hyperparameters", type=str, default="{}") parser.add_argument("parameters", type=str, default="{}") parser.add_argument("metrics", type=str, default="{}") parser.add_argument("git_active_branch", type=str, default=None) parser.add_argument("git_commit_hash", type=str, default=None) parser.add_argument("file", type=FileStorage, location="files", required=True) parser.add_argument("private", type=bool, default=False) args = parser.parse_args() if "file" in args: print("FILE:", args["file"]) filename = models_uploadset.save(args["file"], name=str(uuid.uuid4()) + ".") for arg_name in ["hyperparameters", "parameters", "metrics"]: args[arg_name] = json.loads(args[arg_name]) new_model = Model( user_id=args["user_id"], project_id=args["project_id"], hyperparameters=args["hyperparameters"], parameters=args["parameters"], metrics=args["metrics"], name=args["name"], path=filename, dataset_name=args["dataset_name"], dataset_description=args["dataset_description"], git_active_branch=args["git_active_branch"], git_commit_hash=args["git_commit_hash"], private=args["private"], ) db.session.add(new_model) db.session.commit() return NestedResponse(schema=ModelSchema).dump(new_model)
def check_min_max_tweet(): if request.data: username = json.loads(request.data).get('username') keyword = json.loads(request.data).get('keyword') try: if session['username'] == username: modeltwitter = Model() dict_result = modeltwitter.select_min_max_tweet(keyword) modeltwitter.close_connection() return jsonify(dict_result) else: return redirect(url_for('login')) except Exception as error: print(error) return redirect(url_for('login')) else: return redirect(url_for('login'))
def dispatch_request(self): if not session.get('username'): return redirect(url_for('login')) if request.method == "POST": model = request.form.get('project') if model == '': flash(u'请添加您的模块名') return render_template('add_moel.html') user_id = User.query.filter_by( username=session.get('username')).first().id models = Model.query.filter_by(model_name=model).first() if models: flash(u'模块不能重复') return render_template('add_moel.html') new_moel = Model(model_name=model, model_user_id=user_id) db.session.add(new_moel) db.session.commit() flash(u'添加成功!') return redirect(url_for('model')) return render_template('add_moel.html')
def table_preprocessing_keyword(): if request.data: username = json.loads(request.data).get('username') keyword = json.loads(request.data).get('keyword') try: if session['username'] == username: modeltwitter = Model() data_preprocessing = modeltwitter.select_keyword_not_preprocessing( keyword) modeltwitter.close_connection() # backgroundprocess_preprocessing(keyword, data_preprocessing, username) worker = threading.Thread( target=backgroundprocess_preprocessing, args=( keyword, data_preprocessing, username, )) worker.daemon = True worker.start() chache[session['username']]['statuspreprocessing'] = 1 data = {} data['statuspreprocessing'] = chache[ session['username']]['statuspreprocessing'] session['process_preprocessing'].append(1) session.modified = True return jsonify(data) else: return redirect(url_for('login')) except Exception as error: print(error) return redirect(url_for('login')) else: return redirect(url_for('login'))
def finishedTraining(): ##store model location in db ##add to models table data = request.get_data(as_text=True) data = json.loads(data) user_id = data['user_id'] class_id = data['class_id'] model_loc = data['model_path'] category = data['category'] task_id = User_Task.query.filter_by(user_id=user_id).first().task_id task = Task.query.filter_by(task_id=task_id).first() task.complete = True db.session.commit() model = Model(model_loc=model_loc, eng_loc=model_loc + '/output_labels.txt', for_loc=model_loc + '/for_labels.txt', category=category) db.session.add(model) db.session.commit() class_model = Class_Model(class_id=class_id, model_id=model.model_id) db.session.add(class_model) db.session.commit() return "Model Stored in DB"
def delete_label(): if request.data: username = json.loads(request.data).get('username') id_label = json.loads(request.data).get('id_label') try: if session['username'] == username: modellabel = Model() result = modellabel.delete_label(id_label) modellabel.close_connection() return jsonify(result) else: return redirect(url_for('login')) except Exception as error: print(error) return redirect(url_for('login')) else: return redirect(url_for('login'))
def delete_stopword(): if request.data: username = json.loads(request.data).get('username') word = json.loads(request.data).get('word') try: if session['username'] == username: modelstopword = Model() result = modelstopword.delete_stopword(word) modelstopword.close_connection() return jsonify(result) else: return redirect(url_for('login')) except Exception as error: print(error) return redirect(url_for('login')) else: return redirect(url_for('login'))
def classifier(): form = ClassifierForm() dataset = Dataset.query.all()[-1] if request.method == 'POST' and form.validate_on_submit(): logger.info(f'preprocessing: {form.preprocessing.data}, ' f'classifiers: {form.classifiers.data[0]} ' f'features: {form.features.data}') session['last_models'] = list() for clf_pk in form.classifiers.data: clf = Classifier.query.get(clf_pk) pre = Preprocessing.query.get(form.preprocessing.data) dt = datetime.now(timezone('America/Sao_Paulo')) model = Model(file=' ', datetime=dt, dataset_id=dataset.id, classifier_id=clf.id, preprocessing_id=form.preprocessing.data) for feat_pk in form.features.data: model.features.append(Feature.query.get(feat_pk)) db.session.add(model) db.session.commit() # committing the model to get the pk. model.file = (f'{model.id}_' f'{"-".join(clf.name.lower().split(" "))}_' f'{"-".join(pre.name.lower().split(" "))}_' f'{dt.strftime("%Y%m%d%H%M%S")}') db.session.add(model) db.session.commit() session['last_models'].append(model.id) logger.info(f'last_models: {session["last_models"]}') return redirect(url_for('setting.result')) return render_template('setting/classifier.html', form=form)
def backgroundprocess_preprocessing(keyword, data_preprocessing, username): model = Model() tfidf = TfIdf(model) preprocessing = Preprocessing(model) list_id_tweet = [] list_tweet_stemming = [] list_tfidf = [] try: model.delete_clustered(keyword) for data_tweet in data_preprocessing: id_tweet, tweet_stemming = background_preprocessing( data_tweet, keyword, preprocessing, model) list_id_tweet.append(id_tweet) list_tweet_stemming.append(tweet_stemming) tfidf.word_idf(list_tweet_stemming, keyword) list_idf = model.select_idf(keyword) for index, tweet_steming in enumerate(list_tweet_stemming): id_tweet = list_id_tweet[index] tweet_tfidf = tfidf.tf_idf( preprocessing.tokennizing(tweet_steming), keyword, list_idf) model.update_tfidf(id_tweet, keyword, tweet_tfidf) list_tfidf.append({'id': id_tweet, 'tfidf': tweet_tfidf}) data = sorted(list_tfidf, key=lambda k: k['id']) model.update_vocab_config(data, keyword) except Exception as error: print(error) finally: chache[username]['statuspreprocessing'] = 0 model.close_connection()
def login(): form = LoginForm(request.form) msg = None if 'username' in session: return redirect(url_for('index_admin')) if form.validate_on_submit(): username = request.form['username'] password = request.form['password'] modeluser = Model() account = modeluser.authethiaction_login(username, password) if account: dictperson = {} username_session = account['username'] session['username'] = username_session session['process_crawling'] = [] session['process_preprocessing'] = [] session['process_training'] = [] dictperson['statuscrawling'] = 0 dictperson['statuspreprocessing'] = 0 dictperson['statustraining'] = 0 chache[username_session] = dictperson return redirect(url_for('index_admin')) else: msg = "Wrong user or password. Please try again." elif 'username' not in session: return render_template('admin/pages/login.html', form=form, msg=msg) else: msg = 'Input error' return render_template('admin/pages/login.html', form=form, msg=msg)