{ "cells": [ { "cell_type": "code", "execution_count": 29, "id": "72e26e1c-3fda-4333-9401-4ee15d1bf3b8", "metadata": {}, "outputs": [], "source": [ "model_id = \"sentence-transformers/all-MiniLM-L6-v2\"\n" ] }, { "cell_type": "code", "execution_count": 30, "id": "68f1d64b-12b2-4d2a-a06a-6a7d35c3a6f5", "metadata": {}, "outputs": [], "source": [ "import requests\n", "\n", "api_url = f\"https://api-inference.huggingface.co/pipeline/feature-extraction/{model_id}\"\n", "headers = {\"Authorization\": f\"Bearer {hf_token}\"}" ] }, { "cell_type": "code", "execution_count": 31, "id": "aae00acb-6932-4b46-bc99-a542740c0f33", "metadata": {}, "outputs": [], "source": [ "def query(texts):\n", " response = requests.post(api_url, headers=headers, json={\"inputs\": texts, \"options\":{\"wait_for_model\":True}})\n", " return response.json()" ] }, { "cell_type": "code", "execution_count": 48, "id": "3955111a-6785-4375-b6cd-126f5bd9afd4", "metadata": {}, "outputs": [], "source": [ "\n", "import pandas as pd\n", "\n", "# Pfad zur CSV-Datei\n", "file_path = '/Users/I569961/Downloads/imdb_top_1000.csv'\n", "\n", "# Einlesen der CSV-Datei\n", "data = pd.read_csv(file_path)\n", "\n", "# Konvertieren des DataFrame in einen String\n", "data_string = data.to_string()\n", "\n", "output = query(texts)" ] }, { "cell_type": "code", "execution_count": 41, "id": "c5615ec3-2168-420c-9146-77c37b8df8fb", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
0123456789...374375376377378379380381382383
0-0.0238890.055259-0.011655-0.033414-0.012261-0.024873-0.0126630.0253460.018508-0.083508...-0.161688-0.0464260.0060040.005281-0.0033420.0277540.0204110.0057780.034098-0.006889
1-0.0126880.046874-0.010502-0.020384-0.0133610.0423220.016628-0.004099-0.002607-0.010188...-0.061594-0.020717-0.009082-0.029260-0.0662530.0652570.013229-0.023103-0.0027850.010474
20.0004940.1194120.005229-0.0927340.007773-0.0053250.034506-0.051981-0.006265-0.006111...-0.108326-0.049646-0.073399-0.029898-0.1027340.0621210.0346060.016877-0.0238610.005264
3-0.0297110.023298-0.057041-0.012183-0.0137100.0297960.0637390.001101-0.045124-0.040748...-0.1176820.0319240.0008540.020200-0.020666-0.0051670.0383700.0036170.033993-0.010255
4-0.0256280.070389-0.017380-0.0565670.0285770.0528230.067063-0.052618-0.054702-0.116230...-0.1181450.013343-0.055188-0.0327230.0084360.0191690.048212-0.0404120.0833460.026855
5-0.0226560.0211600.005105-0.0464940.0090740.0414950.054268-0.024185-0.013483-0.075966...-0.1001100.010750-0.031469-0.0048220.0396570.0263840.0455140.059089-0.0175090.007166
6-0.0029110.060791-0.009176-0.0061330.0404920.0365940.002054-0.0313450.031806-0.023495...-0.028763-0.060458-0.018598-0.040189-0.031486-0.0182990.002286-0.0734200.016235-0.000244
7-0.0805260.059888-0.048847-0.040176-0.0633420.0418480.1190450.010652-0.030095-0.004561...-0.1445660.0204040.0230880.005077-0.055645-0.0076750.050791-0.0059890.1345620.034817
8-0.0343880.0725010.014440-0.0366950.0140190.0630700.034683-0.014531-0.059862-0.045383...-0.114763-0.035894-0.019877-0.033375-0.0301680.0394120.0449930.000578-0.0251240.034191
9-0.0059640.025044-0.003182-0.025243-0.039823-0.0127720.0447130.014535-0.038213-0.041149...-0.0576210.0215940.048983-0.044541-0.0301370.0067790.0548540.0299370.0702140.041565
10-0.039008-0.010609-0.007383-0.050190-0.002518-0.0416410.026969-0.014801-0.014127-0.061637...-0.098168-0.031694-0.0521280.014774-0.0911500.0013240.053866-0.0839040.0376840.002314
11-0.095983-0.063012-0.116906-0.059075-0.051323-0.0034390.0186870.006544-0.049057-0.031649...-0.041085-0.008593-0.021544-0.021112-0.0195020.050040-0.0291750.0054980.1528920.024720
12-0.0116000.0565100.016624-0.094690-0.0098650.0723470.044124-0.041175-0.042124-0.102631...-0.1352390.013612-0.049410-0.0069250.085355-0.0078750.030402-0.0290120.001888-0.000133
\n", "

13 rows × 384 columns

\n", "
" ], "text/plain": [ " 0 1 2 3 4 5 6 \\\n", "0 -0.023889 0.055259 -0.011655 -0.033414 -0.012261 -0.024873 -0.012663 \n", "1 -0.012688 0.046874 -0.010502 -0.020384 -0.013361 0.042322 0.016628 \n", "2 0.000494 0.119412 0.005229 -0.092734 0.007773 -0.005325 0.034506 \n", "3 -0.029711 0.023298 -0.057041 -0.012183 -0.013710 0.029796 0.063739 \n", "4 -0.025628 0.070389 -0.017380 -0.056567 0.028577 0.052823 0.067063 \n", "5 -0.022656 0.021160 0.005105 -0.046494 0.009074 0.041495 0.054268 \n", "6 -0.002911 0.060791 -0.009176 -0.006133 0.040492 0.036594 0.002054 \n", "7 -0.080526 0.059888 -0.048847 -0.040176 -0.063342 0.041848 0.119045 \n", "8 -0.034388 0.072501 0.014440 -0.036695 0.014019 0.063070 0.034683 \n", "9 -0.005964 0.025044 -0.003182 -0.025243 -0.039823 -0.012772 0.044713 \n", "10 -0.039008 -0.010609 -0.007383 -0.050190 -0.002518 -0.041641 0.026969 \n", "11 -0.095983 -0.063012 -0.116906 -0.059075 -0.051323 -0.003439 0.018687 \n", "12 -0.011600 0.056510 0.016624 -0.094690 -0.009865 0.072347 0.044124 \n", "\n", " 7 8 9 ... 374 375 376 377 \\\n", "0 0.025346 0.018508 -0.083508 ... -0.161688 -0.046426 0.006004 0.005281 \n", "1 -0.004099 -0.002607 -0.010188 ... -0.061594 -0.020717 -0.009082 -0.029260 \n", "2 -0.051981 -0.006265 -0.006111 ... -0.108326 -0.049646 -0.073399 -0.029898 \n", "3 0.001101 -0.045124 -0.040748 ... -0.117682 0.031924 0.000854 0.020200 \n", "4 -0.052618 -0.054702 -0.116230 ... -0.118145 0.013343 -0.055188 -0.032723 \n", "5 -0.024185 -0.013483 -0.075966 ... -0.100110 0.010750 -0.031469 -0.004822 \n", "6 -0.031345 0.031806 -0.023495 ... -0.028763 -0.060458 -0.018598 -0.040189 \n", "7 0.010652 -0.030095 -0.004561 ... -0.144566 0.020404 0.023088 0.005077 \n", "8 -0.014531 -0.059862 -0.045383 ... -0.114763 -0.035894 -0.019877 -0.033375 \n", "9 0.014535 -0.038213 -0.041149 ... -0.057621 0.021594 0.048983 -0.044541 \n", "10 -0.014801 -0.014127 -0.061637 ... -0.098168 -0.031694 -0.052128 0.014774 \n", "11 0.006544 -0.049057 -0.031649 ... -0.041085 -0.008593 -0.021544 -0.021112 \n", "12 -0.041175 -0.042124 -0.102631 ... -0.135239 0.013612 -0.049410 -0.006925 \n", "\n", " 378 379 380 381 382 383 \n", "0 -0.003342 0.027754 0.020411 0.005778 0.034098 -0.006889 \n", "1 -0.066253 0.065257 0.013229 -0.023103 -0.002785 0.010474 \n", "2 -0.102734 0.062121 0.034606 0.016877 -0.023861 0.005264 \n", "3 -0.020666 -0.005167 0.038370 0.003617 0.033993 -0.010255 \n", "4 0.008436 0.019169 0.048212 -0.040412 0.083346 0.026855 \n", "5 0.039657 0.026384 0.045514 0.059089 -0.017509 0.007166 \n", "6 -0.031486 -0.018299 0.002286 -0.073420 0.016235 -0.000244 \n", "7 -0.055645 -0.007675 0.050791 -0.005989 0.134562 0.034817 \n", "8 -0.030168 0.039412 0.044993 0.000578 -0.025124 0.034191 \n", "9 -0.030137 0.006779 0.054854 0.029937 0.070214 0.041565 \n", "10 -0.091150 0.001324 0.053866 -0.083904 0.037684 0.002314 \n", "11 -0.019502 0.050040 -0.029175 0.005498 0.152892 0.024720 \n", "12 0.085355 -0.007875 0.030402 -0.029012 0.001888 -0.000133 \n", "\n", "[13 rows x 384 columns]" ] }, "execution_count": 41, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import pandas as pd\n", "embeddings = pd.DataFrame(output)\n", "embeddings" ] }, { "cell_type": "code", "execution_count": 49, "id": "9ac9b882-df78-4bda-8ae5-257171d9890c", "metadata": {}, "outputs": [], "source": [ "embeddings.to_csv(\"embeddings.csv\", index=False)" ] }, { "cell_type": "code", "execution_count": 55, "id": "6d573657-d483-441b-afbb-643f487815c5", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "tensor([[-8.9692e-02, -7.1678e-02, -6.4658e-02, -2.5273e-02, -2.5160e-02,\n", " 9.5493e-02, 6.0537e-02, 6.5189e-03, 7.0210e-02, 1.6540e-02,\n", " 3.1756e-02, 3.7128e-02, -3.0189e-02, 1.2188e-01, 4.2055e-03,\n", " 4.7994e-02, 9.5993e-02, 2.8378e-02, 6.5093e-02, -3.7466e-02,\n", " 1.8294e-02, -1.3735e-02, 6.9921e-02, 1.0683e-02, -2.6517e-02,\n", " -6.4150e-03, -1.4047e-02, -3.0414e-02, -1.2173e-01, -8.3550e-02,\n", " 4.4605e-02, 1.8618e-02, -4.5440e-02, -5.3226e-02, -5.2821e-02,\n", " 6.1405e-02, -5.6014e-03, -6.7444e-02, -2.9687e-02, -3.2697e-02,\n", " -9.0033e-02, -5.4642e-03, 7.9094e-02, -1.6152e-02, 9.2060e-02,\n", " -2.1775e-02, 3.0855e-02, -2.7018e-02, 5.1785e-02, 2.2410e-02,\n", " 4.5925e-03, 6.0932e-03, -6.1588e-02, -4.3769e-04, 1.4080e-02,\n", " -1.0000e-01, -5.5338e-02, -4.2764e-02, 3.1288e-02, -8.6337e-02,\n", " -1.2676e-03, -1.8121e-03, -4.2850e-03, -2.9365e-02, 3.6735e-02,\n", " -6.6461e-03, 5.2725e-02, -4.1446e-02, -1.7163e-02, 5.2195e-02,\n", " -3.5301e-02, -1.1078e-02, -8.1348e-03, -6.7892e-02, -7.6523e-02,\n", " -4.1602e-02, -2.3622e-02, -3.7206e-02, -9.2386e-02, -4.3152e-02,\n", " 4.9173e-02, -1.2231e-01, -1.2079e-01, 2.5151e-02, 2.3995e-02,\n", " -2.4697e-03, -1.4411e-02, -2.8515e-04, 5.7351e-03, 6.1428e-02,\n", " -7.7035e-02, 3.1017e-02, -1.0668e-02, 8.4897e-02, -3.5265e-02,\n", " -2.0904e-02, -1.8331e-02, -2.1771e-02, -4.5067e-02, 6.3914e-02,\n", " 2.5410e-02, -1.0044e-02, -7.8166e-02, -3.2381e-02, 1.3421e-01,\n", " -3.8063e-02, 1.1250e-01, -6.6807e-02, -2.6270e-02, 3.0373e-02,\n", " -4.7775e-03, -7.2811e-02, 6.6593e-03, 3.0932e-02, 5.7442e-02,\n", " 1.6413e-02, 8.1363e-02, 2.4339e-02, -3.8978e-02, 4.2294e-03,\n", " 9.0345e-02, 1.3644e-02, 6.1808e-02, -7.1033e-03, -2.0219e-03,\n", " -2.5045e-02, 4.6670e-02, -2.7020e-33, -3.0658e-02, -3.1585e-02,\n", " 5.6400e-02, 3.3047e-02, 4.2517e-02, 1.6862e-02, -1.1733e-02,\n", " -3.6467e-02, -1.9978e-02, 1.1683e-02, -1.1001e-01, -5.7765e-02,\n", " -5.5602e-02, -1.1054e-02, 8.1563e-02, 4.0051e-02, -2.3544e-02,\n", " 3.9336e-02, 2.9382e-02, -1.7138e-02, -6.1261e-02, 1.0746e-01,\n", " -6.7021e-02, 7.3112e-03, -3.8741e-02, -1.1130e-02, -2.4723e-03,\n", " 2.9728e-02, 2.0914e-03, -4.8236e-03, 2.8769e-02, -9.3932e-03,\n", " -4.1794e-03, 6.4001e-02, 1.3524e-01, -3.2297e-02, -7.8227e-02,\n", " -3.4412e-02, -3.1194e-02, 2.8607e-02, -3.1381e-02, 3.9271e-02,\n", " -5.4559e-02, 4.3373e-02, 3.1618e-02, 3.2962e-02, 1.4340e-02,\n", " 6.3454e-02, 1.2816e-02, 8.8069e-02, 9.7940e-03, 3.4391e-03,\n", " 3.8793e-02, 9.7933e-03, -4.0707e-02, 2.9351e-02, 1.6331e-02,\n", " -1.9151e-02, 4.2998e-02, 1.3868e-03, -1.5496e-02, 2.8651e-02,\n", " -1.6117e-02, 4.8554e-02, -3.3424e-02, 3.7980e-02, 5.1681e-02,\n", " 9.4712e-03, 2.1594e-02, -2.7505e-02, -3.4454e-02, 3.3451e-02,\n", " 5.0620e-02, -6.9366e-02, 6.2185e-02, -1.2269e-02, 3.8187e-02,\n", " -4.1367e-02, -7.7467e-02, -5.4399e-02, -8.7326e-02, -3.0441e-03,\n", " -1.8627e-02, 4.1645e-02, -3.1147e-03, 8.9177e-03, -5.0913e-02,\n", " -9.3077e-02, -4.5287e-03, 6.0444e-02, -1.0301e-01, -9.1961e-02,\n", " 6.9155e-02, 3.0234e-02, 9.0851e-02, 3.2439e-33, 8.6552e-02,\n", " -6.5429e-02, 7.8963e-03, 4.3653e-02, 5.5929e-02, 7.0854e-03,\n", " -7.0919e-02, 1.4281e-04, 8.3507e-02, 6.8680e-02, -1.4401e-02,\n", " -3.4416e-03, 4.6490e-02, 7.7277e-03, 1.1520e-01, -2.2469e-02,\n", " 4.4533e-02, -4.1014e-02, 5.2622e-02, 2.5087e-02, 3.6982e-02,\n", " 5.2463e-02, -3.0043e-02, -1.0178e-02, 3.0303e-02, 4.1595e-02,\n", " -2.8276e-02, 3.7373e-02, -3.4403e-02, -1.0499e-02, -2.0811e-02,\n", " -2.3120e-02, 4.5837e-02, 5.1766e-02, -5.1619e-02, 8.7256e-02,\n", " 1.6761e-02, -2.1394e-03, 3.6348e-03, 5.0054e-02, 2.0227e-02,\n", " -1.6015e-02, -2.1217e-02, 1.5421e-01, -1.3158e-02, -2.8398e-02,\n", " -1.3689e-02, 4.7356e-02, -5.7805e-02, 9.0794e-03, -1.1677e-01,\n", " 1.1524e-02, -7.1354e-02, -7.0080e-02, 7.9298e-02, 1.6545e-02,\n", " 2.4228e-02, 6.2592e-03, -1.4449e-02, -1.2710e-02, -3.4715e-02,\n", " 4.9897e-04, -5.4645e-02, 6.3109e-04, -1.5480e-02, 3.5777e-02,\n", " 6.8206e-03, 1.9068e-02, -2.4766e-02, -1.0899e-02, -2.5103e-02,\n", " -6.4506e-03, -5.4571e-02, 2.5454e-02, -9.5684e-03, -3.7722e-02,\n", " 6.2204e-02, -1.7215e-02, 5.9117e-02, -3.8532e-02, 1.3672e-02,\n", " 2.5865e-02, -1.5777e-02, 5.1845e-02, 1.2019e-02, 1.2119e-01,\n", " -1.8528e-02, 1.3776e-02, 1.1833e-03, 8.2213e-02, 3.8837e-02,\n", " 1.3432e-02, 1.3018e-01, -2.1637e-02, -8.7439e-02, -1.1291e-08,\n", " -4.3203e-02, 1.0041e-01, 1.7602e-02, -6.4081e-02, -1.2244e-02,\n", " -1.4865e-02, -7.1396e-02, 1.0025e-01, 3.0179e-02, 5.5649e-02,\n", " 2.0928e-02, 5.4815e-03, 3.5794e-02, 3.0769e-02, -7.5556e-02,\n", " 4.9906e-04, 3.8390e-02, -3.8530e-02, -4.8088e-02, 1.2520e-02,\n", " 9.9632e-03, 3.6153e-03, 6.6720e-02, -5.6117e-02, -2.8780e-02,\n", " 2.7026e-02, -4.5948e-02, -3.1805e-02, 3.7404e-02, 1.4018e-01,\n", " 7.4267e-02, 5.3000e-03, -3.6980e-02, 1.1275e-03, -8.6605e-02,\n", " -6.6038e-02, 4.8514e-02, 6.1135e-03, 5.5289e-03, -4.0952e-02,\n", " -3.3284e-02, 4.5587e-02, 3.3399e-02, -5.0551e-02, 1.5164e-02,\n", " 8.0977e-02, 3.3518e-02, -1.7935e-01, 4.1962e-02, -1.6916e-02,\n", " 1.1325e-02, -4.0028e-02, -4.4969e-02, 8.2986e-02, 4.9503e-02,\n", " 1.1134e-01, 5.6467e-03, 2.0734e-03, -3.6509e-02, 2.9235e-02,\n", " 1.1157e-01, -8.2191e-02, -1.8864e-02, 3.1457e-02]])" ] }, "execution_count": 55, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import torch\n", "\n", "question = [\"Best action movie?\"]\n", "output = query(question)\n", "\n", "query_embeddings = torch.FloatTensor(output)\n", "query_embeddings" ] }, { "cell_type": "code", "execution_count": null, "id": "beb947b2-5cf2-42f8-bfc0-e2c5dd33ba49", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "2ad5f317-769d-41b6-8dcb-eaf3983e1f8a", "metadata": {}, "outputs": [], "source": [ "\n" ] }, { "cell_type": "code", "execution_count": null, "id": "833f206f-bd61-41ab-a79e-c0fdf52159d1", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "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.12.4" } }, "nbformat": 4, "nbformat_minor": 5 }