Skip to content

Commit

Permalink
`Model integrated
Browse files Browse the repository at this point in the history
Signed-off-by: Yash Srivastava <yashsrivastava3005@gmail.com>
  • Loading branch information
Yash Srivastava committed Feb 9, 2019
1 parent 4637731 commit f002726
Show file tree
Hide file tree
Showing 32 changed files with 82 additions and 7 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
.vscode
.DS*
**/.DS*
**/__*/
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
63 changes: 63 additions & 0 deletions flair_detector/reddit_flair_prediction.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# -*- coding: utf-8 -*-
"""Reddit Flair Prediction.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/github/radonys/Reddit-Flair-Detector/blob/master/Reddit_Flair_Prediction.ipynb
## Reddit India Flair Prediction
### Install and Import Required Modules
"""

#!pip install scikit-learn praw nltk

import sklearn
import pickle
import praw
import re
from bs4 import BeautifulSoup
import nltk
nltk.download('all')
from nltk.corpus import stopwords

"""### Variable Declarations and Utility Functions"""

reddit = praw.Reddit(client_id='_4Z0SOGEI31a3Q', client_secret='fzhJE6_cjPqLN6goG4R7cJ6SK3Q', user_agent='reddit-flair-detector', username='radonys', password='Yash@1234')
#loaded_model = pickle.load(open('finalized_model.sav', '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

"""### Detect Reddit India Post Flair"""

def detect_flair(url,loaded_model):

submission = reddit.submission(url=url)

data = {}

data['title'] = submission.title
data['url'] = submission.url

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']

return loaded_model.predict([data['combine']])
File renamed without changes.
2 changes: 1 addition & 1 deletion website/flair_detector/urls.py → flair_detector/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"""
from django.conf.urls import url
from django.contrib import admin
import views
from . import views

urlpatterns = [
url(r'^$', views.index, name="index")
Expand Down
15 changes: 11 additions & 4 deletions website/flair_detector/views.py → flair_detector/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,19 @@
from __future__ import unicode_literals

from django.shortcuts import render, HttpResponse

from django.conf import settings
from . import reddit_flair_prediction as rdf
# Create your views here.
def index(request):

if request.method == 'POST':

model = settings.MODEL_FILE
val = request.POST.get('url')
print(val)
return render(request,"flair_detector/index.html",{"output":val})
return render(request,"flair_detector/index.html",{"output":rdf.detect_flair(val,model)[0]})

return render(request,"flair_detector/index.html")




return render(request,"flair_detector/index.html")
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ <h1>Reddit Flair Detector</h1>
<center><button type="submit" class="btn btn-default">Submit</button></center>
</form><br><br>

{{output}}
<center><h2>{{output}}</h2></center><br>
</div>
<div class="container-fluid bg-grey">
<h5 align="right">Developed by <a href = "https://github.com/radonys">Yash Srivastava</a></h5>
Expand Down
File renamed without changes.
Binary file removed website/.DS_Store
Binary file not shown.
File renamed without changes.
Binary file removed website/flair_detector/__init__.pyc
Binary file not shown.
Binary file removed website/flair_detector/admin.pyc
Binary file not shown.
Binary file removed website/flair_detector/migrations/__init__.pyc
Binary file not shown.
Binary file removed website/flair_detector/models.pyc
Binary file not shown.
Binary file removed website/flair_detector/urls.pyc
Binary file not shown.
Binary file removed website/flair_detector/views.pyc
Binary file not shown.
4 changes: 4 additions & 0 deletions website/website/settings.py → website/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"""

import os
import pickle

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
Expand Down Expand Up @@ -119,3 +120,6 @@
# https://docs.djangoproject.com/en/1.11/howto/static-files/

STATIC_URL = '/static/'

MODEL_PATH = os.path.join(BASE_DIR,'Models/finalized_model.sav')
MODEL_FILE = pickle.load(open(MODEL_PATH,'rb'))
Binary file removed website/templates/.DS_Store
Binary file not shown.
File renamed without changes.
Binary file removed website/website/__init__.pyc
Binary file not shown.
Binary file removed website/website/settings.pyc
Binary file not shown.
Binary file removed website/website/urls.pyc
Binary file not shown.
Binary file removed website/website/wsgi.pyc
Binary file not shown.
File renamed without changes.

0 comments on commit f002726

Please sign in to comment.