Update README.md
Browse files
README.md
CHANGED
@@ -28,8 +28,8 @@ It may produce some harmful output.
|
|
28 |
|
29 |
Examples:
|
30 |
|
31 |
-
| Prompt | BrtGPT-1-Pre |
|
32 |
-
| :------------: | :------------: |
|
33 |
| "Write me a code that prints "Hello World". | "Here's a code that prints "Hello World" in a list of words:```for i in range(1, 2, 3, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5," |
|
34 |
| "Write me a code that generates random number."| Code: |
|
35 |
|
@@ -67,3 +67,100 @@ def generate_random_number(num):
|
|
67 |
# Create a new
|
68 |
```
|
69 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
28 |
|
29 |
Examples:
|
30 |
|
31 |
+
| Prompt | BrtGPT-1-Pre |
|
32 |
+
| :------------: | :------------: |
|
33 |
| "Write me a code that prints "Hello World". | "Here's a code that prints "Hello World" in a list of words:```for i in range(1, 2, 3, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5," |
|
34 |
| "Write me a code that generates random number."| Code: |
|
35 |
|
|
|
67 |
# Create a new
|
68 |
```
|
69 |
|
70 |
+
|
71 |
+
## How to use?
|
72 |
+
|
73 |
+
You can run this code to use:
|
74 |
+
|
75 |
+
```
|
76 |
+
import torch
|
77 |
+
from transformers import PreTrainedTokenizerFast, GPT2LMHeadModel
|
78 |
+
|
79 |
+
def extract_response_between_tokens(text: str) -> str:
|
80 |
+
|
81 |
+
start_token = "<|im_start|>assistant<|im_sep|>"
|
82 |
+
end_token = "<|im_end|>"
|
83 |
+
try:
|
84 |
+
start_idx = text.index(start_token) + len(start_token)
|
85 |
+
end_idx = text.index(end_token, start_idx)
|
86 |
+
return text[start_idx:end_idx]
|
87 |
+
except ValueError:
|
88 |
+
# Tokenlar bulunamazsa orijinal metni döndür
|
89 |
+
return text
|
90 |
+
|
91 |
+
if __name__ == "__main__":
|
92 |
+
model_name_or_path = "Bertug1911/BrtGPT-1-Pre"
|
93 |
+
|
94 |
+
tokenizer = PreTrainedTokenizerFast.from_pretrained(model_name_or_path)
|
95 |
+
model = GPT2LMHeadModel.from_pretrained(model_name_or_path)
|
96 |
+
|
97 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
98 |
+
model.to(device)
|
99 |
+
model.eval()
|
100 |
+
|
101 |
+
user_input = input("Enter something to ask model: ")
|
102 |
+
|
103 |
+
|
104 |
+
messages = [{"role": "user", "content": user_input}]
|
105 |
+
|
106 |
+
|
107 |
+
formatted_prompt = tokenizer.apply_chat_template(
|
108 |
+
messages,
|
109 |
+
tokenize=False,
|
110 |
+
add_generation_prompt=True
|
111 |
+
)
|
112 |
+
|
113 |
+
|
114 |
+
inputs = tokenizer(formatted_prompt, return_tensors="pt").to(device)
|
115 |
+
generated = inputs["input_ids"]
|
116 |
+
|
117 |
+
# Generate config
|
118 |
+
max_new_tokens = 128
|
119 |
+
do_sample = True
|
120 |
+
top_k = 40
|
121 |
+
temperature = 0.8
|
122 |
+
|
123 |
+
im_end_token_id = tokenizer.convert_tokens_to_ids("<|im_end|>")
|
124 |
+
|
125 |
+
with torch.no_grad():
|
126 |
+
for i in range(max_new_tokens):
|
127 |
+
outputs = model(generated)
|
128 |
+
logits = outputs.logits[:, -1, :]
|
129 |
+
logits = logits / temperature
|
130 |
+
|
131 |
+
if top_k > 0:
|
132 |
+
top_k_values, top_k_indices = torch.topk(logits, top_k)
|
133 |
+
logits_filtered = torch.full_like(logits, float('-inf'))
|
134 |
+
logits_filtered.scatter_(1, top_k_indices, top_k_values)
|
135 |
+
logits = logits_filtered
|
136 |
+
|
137 |
+
probs = torch.softmax(logits, dim=-1)
|
138 |
+
|
139 |
+
if do_sample:
|
140 |
+
next_token = torch.multinomial(probs, num_samples=1)
|
141 |
+
else:
|
142 |
+
next_token = torch.argmax(probs, dim=-1, keepdim=True)
|
143 |
+
|
144 |
+
generated = torch.cat([generated, next_token], dim=1)
|
145 |
+
|
146 |
+
if next_token.item() == im_end_token_id:
|
147 |
+
break
|
148 |
+
|
149 |
+
|
150 |
+
|
151 |
+
output = tokenizer.decode(generated[0], skip_special_tokens=False)
|
152 |
+
|
153 |
+
# Special token conversions
|
154 |
+
no_spaces = output.replace(" ", "")
|
155 |
+
step2 = no_spaces.replace("Ġ", " ")
|
156 |
+
formatted_output = step2.replace("Ċ", "\n")
|
157 |
+
|
158 |
+
if not formatted_output.strip().endswith("<|im_end|>"):
|
159 |
+
formatted_output += "<|im_end|>"
|
160 |
+
|
161 |
+
|
162 |
+
assistant_response = extract_response_between_tokens(formatted_output)
|
163 |
+
print("\nModel output:\n", assistant_response)
|
164 |
+
|
165 |
+
```
|
166 |
+
|