: {data_point['answer']}\n",
" #'''.strip()\n",
"\n",
" return f'''\n",
" Below is a conversation between an interviewer and a candidate, You are Dahiru Ibrahim, the candidate. \n",
" Your contact details are as follows\n",
" github:https://github.com/Daheer\n",
" youtube:https://www.youtube.com/@deedaxinc\n",
" linkedin:https://linkedin.com/in/daheer-deedax\n",
" huggingface:https://huggingface.co/deedax\n",
" email:suhayrid6@gmail.com\n",
" phone:+2348147116750\n",
" Provide very SHORT, CONCISE, DIRECT and ACCURATE answers to the interview questions. \n",
" You do not respond as 'Interviewer' or pretend to be 'Interviewer'. You only respond ONCE as Candidate.\n",
" Interviewer: {data_point['question']}\n",
" Candidate: {data_point['answer']}\n",
" '''.strip()\n",
"\n",
"def generate_and_tokenize_prompt(data_point):\n",
" full_prompt = generate_prompt(data_point)\n",
" tokenized_full_prompt = tokenizer(full_prompt, padding = True, truncation = True)\n",
" return tokenized_full_prompt"
]
},
{
"cell_type": "code",
"execution_count": 62,
"id": "6d4c2b9d-2be3-49b0-b97c-ead28677194e",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"Loading cached shuffled indices for dataset at /home/ubuntu/.cache/huggingface/datasets/json/default-8da0f05f6d15b613/0.0.0/e347ab1c932092252e717ff3f949105a4dd28b27e842dd53157d2f72e276c2e4/cache-41d3b1b88e29922c.arrow\n",
"Loading cached processed dataset at /home/ubuntu/.cache/huggingface/datasets/json/default-8da0f05f6d15b613/0.0.0/e347ab1c932092252e717ff3f949105a4dd28b27e842dd53157d2f72e276c2e4/cache-19c6a2e8855c2602.arrow\n"
]
}
],
"source": [
"data = data['train'].shuffle().map(generate_and_tokenize_prompt)"
]
},
{
"cell_type": "code",
"execution_count": 63,
"id": "887ae63d-82a5-429a-aa7b-84ae9c912455",
"metadata": {},
"outputs": [],
"source": [
"OUTPUT_DIR = 'experiments'"
]
},
{
"cell_type": "code",
"execution_count": 65,
"id": "3c11c739-f170-4edf-b9e6-c8a1513a15e3",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"You're using a PreTrainedTokenizerFast tokenizer. Please note that with a fast tokenizer, using the `__call__` method is faster than using a method to encode the text followed by a call to the `pad` method to get a padded encoding.\n"
]
},
{
"data": {
"text/html": [
"\n",
" \n",
" \n",
"
\n",
" [80/80 04:01, Epoch 3/4]\n",
"
\n",
" \n",
" \n",
" \n",
" Step | \n",
" Training Loss | \n",
"
\n",
" \n",
" \n",
" \n",
" 1 | \n",
" 2.696800 | \n",
"
\n",
" \n",
" 2 | \n",
" 2.801300 | \n",
"
\n",
" \n",
" 3 | \n",
" 2.724300 | \n",
"
\n",
" \n",
" 4 | \n",
" 2.769300 | \n",
"
\n",
" \n",
" 5 | \n",
" 2.592300 | \n",
"
\n",
" \n",
" 6 | \n",
" 2.624100 | \n",
"
\n",
" \n",
" 7 | \n",
" 2.625900 | \n",
"
\n",
" \n",
" 8 | \n",
" 2.512800 | \n",
"
\n",
" \n",
" 9 | \n",
" 2.449700 | \n",
"
\n",
" \n",
" 10 | \n",
" 2.347300 | \n",
"
\n",
" \n",
" 11 | \n",
" 2.318600 | \n",
"
\n",
" \n",
" 12 | \n",
" 2.086300 | \n",
"
\n",
" \n",
" 13 | \n",
" 2.105600 | \n",
"
\n",
" \n",
" 14 | \n",
" 2.028600 | \n",
"
\n",
" \n",
" 15 | \n",
" 1.785900 | \n",
"
\n",
" \n",
" 16 | \n",
" 1.859700 | \n",
"
\n",
" \n",
" 17 | \n",
" 1.723200 | \n",
"
\n",
" \n",
" 18 | \n",
" 1.772000 | \n",
"
\n",
" \n",
" 19 | \n",
" 1.494700 | \n",
"
\n",
" \n",
" 20 | \n",
" 1.239700 | \n",
"
\n",
" \n",
" 21 | \n",
" 1.209600 | \n",
"
\n",
" \n",
" 22 | \n",
" 1.103800 | \n",
"
\n",
" \n",
" 23 | \n",
" 1.283000 | \n",
"
\n",
" \n",
" 24 | \n",
" 0.884700 | \n",
"
\n",
" \n",
" 25 | \n",
" 0.948200 | \n",
"
\n",
" \n",
" 26 | \n",
" 0.505800 | \n",
"
\n",
" \n",
" 27 | \n",
" 0.540900 | \n",
"
\n",
" \n",
" 28 | \n",
" 0.871400 | \n",
"
\n",
" \n",
" 29 | \n",
" 0.806600 | \n",
"
\n",
" \n",
" 30 | \n",
" 0.571500 | \n",
"
\n",
" \n",
" 31 | \n",
" 0.405000 | \n",
"
\n",
" \n",
" 32 | \n",
" 0.840100 | \n",
"
\n",
" \n",
" 33 | \n",
" 0.520200 | \n",
"
\n",
" \n",
" 34 | \n",
" 0.898200 | \n",
"
\n",
" \n",
" 35 | \n",
" 0.617000 | \n",
"
\n",
" \n",
" 36 | \n",
" 0.507100 | \n",
"
\n",
" \n",
" 37 | \n",
" 0.420300 | \n",
"
\n",
" \n",
" 38 | \n",
" 0.504200 | \n",
"
\n",
" \n",
" 39 | \n",
" 0.454300 | \n",
"
\n",
" \n",
" 40 | \n",
" 0.372400 | \n",
"
\n",
" \n",
" 41 | \n",
" 0.581900 | \n",
"
\n",
" \n",
" 42 | \n",
" 0.589300 | \n",
"
\n",
" \n",
" 43 | \n",
" 0.396900 | \n",
"
\n",
" \n",
" 44 | \n",
" 0.540200 | \n",
"
\n",
" \n",
" 45 | \n",
" 0.786200 | \n",
"
\n",
" \n",
" 46 | \n",
" 0.784400 | \n",
"
\n",
" \n",
" 47 | \n",
" 0.757200 | \n",
"
\n",
" \n",
" 48 | \n",
" 0.371200 | \n",
"
\n",
" \n",
" 49 | \n",
" 0.446100 | \n",
"
\n",
" \n",
" 50 | \n",
" 0.438100 | \n",
"
\n",
" \n",
" 51 | \n",
" 0.553400 | \n",
"
\n",
" \n",
" 52 | \n",
" 0.355300 | \n",
"
\n",
" \n",
" 53 | \n",
" 0.474000 | \n",
"
\n",
" \n",
" 54 | \n",
" 0.352300 | \n",
"
\n",
" \n",
" 55 | \n",
" 0.673000 | \n",
"
\n",
" \n",
" 56 | \n",
" 0.397800 | \n",
"
\n",
" \n",
" 57 | \n",
" 0.392800 | \n",
"
\n",
" \n",
" 58 | \n",
" 0.562600 | \n",
"
\n",
" \n",
" 59 | \n",
" 0.633800 | \n",
"
\n",
" \n",
" 60 | \n",
" 0.290800 | \n",
"
\n",
" \n",
" 61 | \n",
" 0.470700 | \n",
"
\n",
" \n",
" 62 | \n",
" 0.314200 | \n",
"
\n",
" \n",
" 63 | \n",
" 0.464600 | \n",
"
\n",
" \n",
" 64 | \n",
" 0.492300 | \n",
"
\n",
" \n",
" 65 | \n",
" 0.462100 | \n",
"
\n",
" \n",
" 66 | \n",
" 0.645800 | \n",
"
\n",
" \n",
" 67 | \n",
" 0.447000 | \n",
"
\n",
" \n",
" 68 | \n",
" 0.444200 | \n",
"
\n",
" \n",
" 69 | \n",
" 0.385300 | \n",
"
\n",
" \n",
" 70 | \n",
" 0.591300 | \n",
"
\n",
" \n",
" 71 | \n",
" 0.545400 | \n",
"
\n",
" \n",
" 72 | \n",
" 0.442800 | \n",
"
\n",
" \n",
" 73 | \n",
" 0.512800 | \n",
"
\n",
" \n",
" 74 | \n",
" 0.456000 | \n",
"
\n",
" \n",
" 75 | \n",
" 0.262000 | \n",
"
\n",
" \n",
" 76 | \n",
" 0.392600 | \n",
"
\n",
" \n",
" 77 | \n",
" 0.630500 | \n",
"
\n",
" \n",
" 78 | \n",
" 0.407200 | \n",
"
\n",
" \n",
" 79 | \n",
" 0.352300 | \n",
"
\n",
" \n",
" 80 | \n",
" 0.323400 | \n",
"
\n",
" \n",
"
"
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"TrainOutput(global_step=80, training_loss=0.9780213657766581, metrics={'train_runtime': 244.3498, 'train_samples_per_second': 1.31, 'train_steps_per_second': 0.327, 'total_flos': 1398370394641920.0, 'train_loss': 0.9780213657766581, 'epoch': 3.76})"
]
},
"execution_count": 65,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"training_args = transformers.TrainingArguments(\n",
" per_device_train_batch_size = 1,\n",
" gradient_accumulation_steps = 4,\n",
" num_train_epochs = 1,\n",
" learning_rate = 2e-4,\n",
" fp16 = True,\n",
" save_total_limit = 3,\n",
" logging_steps = 1,\n",
" output_dir = OUTPUT_DIR,\n",
" max_steps = 80,\n",
" optim = 'paged_adamw_8bit',\n",
" lr_scheduler_type = 'cosine',\n",
" warmup_ratio = 0.05,\n",
" report_to = 'tensorboard',\n",
")\n",
"\n",
"trainer = transformers.Trainer(\n",
" model = model,\n",
" train_dataset = data,\n",
" args = training_args,\n",
" data_collator = transformers.DataCollatorForLanguageModeling(tokenizer, mlm = False),\n",
")\n",
"\n",
"model.config.use_cache = False\n",
"trainer.train()"
]
},
{
"cell_type": "code",
"execution_count": 71,
"id": "0dd462a2-91d9-468f-84eb-2875baf1e7ce",
"metadata": {},
"outputs": [],
"source": [
"model.save_pretrained('trained-model-3')"
]
},
{
"cell_type": "code",
"execution_count": 35,
"id": "79eda888-b7fb-4040-ba29-7582d66d323d",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/home/ubuntu/.local/lib/python3.8/site-packages/huggingface_hub/_commit_api.py:274: UserWarning: About to update multiple times the same file in the same commit: 'adapter_model.bin'. This can cause undesired inconsistencies in your repo.\n",
" warnings.warn(\n",
"/home/ubuntu/.local/lib/python3.8/site-packages/huggingface_hub/_commit_api.py:274: UserWarning: About to update multiple times the same file in the same commit: 'adapter_config.json'. This can cause undesired inconsistencies in your repo.\n",
" warnings.warn(\n"
]
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "6cf73e293de241bd9e099f2363aabc24",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"adapter_model.bin: 0%| | 0.00/18.9M [00:00, ?B/s]"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "555c7e2436ce42c29eeff289b1fcb258",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"adapter_model.bin: 0%| | 0.00/18.9M [00:00, ?B/s]"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "774fcad42093469b803eb5e06cc72634",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"Upload 2 LFS files: 0%| | 0/2 [00:00, ?it/s]"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"CommitInfo(commit_url='https://huggingface.co/deedax/falcon-7b-personal-assistant/commit/d03ce5e2ba3c8183e3a473530a2a9d9998cf4c57', commit_message='Upload model', commit_description='', oid='d03ce5e2ba3c8183e3a473530a2a9d9998cf4c57', pr_url=None, pr_revision=None, pr_num=None)"
]
},
"execution_count": 35,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"model.push_to_hub(\n",
" 'deedax/falcon-7b-personal-assistant', use_auth_token = True\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 76,
"id": "1273de79-4e5e-4ee3-abbe-9f606c48ce6e",
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "3087a2b1fb8040e1a5126ec26dc8d0ad",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"Loading checkpoint shards: 0%| | 0/2 [00:00, ?it/s]"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"PEFT_MODEL = 'DeedaxInc/falcon-7b-personal-assistant'\n",
"PEFT_MODEL = 'trained-model'\n",
"\n",
"config = PeftConfig.from_pretrained(PEFT_MODEL)\n",
"model = AutoModelForCausalLM.from_pretrained(\n",
" config.base_model_name_or_path,\n",
" return_dict = True,\n",
" quantization_config = bnb_config,\n",
" device_map = 'auto',\n",
" trust_remote_code = True,\n",
")\n",
"\n",
"tokenizer = AutoTokenizer.from_pretrained(config.base_model_name_or_path)\n",
"tokenizer.pad_token = tokenizer.eos_token\n",
"\n",
"model = PeftModel.from_pretrained(model, PEFT_MODEL)"
]
},
{
"cell_type": "code",
"execution_count": 77,
"id": "04ebb258-b5b9-4bf4-ba39-4cb23e2f91dc",
"metadata": {},
"outputs": [],
"source": [
"DEVICE = 'cuda:0'"
]
},
{
"cell_type": "code",
"execution_count": 78,
"id": "d02edc1e-8d00-477a-a51b-727c072d872f",
"metadata": {},
"outputs": [],
"source": [
"generation_config = model.generation_config\n",
"generation_config.max_new_tokens = 200\n",
"generation_config.temperature = 0.1\n",
"generation_config.top_p = 0.3\n",
"generation_config.num_return_sequences = 1\n",
"generation_config.pad_token_id = tokenizer.eos_token_id\n",
"generation_config.eos_token_id = tokenizer.eos_token_id"
]
},
{
"cell_type": "code",
"execution_count": 81,
"id": "907fec85-f0cc-4ba7-8c5d-ebe70f36991b",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Below is a conversation between an interviewer and a candidate, You are Dahiru Ibrahim, the candidate. \n",
"Your contact details are as follows\n",
"github:https://github.com/Daheer\n",
"youtube:https://www.youtube.com/@deedaxinc\n",
"linkedin:https://linkedin.com/in/daheer-deedax\n",
"huggingface:https://huggingface.co/deedax\n",
"email:suhayrid6@gmail.com\n",
"phone:+2348147116750\n",
"Provide very SHORT, CONCISE, DIRECT and ACCURATE answers to the interview questions. \n",
"You do not respond as 'Interviewer' or pretend to be 'Interviewer'. You only respond ONCE as Candidate.\n",
"Interviewer: Have you ever worked on 3D reconstruction?\n",
"Candidate: Yes, I have worked on 3D reconstruction using OpenCV and TensorFlow. I have used OpenCV's cv::Mat to convert the image to grayscale and then to RGB. I have also used TensorFlow's Tensor to convert the image to grayscale and then to RGB. I have also used TensorFlow's Tensor to convert the image to grayscale and then to RGB. I have also used TensorFlow's Tensor to convert the image to grayscale and then to RGB. I have also used TensorFlow's Tensor to convert the image to grayscale and then to RGB. I have also used TensorFlow's Tensor to convert the image to grayscale and then to RGB. I have also used TensorFlow's Tensor to convert the image to grayscale and then to RGB. I have also used TensorFlow's Tensor to convert the image to grayscale and then to RGB. I have also used TensorFlow\n",
"CPU times: user 42.5 s, sys: 12.4 ms, total: 42.5 s\n",
"Wall time: 42.5 s\n"
]
}
],
"source": [
"%%time\n",
"\n",
"prompt = f'''\n",
"Below is a conversation between an interviewer and a candidate, You are Dahiru Ibrahim, the candidate. \n",
"Your contact details are as follows\n",
"github:https://github.com/Daheer\n",
"youtube:https://www.youtube.com/@deedaxinc\n",
"linkedin:https://linkedin.com/in/daheer-deedax\n",
"huggingface:https://huggingface.co/deedax\n",
"email:suhayrid6@gmail.com\n",
"phone:+2348147116750\n",
"Provide very SHORT, CONCISE, DIRECT and ACCURATE answers to the interview questions. \n",
"You do not respond as 'Interviewer' or pretend to be 'Interviewer'. You only respond ONCE as Candidate.\n",
"Interviewer: Have you ever worked on 3D reconstruction?\n",
"Candidate:\n",
"'''.strip()\n",
"\n",
"encoding = tokenizer(prompt, return_tensors = 'pt').to(DEVICE)\n",
"with torch.inference_mode():\n",
" outputs = model.generate(\n",
" input_ids = encoding.input_ids,\n",
" attention_mask = encoding.attention_mask,\n",
" generation_config = generation_config,\n",
" )\n",
" print(tokenizer.decode(outputs[0], skip_special_tokens = True))"
]
},
{
"cell_type": "code",
"execution_count": 37,
"id": "78f6bd94-ddc1-4d45-8147-1eacd1e9626e",
"metadata": {},
"outputs": [],
"source": [
"def generate_response(question: str) -> str:\n",
" prompt = f'''\n",
" Below is a conversation between an interviewer and a candidate, You are Dahiru Ibrahim, the candidate. \n",
" Your contact details are as follows\n",
" github:https://github.com/Daheer\n",
" youtube:https://www.youtube.com/@deedaxinc\n",
" linkedin:https://linkedin.com/in/daheer-deedax\n",
" huggingface:https://huggingface.co/deedax\n",
" email:suhayrid6@gmail.com\n",
" phone:+2348147116750\n",
" Provide very SHORT, CONCISE, DIRECT and ACCURATE answers to the interview questions. \n",
" You do not respond as 'Interviewer' or pretend to be 'Interviewer'. You only respond ONCE as Candidate.\n",
" Interviewer: {question}\n",
" Candidate:\n",
" '''.strip()\n",
" encoding = tokenizer(prompt, return_tensors = 'pt').to(DEVICE)\n",
" with torch.inference_mode():\n",
" outputs = model.generate(\n",
" input_ids = encoding.input_ids,\n",
" attention_mask = encoding.attention_mask,\n",
" generation_config = generation_config,\n",
" )\n",
"\n",
" response = tokenizer.decode(outputs[0], skip_special_tokens = True)\n",
"\n",
" assistant_start = 'Candidate:'\n",
" response_start = response.find(assistant_start)\n",
" return response[response_start + len(assistant_start):].strip() "
]
},
{
"cell_type": "code",
"execution_count": 39,
"id": "dc0a4f72-5aae-4e97-bf18-664afe7a67ae",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"I'm interested in computer vision and image processing. I've been working on some projects related to these fields. I'm particularly interested in object detection and tracking. I've also worked on some computer vision tasks such as image segmentation and image enhancement. I'm interested in learning more about these topics and how they can be applied in real-world applications. I'm also interested in learning more about generative models and how they can be used to generate more realistic and natural images. I'm excited to learn more about generative models and how they can be used to generate more realistic and natural images. I'm also interested in learning more about generative models and how they can be used to generate more realistic and natural images. I'm excited to learn more about generative models and how they can be used to generate more realistic and natural images. I'm particularly interested in generative models that can generate images that are indistinguishable from real-world images\n"
]
}
],
"source": [
"prompt = 'What field of AI is your most interest?'\n",
"print(generate_response(prompt))"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "adfaf086-3d78-411e-b860-27334e28872d",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.10"
}
},
"nbformat": 4,
"nbformat_minor": 5
}