Skip to content

Commit

Permalink
setup integrating wordcloud viz
Browse files Browse the repository at this point in the history
  • Loading branch information
noah40povis committed Jul 30, 2020
1 parent 784261f commit cb178a1
Show file tree
Hide file tree
Showing 3 changed files with 141 additions and 6 deletions.
2 changes: 2 additions & 0 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,7 @@ bs4 = "*"
matplotlib = "*"
pydantic = "*"
joblib = {version = "0.16.0"}
stylecloud = "*"

[requires]
python_version = "3.7"
114 changes: 109 additions & 5 deletions Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 30 additions & 1 deletion app/api/viz.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@
import pandas as pd
import plotly.express as px
import plotly.graph_objects as go
import stylecloud

from .predict import Item
from .predict import Item, pred
from joblib import load

router = APIRouter()
sfw_model = load('nn_cleaned.joblib')
sfw_tfidf = load('tfidf_cleaned.joblib')
ns_model = load('subreddit_mvp.joblib')

tfidf = sfw_tfidf
model = sfw_model
Expand Down Expand Up @@ -48,3 +50,30 @@ async def viz(postbody: Item):

return fig.to_json()


@router.post('/wordclouds'):
async def wordclouds(postbody: Item):
"""generate word clouds
"""
df = pd.read_csv('25325_subreddits.csv')
list_in = pred(postbody, model= ns_model)['recommendations']
data = {} #dict of serialized imgs
for i, subreddit in enumerate(list_in):
x = df[df['subreddit']== subreddit]
y = x['text'].str.cat(sep=', ')
filename = f'reddit{i}.png'
stylecloud.gen_stylecloud(text = y,
icon_name='fab fa-reddit-alien',
palette='colorbrewer.diverging.Spectral_11',
background_color='black',
gradient='horizontal',
output_name=filename)


with open(filename, mode='rb') as file:
img = file.read()
key = f"img{i}"
data[key] = base64.encodebytes(img).decode("utf-8")

return data

0 comments on commit cb178a1

Please sign in to comment.