-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
90 lines (63 loc) · 2.59 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import numpy as np
from flask import Flask, request, jsonify, render_template
import pickle
from nltk.corpus import stopwords
import sklearn
import pickle
import praw
import re
from bs4 import BeautifulSoup
import nltk
app = Flask(__name__, static_url_path='/static')
model = pickle.load(open('model.pkl', 'rb'))
REPLACE_BY_SPACE_RE = re.compile('[/(){}\[\]\|@,;]')
BAD_SYMBOLS_RE = re.compile('[^0-9a-z #+_]')
STOPWORDS = set(stopwords.words('english'))
def clean_text(text):
text = BeautifulSoup(text, "lxml").text
text = text.lower()
text = REPLACE_BY_SPACE_RE.sub(' ', text)
text = BAD_SYMBOLS_RE.sub('', text)
text = ' '.join(word for word in text.split() if word not in STOPWORDS)
return text
@app.route('/')
def home():
return render_template('index.html')
@app.route('/predict',methods=['POST'])
def predict():
reddit = praw.Reddit(client_id='V4ajjSi0MXlYDw', client_secret='4GrGn3IamlL5pLNvVFlk4GmZ1XQ', user_agent='research_pur')
int_features = request.form['redditurl']
processed_text = int_features.lower()
print(processed_text)
submission = reddit.submission(url=processed_text)
data = {}
data['title'] = submission.title
data['title_un'] = submission.title
data['url'] = submission.url
data['selftext'] = submission.selftext
submission.comments.replace_more(limit=None)
comment = ''
for top_level_comment in submission.comments:
comment = comment + ' ' + top_level_comment.body
data["comment"] = comment
data['title'] = clean_text(data['title'])
data['comment'] = clean_text(data['comment'])
data['combine'] = data['title'] + data['comment'] + data['url']
# prediction = model.predict(int_features)
output = model.predict([data['combine']])
return render_template('index.html', prediction_text='The flair for the subreddit post is : {}'.format(output))
@app.route('/about',methods=['POST'])
def about():
reddit = praw.Reddit(client_id='V4ajjSi0MXlYDw', client_secret='4GrGn3IamlL5pLNvVFlk4GmZ1XQ', user_agent='research_pur')
int_features = request.form['redditurl']
processed_text = int_features.lower()
print(processed_text)
data = {}
submission = reddit.submission(url=processed_text)
data['title'] = submission.title
data['title_un'] = submission.title
data['url'] = submission.url
data['selftext'] = submission.selftext
return render_template('about.html', title = data['title_un'], About = data['selftext'])
if __name__ == "__main__":
app.run(host="localhost", port=8090, debug=True)