<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Yogesh Ingale</title>
    <description>The latest articles on DEV Community by Yogesh Ingale (@yogingale).</description>
    <link>https://dev.to/yogingale</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F440642%2F9abb9d18-8005-4c36-84c8-0620ab20c4b1.jpeg</url>
      <title>DEV Community: Yogesh Ingale</title>
      <link>https://dev.to/yogingale</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/yogingale"/>
    <language>en</language>
    <item>
      <title>How I Created Quotes Generator Website Using Python</title>
      <dc:creator>Yogesh Ingale</dc:creator>
      <pubDate>Sat, 25 Jul 2020 22:48:15 +0000</pubDate>
      <link>https://dev.to/yogingale/how-i-created-quotes-generator-website-using-python-4ln9</link>
      <guid>https://dev.to/yogingale/how-i-created-quotes-generator-website-using-python-4ln9</guid>
      <description>&lt;h1&gt;
  
  
  Eureka!
&lt;/h1&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;In this post, I have shared my experience in each stage of building &lt;a href="https://quotes-maker.com/" rel="noopener noreferrer"&gt;https://quotes-maker.com/&lt;/a&gt; website, and here is the &lt;a href="https://github.com/yogingale/quotes-maker-app" rel="noopener noreferrer"&gt;code&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Searching Dataset
&lt;/h2&gt;

&lt;p&gt;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 &lt;a href="https://www.kaggle.com/manann/quotes-500k" rel="noopener noreferrer"&gt;https://www.kaggle.com/manann/quotes-500k&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Dataset Cleanup
&lt;/h2&gt;

&lt;p&gt;This dataset has 3 columns:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;quote, author, category&lt;br&gt;
“you were chasing stones forever forgetting about diamonds”, Skye Elf, [life, inspirational]&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;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.&lt;br&gt;
objects: I used NLP from spacy which made it really easy to extract objects from any complex sentence. It goes like -&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;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)
&amp;gt;&amp;gt;&amp;gt; [courage, love]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;h2&gt;
  
  
  Where should I put this data?
&lt;/h2&gt;

&lt;p&gt;I used MongoDB to host the dataset as a database. I used MongoDB’s &lt;a href="https://docs.atlas.mongodb.com/tutorial/deploy-free-tier-cluster/#procedure" rel="noopener noreferrer"&gt;Free cluster&lt;/a&gt; with 512 MB of storage free which was more than enough for me.&lt;br&gt;
Deployment on Mongo is quite easy. Convert the CSV to JSON and insert it into Mongo.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# 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)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  AI Magic
&lt;/h2&gt;

&lt;p&gt;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.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import boto3

boto3.client("rekognition").detect_labels(Image={"Bytes": encoded_image_string})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fbtc7m4ldyqse7nrq1jm5.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fbtc7m4ldyqse7nrq1jm5.jpg" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Put it all together!
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Flask
&lt;/h3&gt;

&lt;p&gt;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 &lt;code&gt;youngsoul&lt;/code&gt; in &lt;a href="https://github.com/youngsoul/flask_starter" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

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

&lt;p&gt;I added my logic in above flask template and we are good to go &lt;a href="http://quotes-maker.com" rel="noopener noreferrer"&gt;http://quotes-maker.com&lt;/a&gt; :)&lt;/p&gt;

&lt;h2&gt;
  
  
  Hosting and Deployments
&lt;/h2&gt;

&lt;p&gt;Till now all the resources used are free and guess what rest is also free except buying a new domain name.&lt;/p&gt;

&lt;p&gt;Heroku’s &lt;a href="https://www.heroku.com/pricing" rel="noopener noreferrer"&gt;Free and Hobby pack&lt;/a&gt; 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:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.travis.yml

deploy:
  provider: heroku
  api_key: ...
  app:
    stage: my-app-staging
    master: my-app-production
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Domain and CDN
&lt;/h2&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://community.cloudflare.com/t/step-1-adding-your-domain-to-cloudflare/64309" rel="noopener noreferrer"&gt;Cloudflare&lt;/a&gt; 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.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

</description>
      <category>python</category>
      <category>aws</category>
      <category>architecture</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
