{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": { "execution": { "iopub.execute_input": "2025-03-13T10:03:08.356914Z", "iopub.status.busy": "2025-03-13T10:03:08.356580Z", "iopub.status.idle": "2025-03-13T10:03:08.533121Z", "shell.execute_reply": "2025-03-13T10:03:08.532252Z", "shell.execute_reply.started": "2025-03-13T10:03:08.356884Z" } }, "outputs": [], "source": [ "from kaggle_secrets import UserSecretsClient\n", "user_secrets = UserSecretsClient()\n", "secret_value_0 = user_secrets.get_secret(\"HF_Token\")" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "_cell_guid": "b1076dfc-b9ad-4769-8c92-a6c4dae69d19", "_uuid": "8f2839f25d086af736a60e9eeb907d3b93b6e0e5", "execution": { "iopub.execute_input": "2025-03-13T10:03:08.534578Z", "iopub.status.busy": "2025-03-13T10:03:08.534321Z", "iopub.status.idle": "2025-03-13T10:14:01.403519Z", "shell.execute_reply": "2025-03-13T10:14:01.402735Z", "shell.execute_reply.started": "2025-03-13T10:03:08.534558Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Using device: cuda\n" ] }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "a9551b71420a46b3ba4ccabf52a6a351", "version_major": 2, "version_minor": 0 }, "text/plain": [ "tokenizer_config.json: 0%| | 0.00/48.0 [00:00" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "import matplotlib.pyplot as plt\n", "\n", "# Assuming you store losses during training like this:\n", "train_losses = []\n", "test_losses = []\n", "\n", "for epoch in range(epochs):\n", " total_train_loss = 0\n", " total_test_loss = 0\n", "\n", " model.train()\n", " for batch in train_dataloader:\n", " inputs, labels = batch\n", " optimizer.zero_grad()\n", " outputs = model(**inputs)\n", " loss = loss_fn(outputs.logits, labels)\n", " loss.backward()\n", " optimizer.step()\n", " total_train_loss += loss.item()\n", " \n", " model.eval()\n", " with torch.no_grad():\n", " for batch in test_dataloader:\n", " inputs, labels = batch\n", " outputs = model(**inputs)\n", " loss = loss_fn(outputs.logits, labels)\n", " total_test_loss += loss.item()\n", "\n", " train_losses.append(total_train_loss / len(train_dataloader))\n", " test_losses.append(total_test_loss / len(test_dataloader))\n", "\n", "# Plot the loss curves\n", "plt.plot(range(1, epochs+1), train_losses, label=\"Train Loss\")\n", "plt.plot(range(1, epochs+1), test_losses, label=\"Test Loss\")\n", "plt.xlabel(\"Epochs\")\n", "plt.ylabel(\"Loss\")\n", "plt.legend()\n", "plt.title(\"Training vs. Test Loss\")\n", "plt.show()\n" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "execution": { "iopub.execute_input": "2025-03-13T10:22:02.540339Z", "iopub.status.busy": "2025-03-13T10:22:02.540114Z", "iopub.status.idle": "2025-03-13T10:22:02.642619Z", "shell.execute_reply": "2025-03-13T10:22:02.642022Z", "shell.execute_reply.started": "2025-03-13T10:22:02.540320Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "User: Tell me a joke\n", "Bot: Why don't skeletons fight each other? They don't have the guts!\n", "\n", "User: Recommend a good book\n", "Bot: Reading books can broaden your knowledge, enhance creativity, and provide a great way to relax.\n", "\n", "User: What's the weather like today?\n", "Bot: You can check the weather on a weather app or website.\n", "\n", "User: How can I save money?\n", "Bot: Investing in stocks, mutual funds, or real estate can help grow your wealth over time.\n", "\n", "User: How do I meditate?\n", "Bot: Meditation can reduce stress, improve focus, and promote emotional well-being.\n", "\n" ] } ], "source": [ "test_inputs = [\n", " \"Tell me a joke\", \n", " \"Recommend a good book\", \n", " \"What's the weather like today?\", \n", " \"How can I save money?\", \n", " \"How do I meditate?\"\n", "]\n", "\n", "for input_text in test_inputs:\n", " response = predict_intent(input_text)\n", " print(f\"User: {input_text}\\nBot: {response}\\n\")\n" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "execution": { "iopub.execute_input": "2025-03-13T10:33:46.308702Z", "iopub.status.busy": "2025-03-13T10:33:46.308278Z", "iopub.status.idle": "2025-03-13T10:33:46.964362Z", "shell.execute_reply": "2025-03-13T10:33:46.963545Z", "shell.execute_reply.started": "2025-03-13T10:33:46.308678Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Model saved to bert_chatbot_model.pth\n", "Tokenizer saved to bert_chatbot_tokenizer\n" ] } ], "source": [ "# Define save paths\n", "model_path = \"bert_chatbot_model.pth\"\n", "tokenizer_path = \"bert_chatbot_tokenizer\"\n", "\n", "# Save model state dictionary\n", "torch.save(model.state_dict(), model_path)\n", "\n", "# Save tokenizer\n", "tokenizer.save_pretrained(tokenizer_path)\n", "\n", "print(f\"Model saved to {model_path}\")\n", "print(f\"Tokenizer saved to {tokenizer_path}\")" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "execution": { "iopub.execute_input": "2025-03-13T10:38:56.933623Z", "iopub.status.busy": "2025-03-13T10:38:56.933265Z", "iopub.status.idle": "2025-03-13T10:38:57.968705Z", "shell.execute_reply": "2025-03-13T10:38:57.967785Z", "shell.execute_reply.started": "2025-03-13T10:38:56.933591Z" } }, "outputs": [ { "data": { "text/plain": [ "('chatbot_model/tokenizer_config.json',\n", " 'chatbot_model/special_tokens_map.json',\n", " 'chatbot_model/vocab.txt',\n", " 'chatbot_model/added_tokens.json')" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "model.save_pretrained(\"chatbot_model\")\n", "tokenizer.save_pretrained(\"chatbot_model\")" ] } ], "metadata": { "kaggle": { "accelerator": "nvidiaTeslaT4", "dataSources": [ { "datasetId": 6859551, "sourceId": 11016856, "sourceType": "datasetVersion" } ], "dockerImageVersionId": 30919, "isGpuEnabled": true, "isInternetEnabled": true, "language": "python", "sourceType": "notebook" }, "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.20" } }, "nbformat": 4, "nbformat_minor": 4 }