Skip to content

prajwalcr/AutoCompose

Repository files navigation

AutoCompose

Open Source? Yes! MIT license Website shields.io

home screen

Table of Contents

Technologies Used

GutenTag

GutenTag tool was used to build the required poetry corpus from the Project Gutenberg website which is a massive online database containing over 59000 eBooks.

EmoLex

EmoLex is a crowdsourced dataset of associations between words and emotions. It was used to classify the poetry corpus based on emotion of the poems. A language model was built for each emotion by using poems corresponding to that emotion.

HuggingFace

HuggingFace is an AI community that provides various open source models in natural language processing. OpenAI's GPT-2 model was used from HuggingFace's model hub which was fine-tuned on a large poetry corpus.

Flask

Flask is a micro web framework written in Python. It was used to build a web app which is used to interact with the fine-tuned GPT-2 models.

Algorithmia

Algorithmia is a MLOps tool that provides a simple and faster way to deploy your machine learning model into production. It was used to build a web API to interact with the model. Users can make API requests to the model through the flask app.

Heroku

Heroku is a platform as a service (PaaS) that enables developers to build, run, and operate applications entirely in the cloud. It was used to deploy the web app so that it could be accessed by users from all over the world 24x7.

About the Project

Idea

This project was developed with the aim of creating a tool that can automatically compose original poetry within an instant embedding the required emotion. OpenAI's GPT-2 was chosen as the base language model because it's transformer architecture with masked self-attention heads has enabled it to perform exceptionally well with generation related tasks.

Data

The dataset was obtained from the Project Gutenberg website. The EmoLex tool was used to segment the poetry corpus into 8 different emotion classes - anger, anticipation, disgust, fear, joy, sadness, surprise and trust. The poems which did not fit into any of these categories were added into the neutral category.

Models

Initially, the GPT-2 model was trained on the neutral poems. This gave us the 'neutral' model to generate any general poem. Models for each emotion were created by fine-tuning this 'neutral' model on poems of corresponding emotions. The entire process of building these models can be found in colab Open In Colab. These models can also be found in the Huggingface model hub.
Following are the links to the models of each emotion:

Deploying Working Demo

Algorithmia was used to build an API to interact with these models. A flask app was created to allow users to send API requests to Algorithmia and receive the generated poems. The app was deployed and made live using Heroku.

Directory Structure

📦AutoCompose
┣ 📂data           --> Contains entire poetry corpus as well as poem collections of each emotion stored in json format
┣ 📂models         --> Contains language models fine-tuned for each emotion
┃ ┣ 📂anger
┃ ┣ 📂anger2
┃ ┣ 📂anticipation2
┃ ┣ 📂disgust2
┃ ┣ 📂fear
┃ ┣ 📂fear2
┃ ┣ 📂joy
┃ ┣ 📂joy2
┃ ┣ 📂neutral
┃ ┣ 📂sadness2
┃ ┣ 📂surprise2
┃ ┣ 📂trust2
┣ 📂notebooks      --> Contains .ipynb files used to work with dataset and train the model
┣ 📂outputs        --> Contains 25 poems generated by the model for each emotion
┣ 📂readme-assets  --> Contains images and gifs used in readme file
┣ 📂src            --> Contains flask app and other supporting python files
┃ ┣ 📂static
┃ ┃ ┗ 📂images
┃ ┣ 📂templates

Sample Poems

Anger

Angry Thoughts

I am not jealous; but this does vex
My heart so full of misery that I cannot stop
From angry thoughts.  This brings me relief,
And a painful torment; and I
Am content to part from thee alone.

Anticipation

God in Mystery

Thence down the river to the mountain-side
I travelled with my boats and my oarsmen,
To seek a haven near the southern cape
Where dwelt some mighty god in mystery,
Unknowing I wandered therefrom, so
Unconscious I lay, and rested there.

Disgust

Daisies and Filth

The little pool, by the house-wall,
Is covered with daisies and filth;
The fountains are withered and dim,
And the lizards writhe in rags.

Fear

I See Thee in a Far Corner

I see thee in a far corner,
With a stealthy stride, and a trembling soul
I know thou, Dear, who should'st not fear
That I should come near thee!
I know thee.

Joy

Gentle-Throat

Thy soul has joy, and thy soul is free,
And all the things it loves can live
In thy love for me and thee!
O gentle-throat, why sing so low,
Or do thy music too loudly?
Whence are all thy numbers fit?
If that thy mind is fix'd upon one,
It joys not to hear thee speak.

Neutral

Moon

He took them home, and soon set them down
In a chest at the door of his room,
And asked them if they knew
What the moon was about.

Sadness

Dear Heart

For you, dear heart, so lone and worn and weary,
Hear me, and the silence dies,
That still I think you must come near me,
And not so be blest.

Surprise

Nature's Own

He is a wild magician,
Who takes up the air and vanishes
As though his name were Nature's own,
And that it were Nature's own prompting,
That he should leave school in such a way.

Trust

Damsels in Disguise

He sees the aged damsels in disguise,
And how the princely damsels are dressed;
The queen of Sariola, as she stands,
Is clad in rich embroidery;
And every thing that she does wear
Is a royal dame in finest form,
And a queen in fairest posture too.