|
9 | 9 | }, |
10 | 10 | { |
11 | 11 | "cell_type": "code", |
12 | | - "execution_count": 4, |
| 12 | + "execution_count": null, |
13 | 13 | "metadata": {}, |
14 | 14 | "outputs": [], |
15 | 15 | "source": [ |
16 | 16 | "import os\n", |
17 | 17 | "\n", |
18 | 18 | "import dotenv\n", |
19 | | - "from azure.identity import DefaultAzureCredential, get_bearer_token_provider\n", |
| 19 | + "from azure.identity import AzureDeveloperCliCredential, get_bearer_token_provider\n", |
20 | 20 | "from azure.search.documents import SearchClient\n", |
21 | 21 | "from azure.search.documents.indexes import SearchIndexClient\n", |
22 | 22 | "from azure.search.documents.indexes.models import (\n", |
|
36 | 36 | "\n", |
37 | 37 | "AZURE_SEARCH_SERVICE = os.getenv(\"AZURE_SEARCH_SERVICE\")\n", |
38 | 38 | "AZURE_SEARCH_ENDPOINT = f\"https://{AZURE_SEARCH_SERVICE}.search.windows.net\"\n", |
39 | | - "AZURE_SEARCH_IMAGES_INDEX = \"images-index\"\n", |
40 | | - "azure_credential = DefaultAzureCredential()\n", |
| 39 | + "AZURE_SEARCH_IMAGES_INDEX = \"images-index4\"\n", |
| 40 | + "azure_credential = AzureDeveloperCliCredential(tenant_id=os.getenv(\"AZURE_TENANT_ID\"))\n", |
41 | 41 | "search_client = SearchClient(AZURE_SEARCH_ENDPOINT, AZURE_SEARCH_IMAGES_INDEX, credential=azure_credential)" |
42 | 42 | ] |
43 | 43 | }, |
|
50 | 50 | }, |
51 | 51 | { |
52 | 52 | "cell_type": "code", |
53 | | - "execution_count": 5, |
| 53 | + "execution_count": 11, |
54 | 54 | "metadata": {}, |
55 | 55 | "outputs": [ |
56 | 56 | { |
57 | 57 | "data": { |
58 | 58 | "text/plain": [ |
59 | | - "<azure.search.documents.indexes.models._index.SearchIndex at 0x120d3efd0>" |
| 59 | + "<azure.search.documents.indexes.models._index.SearchIndex at 0x12c3ba910>" |
60 | 60 | ] |
61 | 61 | }, |
62 | | - "execution_count": 5, |
| 62 | + "execution_count": 11, |
63 | 63 | "metadata": {}, |
64 | 64 | "output_type": "execute_result" |
65 | 65 | } |
|
94 | 94 | "cell_type": "markdown", |
95 | 95 | "metadata": {}, |
96 | 96 | "source": [ |
97 | | - "### Configure Azure Computer Vision multi-modal embeddings API " |
| 97 | + "### Configure Azure AI Vision multi-modal embeddings API " |
98 | 98 | ] |
99 | 99 | }, |
100 | 100 | { |
101 | 101 | "cell_type": "code", |
102 | | - "execution_count": 6, |
| 102 | + "execution_count": 13, |
103 | 103 | "metadata": {}, |
104 | 104 | "outputs": [], |
105 | 105 | "source": [ |
|
110 | 110 | "from PIL import Image\n", |
111 | 111 | "\n", |
112 | 112 | "token_provider = get_bearer_token_provider(azure_credential, \"https://cognitiveservices.azure.com/.default\")\n", |
113 | | - "AZURE_COMPUTERVISION_SERVICE = os.getenv(\"AZURE_COMPUTERVISION_SERVICE\")\n", |
114 | | - "AZURE_COMPUTER_VISION_URL = f\"https://{AZURE_COMPUTERVISION_SERVICE}.cognitiveservices.azure.com/computervision/retrieval\"\n", |
| 113 | + "AZURE_VISION_ENDPOINT = os.getenv(\"AZURE_VISION_ENDPOINT\")\n", |
| 114 | + "AZURE_AIVISION_URL = f\"{AZURE_VISION_ENDPOINT}/computervision/retrieval\"\n", |
115 | 115 | "\n", |
116 | 116 | "def get_model_params():\n", |
117 | | - " return {\"api-version\": \"2023-02-01-preview\", \"modelVersion\": \"latest\"}\n", |
| 117 | + " return {\"api-version\": \"2024-02-01\", \"model-version\": \"2023-04-15\"}\n", |
118 | 118 | "\n", |
119 | 119 | "def get_auth_headers():\n", |
120 | 120 | " return {\"Authorization\": \"Bearer \" + token_provider()}\n", |
121 | 121 | "\n", |
122 | 122 | "def get_image_embedding(image_file):\n", |
123 | 123 | " mimetype = mimetypes.guess_type(image_file)[0]\n", |
124 | | - " url = f\"{AZURE_COMPUTER_VISION_URL}:vectorizeImage\"\n", |
| 124 | + " url = f\"{AZURE_AIVISION_URL}:vectorizeImage\"\n", |
125 | 125 | " headers = get_auth_headers()\n", |
126 | 126 | " headers[\"Content-Type\"] = mimetype\n", |
127 | | - " # add error checking\n", |
128 | 127 | " response = requests.post(url, headers=headers, params=get_model_params(), data=open(image_file, \"rb\"))\n", |
129 | 128 | " if response.status_code != 200:\n", |
130 | 129 | " print(image_file, response.status_code, response.json())\n", |
131 | 130 | " return response.json()[\"vector\"]\n", |
132 | 131 | "\n", |
133 | 132 | "def get_text_embedding(text):\n", |
134 | | - " url = f\"{AZURE_COMPUTER_VISION_URL}:vectorizeText\"\n", |
| 133 | + " url = f\"{AZURE_AIVISION_URL}:vectorizeText\"\n", |
135 | 134 | " return requests.post(url, headers=get_auth_headers(), params=get_model_params(),\n", |
136 | 135 | " json={\"text\": text}).json()[\"vector\"]" |
137 | 136 | ] |
|
145 | 144 | }, |
146 | 145 | { |
147 | 146 | "cell_type": "code", |
148 | | - "execution_count": 7, |
| 147 | + "execution_count": 14, |
149 | 148 | "metadata": {}, |
150 | 149 | "outputs": [], |
151 | 150 | "source": [ |
|
166 | 165 | }, |
167 | 166 | { |
168 | 167 | "cell_type": "code", |
169 | | - "execution_count": 8, |
| 168 | + "execution_count": 15, |
170 | 169 | "metadata": {}, |
171 | 170 | "outputs": [ |
172 | 171 | { |
|
177 | 176 | "<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=640x451>" |
178 | 177 | ] |
179 | 178 | }, |
180 | | - "execution_count": 8, |
| 179 | + "execution_count": 15, |
181 | 180 | "metadata": {}, |
182 | 181 | "output_type": "execute_result" |
183 | 182 | } |
|
189 | 188 | }, |
190 | 189 | { |
191 | 190 | "cell_type": "code", |
192 | | - "execution_count": 9, |
| 191 | + "execution_count": 16, |
193 | 192 | "metadata": {}, |
194 | 193 | "outputs": [ |
195 | 194 | { |
|
213 | 212 | }, |
214 | 213 | { |
215 | 214 | "cell_type": "code", |
216 | | - "execution_count": 10, |
| 215 | + "execution_count": 17, |
217 | 216 | "metadata": {}, |
218 | 217 | "outputs": [ |
219 | 218 | { |
|
224 | 223 | "<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=640x433>" |
225 | 224 | ] |
226 | 225 | }, |
227 | | - "execution_count": 10, |
| 226 | + "execution_count": 17, |
228 | 227 | "metadata": {}, |
229 | 228 | "output_type": "execute_result" |
230 | 229 | } |
|
235 | 234 | }, |
236 | 235 | { |
237 | 236 | "cell_type": "code", |
238 | | - "execution_count": 11, |
| 237 | + "execution_count": null, |
239 | 238 | "metadata": {}, |
240 | 239 | "outputs": [ |
241 | 240 | { |
|
259 | 258 | }, |
260 | 259 | { |
261 | 260 | "cell_type": "code", |
262 | | - "execution_count": 12, |
| 261 | + "execution_count": 19, |
263 | 262 | "metadata": {}, |
264 | 263 | "outputs": [ |
265 | 264 | { |
|
270 | 269 | "<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=640x404>" |
271 | 270 | ] |
272 | 271 | }, |
273 | | - "execution_count": 12, |
| 272 | + "execution_count": 19, |
274 | 273 | "metadata": {}, |
275 | 274 | "output_type": "execute_result" |
276 | 275 | } |
|
0 commit comments