pytorch t5 text2text-generation en transformers feedback comment generation for writing learning autotrain_compatible
---
language:
- en

tags:
- feedback comment generation for writing learning

licenses:
- cc-by-nc-sa
---
## Model overview

This model was trained in terms of [GenChal 2022: Feedback Comment Generation for Writing Learning](https://fcg.sharedtask.org/) shared task

In this task, the model gets the string with text with the error and the exact span of the error and should return the comment in natural language, which explains the nature of the error.

## Model training details

#### Data

The data was provided in the following way

```
input sentence [\t] offset range [\t] feedback comment 
```

Here are some examples
```
The smoke flow my face .	10:17	When the <verb> <<flow>> is used as an <intransitive verb> to express ''to move in a stream'', a <preposition> needs to be placed to indicate the direction. 'To' and 'towards' are <prepositions> that indicate direction.

I want to stop smoking during driving bicycle .	23:29	A <gerund> does not normally follow the <preposition> <<during>>. Think of an expression using the <conjunction> 'while' instead of a <preposition>.

```

Grammar termins are highlighted with '< ... >' marks and word examples - with '<< ... >>'

####  Data preprocessing

We lowercased the text, split it from any punctuation, including task specific marks (<< >>) and explicitly pointed out the error in the original text using << >>.

```
the smoke < < flow > > < < my > > face .	10:17 When the < verb > < < flow > > is used as an < intransitive verb > to express '' to move in a stream '', a < preposition > needs to be placed to indicate the direction. ' to ' and ' towards ' are < prepositions > that indicate direction .

i want to stop smoking < < during > > driving bicycle .	23:29	a < gerund > does not normally follow the < preposition > < < during > > . think of an expression using the < conjunction > ' while ' instead of a < preposition > .
```

####  Data augmentation

The main feature of our training pipeline was data augmentation. The idea of the augmentation is as follows: we cut the existing text with error after the last word which was syntactically connected to the words inside the error span (syntactic dependencies were automatically parsed with spacy) and this cut version of the text with error was used as a prompt for language model (we used [GPT-Neo 1.3B](https://huggingface.co/EleutherAI/gpt-neo-1.3B)).

Using both initial and augmented data we fine-tuned [t5-large](https://huggingface.co/t5-large).

## How to use

```python

from transformers import T5ForConditionalGeneration, AutoTokenizer

text_with_error = 'I want to stop smoking during driving bicycle .'
error_span = '23:29'

off1, off2 = list(map(int,error_span.split(":")))
text_with_error_pointed = text_with_error [:off1] + "< < " + re.sub("\s+", " > > < < ", text_with_error [off1:off2].strip()) + " > > " + text_with_error[off2:]
text_with_error_pointed = re.sub("\s+", " ", text_with_error_pointed .strip()).lower()

tokenizer = AutoTokenizer.from_pretrained("SkolkovoInstitute/GenChal_2022_nigula")
model = T5ForConditionalGeneration.from_pretrained("SkolkovoInstitute/GenChal_2022_nigula").cuda();
model.eval();

def paraphrase(text, model, temperature=1.0, beams=3):
    texts = [text] if isinstance(text, str) else text
    inputs = tokenizer(texts, return_tensors='pt', padding=True)['input_ids'].to(model.device)
    result = model.generate(
        inputs, 
#         num_return_sequences=n or 1, 
        do_sample=False, 
        temperature=temperature, 
        repetition_penalty=1.1, 
        max_length=int(inputs.shape[1] * 3) ,
#         bad_words_ids=[[2]],  # unk
        num_beams=beams,
    )
    texts = [tokenizer.decode(r, skip_special_tokens=True) for r in result]
    if isinstance(text, str):
        return texts[0]
    return texts
    
    
 paraphrase([pointed_example], model)
 
 # expected output: ["a  gerund > does not normally follow the  preposition >   during > >. think of an expression using the  conjunction >'while'instead of a  preposition >."]


```


## Licensing Information

[Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License][cc-by-nc-sa].

[![CC BY-NC-SA 4.0][cc-by-nc-sa-image]][cc-by-nc-sa]

[cc-by-nc-sa]: http://creativecommons.org/licenses/by-nc-sa/4.0/
[cc-by-nc-sa-image]: https://i.creativecommons.org/l/by-nc-sa/4.0/88x31.png