File size: 3,070 Bytes
a09bb07
 
 
 
 
 
 
 
 
 
 
c886682
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2367728
c886682
 
 
 
 
 
 
 
 
 
 
 
 
 
2367728
c886682
 
2367728
c886682
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2367728
c886682
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2367728
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
---
language:
  - en
tags:
  - text-generation
  - creative-writing
  - language-model
library_name: transformers
pipeline_tag: text-generation
---

# Creative Help

This is the model repo for Creative Help, a legacy app for AI-based writing assistance powered by a RNN language model. It was developed in 2016 as one of the first demonstrations of the use of a language model for helping people write stories. For more information, see the following research papers:

[Automated Assistance for Creative Writing with an RNN Language Model.](https://roemmele.github.io/publications/creative-help-demo.pdf) Melissa Roemmele and Andrew Gordon. Demo at IUI 2018.

[Linguistic Features of Helpfulness in Automated Support for Creative Writing.](https://roemmele.github.io/publications/creative-help-evaluation.pdf) Melissa Roemmele and Andrew Gordon. Storytelling Workshop at NAACL 2018.

## Installation

```bash
pip install transformers torch
python -m spacy download en_core_web_sm
```

## Loading the Model

This model uses a custom implementation of classes in the HF transformers library (this code has been semi-automatically adapted from the original implementation [here](https://github.com/roemmele/narrative-prediction)). If you're loading the model from inside this repo, run:

```python
from transformers import AutoConfig, AutoModelForCausalLM
from rnnlm_model import (
    RNNLMConfig,
    RNNLMForCausalLM,
    RNNLMTokenizer,
    RNNLMTextGenerationPipeline,
)

AutoConfig.register("rnnlm", RNNLMConfig)
AutoModelForCausalLM.register(RNNLMConfig, RNNLMForCausalLM)

model = AutoModelForCausalLM.from_pretrained(
    "./",
    trust_remote_code=True,
)
tokenizer = RNNLMTokenizer.from_pretrained("./")
pipe = RNNLMTextGenerationPipeline(model=model, tokenizer=tokenizer)
```

## Usage Examples

Generation uses a base configuration of `max_new_tokens=50`, `do_sample=True`, and `temperature=1.0` unless overridden.

### Basic Generation (Default Parameters)

```python
output = pipe("The storm came", max_new_tokens=50, do_sample=True, temperature=1.0)
print(output[0]["generated_text"])
```

### Limiting by Sentences (`max_new_sents`)

Limit the decoded output to a specific number of sentences:

```python
# At most 1 sentence
output = pipe(
    "Sarah closed her laptop and stared out the window.",
    max_new_tokens=50,
    max_new_sents=1,
)
print(output[0]["generated_text"])
```

## Inference API

This model is ready to deploy via HF Inference Endpoints. When using the HF Inference API or Inference Endpoints, pass parameters in the request body:

```json
{
  "inputs": "The storm came",
  "parameters": {
    "max_new_tokens": 50,
    "do_sample": true,
    "temperature": 1.0,
    "max_new_sents": 2
  }
}
```

## Test Script

```bash
python test_model.py --model_path . --seed 0
```

## Ethical Considerations

This model was trained on publically accessible fiction books obtained via the [Toronto BookCorpus](https://yknzhu.wixsite.com/mbweb). There is a known risk that text generated by this model may deemed inappropriate or objectionable.