DEV Community

Yogesh Ingale
Yogesh Ingale

Posted on

How I Created Quotes Generator Website Using Python

Eureka!

While surfing through my Instagram feed, a random thought struck in my head. How about creating a tool that gives me random quotes by analyzing the picture and moods. I can’t get this thought out of my mind and I geared up to build something exciting.

In this post, I have shared my experience in each stage of building https://quotes-maker.com/ website, and here is the code.

Searching Dataset

First I wanted a large dataset for quotes/captions. Websites like kaggle.com are best for such datasets. I found a Dataset with 500,000 quotes on https://www.kaggle.com/manann/quotes-500k.

Dataset Cleanup

This dataset has 3 columns:

quote, author, category
“you were chasing stones forever forgetting about diamonds”, Skye Elf, [life, inspirational]

I needed more to it, as you must have seen on the website, quotes are generated based on objects from the image and user-selected moods.
objects: I used NLP from spacy which made it really easy to extract objects from any complex sentence. It goes like -

import spacy

nlp = spacy.load("en_core_web_sm")
OBJECTS = {"dobj", "dative", "attr", "oprd"}
doc = nlp("Have enough courage to trust love one more time and always one more time.")
objects = [tok for tok in doc if (tok.dep_ in OBJECTS)]

print(objects)
>>> [courage, love]
Enter fullscreen mode Exit fullscreen mode

moods: Existing dataset had a well-defined category column (“life, inspirational” from the above example). I just split them to get moods of sentences.

Where should I put this data?

I used MongoDB to host the dataset as a database. I used MongoDB’s Free cluster with 512 MB of storage free which was more than enough for me.
Deployment on Mongo is quite easy. Convert the CSV to JSON and insert it into Mongo.

# Import json to db
with open("quotes.json") as f:
    file_data = json.load(f)

# Insert in DB
import pymongo

client = pymongo.MongoClient("mongodb+srv://db-name:password@cluster0-9y16x.mongodb.net/db-name")
db = client.collection_name
db.collection_name.insert_many(file_data)
Enter fullscreen mode Exit fullscreen mode

AI Magic

To do image analysis in python you don’t need to be an AI expert or know complex algorithms, all you need to know is how to use existing AI APIs. I used Amazon Rekognition to analyze the image. This gives me the objects/labels present in the image.

import boto3

boto3.client("rekognition").detect_labels(Image={"Bytes": encoded_image_string})
Enter fullscreen mode Exit fullscreen mode

Alt Text

Put it all together!

Flask

There are many Website generators out there. I used AppSeeds and Creative Tim’s theme for this site. It needed some refinement which is done by youngsoul in here.

Flask’s Blueprints are used in the project to create components for each part of the site, for example, authorizers, DB service, main pages.

I added my logic in above flask template and we are good to go http://quotes-maker.com :)

Hosting and Deployments

Till now all the resources used are free and guess what rest is also free except buying a new domain name.

Heroku’s Free and Hobby pack is enough for handling small applications like mine. For the deployments, the integration of Travis and Heroku is cherry on top. You just need to add below code in .travis.yml file and on each commit your application will be automatically deployed in Heroku:

.travis.yml

deploy:
  provider: heroku
  api_key: ...
  app:
    stage: my-app-staging
    master: my-app-production
Enter fullscreen mode Exit fullscreen mode

Domain and CDN

Once the site is deployed on Heroku, we need to add a domain in front of the site. I purchased the domain from GoDaddy and added it to Cloudflare.

Cloudflare is a network of data centers that sits between your web server and the rest of the internet. It’s a great tool that provides Free SSL, traffic insights and protects the site from multiple attacks. Once the site is added in Cloudflare you need to change it’s NameServers and add DNS.

You can also create your own site and host it on the internet. I tried to share the steps I followed to get one of my weird and crazy idea out in the world, I hope it helps.

Oldest comments (2)

Collapse
 
lachlanvass profile image
lachlanvass

Great example of how to pull together various resources (cloud, backend) to create a functional application

Collapse
 
yogingale profile image
Yogesh Ingale

Thanks for the kind words!