Update README.md
Browse files
README.md
CHANGED
|
@@ -27,36 +27,39 @@ The backbone model of COSMO is the [lm-adapted T5](https://huggingface.co/google
|
|
| 27 |
|
| 28 |
### How to use
|
| 29 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
```python
|
|
|
|
| 31 |
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
| 32 |
|
|
|
|
| 33 |
tokenizer = AutoTokenizer.from_pretrained("allenai/cosmo-xl")
|
| 34 |
-
model = AutoModelForSeq2SeqLM.from_pretrained("allenai/cosmo-xl")
|
| 35 |
-
# model.to('cuda')
|
| 36 |
|
| 37 |
-
def set_input(
|
| 38 |
-
input_text = " <turn> ".join(
|
| 39 |
|
| 40 |
-
if
|
| 41 |
-
input_text =
|
| 42 |
|
| 43 |
-
if
|
| 44 |
-
input_text =
|
| 45 |
-
|
| 46 |
|
| 47 |
return input_text
|
| 48 |
|
| 49 |
-
def generate(
|
| 50 |
"""
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
"""
|
| 55 |
|
| 56 |
-
input_text = set_input(
|
| 57 |
|
| 58 |
-
inputs = tokenizer([input_text], return_tensors="pt")
|
| 59 |
-
# inputs = inputs.to('cuda')
|
| 60 |
outputs = model.generate(inputs["input_ids"], max_new_tokens=128, temperature=1.0, top_p=.95, do_sample=True)
|
| 61 |
response = tokenizer.decode(outputs[0], skip_special_tokens=True, clean_up_tokenization_spaces=False)
|
| 62 |
|
|
@@ -65,11 +68,11 @@ def generate(narrative, instruction, dialogue_history):
|
|
| 65 |
situation = "Cosmo had a really fun time participating in the EMNLP conference at Abu Dhabi."
|
| 66 |
instruction = "You are Cosmo and you are talking to a friend." # You can also leave the instruction empty
|
| 67 |
|
| 68 |
-
|
| 69 |
"Hey, how was your trip to Abu Dhabi?"
|
| 70 |
]
|
| 71 |
|
| 72 |
-
response = generate(situation, instruction,
|
| 73 |
print(response)
|
| 74 |
```
|
| 75 |
|
|
|
|
| 27 |
|
| 28 |
### How to use
|
| 29 |
|
| 30 |
+
> 💡 <b>Note:</b> The HuggingFace inference API for Cosmo is not working correctly, we gently guide you to [our repository](https://hyunw.kim/sodaverse) to try out the demo code!
|
| 31 |
+
|
| 32 |
+
Below is a simple code snippet to get Cosmo running :)
|
| 33 |
+
|
| 34 |
```python
|
| 35 |
+
import torch
|
| 36 |
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
| 37 |
|
| 38 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 39 |
tokenizer = AutoTokenizer.from_pretrained("allenai/cosmo-xl")
|
| 40 |
+
model = AutoModelForSeq2SeqLM.from_pretrained("allenai/cosmo-xl").to(device)
|
|
|
|
| 41 |
|
| 42 |
+
def set_input(situation_narrative, role_instruction, conversation_history):
|
| 43 |
+
input_text = " <turn> ".join(conversation_history)
|
| 44 |
|
| 45 |
+
if role_instruction != "":
|
| 46 |
+
input_text = "{} <sep> {}".format(role_instruction, input_text)
|
| 47 |
|
| 48 |
+
if situation_narrative != "":
|
| 49 |
+
input_text = "{} <sep> {}".format(situation_narrative, input_text)
|
|
|
|
| 50 |
|
| 51 |
return input_text
|
| 52 |
|
| 53 |
+
def generate(situation_narrative, role_instruction, conversation_history):
|
| 54 |
"""
|
| 55 |
+
situation_narrative: the description of situation/context with the characters included (e.g., "David goes to an amusement park")
|
| 56 |
+
role_instruction: the perspective/speaker instruction (e.g., "Imagine you are David and speak to his friend Sarah").
|
| 57 |
+
conversation_history: the previous utterances in the conversation in a list
|
| 58 |
"""
|
| 59 |
|
| 60 |
+
input_text = set_input(role_narrative, role_instruction, conversation_history)
|
| 61 |
|
| 62 |
+
inputs = tokenizer([input_text], return_tensors="pt").to(device)
|
|
|
|
| 63 |
outputs = model.generate(inputs["input_ids"], max_new_tokens=128, temperature=1.0, top_p=.95, do_sample=True)
|
| 64 |
response = tokenizer.decode(outputs[0], skip_special_tokens=True, clean_up_tokenization_spaces=False)
|
| 65 |
|
|
|
|
| 68 |
situation = "Cosmo had a really fun time participating in the EMNLP conference at Abu Dhabi."
|
| 69 |
instruction = "You are Cosmo and you are talking to a friend." # You can also leave the instruction empty
|
| 70 |
|
| 71 |
+
conversation = [
|
| 72 |
"Hey, how was your trip to Abu Dhabi?"
|
| 73 |
]
|
| 74 |
|
| 75 |
+
response = generate(situation, instruction, conversation)
|
| 76 |
print(response)
|
| 77 |
```
|
| 78 |
|