File size: 2,553 Bytes
baad382 |
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 |
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"id": "28e561c1-96da-4fc9-9261-16c4562b057b",
"metadata": {},
"outputs": [],
"source": [
"import gradio as gr\n",
"import numpy as np\n",
"import pandas as pd\n",
"import matplotlib.pyplot as plt\n",
"import torch\n",
"from torch.utils.data import Dataset, DataLoader\n",
"from transformers import AutoModel, AutoTokenizer"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "10ff0dd7-7cd1-4eb8-824e-56b3d640b271",
"metadata": {},
"outputs": [],
"source": [
"from transformers import BlipForConditionalGeneration, AutoProcessor\n",
"\n",
"model = BlipForConditionalGeneration.from_pretrained(\"cassmussard/BLIP_airbnb\")\n",
"processor = AutoProcessor.from_pretrained(\"Salesforce/blip-image-captioning-base\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "fd119e29-aaf4-4aec-af80-77b8e11fb82f",
"metadata": {},
"outputs": [],
"source": [
"model.eval()\n",
"def predict(image):\n",
" inputs = processor(images=image, return_tensors=\"pt\")\n",
" pixel_values = inputs[\"pixel_values\"]\n",
" generated_ids = model.generate(pixel_values=pixel_values, max_length=50)\n",
" generated_caption = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]\n",
" return generated_caption"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "435fdb1e-167f-45dc-bbbb-b8d0e05becbb",
"metadata": {},
"outputs": [],
"source": [
"iface = gr.Interface(fn=predict, \n",
" inputs=\"image\", \n",
" outputs='label',\n",
" live=True,\n",
" description=\"Draw a number on the sketchpad to see the model's prediction.\",\n",
" ).launch(debug=True, share=True);\n",
"iface.launch(share=True)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e4ffb76c-667e-4a4e-8fa5-bc3d12e61e83",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "venv",
"language": "python",
"name": "venv"
},
"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.12"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
|