{ "cells": [ { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "view-in-github" }, "source": [ "\"Open\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "!pip install -q requests" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "id": "DW1zKIWiO-75" }, "outputs": [], "source": [ "# Set the following API Keys in the Python environment. Will be used later.\n", "HF_API_TOKEN = \"[HF_API_TOKEN]\"" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "id": "LDzGdD_fOVqD" }, "outputs": [], "source": [ "import requests\n", "\n", "\n", "def query(payload, model_name):\n", " API_URL = f\"https://api-inference.huggingface.co/models/{model_name}\"\n", " headers = {\"Authorization\": f\"Bearer {HF_API_TOKEN}\"}\n", "\n", " response = requests.post(API_URL, headers=headers, json=payload)\n", "\n", " return response.json()" ] }, { "cell_type": "markdown", "metadata": { "id": "Ja_HCApgX4kh" }, "source": [ "# API\n" ] }, { "cell_type": "markdown", "metadata": { "id": "LkTu5QCuUlgK" }, "source": [ "## GPT-2 for Text Completion\n" ] }, { "cell_type": "code", "execution_count": 54, "metadata": { "id": "dKYyNb3ePxC0" }, "outputs": [], "source": [ "data = query({\"inputs\": \"Can you please let us know more details about\"}, \"gpt2\")" ] }, { "cell_type": "code", "execution_count": 55, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "7JnvDiRkP6Oy", "outputId": "996bae88-a023-4e0f-cb40-3d2291500bd5" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Can you please let us know more details about 4C Williams and PitchSwitch? Let us know on Instagram, Instagram and Google+.\n", "\n", "All Drummer III stickers since December 2015, will give you his personal favorites, including the Hasbro Bros. swag, the Narrows with an acoustic guitar and great new Fall Trailer some matchstick!\n", "\n", "Tell Ann and Matt to FLAMING out their Stardust Arses and raise $1K.\n", "\n", "Inspired by Novice Density, Anthony Bonanni of Atten.\n" ] } ], "source": [ "print(data[0][\"generated_text\"])" ] }, { "cell_type": "markdown", "metadata": { "id": "ZQDcZ_V8UoJk" }, "source": [ "## Llama-3 8B\n" ] }, { "cell_type": "code", "execution_count": 48, "metadata": { "id": "LlKTaekdRAc8" }, "outputs": [], "source": [ "data = query(\n", " {\"inputs\": \"Write a poem about blue sky.\"}, \"meta-llama/Meta-Llama-3-8B-Instruct\"\n", ")" ] }, { "cell_type": "code", "execution_count": 49, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "t-l92bDtRAat", "outputId": "5dc5d703-125a-40ec-b3a0-f0bc99ee7d65" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Write a poem about blue sky. A blue sky is a type of sky that is a deep shade of blue, often associated with clear weather. Here is a poem about a blue sky:\n", "\n", "A blue sky stretches wide and high,\n", "A canvas of serenity, touching the eye.\n", "Deep and calming, it's a wondrous sight,\n", "A blue sky that lifts the spirits, banishes the night.\n", "\n", "Upon its expanse, a few white cloud rise,\n", "Like wisps of cotton, gently drifting by.\n", "The sun shines bright, its\n" ] } ], "source": [ "data[0][\"generated_text\"]" ] }, { "cell_type": "markdown", "metadata": { "id": "QKPe-_wGUxBq" }, "source": [ "## Summarization\n" ] }, { "cell_type": "code", "execution_count": 69, "metadata": { "id": "Fbc6cOXLURqN" }, "outputs": [], "source": [ "text_to_summarize = \"\"\"Wall Street was quiet Friday ahead of the monthly jobs report.\n", "S&P and Nasdaq futures were little changed and Dow futures were a couple of points lower.\n", "It’s been a shortened week for traders, with markets closing early Wednesday and closed all day Thursday in observance of the July 4th holiday.\n", "Investors are looking for another Goldilocks jobs report Friday that shows a gentle cooling-off of the labor market.\n", "Wednesday’s ADP data, which showed that private payrolls fell to 150,000, created little reaction among traders\n", "— but the government’s monthly tally will likely trigger more of a response.\"\"\"\n", "\n", "data = query({\"inputs\": text_to_summarize}, \"facebook/bart-large-cnn\")" ] }, { "cell_type": "code", "execution_count": 71, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 70 }, "id": "-gyShPJTURlT", "outputId": "0e902878-1ab2-4804-f807-092340fb357e" }, "outputs": [ { "data": { "application/vnd.google.colaboratory.intrinsic+json": { "type": "string" }, "text/plain": [ "'S&P and Nasdaq futures were little changed and Dow futures were a couple of points lower. It’s been a shortened week for traders, with markets closing early Wednesday and closed all day Thursday in observance of the July 4th holiday. Wednesday’S ADP data, which showed that private payrolls fell to 150,000, created little reaction among traders.'" ] }, "execution_count": 71, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data[0][\"summary_text\"]" ] }, { "cell_type": "markdown", "metadata": { "id": "0o8zEflXVIkn" }, "source": [ "## Sentiment Analysis\n" ] }, { "cell_type": "code", "execution_count": 61, "metadata": { "id": "1osFZIZaVILp" }, "outputs": [], "source": [ "text_to_analyse = \"\"\"I love how this app simplifies complex tasks \\\n", "effortlessly . I'm frustrated by the frequent errors in the software's \\\n", "latest update\"\"\"\n", "\n", "data = query(\n", " {\"inputs\": text_to_summarize}, \"distilbert-base-uncased-finetuned-sst-2-english\"\n", ")" ] }, { "cell_type": "code", "execution_count": 64, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "jydfp3NeURik", "outputId": "48f99b70-eadc-4e41-e2c0-262ea1b24007" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[{'label': 'POSITIVE', 'score': 0.9969911575317383}, {'label': 'NEGATIVE', 'score': 0.0030087968334555626}]\n" ] } ], "source": [ "print(data[0])" ] }, { "cell_type": "markdown", "metadata": { "id": "Ban4tf-Xdp3-" }, "source": [ "## Example of Loading Model\n" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "rnSMaBhDdbvm", "outputId": "3dc63f31-cd7f-45a3-e926-a0138cf2ea04" }, "outputs": [ { "data": { "text/plain": [ "{'error': 'Model mistralai/Mistral-Nemo-Instruct-2407 is currently loading',\n", " 'estimated_time': 979.8225708007812}" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data" ] }, { "cell_type": "markdown", "metadata": { "id": "dPWBYfqMX0in" }, "source": [ "# Local\n" ] }, { "cell_type": "markdown", "metadata": { "id": "fJJRdFZXVYqG" }, "source": [ "## Text-to-image\n" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "9mul8JIVURa4", "outputId": "570f1692-e873-4f26-a008-4e7cb186b004" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m2.2/2.2 MB\u001b[0m \u001b[31m10.6 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m21.3/21.3 MB\u001b[0m \u001b[31m40.2 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[?25h" ] } ], "source": [ "!pip install -q diffusers[\"torch\"] transformers" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 767, "referenced_widgets": [ "c223b971b7bf4c209f67bce265f8c1f7", "73fb24adcfb34d53bd084ad1c93fda7e", "f982d29ce874468e9f641185ce2002c0", "ab6e7ce5342149c18b82a010c46e8ba5", "61713ea66a5641e0ab41c88ad8cfc973", "903f54a8184043a0b864e89714a737a6", "4ff8066b5687405294dc75709cece55d", "cc51f7134bcf49a686aa0b4c7b60fd7d", "32e0f8b2d888463496f9124d7481504a", "913bd87b39ee42e28594bb052ee279a7", "c0179acb6f56403ab78cd1d11f0ed785", "6616b8f5be4644e8b9aa990a127304c6", "fb288c254a854d12bdf1bf45ef1fd154", "3d8e7cbf909c49a981c644f88b5debf6", "d6c8c041c748434fb15248069127b3a6", "5a6be4152402418ea7f532746819dda7", "d903cf5eda8849e6878d8ddb34cf510b", "60fa3699ad594aeb923fafb736d8e9a3", "94b21bd28df941e39efc75a9aa7fbf84", "8892011e8bc14e7c9dd83e7d9476cecc", "2dbac1f7a27849cfbb23747cc12757dd", "7881066ee38a423189d8d54342bc680c", "8c9f931f35284a77bbe34a66a2bb8694", "a1690535f4594edc92a42e187e38b1d8", "f717b8fbdef4448c8c511735da9d126e", "531cd837573241fe9b22ac95bddff4df", "3426613529a04c9dab669a67d935fca6", "e90dac22072140d282affbba912e11f2", "f92258d83195474d99c4c2e66a526887", "1ec5b51f033b43dca5efeaac04b84463", "7aac6fef6395465493cd7ad6213b8dc2", "45dda81861664c30823bedba5d035329", "cd84e64780b24b99a42849ec36a07d22", "2cd6420a99184279955d50b2a24b74e1", "7dc5ad904ad64631a9741c2fea33e4ee", "7ce4ef01a8354e7b8e2e5ccb54026cdb", "1af260f02ee74802b1229af37507a7d1", "5cab9eb5b07045e89193d8ad02bfc94b", "be08fcdfb2234342b08b7cfc7d52b897", "ed2c2daf251f45fea6a555b2fdc5cc71", "cc4dc61769254cbd8a8a7fc5fb7fe7b2", "90898889ff4b4cd682808ec3db91043e", "08223796c7d8457185ca9f967ef641d1", "a6742e1252924a46810ccb626d6fc719", "74a50d7871354ea6ae51f14497dbbab6", "082d5475cfef476c9430f7f73264dfc4", "7bd4768e0d264d8a84c7af82efd42ebb", "03fbc22c9bca4c85a9d5f099c9bb85fb", "b219d3ae4dfb454fb5d19c2115ad28a0", "08970cab24054dc5b84343a2e14707c7", "c72ab52ad1eb407a98855fdcc4b61756", "beb56cd7ec85408a8c6873afca8702b8", "b234ae5ec3f8407481929424cb92c2ec", "58d5f28147fd44929d0cf2cc56e67d40", "5fd37ae8150c4cc18c9c6ea66dd46432", "9a718210326d4cbf99ca33b72e593206", "555729f723c74805be814c64b97d73c3", "d419700a13f4498ab4102e8b811e23b7", "846052c025fd45009226c47841b48ef1", "fac7d5ee12c840daa55ebad6ba05a4f0", "fc0a005dd96a4a8dbf4dea7e1e17f9ef", "e9325881f918461dae7906ee840b056f", "3ab76b25906b4cba8fb417fa68dc5a29", "52381346ee024928960c7c94412392f0", "54176289b06546ec84b0dd1626ba7572", "89dc923410c149339019d4e901200f4f", "dfaf97015da34fc7a4eeee270b8fc2c4", "9bebe129a9354a84a58f0a4f0b370006", "1fc627cc8d364a06b99cd828ebaf610f", "a95c0404deb5499ca7a3598e13927815", "21ada111bcd642e0919871466e5e2364", "5b45cfcb34014950b0ea549b4eecd8e4", "9f13d9da92374e0da366a7a954b13f0e", "1695f394eec045bf8ab69d2b5dec2b12", "b5e611da9b8a47a78234906d49693db8", "f2a5729ea45e4d59b5f2b4ae4b1ad33d", "a489e6daffd74dd990cbad9133580abf", "cac2810122c54795b7bba27e09570359", "c79ec3c593a343c4a28c7965a2811194", "e7b6478f14294cdab391403b17601303", "aa88bcbf698a4649bc604d0eea2a869a", "88f9a84ff83645e58a19fb4366bc2701", "4f541278e9c24eceaedfc206c4db515a", "00da1db451424d9fb0d97a9dbf42443e", "f2e150c037bf41119b3c75d9847865e5", "c89b891ac9a9460787671ee369decc7d", "41434b1fe0c042ef99b96d6c1fc3a2a0", "2ad2213e28ae4dd89a28f7c1c8ed796f", "8ffe241f2a3c4bf6aaf739a1d58c596e", "e233eebf7177468e905f6c921a8dc143", "7d887ca434cd403a9135796a0e1c13bc", "559cfcca80dc4fbf8482f8a8d9dd1aff", "5943c331502f4af3940cb6ecc9dade8d", "1af8b2feeb1741f8b7bc30957acae0b0", "5555cb8b8c244a879ae0326323d16011", "b8ce83ed484c4b6dae9a2760fcbd19f4", "f62d3fc075134e568cefe3acff93764d", "40ef0cf682d340eabac54ce63e396f83", "04c84de3bd414595822983a8120b550f", "c0da1b604ba442598e1e5ddd5df36c0d", "b2048ba608224621b92494f70eb7f218", "623791ece0714b4e8c67249e62655c77", "6fe62cdd823c49899c59575585514df6", "4985083bc61f4e78bee70c41ed94e2c4", "aa95a71916d1431fa4a41eacaac2718d", "8b99cfcefe6c46e28248606dc92cbc06", "493e64e37f3d4ae0a1d0ee88ad5d71d4", "c1a3367f50aa424fb4ffd2c4654d384b", "562ff088e2e64fe692305fd0f2d7ebeb", "b9dc943115fc4489a8469c533574af63", "fe9c14f8c2f747849b74fa061dbd08b0", "f0c8a5c812554f3dae38fcda6eac9608", "3d8a0bfe47d842a8a8a360c375915117", "cc3b549cbbca458bbcc33d188b875202", "311ad28fdeb04718a4ce4cca28d0293e", "b06f03aa73ef4878b362b2345fbccf83", "23bcb341e2f24c52baeecd786405202e", "07f852f886e24cada4bbe9b05b31d6c2", "034b227692d84e93b5c87594da330bb8", "1d687dd42aab4d2887d11bca1fd503e0", "07aa0d155ba24c2f945ee3fe2ef0e933", "b66f018ef2b14d2288c53f52d9aea27a", "53187803694144d3a2621c5701183588", "43638c85dd8b4f8f90e3d11b026c1d84", "66a07fa128b04b3caaee19442d15f543", "314360d38d68408b8b9624b4777cda36", "50e5a7fa62c842acb680ebd042d30da4", "cafc186aa7604281a9c32b33cf28bfe0", "4424ed29a29b43b1aa1b22eff634fe9a", "9f6183d5a0ab44318f321b09c77c59eb", "3fe3f0814c134ae98c67c5e4f8c42644", "602fbcc6dddc489eb46df363447725a7", "fc0a3119093f4c85b293b3a7f5228699", "a02e49dc56be417bb0004e2489ed90e4", "0a7f36f69a914354bc4a2fd80e45dd57", "5442c5130a7141c88d580bf01deece89", "3917a06e59ba46e099b5e71c50cba9dd", "e8690e891a424fd6998cc57910f675d5", "925c66bdd40a4dfdbb06b5cb4d40ad59", "18826e71e8b44ee8b5f8e716848b7be9", "da746d2951274f3dbb83bf42eb27210c", "ed2174c1360844e1bc3344abbf16a563", "710ac26830f34115b5c47556cdfe2658", "007b46d270c241f5846cc0516b7ec340", "1ca67ec3eb324aba95b8d14462f03ae7", "44ced56ae8814eaaa1eebbad86b3e625", "fe1bfecf204b407681d6af5f185b17a8", "9a9c21297ad841f694fc6b66732254f9", "59ae7b0393bc4345a5dbcfc4ac599e87", "02781e98da4e4651b1fa29c28e6e9548", "2e72670c700641b49d049d93560a2d2d", "c919bb00a5b54396a6dbef8dfd892268", "4095a5ba994b46fa808dd68f5f710a03", "a006619051d44927abfec979a741436d", "17540bf5c5bc4c1abed57e0566834266", "565468247edc4120875fa5a4e4409c89", "5b1ec2b5ba574039ad46699bb561c628", "801d520d6617404cab2be04b6811238a", "43f4e2ff34b5424b8f282c3e66448bf8", "fec1a5c701034978af2e89412fa595e1", "d915cb534da449258cc52aaee8a4f073", "1196d60590074804a95cb4da167406c3", "8b674d64a7f146deb9abbaf5e77c9a44", "bb5a8ad54650471ab45cd8f2db531b9b", "2e2bcbe0a352491f937c3d0974ebcaae", "8cc4b5424a1747ff97c210c707b21dee", "fe30ff86d0fe49859b8b761fa467a47a", "2e406af985ea4b37846fc7ee7a3b5ecc", "3d28f23e70d14b68953076d6ca579687", "74ef654668ff4a21a4dce32d49f9e0a9", "59f12c6847ff42bc9fcf06df5b184ca3", "b673cd4bd5604735b71987302ac1d07e", "f7ea677aa30441ee963922cb18446f5d", "1833ba8a1baa43df8db01eb39b10d21c", "0dc98d7a85f94cdabaf17129ae28c769", "fc340ac3cbfa4bb983a6f693c0b172ea", "93ce51c5c3e04b7c89ee19bf5fd7d267", "c13b46c7c95941fa9c28dd3c85e940ec", "a5769db6fcfe4c6f8c0d9b927a19858d", "f6f2bc547f174e92b365452a2d9f7dbb", "e678f3a68fe142babeb73a7119af84c8", "cf763fdc46214dbe9828927f8679eaa0", "2b5e633356f54309a827b01ad41cf6ee", "fc9e1e6b8ad647d3860a715503ab1a2c", "7ecb6248a0d54fb3afef0c404ab0753c", "10a68953d57a44a78d72a6cf59ca8315", "9228fd1f60a54c85bf8e6b9d0514358b", "f12f0348d86f4811a2aee580d99ea48c", "357b99215ad243a8be221dccb3ac94b4", "55851f63306f4da783369e490bb4f899", "c0321b82975b4fd1950ec5adfbddcd3a", "82a8938be846489c9adbe36019683fff", "3ecd5cb9184b4d16b5e1ace618ac66a5", "235e932ae54541aa9ccaf527547e2c63", "5ad7749e3bd14d5da240fead7ae93da2", "c466bb0f6a764dcf926e946c1a581171", "59154c3861e341209e399c9310119b6f", "23bb161e06dd4797b21792895433be72", "6e225678c2cf476db973e1b769124a21", "a8ab98d1a0424510994c969c3fb5f6cb", "a13a3dc4a39d47ae9cc987e082837da0", "da92aef28a0d4114b8c5b6b1167a089f", "7d1da5ad120442d799c6821c597a3f31", "eaca945633494c4cb07c01b00b4ef689", "ace0ea1d143446a5a28ea3c64424dd48", "4032031b15a6457fb716b23d249df2e1", "9b6a2002d0644c58bd983529c4193b10", "dac38de22bfb4c898a6c18d38be32f36", "79d0d3f351c447548e45ebd07b5666ef" ] }, "id": "3YRLYIhlVhCD", "outputId": "48e54e5f-6d76-4460-ed0e-b88c14117b16" }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "The cache for model files in Transformers v4.22.0 has been updated. Migrating your old cache. This is a one-time only operation. You can interrupt this and resume the migration later on by calling `transformers.utils.move_cache()`.\n" ] }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "c223b971b7bf4c209f67bce265f8c1f7", "version_major": 2, "version_minor": 0 }, "text/plain": [ "0it [00:00, ?it/s]" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stderr", "output_type": "stream", "text": [ "/usr/local/lib/python3.10/dist-packages/huggingface_hub/utils/_token.py:89: UserWarning: \n", "The secret `HF_TOKEN` does not exist in your Colab secrets.\n", "To authenticate with the Hugging Face Hub, create a token in your settings tab (https://huggingface.co/settings/tokens), set it as secret in your Google Colab and restart your session.\n", "You will be able to reuse this secret in all of your notebooks.\n", "Please note that authentication is recommended but still optional to access public models or datasets.\n", " warnings.warn(\n" ] }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "6616b8f5be4644e8b9aa990a127304c6", "version_major": 2, "version_minor": 0 }, "text/plain": [ "model_index.json: 0%| | 0.00/541 [00:00