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 4 |
+
"cell_type": "markdown",
|
| 5 |
+
"id": "9064caea",
|
| 6 |
+
"metadata": {},
|
| 7 |
+
"source": [
|
| 8 |
+
"# Fine-Tuning AI Models on Personal Datasets with Hugging Face\n",
|
| 9 |
+
"\n",
|
| 10 |
+
"This notebook guides students through fine-tuning AI models for **sentiment analysis, chatbot responses, and image classification** using their own datasets uploaded to Hugging Face.\n",
|
| 11 |
+
"\n",
|
| 12 |
+
"## Install Necessary Libraries\n",
|
| 13 |
+
"```python\n",
|
| 14 |
+
"!pip install transformers datasets torch torchvision\n",
|
| 15 |
+
"```\n",
|
| 16 |
+
"\n",
|
| 17 |
+
"## Import Libraries\n",
|
| 18 |
+
"```python\n",
|
| 19 |
+
"from transformers import AutoModelForSequenceClassification, AutoModelForCausalLM, TrainingArguments, Trainer, AutoTokenizer, ViTForImageClassification, ViTFeatureExtractor\n",
|
| 20 |
+
"from datasets import load_dataset\n",
|
| 21 |
+
"import torch\n",
|
| 22 |
+
"import numpy as np\n",
|
| 23 |
+
"from sklearn.metrics import accuracy_score\n",
|
| 24 |
+
"from PIL import Image\n",
|
| 25 |
+
"from torchvision import transforms\n",
|
| 26 |
+
"```\n",
|
| 27 |
+
"\n",
|
| 28 |
+
"## Load Dataset from Hugging Face\n",
|
| 29 |
+
"```python\n",
|
| 30 |
+
"dataset_name = \"your-huggingface-username/your-dataset-name\"\n",
|
| 31 |
+
"dataset = load_dataset(dataset_name)\n",
|
| 32 |
+
"```\n",
|
| 33 |
+
"\n",
|
| 34 |
+
"## Fine-Tuning Sentiment Analysis Model (Good/Average/Bad)\n",
|
| 35 |
+
"```python\n",
|
| 36 |
+
"model_name = \"bert-base-uncased\"\n",
|
| 37 |
+
"tokenizer = AutoTokenizer.from_pretrained(model_name)\n",
|
| 38 |
+
"\n",
|
| 39 |
+
"def tokenize_function(examples):\n",
|
| 40 |
+
" return tokenizer(examples[\"text\"], padding=\"max_length\", truncation=True)\n",
|
| 41 |
+
"\n",
|
| 42 |
+
"tokenized_datasets = dataset.map(tokenize_function, batched=True)\n",
|
| 43 |
+
"\n",
|
| 44 |
+
"model = AutoModelForSequenceClassification.from_pretrained(model_name, num_labels=3)\n",
|
| 45 |
+
"\n",
|
| 46 |
+
"training_args = TrainingArguments(\n",
|
| 47 |
+
" output_dir=\"./results\",\n",
|
| 48 |
+
" evaluation_strategy=\"epoch\",\n",
|
| 49 |
+
" save_strategy=\"epoch\",\n",
|
| 50 |
+
" learning_rate=2e-5,\n",
|
| 51 |
+
" per_device_train_batch_size=8,\n",
|
| 52 |
+
" per_device_eval_batch_size=8,\n",
|
| 53 |
+
" num_train_epochs=3,\n",
|
| 54 |
+
" weight_decay=0.01,\n",
|
| 55 |
+
")\n",
|
| 56 |
+
"\n",
|
| 57 |
+
"def compute_metrics(eval_pred):\n",
|
| 58 |
+
" logits, labels = eval_pred\n",
|
| 59 |
+
" predictions = np.argmax(logits, axis=-1)\n",
|
| 60 |
+
" return {\"accuracy\": accuracy_score(labels, predictions)}\n",
|
| 61 |
+
"\n",
|
| 62 |
+
"trainer = Trainer(\n",
|
| 63 |
+
" model=model,\n",
|
| 64 |
+
" args=training_args,\n",
|
| 65 |
+
" train_dataset=tokenized_datasets[\"train\"],\n",
|
| 66 |
+
" eval_dataset=tokenized_datasets[\"test\"],\n",
|
| 67 |
+
" compute_metrics=compute_metrics,\n",
|
| 68 |
+
")\n",
|
| 69 |
+
"\n",
|
| 70 |
+
"trainer.train()\n",
|
| 71 |
+
"trainer.save_model(\"./fine_tuned_model\")\n",
|
| 72 |
+
"\n",
|
| 73 |
+
"def test_model(text):\n",
|
| 74 |
+
" inputs = tokenizer(text, return_tensors=\"pt\", truncation=True, padding=True)\n",
|
| 75 |
+
" with torch.no_grad():\n",
|
| 76 |
+
" logits = model(**inputs).logits\n",
|
| 77 |
+
" prediction = torch.argmax(logits, dim=-1).item()\n",
|
| 78 |
+
" label_map = {0: \"Good\", 1: \"Average\", 2: \"Bad\"}\n",
|
| 79 |
+
" return label_map[prediction]\n",
|
| 80 |
+
"\n",
|
| 81 |
+
"print(test_model(\"I feel great about my work today!\"))\n",
|
| 82 |
+
"```\n",
|
| 83 |
+
"\n",
|
| 84 |
+
"## Fine-Tuning Chatbot Model (DialoGPT)\n",
|
| 85 |
+
"```python\n",
|
| 86 |
+
"chatbot_model_name = \"microsoft/DialoGPT-small\"\n",
|
| 87 |
+
"chatbot_model = AutoModelForCausalLM.from_pretrained(chatbot_model_name)\n",
|
| 88 |
+
"chatbot_tokenizer = AutoTokenizer.from_pretrained(chatbot_model_name)\n",
|
| 89 |
+
"\n",
|
| 90 |
+
"def train_chatbot():\n",
|
| 91 |
+
" chatbot_datasets = dataset.map(lambda x: chatbot_tokenizer(x['question'], x['answer'], truncation=True, padding=True), batched=True)\n",
|
| 92 |
+
" trainer = Trainer(\n",
|
| 93 |
+
" model=chatbot_model,\n",
|
| 94 |
+
" args=training_args,\n",
|
| 95 |
+
" train_dataset=chatbot_datasets[\"train\"],\n",
|
| 96 |
+
" eval_dataset=chatbot_datasets[\"test\"],\n",
|
| 97 |
+
" )\n",
|
| 98 |
+
" trainer.train()\n",
|
| 99 |
+
" chatbot_model.save_pretrained(\"./fine_tuned_chatbot\")\n",
|
| 100 |
+
"\n",
|
| 101 |
+
"def test_chatbot(prompt):\n",
|
| 102 |
+
" inputs = chatbot_tokenizer(prompt, return_tensors=\"pt\")\n",
|
| 103 |
+
" response = chatbot_model.generate(**inputs, max_length=100)\n",
|
| 104 |
+
" return chatbot_tokenizer.decode(response[0], skip_special_tokens=True)\n",
|
| 105 |
+
"\n",
|
| 106 |
+
"print(test_chatbot(\"Tell me about my family history.\"))\n",
|
| 107 |
+
"```\n",
|
| 108 |
+
"\n",
|
| 109 |
+
"## Fine-Tuning Image Classification Model (ViT)\n",
|
| 110 |
+
"```python\n",
|
| 111 |
+
"image_model_name = \"google/vit-base-patch16-224-in21k\"\n",
|
| 112 |
+
"feature_extractor = ViTFeatureExtractor.from_pretrained(image_model_name)\n",
|
| 113 |
+
"image_model = ViTForImageClassification.from_pretrained(image_model_name, num_labels=2)\n",
|
| 114 |
+
"\n",
|
| 115 |
+
"def preprocess_image(image_path):\n",
|
| 116 |
+
" image = Image.open(image_path).convert(\"RGB\")\n",
|
| 117 |
+
" return feature_extractor(images=image, return_tensors=\"pt\")\n",
|
| 118 |
+
"\n",
|
| 119 |
+
"def train_image_model():\n",
|
| 120 |
+
" image_datasets = dataset.map(lambda x: {'pixel_values': preprocess_image(x['image'])}, batched=True)\n",
|
| 121 |
+
" trainer = Trainer(\n",
|
| 122 |
+
" model=image_model,\n",
|
| 123 |
+
" args=training_args,\n",
|
| 124 |
+
" train_dataset=image_datasets[\"train\"],\n",
|
| 125 |
+
" eval_dataset=image_datasets[\"test\"],\n",
|
| 126 |
+
" )\n",
|
| 127 |
+
" trainer.train()\n",
|
| 128 |
+
" image_model.save_pretrained(\"./fine_tuned_image_model\")\n",
|
| 129 |
+
"\n",
|
| 130 |
+
"def test_image(image_path):\n",
|
| 131 |
+
" inputs = preprocess_image(image_path)\n",
|
| 132 |
+
" with torch.no_grad():\n",
|
| 133 |
+
" logits = image_model(**inputs).logits\n",
|
| 134 |
+
" prediction = torch.argmax(logits, dim=-1).item()\n",
|
| 135 |
+
" label_map = {0: \"Attentive\", 1: \"Distracted\"}\n",
|
| 136 |
+
" return label_map[prediction]\n",
|
| 137 |
+
"\n",
|
| 138 |
+
"print(test_image(\"path_to_student_zoom_image.jpg\"))\n",
|
| 139 |
+
"```\n",
|
| 140 |
+
"\n",
|
| 141 |
+
"This notebook provides students with an end-to-end guide for **customizing AI models** with their **own datasets**. 🚀\n"
|
| 142 |
+
]
|
| 143 |
+
}
|
| 144 |
+
],
|
| 145 |
+
"metadata": {},
|
| 146 |
+
"nbformat": 4,
|
| 147 |
+
"nbformat_minor": 5
|
| 148 |
+
}
|