|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "# Python: Using other python AI LLM packages - Transformers & pyTorch examples" |
| 8 | + ] |
| 9 | + }, |
| 10 | + { |
| 11 | + "cell_type": "markdown", |
| 12 | + "metadata": {}, |
| 13 | + "source": [ |
| 14 | + "### *Copyright 2024-today Dr. George Papagiannakis, papagian@csd.uoc.gr*\n", |
| 15 | + "*All Rights Reserved*\n", |
| 16 | + "### *University of Crete & Foundation for Research & Technology - Hellas (FORTH)*" |
| 17 | + ] |
| 18 | + }, |
| 19 | + { |
| 20 | + "cell_type": "markdown", |
| 21 | + "metadata": {}, |
| 22 | + "source": [ |
| 23 | + "### Example `Transformers` script" |
| 24 | + ] |
| 25 | + }, |
| 26 | + { |
| 27 | + "cell_type": "markdown", |
| 28 | + "metadata": {}, |
| 29 | + "source": [ |
| 30 | + "You need to install `pip install 'transformers[torch]` to run this script. You also need to install pyTorch as backend for the transformers package: `pip install 'transformers[torch]' `" |
| 31 | + ] |
| 32 | + }, |
| 33 | + { |
| 34 | + "cell_type": "code", |
| 35 | + "execution_count": null, |
| 36 | + "metadata": {}, |
| 37 | + "outputs": [], |
| 38 | + "source": [ |
| 39 | + "# Load model directly\n", |
| 40 | + "from transformers import AutoTokenizer, AutoModelForCausalLM, LlamaTokenizer\n", |
| 41 | + "import torch\n", |
| 42 | + "\n", |
| 43 | + "# Bug: ValueError: Tokenizer class LlamaTokenizer does not exist or is not currently imported.\n", |
| 44 | + "# Solution: pip3 install sentencepiece\n", |
| 45 | + "# Info: https://github.com/huggingface/transformers/issues/22222\n", |
| 46 | + "\n", |
| 47 | + "# Bug: ImportError: cannot import name 'LlamaTokenizer' from 'transformers'\n", |
| 48 | + "# Solution: pip3 install git+https://github.com/huggingface/transformers\n", |
| 49 | + "# Info: https://stackoverflow.com/questions/75907910/importerror-cannot-import-name-llamatokenizer-from-transformers\n", |
| 50 | + "\n", |
| 51 | + "tokenizer = AutoTokenizer.from_pretrained(\"mistralai/Mistral-7B-Instruct-v0.2\", padding_side=\"left\")\n", |
| 52 | + "model = AutoModelForCausalLM.from_pretrained(\"mistralai/Mistral-7B-Instruct-v0.2\")\n", |
| 53 | + "\n", |
| 54 | + "while True:\n", |
| 55 | + " prompt = input(\"Input your prompt: \")\n", |
| 56 | + "\n", |
| 57 | + " # https://stackoverflow.com/questions/74748116/huggingface-automodelforcasuallm-decoder-only-architecture-warning-even-after\n", |
| 58 | + " input_ids = tokenizer.encode(tokenizer.eos_token + prompt, return_tensors=\"pt\")\n", |
| 59 | + " \n", |
| 60 | + " print('generating response...')\n", |
| 61 | + " output = model.generate(input_ids, max_length=20, pad_token_id=tokenizer.eos_token_id)\n", |
| 62 | + "\n", |
| 63 | + " decoded_output = tokenizer.decode(output[0], skip_special_tokens=True)\n", |
| 64 | + "\n", |
| 65 | + " print(\"Response: \", decoded_output)\n" |
| 66 | + ] |
| 67 | + }, |
| 68 | + { |
| 69 | + "cell_type": "markdown", |
| 70 | + "metadata": {}, |
| 71 | + "source": [ |
| 72 | + "### Example `pyTorch` script" |
| 73 | + ] |
| 74 | + }, |
| 75 | + { |
| 76 | + "cell_type": "markdown", |
| 77 | + "metadata": {}, |
| 78 | + "source": [ |
| 79 | + "You need to install `pip install torch` to run this script or for Apple Silicon M1: `conda install pytorch torchvision torchaudio -c pytorch-nightly`" |
| 80 | + ] |
| 81 | + }, |
| 82 | + { |
| 83 | + "cell_type": "code", |
| 84 | + "execution_count": null, |
| 85 | + "metadata": {}, |
| 86 | + "outputs": [], |
| 87 | + "source": [ |
| 88 | + "# example to verify pyTorch is working\n", |
| 89 | + "import torch\n", |
| 90 | + "\n", |
| 91 | + "if torch.backends.mps.is_available():\n", |
| 92 | + " mps_device = torch.device(\"mps\")\n", |
| 93 | + " x = torch.ones(1, device=mps_device)\n", |
| 94 | + " print (x)\n", |
| 95 | + "else:\n", |
| 96 | + " print (\"MPS device not found.\")" |
| 97 | + ] |
| 98 | + }, |
| 99 | + { |
| 100 | + "cell_type": "code", |
| 101 | + "execution_count": null, |
| 102 | + "metadata": {}, |
| 103 | + "outputs": [], |
| 104 | + "source": [ |
| 105 | + "# another example to verify pyTorch is working\n", |
| 106 | + "\n", |
| 107 | + "import sys\n", |
| 108 | + "import platform\n", |
| 109 | + "import torch\n", |
| 110 | + "import pandas as pd\n", |
| 111 | + "import sklearn as sk\n", |
| 112 | + "\n", |
| 113 | + "has_gpu = torch.cuda.is_available()\n", |
| 114 | + "has_mps = getattr(torch,'has_mps',False)\n", |
| 115 | + "device = \"mps\" if getattr(torch,'has_mps',False) \\\n", |
| 116 | + " else \"gpu\" if torch.cuda.is_available() else \"cpu\"\n", |
| 117 | + "\n", |
| 118 | + "print(f\"Python Platform: {platform.platform()}\")\n", |
| 119 | + "print(f\"PyTorch Version: {torch.__version__}\")\n", |
| 120 | + "print()\n", |
| 121 | + "print(f\"Python {sys.version}\")\n", |
| 122 | + "print(f\"Pandas {pd.__version__}\")\n", |
| 123 | + "print(f\"Scikit-Learn {sk.__version__}\")\n", |
| 124 | + "print(\"GPU is\", \"available\" if has_gpu else \"NOT AVAILABLE\")\n", |
| 125 | + "print(\"MPS (Apple Metal) is\", \"AVAILABLE\" if has_mps else \"NOT AVAILABLE\")\n", |
| 126 | + "print(f\"Target device is {device}\")" |
| 127 | + ] |
| 128 | + }, |
| 129 | + { |
| 130 | + "cell_type": "markdown", |
| 131 | + "metadata": {}, |
| 132 | + "source": [ |
| 133 | + "### Example `Tensorflow` script" |
| 134 | + ] |
| 135 | + }, |
| 136 | + { |
| 137 | + "cell_type": "markdown", |
| 138 | + "metadata": {}, |
| 139 | + "source": [ |
| 140 | + "You need to instsall `pip install tensorflow` to run this script. Also for Apple Silicon M1: `pip install tensorflow-macos` and `pip install tensorflow_datasets` as well as `conda install -c apple tensorflow-deps`" |
| 141 | + ] |
| 142 | + }, |
| 143 | + { |
| 144 | + "cell_type": "code", |
| 145 | + "execution_count": 5, |
| 146 | + "metadata": {}, |
| 147 | + "outputs": [], |
| 148 | + "source": [ |
| 149 | + "# example to verify TensorFlow is working\n", |
| 150 | + "\n", |
| 151 | + "import tensorflow as tf\n", |
| 152 | + "import tensorflow_datasets as tfds" |
| 153 | + ] |
| 154 | + } |
| 155 | + ], |
| 156 | + "metadata": { |
| 157 | + "kernelspec": { |
| 158 | + "display_name": "elementsProject", |
| 159 | + "language": "python", |
| 160 | + "name": "python3" |
| 161 | + }, |
| 162 | + "language_info": { |
| 163 | + "codemirror_mode": { |
| 164 | + "name": "ipython", |
| 165 | + "version": 3 |
| 166 | + }, |
| 167 | + "file_extension": ".py", |
| 168 | + "mimetype": "text/x-python", |
| 169 | + "name": "python", |
| 170 | + "nbconvert_exporter": "python", |
| 171 | + "pygments_lexer": "ipython3", |
| 172 | + "version": "3.8.18" |
| 173 | + } |
| 174 | + }, |
| 175 | + "nbformat": 4, |
| 176 | + "nbformat_minor": 2 |
| 177 | +} |
0 commit comments