visit
As more and more artificial intelligence is entering into the world, more and more emotional intelligence must enter into leadership. -Amit Ray
In this article, we are going to build a Neural Network powered Artificial Intelligence Application that enables you to transfer the tone of your text from Casual-to-Formal, Formal-to-Casual, Active-to-Passive, Passive-to-Active, we will also host our Application on Hugging Face spaces.
For this article, we will be making use of the Open Source library
StyleFormer is a Neural Language Style Transfer framework that transfers natural language text smoothly between fine-grained language styles like formal/casual, active/passive, and many more. It was created by
If at any point during the tutorial, you can always refer to:
pip install gradio
!pip install gradio
pip install git+//github.com/PrithivirajDamodaran/Styleformer.git
!pip install git+//github.com/PrithivirajDamodaran/Styleformer.git
To install your version of PyTorch visit the
#Importingdependancies
from styleformer import Styleformer
import gradio as gr
import torch
import warnings
warnings.filterwarnings("ignore")
Above we imported our dependent libraries and told python to ignore all warnings in the last line.
def set_seed(seed):
torch.manual_seed(seed)
if torch.cuda.is_available():
torch.cuda.manual_seed_all(seed)
set_seed(1234)
Above we set the torch seed in other to enable reproducibility. Unfortunately, explaining this is way out of the scope of this article but if you want to know more about this feature, check out the
#Casual-Formal
sf_0 = Styleformer(style=0)
#Formal-Casual
sf_1 = Styleformer(style=1)
#Active-Passive
sf_2 = Styleformer(style=2)
#Passive-Active
sf_3 = Styleformer(style=3)
NOTE: For the style argument, you pass 0 for the Casual to Formal model, 1 for the Formal to Casual model, 2 for the Active to Passive model, 3 for the Passive to Active model.
def func(text, tone):
if tone=="Casual-Formal":
return sf_0.transfer(text)
elif tone=="Formal-Casual":
return sf_1.transfer(text)
elif tone=="Active-Passive":
return sf_2.transfer(text)
eliif tone=="Passive-Active":
return sf_3.transfer(text)
else:
return "No available Transfers"
Above we created a function that will act as a workflow to tell gradio how we plan to process our input text, we created conditional statements which will enable gradio to efficiently and correctly send text input to the model requested by the user.
#Initalizing Gradio App
app_description = "This model transforms the tone of the text, from formal to informal, from Active to Passive. Choose your option below."
app_title = "Tone Transfer"
app = gr.Interface(func,["text",gr.inputs.Radio(["Casual-Formal", "Formal-Casual", "Active-Passive","Passive-Active"])],"text",description=app_description, title=app_title)
app.launch()
Above we wrote down the description and the title of our application. We will pass the variables later into our app Interface.
For the app interface, we passed our wrapping function, our input UI component is made up of a text box that will take in the user's text, then a radio where the user will choose what style process he would like to undertake either Casual-to-Formal, Formal-To-Casual, etc.
We then passed our description and title variables.
To host on Hugging face spaces, create a
When you want to push the final repo containing your
git+//github.com/PrithivirajDamodaran/Styleformer.git
torch