IBepyProgrammer

How to do Grammar correction using Python and Gramformer

3 min read
How to do Grammar correction using Python and Gramformer

What is Gramformer?

Gramformer is a model that improves and corrects grammar with a few lines of code. It was created by Prithiv-Raj-Dhamodharan and all the documentation and resources can be found in his GitHub repository, https://github.com/PrithivirajDamodaran/Gramformer. Some of the use cases for Gramformer include:

  1. Performing post-processing to machine-generated text.
    Machine language generation is rapidly gaining popularity and it is expected that post-processing of machine-generated text will become a common practice soon. These include:
  • Machine Translated output.
  • Speech-to-text output.
  • Handwritten text recognition output.
  • Text Summarization Output.
  • Image caption output.
  • Data to Text output.
  • Text generated using controlled text generation techniques.
  1. Assisted writing is a tool designed to help humans improve their writing skills. It corrects spelling, grammar, and punctuation errors to make the text clearer and more concise. This can also be integrated into custom Text editors of your Apps

Setup Gramformer

Since Gramformer is dependent on Pytorch. We will first need to install Pytorch. We can do so by navigating to the official Pytorch site and configuring your installation. All the installation instructions can be found at “ https://pytorch.org/get-started/locally/ ”.

We can then proceed to install Gramformer using pip. This is done by opening your terminal and running the command below:

pip install -U git+https://github.com/PrithivirajDamodaran/Gramformer.git

If you are running the project using Jupyter Notebook, you can run the command in the Jupyter Notebook cell by adding “!” before the pip as shown below:

!pip install -U git+https://github.com/PrithivirajDamodaran/Gramformer.git

After the two dependencies are installed, we can now install Gradio using pip as we are planning to show how we can our grammar correction code into a simple app.

!pip install gradio

Build the program

After all of our installations are complete, we can then import and instantiate Gramformer to utilize all the functionality available using the model.

We instantiate Gramformer by:

from gramformer import Gramformer

We then store the Gramformer class in a variable:

gf = Gramformer(models=1, use_gpu=False)

From the Gramformer documentation, when we specify models=0 will load the detector, models=1 will load the highlighter, models=2 will load the corrector, and models=3 will load all three.

At the time of writing this article, the detector was not available but the highlighter and the corrector were available and could be used.

Note that "use_gpu" is not critical. We can set it to "True" if you have a dedicated NVidia or AMD GPU. For NVidia GPUs, you will have to install CUDA and cuDNN and as for AMD GPUs, you will have to install ROCm.

You can set "use_gpu=True" if you are also on Google Colab

We can now run grammar correction using the Gramformer model. We can demonstrate the model using the following example:

sentence = str(input("Enter sentence: "))

gf.correct(sentence)

Note that input can be passed to the gramformer model if it is a string. The returned output will be stored inside a list.

We can now use Gradio and test our code in a graphical user interface or even our web browser.

Let's start by importing the Gradio library and then create a function that will instantiate the Gramformer model and then be packaged in the Gradio interface.

import gradio as gr
def correct_sentence(sentence):

    result = gf.correct(sentence)

    return result

We can then use the function created above in our gradio interface and launch the application.

test_app = gr.Interface(fn=correct_sentence,

                        inputs="text",

                        title="Gramformer Example",

                        outputs="text")

test_app.launch()
Error encountered during project development and how it was solved below.

Some of the errors encountered were solved by opening our terminal and running these commands.

python -m spacy download en_core_web_lg
python -m spacy download en_core_web_sm

Conclusion

In this article, we learn how we can use Gramformer to accomplish grammar correction tasks by writing a simple Python script and deploying a working application using Gradio. All the resources are linked to the article and you can also find a working project on my GitHub repository https://github.com/IBepyProgrammer/How-to-use-Gramformer.

If you found this article helpful consider subscribing and sharing.

Thank you.

Sign up for our newsletter

Don't miss anything. Get all the latest posts delivered to your inbox. No spam!