File size: 2,601 Bytes
6a6df5e | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 | {
"cells": [
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "77414e9d91534e578d51cced47102e57",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"Loading checkpoint shards: 0%| | 0/3 [00:00<?, ?it/s]"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"You're using a GemmaTokenizerFast 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"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"TAX INVOICE\n",
"Bill No. 10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\n"
]
}
],
"source": [
"from transformers import AutoProcessor, PaliGemmaForConditionalGeneration\n",
"import requests\n",
"from PIL import Image\n",
"\n",
"model_id = \"google/paligemma-3b-mix-224\"\n",
"model = PaliGemmaForConditionalGeneration.from_pretrained(model_id)\n",
"processor = AutoProcessor.from_pretrained(model_id)\n",
"\n",
"prompt = \"ocr\"\n",
"image_file = \"sample_invoice.png\"\n",
"raw_image = Image.open(image_file)\n",
"inputs = processor(prompt, raw_image, return_tensors=\"pt\")\n",
"output = model.generate(**inputs, max_new_tokens=100)\n",
"\n",
"print(processor.decode(output[0], skip_special_tokens=True)[len(prompt):])\n",
"# bee\n"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"GPU is not available\n"
]
}
],
"source": [
"import torch\n",
"\n",
"if torch.cuda.is_available():\n",
" print(\"GPU is available\")\n",
"else:\n",
" print(\"GPU is not available\")"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "gemini_gemma",
"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.10.14"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
|