Commit
·
edb0d3b
1
Parent(s):
deb8680
Update README.md
Browse files
README.md
CHANGED
@@ -28,9 +28,27 @@ tokenizer = GPT2Tokenizer.from_pretrained("RaushanTurganbay/GPT2_instruct_tuned"
|
|
28 |
model = GPT2LMHeadModel.from_pretrained("RaushanTurganbay/GPT2_instruct_tuned")
|
29 |
|
30 |
# Generate responses
|
31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
32 |
inputs = tokenizer(prompt, return_tensors="pt")
|
33 |
-
outputs = model.generate(**inputs, max_length=150
|
34 |
|
35 |
-
print(tokenizer.
|
36 |
```
|
|
|
28 |
model = GPT2LMHeadModel.from_pretrained("RaushanTurganbay/GPT2_instruct_tuned")
|
29 |
|
30 |
# Generate responses
|
31 |
+
class StoppingCriteriaSub(StoppingCriteria):
|
32 |
+
def __init__(self, stops=[], encounters=1):
|
33 |
+
super().__init__()
|
34 |
+
self.stops = [stop.to("cuda") for stop in stops]
|
35 |
+
def __call__(self, input_ids: torch.LongTensor, scores: torch.FloatTensor):
|
36 |
+
for stop in self.stops:
|
37 |
+
if torch.all((stop == input_ids[0][-len(stop):])).item():
|
38 |
+
return True
|
39 |
+
return False
|
40 |
+
|
41 |
+
|
42 |
+
def stopping_criteria(tokenizer, stop_words):
|
43 |
+
stop_words_ids = [tokenizer(stop_word, return_tensors='pt')['input_ids'].squeeze() for stop_word in stop_words]
|
44 |
+
stopping_criteria = StoppingCriteriaList([StoppingCriteriaSub(stops=stop_words_ids)])
|
45 |
+
return stopping_criteria
|
46 |
+
|
47 |
+
# Generate responses
|
48 |
+
stopping = stopping_criteria(tokenizer, ["\n\nHuman:"])
|
49 |
+
prompt = "\n\nHuman: {your_instruction}\n\nAssistant:"
|
50 |
inputs = tokenizer(prompt, return_tensors="pt")
|
51 |
+
outputs = model.generate(**inputs, stopping_criteria=stopping, max_length=150)
|
52 |
|
53 |
+
print("Model Response:", tokenizer.batch_decode(outputs))
|
54 |
```
|