cognitivess
commited on
Update README.md
Browse files
README.md
CHANGED
@@ -175,68 +175,7 @@ test_prompt = "Who are you?"
|
|
175 |
generated_response = generate_text(model, tokenizer, test_prompt, max_length=100)
|
176 |
print(f"Generated response:\n{generated_response}")
|
177 |
|
178 |
-
print("Testing completed.")
|
179 |
-
from transformers import AutoTokenizer, AutoModelForCausalLM
|
180 |
-
from peft import PeftModel, PeftConfig
|
181 |
-
|
182 |
-
# Set the device
|
183 |
-
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
184 |
-
print(f"Using device: {device}")
|
185 |
-
|
186 |
-
# Load the tokenizer
|
187 |
-
tokenizer = AutoTokenizer.from_pretrained("CognitivessAI/cognitivess")
|
188 |
-
|
189 |
-
# Load the PEFT configuration
|
190 |
-
peft_config = PeftConfig.from_pretrained("CognitivessAI/cognitivess")
|
191 |
-
|
192 |
-
# Load the base model
|
193 |
-
base_model = AutoModelForCausalLM.from_pretrained(
|
194 |
-
peft_config.base_model_name_or_path,
|
195 |
-
device_map="auto",
|
196 |
-
torch_dtype=torch.float16
|
197 |
-
)
|
198 |
-
|
199 |
-
# Load the PEFT model
|
200 |
-
model = PeftModel.from_pretrained(base_model, "CognitivessAI/cognitivess")
|
201 |
-
|
202 |
-
# Move the model to the appropriate device
|
203 |
-
model = model.to(device)
|
204 |
-
|
205 |
-
# Set the model to evaluation mode
|
206 |
-
model.eval()
|
207 |
-
|
208 |
-
# Function for text generation using the chat template
|
209 |
-
def generate_text(model, tokenizer, input_text, max_length=8192, temperature=0.7, top_p=0.95):
|
210 |
-
messages = [
|
211 |
-
{"role": "user", "content": input_text}
|
212 |
-
]
|
213 |
-
chat_input = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
|
214 |
-
inputs = tokenizer(chat_input, return_tensors='pt', padding=True, truncation=True, max_length=8192)
|
215 |
-
input_ids = inputs['input_ids'].to(device)
|
216 |
-
attention_mask = inputs['attention_mask'].to(device)
|
217 |
-
try:
|
218 |
-
generated_text_ids = model.generate(
|
219 |
-
input_ids,
|
220 |
-
attention_mask=attention_mask,
|
221 |
-
max_length=max_length,
|
222 |
-
temperature=temperature,
|
223 |
-
top_p=top_p,
|
224 |
-
do_sample=True,
|
225 |
-
eos_token_id=tokenizer.eos_token_id
|
226 |
-
)
|
227 |
-
generated_text = tokenizer.decode(generated_text_ids[0], skip_special_tokens=True)
|
228 |
-
# Extract the assistant's response
|
229 |
-
response = generated_text.split("GPT4 Correct Assistant")[-1].strip()
|
230 |
-
return response
|
231 |
-
except Exception as e:
|
232 |
-
print(f"Error in text generation: {e}")
|
233 |
-
return "I'm sorry, I encountered an error while generating a response."
|
234 |
-
|
235 |
-
# Test the model
|
236 |
-
test_prompt = "Who are you?"
|
237 |
-
generated_response = generate_text(model, tokenizer, test_prompt, max_length=100)
|
238 |
-
print(f"Generated response:\n{generated_response}")
|
239 |
-
|
240 |
|
241 |
```
|
242 |
|
|
|
175 |
generated_response = generate_text(model, tokenizer, test_prompt, max_length=100)
|
176 |
print(f"Generated response:\n{generated_response}")
|
177 |
|
178 |
+
print("Testing completed.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
179 |
|
180 |
```
|
181 |
|