Пример #1
0
def post_predict_day():
    answer = {}
    content = request.json
    if 'time' in content and 'title' in content and 'text' in content and 'subreddit' in content:
        time = content['time']
        title = content['title']
        text = content['text']
        subreddit = content['subreddit']
        if subreddit > 0 and subreddit <= len(rd.subreddit_list):
            titles, times, subreddits, texts = rd.dailydata(
                title, time, subreddit, text)
            predictions = rm.getprediction(model, titles, times, subreddits,
                                           texts)
            answer = {"times": times, "predictions": predictions}
        else:
            answer = {
                "error":
                "Subreddit must be an integer between 1 and " +
                str(len(rd.subreddit_list))
            }
    else:
        answer = {
            "error":
            "Missing one or more fields. Please provide time, title, text, and subreddit"
        }
    return jsonify(answer)
Пример #2
0
def post_predict():
    answer = {}
    content = request.json
    if 'time' in content and 'title' in content and 'text' in content and 'subreddit' in content:
        time = [content['time']]
        title = [content['title']]
        text = [content['text']]
        subreddit = [content['subreddit']]
        if subreddit[0] > 0 and subreddit[0] <= len(rd.subreddit_list):
            predictions = rm.getprediction(model, title, time, subreddit, text)
            answer = {"prediction": predictions[0]}
        else:
            answer = {
                "error":
                "Subreddit must be an integer between 1 and " +
                str(len(rd.subreddit_list))
            }
    else:
        answer = {
            "error":
            "Missing one or more fields. Please provide time, title, text, and subreddit"
        }
    return jsonify(answer)
#mongodb setup
connection_string = "mongodb://localhost:27017" if len(
    sys.argv) == 1 else sys.argv[1]
client = MongoClient(connection_string)
db = client["reddit-comment-vote-predictor"]
collection = db.comments

model = rm.getmodelandweights()

comments = []
database_comments = collection.find().limit(200)

for comment in database_comments:
    comments += [comment]

titles = [c['submission_title'] for c in comments]
times = [c['timepostedutc'] for c in comments]
subreddits = [rd.convertsubreddittoint(c['subreddit']) for c in comments]
texts = [c['text'] for c in comments]

predictions = rm.getprediction(model, titles, times, subreddits, texts,
                               collection)

predictions.sort()
predictions = rd.removedecimals(predictions)

print(predictions)

print("Max prediction: " + str(max(predictions)))
print("Min prediction: " + str(min(predictions)))
modelgenerative = rmg.getmodel(vocab_size=vocab_size,
                               embedding_dim=rmg.embedding_dim,
                               rnn_units=rmg.rnn_units,
                               batch_size=1)

modelgenerative.load_weights(rmg.checkpoint_dir)

modelgenerative.build(tf.TensorShape([1, None]))

#Model which predicts if a comment will be removed on /r/science
modelscience = rms.getmodelandweights()

commentstoremove = []
obtainedcommentstoremovetime = None

predictions = rm.getprediction(model, ["title"], [111111], [1], ["text"],
                               collection)

print("Predictions:")
print(predictions)

predictions_science = rms.getprediction(modelscience, ["title"], ["text"])

print("Science predictions:")
print(predictions_science)

generatedtext = rmg.generatesentence(modelgenerative, "This ", char2idx,
                                     idx2char)

print("Generated text:")
print(generatedtext)