def move_article(): article_id = request.json["article_id"] cluster_from = request.json["cluster_from"] cluster_to = request.json["cluster_to"] if not article_id or not cluster_from or not cluster_to: return jsonify({ "error": "argument `article_id`, `cluster_from` and `cluster_to` must be in JSON input" }), 400 # Get info from database result = db["articles"].find_one({"_id": bson.ObjectId(oid=article_id)}) cluster_from = db["clusters"].find_one( {"_id": bson.ObjectId(oid=cluster_from)}) cluster_to = db["clusters"].find_one( {"_id": bson.ObjectId(oid=cluster_to)}) if not cluster_from or not cluster_to or not result: return jsonify({"error": "An id is invalid"}), 400 article = Article.from_database(db, result) transformer_outputs = models["categorisation"].transformer( np.array([article.token_ids]).reshape(1, -1))[0][0] article.cls_token = transformer_outputs[0].numpy().tolist() clusters.move_article(article, cluster_from, cluster_to) return jsonify({"success": True}), 200
def get_article(): article_id = request.args.get("_id") if article_id == None: return jsonify({"error": "You must set an article id"}), 400 result = db["articles"].find_one({"_id": bson.ObjectId(oid=article_id)}) if result: return jsonify(Article.from_database(db, result).toJson(categories)) else: return jsonify({"error": "No article found with this id"}), 400
def cluster_from_database(): ''' Recreate clusters ''' # Remove clusters db["clusters"].drop() db["event"].drop() clusters.p_micro_clusters = [] clusters.o_micro_clusters = [] if clusters.simulation: clusters.t = dt.timestamp() # Get all articles results = db["articles"].find() articles = [] for result in results: articles.append(Article.from_database(db, result)) threading.Thread(target=batch_fit, args=([articles])).start() return jsonify({"success": True})
def get_article_list(): ''' Get all articles since date ''' from_date, err = utils.date_from_request(request, "from") if err: return err, 400 to_date, err = utils.date_from_request(request, "to", allow_missing=True) if err: return err, 400 results = db["articles"].find({ "created_at": { "$gte": from_date.timestamp(), "$lt": to_date.timestamp() } }) articles = [] for result in results: articles.append(Article.from_database(db, result).toJson(categories)) return jsonify({"articles": articles})