Predictive Hacks

Text Generation for Instagram using N-Grams

Text generation using python for instagram

Text Generation has many approaches such as Keras and Neural Nets but in this post, we are making a simple Text Generation algorithm using N-Grams. We will use the posts of the popular Instagram page secrets2success which contains Images with famous Quotes and motivational phrases and based on this post, we will extract the text of the images to use them as our corpus.

The first step is to extract the posts of the page. We are using the Instagram-Scraper which is a command-line application written in Python that scrapes and downloads an Instagram user’s photos and videos. Through the Instagram-Scraper, a folder including a JSON file containing the data we need will be created.

instagram-scraper 'secrets2success' -u 'username' -p 'password' --maximum 200  --media-metadata  --media-types none

Now that we have the data we can read them using Pandas.

import pandas as pd 
import numpy as np 
import re
import PIL
from PIL import Image
import requests
import pytesseract
from io import BytesIO
from PIL import ImageFilter
from PIL import ImageEnhance
from IPython.display import display


#the following step needed to convert the JSON in a structured dataframe
df = pd.DataFrame(df['GraphImages'].values.tolist())

#we are droping the videos


So now that we have the image-URLs we can create a function to extract the text using Pytesseract. You can learn more about it here.

def extract_text(url):
    response = requests.get(url)
    img =
    text = pytesseract.image_to_string(img.convert('L')) 
    text=re.sub('\n',' ',text)

    #the Qi is how the Pytesseract reads the logo of the page so we are removing it
    text=re.sub(r'\bQi\b',' ',text)
    text=re.sub('\|',' ',text)
    text=re.sub('\s+',' ',text)

#we are applying the function to our URLs

corpus=df['display_url'].apply(lambda x:extract_text(x))

#we are adding an end of sentence word(eos) at the end of the sentence to help
#us know when the sentence ends when generating a sentence
sentences=[i+" eos" for i in corpus]

An easy way to create N-Grams along with their counts is to use CountVectoriser, which will automatically create our preferred N-Grams cleaned from punctuations. Then, by summing up the sparse matrix we get from CountVectoriser we can get their counts. In our case, we are using Tri-Grams.

from sklearn.feature_extraction.text import CountVectorizer
v = CountVectorizer(ngram_range=(3,3))
x = v.fit_transform(sentences)

f = pd.DataFrame(x.toarray()) 

            trigrams  count
2776     type yes if     24
3064      yes if you     24
1140    if you agree     11
3076   you agree eos     10
1142  if you believe      8

So now we have a DataFrame that contains the Tri-Grams of the whole corpus and their count.

How does the N-Gram Text Generator model work?

The logic behind the model is the following – given two words, we check for Tri-Grams that start with these words and the algorithm picks one based on its probability. For example, let’s say we have the words “if” and “you” and there are the following trigrams that start with them along with their counts “if you agree”:25 and “if you believe”:5. The algorithm will pick one of these two based on the probabilities 25/30 for the first one and 5/30 for the second one. This will continue until we have the word “eos” as our next word. The function is the following

The Text Generation Algorithm

def word_gen(x):
    list_words=x.split(' ')
    while list_words[-1]!='eos':
        t=data[data['trigrams'].str.startswith(' '.join(list_words[-2:]))]
        word=np.random.choice(t['trigrams'], p=t['count'])
        list_words.append(word.split(' ')[-1])
    print(' '.join(list_words[:-1]))

Let’s try it!

print(word_gen('2020 is'))
2020 is going to be like the rock that the waves keep crashing over it stands unmoved and the raging of the thoughts you are doing oases.
print(word_gen('if you'))
if you believe in two principles your attitude is more important than your capabilities

So, this is not the best Text Generation algorithm but it is an example that with simple NLP, we can make great NLG models without using complex machine learning algorithms. If you want to learn more about NLG check out this post.


Share This Post

Share on facebook
Share on linkedin
Share on twitter
Share on email

1 thought on “Text Generation for Instagram using N-Grams”

Leave a Comment

Subscribe To Our Newsletter

Get updates and learn from the best

More To Explore


Image Captioning with HuggingFace

Image captioning with AI is a fascinating application of artificial intelligence (AI) that involves generating textual descriptions for images automatically.


Intro to Chatbots with HuggingFace

In this tutorial, we will show you how to use the Transformers library from HuggingFace to build chatbot pipelines. Let’s