Methods for Text Style Transfer: Text Detoxification Case

Firstly, we will shortly introduce the research direction of NLP for Social Good. Then, we will show the main direction of research in text style transfer field. This field suffers from the lack of parallel data. We will describe our approach for such parallel dataset collection and show that it can be applied for any language. Then, we will show how monolingual, multilingual, and cross-lingual models can be trained for texts detoxification. In the end, we will discuss ethical issues connected with this task and tackling of toxic and hate speech in general. The whole presented work is based on the peer-reviewed papers from ACL and EMNLP conferences.

This session took place in track Natural Language Processing and was classified suitable for intermediate domain / intermediate python by the speaker.

Transcript (auto)

Auto-generated from the recording utilizing Open-Source AI. Speaker labels (Speaker 1, Speaker 2) reflect diarization, not identity. Timestamps refer to the recording.

Speaker 1 [00:02]

Hello everyone, thank you very much for coming and thank you for joining online. Yeah, so as it was said, right now I'm obtaining a position in academia and this talk will be about the results from my PhD research and yeah so in this talk it will be an attempt how to connect academic results in all this rapid AI development system and with maybe future industrial real cases. So also I will make a small remark that this talk requires that you're familiar with transformer models, so these are very modern NLP models and that maybe you once in your life have already tried to download models from Hagen phase and maybe tried to continue them, so I will assume that you are familiar with this. So the fight of toxicity I would say here was rapidly developed since 2021. Several companies tried to somehow fight toxic language on their platforms and Instagram announced a model integrated into their platform where they can detect toxic messages, toxic responses by keywords and you can block or hide such kind of messages and users. Also Facebook introduced a kind of like the same approach how to fight toxic messages and hateful messages so for instance if your moderator is some group and the system can detect that maybe the conversation is already at very high degree and it can announce that yeah please take a look and take care of conversation in your group and yeah so indeed there can be a problem unfortunately in social networks when users can be toxic in conversations or in the comments and unfortunately right now the majority of the platforms they just blocking this message but we saw that maybe it can be a nice idea if the message would not be blocked stopping the conversation at all but maybe the system can recommend that yeah your message is toxic but maybe you can rephrase it like this yeah just recommendation on the other hand toxic can not be just only humans but also can be chatbots and if the chatbot is trained on some large internet data yeah it can be at some case also be toxic and there was even cases also from chatbots of big companies. And so in the end, right now, at very huge rapid development of large language models, indeed the responses from these models can be indeed quite toxic. So you are very aware of CharGPT and even of GPT-4. So these models can have different roles. You can assign a role to these models. And at some point, if you assign the role to their different historical personalities, it can become toxic at some point so i have a link here to the scientific article where the success was discovered and researched in different cases so yeah you can refer to this article more for more details yeah so right now i will be as this academic data scientist and i will tell you that uh if you want to perform textile transfers so what we want to find out we want to find out such a function that given a text in some source style and a target style where we want to transfer for this text, then we want to produce a new text that will be indeed in a new style that was taken as an input. Also the context of the text will be preserved as much as possible and as much as needed for the task, and also the text will be fluent. And these three criteria can be assured with some specific functions. So in terms of detoxification, what does it mean in normal language? So we want to indeed transfer style of text from toxic to non-toxic, also we want to save the meaningful content of the sentence, of the text, and also of course we want to have our text fluent. And yeah, so here you can see examples what we want to achieve with our model. But yeah, if we talk about text on transfer task in general, all approaches that was developed before can be divided into two big groups unsupervised approaches and supervised approaches. What is the difference? The difference is that if you have only some textification corpus here because for toxic language there are a lot of corpus where you have labels the text is toxic or non-toxic but these texts are not parallel and you want to somehow to transfer the style or it can be formal informal any textification task. So if you have only such corpus and And these pairs are not parallel, as in machine-trained task, that you need somehow to find out some heuristic how you can transfer the style. One of the approaches can be pretty straightforward. So for instance, talking about toxicity case, you can just delete maybe some obscene lexicon, or you can just delete some bad words and that's it. But in this case, you can lose some content if you just delete the words from your text. Maybe you can try to find a more sophisticated approach and you can try to find the substitution of this phrase that you want to delete, maybe in some non-toxic part of your corpora. So maybe something with cosine similarity, you can just find the text more similar to the original text. What else we can use? So we can actually use the power of transformer models, and if we think about BERT model, one of the tasks at which it was pre-trained, it was masking modeling. So how we can apply it for our case? For instance, we can, yeah, So we have an input, some attacks with some toxic part. We can detect this toxic part with some actually not so sophisticated model. So it can be even just some low progression classification model just retrained on the corpora and it can already work pretty good. So we can then mask this model, mask this token, sorry, and then we can ask the model to substitute, to finance substitution for this masked part. And actually, yes, as Bert was retrained this specific task, it will find out for you pretty similar to the original text substitution. The only thing is here is that we also need to ensure that the substitution is also in the correct style. So in our case, it's already also non-toxic. So we can as well use the scores from classifiers that we did for our first step and rerun the candidates that the content is similar to the original token, and also the style of this token is also non-toxic. And yeah, so in the end we can have pretty fluent sentence. So unfortunately the problem with these models can be that indeed substitution can work not so good, and maybe in the end we need to substitute not just like token by token, yeah, word by word, but maybe it can be a phrase, it can be just several tokens. And the task of generating a phrase from a mask for such language models is actually quite challenging task. So this can work, first of all, but this must have some drawbacks. But if we are lucky, and if we have such parallel corpus, where we can have the pairs of toxic text and natural text, we can fine-tune some large language model for this generation task. And yes, you can fine-tune GPT, GPT-2, GPT-5, BART, and any other new already appeared language models. So the thing here is we need to have such parallel corpus. And yeah, so talking about other corpus from different tasks from text on transfer, unfortunately, there are two, three maybe of them. The first one is Bible data size. It was collected actually throughout the generations, centuries, and even thousands of years, just by here translating and moderating the language of Bible to a modern language. And another corpus is changing the formality style. So yeah, actually this corpus is a modern corpus already, but the collection was manually done by his authors of the paper. So toxicity, we have actually a lot of data sets for English language and for Russian, German, Chinese languages, and many other languages, but these are just classification data sets. They have a lot of samples and also different labels about different types of toxicity, but unfortunately they are not parallel. So in my research, we asked ourselves such a question and said hypothesis, so maybe if we collect such parallel corpus for dexification task, and if we fine-tune language model based on this corpus, then we can improve the dexification task performance and yeah, achieve quite good results. So yeah, the research also was about how to define such a pipeline to collect such kind of corpus. So we want to achieve that for each toxic input, we have one, two, three, several paraphrases. Also, again, the content is the same as much as possible to the original sentence. The style is indeed non-toxic. The new text is fluent text, and we do not want to dedicate the half a year of our personal time to annotate all this corpus. So what we created. Ah, yeah, sorry, a small also remark about type of specificity. Indeed, if you look at, for instance, Duxo corpus, there are different types of it, and some of them are quite difficult to handle. So we've worked in our research only with true, I would say, simple types of topicity, because you can see that there is just usage of some obscene lexicon or some insults, quite local insults, that can be indeed eliminated from the text, and the content will be anyway saved, but the content of the text is already non-toxic. Because if you look at the upper part of this table, if you think about how you can paraphrase it, that it can become non-toxic, you can understand that you need, indeed, to significantly paraphrase this text and, indeed, to change the content because the origin of the text is quite toxic and hateful and is quite hard to handle. So we selected only those samples, and, yeah, so we prepared the data sets. The text in the data sets of originally labeled for textification tasks can be different. It can be just sentence or several sentences or even a paragraph. So we separated only sentences. Then we selected sentences that are marked as toxic with our classification model. And also we selected sentences that are not super short because they can be just, as you can see here, just several obscene lexicon standing in one sentence in one sentence and there is no super much meaningful content there and also not super long so it will be easier for annotators to paraphrase those sentences. Then we created an annotation task where we indeed asked annotators so please paraphrase this sentence that it will be non-toxic, it will not sound offensive. Yeah, so also of course we left for for annotators option not to paraphrase the text because indeed it can be just simply non-toxic, so the classifier made a mistake, it can happen. Also, the text can be anyway also with a lot of obscene lexicon, it can be non-meaningful, or if you want to remove the toxic part of the sentence, it can change the meaning too much. But anyway, you can see that here the text field where the annotators should have written their text, it's just, yeah, text field, you can write anything here. So we indeed want to ensure somehow the quality of our samples. So yeah, so then we created the next task where we already can solve a just simple classification task comparing two sentences and verifying if the content is preserved. And yes, annotators should have asked for it if the content is preserved or not. If it was not preserved, so we didn't pay for the first task for this sample to annotators, it was successful actually paraphrasing that we go to the next stage and check if the style has changed again if unfortunately it was not so we didn't pay for the sample for the annotators from the first task but if it's indeed right now already non-toxic that yeah we are lucky we have a nice sample for our data set and the annotators are paid for their job so in the end the whole pipeline looked as follows for English data set collection and it was done with the local AI here platform so we have these three stages of annotation and actually yeah from each stage the separate corpus can be created and everything also yes the annotation results are also available online so i will tell about this later uh yeah and even then we tried to expand this collection pipeline to other language so for russian language and each was actually not so difficult didn't require so many steps so firstly of course you need to have a labeled toxicity classification corpus for your language but if you if you have such then you can just adopt the interfaces of the task for your language you can just translate it so yeah adopt also training tasks for annotators and then create corresponding tasks and theoretically can it can be used for any language. So as a result, we have publicly available two corpus for text identification for English and Russian language, and it's about several thousands of sentences, and you can see also the cost that was used to collect such corpus. So then what's next? We need to fine-tune language models based on this corpus. And yeah, just here are the results so that you can take a look at what we wanted to have and what samples you can observe in our datasets. Also, how to evaluate the models that we will create, actually it's also not a trivial question because it's not just machine discussion task or some classification task, it's text generation task and it requires also a specific amount of work. So as you remember the definition of text transfer, we have the three parameters and actually we want indeed to ensure the three parameters in our evaluation metrics. So what we can do for ensuring style transfer accuracy, we can, as well, use a toxicity classifier just contuned on some toxicity classification data set. How to relate content similarity? We can use cosine similarity between embedding of the text. And for this, we use L'Observé embeddings. Also, fluency is also a non-trivial, actually, metric. So firstly, we thought about to use perplexity. But unfortunately, it has quite low correlations with human annotations. So we use classifier for this, trained on language susceptibility corpus. So really I recommend this approach to ensure the quality of your text and also to have a single method for all the three metrics. And we accumulate each as geometric mean. So we multiply here, sample ways, these three parameters. So yeah, for instance, if you have one in cell transfer accuracy, so indeed the cell is non-toxic. If you have one in content, so content is saved. If you have one in fluency, then you have perfect sample. But if one of the metrics is zero, so unfortunately the sample cannot be shown to the user, yeah, potentially, and it should not be considered as a good sample. And just then calculate mean between all of them. And to evaluate similarity with human annotations, yes, with this written by annotators paraphrases, we calculated blue metric. And also we actually tried to perform manual evaluation. So actually the idea is the same as for annotation. I won't stop so deeply here, but yeah. So if you want to check the quality of the detection direction task, it's also nice to have manual evaluation. So it will be the point here. Yeah, so the results. So you have here a pretty standard table of results from academic paper. Indeed, if you take a look on different baselines, quite of them are pretty simple, but also actually have pretty nice results. What you can see is that our models perform way better than all previous baselines. And that was automatic evaluation. So if we take a look on my evaluation, where we manually checked the samples from test sets, you can see that the difference is really huge. So our model has a pretty nice similarity and very high fluency. Maybe sometimes not catching all the types of duplicity, but yeah, in general, it performs very nice. And you can see also here the examples, how different models can paraphrase, can dexify the toxic sentences. So yeah, if you just delete some words, the content can be missing. conditional BERT model can paraphrase and can find substitution quite maybe not accurate. Also another baseline barrage die here, it's also pretty nice, but also if you can see on the second example, sometimes paraphrases can be anyway quite similar and anyway a little bit toxic to the original sentence. But our model saves the content very precisely and you can see that this is very nice and toxic fluent sentences. And also I have the table, the same table for Russian language, also our models of the forms of all the baselines. If you look at manual ideation, you also can see the huge differences between the scores, and if later you want to take a look, and if you're familiar with Russian language, you can also read the samples as well. Yeah, and then actually when we did our research, another question arise. So we have this corpus for Russian language, and also we even dedicated our time and resources to collect for Russian language, but what about other languages? So what we can do for them, or maybe if we can train the models that that will perform detoxification for any language, it can be multilingual model with such a task. Actually, it's very not trivial also question and so the research is still ongoing, but I will share with you a recipe how you can transfer the knowledge of detoxification for other languages. So first of all, if you just want to achieve the certification for your language, so what you can simply do, you can use a translation model or detoxification model, English model as an API, as a black box. So you can just translate your input with some Google, DeepL, any other API translation models, and then you can deducesify the sample with, for instance, our English certification model, and then translate the answer from a model to your language as well. So yeah, you can see that it actually work pretty nice already. Unfortunately, there is a dependency to some external API here for the translation or for lexification, and yeah, so it's dependency on some other models. Also, what you can do, so you can actually translate the corpus, and also it's, yeah, it's also a dependency on one hand, but it's also a dependency in one run, so you can translate the whole corpus, and then you can also train your lexification model based on this corpus. So the trick here is that, of course, maybe this corpus will not cover the whole toxicity types that are present for your language, because yeah, even if toxicity can be on one hand the same, different language can be different, different types of toxicity, different expressions for this, so it maybe not cover all the cases, but anyway, for their baseline model, it can be also pretty nice. But if you want to get yourself, get your hands dirty, what you can do, so you can use adapter training trick where you can insert adapter layer between encoder and decoder of your language model, fine-tune this adapter, and the trick here is multilingual language model, you can fine-tune the adapter layer only for the classification task, and as the model already is multilingual and adapter is fine-tuned on the classification, even if it's only in the classification, anyway it can handle their input in other language and will produce already an output in your language. So yeah, it can be your model specifically allocated on your server, on your computer, on whatever you are using, and it can be... So yeah, training an adapter is way easier than training, fine-tuning the whole large language model. Yeah, from this point, actually, the last question left is that, of course, with all these advances, you are sitting and thinking can actually charGPT perform textification and replace two years of my research. So yeah, let's take a look. So yeah, I tried as examples which were present in this presentation and feed them to charGPT and so you can see the results, yeah, maybe several seconds to read them. So what I personally think about this, so of course it performs textification nicely And yeah, because this model was trained and fine-tuned and adjusted a lot to perform just in generally very polite and non-toxic text. But this is actually a scene. So if you read the sentences, they are very, very polite, like even too polite in my opinion. And moreover, you can see that the model can hallucinate, it can maybe add in some additional sentences, additional text. And if you want, if you think about how to use this model as just a black box, an API to feed toxic text and to feed out, yeah, just non-toxic paraphrase, it can be actually challenging to use. So the use case, as I sort of here, if you want to just indeed ensure that your chat report is non-toxic, ChatGPT can be really quite a solution. And this sentence will be very polite, but our search, so I will emphasize this, it was was not transforming from toxic to polite sentences, but from toxic to non-toxic. And actually, this is a difference. And when I saw how it can be applied for current advances for training clutch language models. So if you think about how to pre-train your large language model, your chatbot, or for any other user, so some big language model, and if you want to use some on-the-source data or even data, and that can be a case that maybe some of the texts are toxic, some of the samples, some of the parts of the text are toxic. So actually our model is perfect to do this pre-processing and just to filter the specificity. So you can just simply detect if the sentences, some sentences are toxic and apply our model. And the thing is that it will save the content very, very much and very, very accurate. So the usage of, and there will be no addition of any additional text or noise. And also if you think about, so yeah, maybe later, it will be indeed a chatbot or some model with human input, so maybe some question answering system or whatever, and there can be a risk that even the users can be just toxic just because they want to be or they just want to stress your model, but you also can have this filter that you are trying to detect toxicity from the user and also detoxify the input from a user, and again, the content will be saved as much as possible so there will be no additional noise or there will not be a usage in any details. And yeah, maybe in the end you also can use a model to have this clear filter of toxicity and yeah, to show the user and not only the output. Yeah, so actually talking about what is available online. Actually, everything is available online. So there is a QR code and the link to GitHub wrapper where you can have all the list of the models, all the data sets available from annotations, from fine-tuning. So yeah, we have sort of models for Russian and English language, and also we can even have some several demos. If you are a fan of Telegram, you can also try our bot, and you can also test some of our models. Yeah, and also there is, in GitHub Rapport, there is a list of all papers that we wrote during all these years. Of course, it was a huge effort from our team. It was really dedicated work for almost two years. And I really thank everybody who helped me during my research. And yeah, so actually the seat, feel free to contact and feel free to share your feedback. What do you think about our results? Thank you very much.

Speaker 2 [24:43]

Thank you so much for that excellent talk we have a few questions on Slido. I'll just go over them First one is detect plus substitute fast enough to alter user Texas live while they are posting it

Speaker 1 [24:58]

So, I mean, like approach just to, again, like to use this masking language modeling. Yes, we just took a check if I understand the question correctly. And if the speed of this model is, yeah, is, are those models quicker than just sequence models? Am I understanding correctly the question? Yeah. Yeah, okay, so I'm assuming that I understand it correctly. And yeah, so actually it's not, I would say that it's not quicker because of this re-ranking step. So if you just ask the model to make a substitution for your mask, it indeed works quite quickly. But you need to do this re-ranking about the toxicity score, and you need to either rank the classifier, as well as your inference step, or you need to use the toxicity scores. But anyway, this ranking step can be quite challenging and quite time consuming. So just to mention, so we train base version of BART and T5, so they are not of much memory required and they perform quite quickly. So I would say here that our models perform with quite adequate speech and already can be used.

Speaker 2 [26:15]

Super. I'm not sure if we have enough time, but let's try sequentially. Thank you for the talk. I can see your model being useful for Twitter. Now the question, do you think text style transfer can be used as a data augmentation technique?

Speaker 1 [26:27]

Oh, yeah, so it's very nice question and I'm sure that it can be used. Yes, definitely and indeed true Yeah, so even true Yeah to have in a in a data set in train data set for language model to have several samples in different styles so it can very perfectly argument Your train data set moreover if you have such a data set with already several Yes, it's parallel pairs, for instance here you have a sample in formal or in informal or in formal style, in toxic and non-toxic style. And even your language model can perform already several tasks simultaneously, so it can be reused for several tasks and not be fine-tuned for additional tasks. So just thinking about this green NLP point. So the re-usage of training data set and the re-usage of language models is right now a trend. And yeah, so I think it's a very nice use case for our model as well.

Speaker 2 [27:26]

Excellent. I'll try to combine two questions because I think they're asking similar things. How do you address sentiment while detoxifying the sentence in your research? Since swearing can be used in a positive context as well.

Speaker 1 [27:38]

The related question.

Speaker 2 [27:41]

happen if a sentence is just an insult without any other content.

Speaker 1 [27:44]

Yeah, so, yes, great question. So about the sentiment, so I don't remember if I have the samples in this presentation. So the thing is that indeed the sentence can be toxic and it can be negative. And when we transfer in style from toxic to non-toxic, we're just deleting this toxicity style, but we do not delete the sentiment. So still the sentence can be a negative sentiment. And yeah, for us, it was also important. So even we have some training examples for annotators that we should not try to make the sentence more positive or more nice so we just need to eliminate this toxicity step style and the second part

Speaker 2 [28:24]

What should happen if a sentence is just

Speaker 1 [28:27]

sentences, just sentences? Yeah, so as I showed, so even in the processing step, we tried to eliminate from candidates to rephrase such texts where there is just a continuation of several obscene lexicologist insults. So yeah, the thing is that in such texts, there was no meaningful content. And unfortunately, we cannot detoxify is that it will bring some content. So of course, yeah, the things that happen to such samples just to delete all obscene lexicon it will be just empty string unfortunately so yeah actually i can maybe link here so also from previous acl there was a nice paper called positive framing so i call this model a model who has several sessions with therapists so it's indeed if you have just very rude sentence so the nice thing to say yeah i just don't like your opinion because usually people write such comments when they disagree with something they're very angry so yeah i don't like your opinion or i disagree with this or so you can just substitute uh so yeah huge only obscene lexicon samples with just key phrases and it will be already enough

Speaker 2 [29:40]

Cool. Another question. This is many questions. I think it's a very engaging talk. Do you think it would be possible to use a cyclical consistency loss like CycleGAN to train the style transfer without...

Speaker 1 [29:53]

Yeah, so I would say that you can find, if you don't have parallel corpus, you can find a lot of heuristics that will work pretty nice, but unfortunately, a lot of research was done that it do not perform so nicely when you have parallel corpus, so it's just unfortunately a case, so a question for us, for instance, right now, as you saw, so for our corpus, we collected several thousands of samples, but the thing it can be is that actually we don't require so many thousands of samples maybe it's enough to have one thousand or five hundreds but the samples can be just different covering all different types of toxicity so for us it will be like next step of research how we can define how we can reduce the corpus collection but yeah so heuristics can be different also you can try and try to embed style and content in different embedding space and then try to sample uh yeah style and text and content from this space but unfortunately unfortunately because we do not have such nice compression of text so not precise compression of text into embedding space it's still not so accurate

Speaker 2 [30:59]

OK, maybe one more. Should we really whitewash sentences like, your examples are idiotic? Language can be quite nuanced and individual.

Speaker 1 [31:09]

And this is absolutely true. So I have actually a lot of questions about this. And the thing is that, of course, you need to think about your use case. So I'm talking about, yeah, so this is a model. We created it because we had a request how we can filter toxicity from chatbots. And then we, yeah, we were developing our model. We were developing our use cases where we can apply this. But if you think that maybe some cases should not be detoxified in your application, or for instance, it's okay to have some opposite lexicon in your domain. Also there can be a case, yes, domains are different. For instance, in some gaming domain, some lexicon is indeed not toxic or unusual or et cetera. So just you need to think about how you will apply this model, what should be applied from the corpus. So yeah, just you need to think really about your use case.

Speaker 2 [32:00]

Yeah, thank you so much. There are a few more questions, but maybe you can take them offline.

Daryna Dementieva

About — in the speaker's own words

I am a postdoctoral researcher at TUM. Currenlty, I am involved into the project of eXplainable AI. In 2022, I obtained my PhD under the supervision of Pr. Alexander Panchenko, Skoltech. My PhD research was connected with such important sociological issues as Fake News Detection and Texts Detoxification. More broadly, I am super interested in the NLP for Social Good research direction. Besides academical experience, I also was involved in several industrial projects in different companies: Visiology, Moscow, Russian Federation; Beiersdorf, Hamburg, Germany. Now, obtained industrial experience helps me a lot in my research.

Social card for talk: Methods for Text Style Transfer: Text Detoxification Case