{"id":13412219,"url":"https://github.com/rish-16/gpt2client","last_synced_at":"2025-03-14T18:30:34.861Z","repository":{"id":52175240,"uuid":"200458612","full_name":"rish-16/gpt2client","owner":"rish-16","description":"✍🏻 gpt2-client: Easy-to-use TensorFlow Wrapper for GPT-2 117M, 345M, 774M, and 1.5B Transformer Models 🤖 📝 ","archived":true,"fork":false,"pushed_at":"2021-06-24T04:59:19.000Z","size":3353,"stargazers_count":372,"open_issues_count":7,"forks_count":74,"subscribers_count":16,"default_branch":"master","last_synced_at":"2024-08-11T09:54:47.221Z","etag":null,"topics":["gpt-2","machine-learning","openai","tensorflow","text-generation","transformer"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/rish-16.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-08-04T06:35:45.000Z","updated_at":"2024-05-17T17:05:32.000Z","dependencies_parsed_at":"2022-09-01T18:41:00.861Z","dependency_job_id":null,"html_url":"https://github.com/rish-16/gpt2client","commit_stats":null,"previous_names":[],"tags_count":21,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rish-16%2Fgpt2client","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rish-16%2Fgpt2client/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rish-16%2Fgpt2client/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rish-16%2Fgpt2client/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/rish-16","download_url":"https://codeload.github.com/rish-16/gpt2client/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243625017,"owners_count":20321217,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["gpt-2","machine-learning","openai","tensorflow","text-generation","transformer"],"created_at":"2024-07-30T20:01:22.260Z","updated_at":"2025-03-14T18:30:34.449Z","avatar_url":"https://github.com/rish-16.png","language":"Python","funding_links":["https://www.buymeacoffee.com/qHtxL0S"],"categories":["Python"],"sub_categories":[],"readme":"\u003ch1 align=\"center\"\u003egpt2-client (Archived)\u003c/h1\u003e\n\n\u003cp align=\"center\"\u003eEasy-to-use Wrapper for GPT-2 124M, 345M, 774M, and 1.5B Transformer Models\u003c/p\u003e\n\n\u003cp align=\"center\"\u003e\n\n  \u003ca\u003e\n    \u003ca style=\"margin: 0 5px\" href=\"https://pypi.org/project/gpt2-client/\"\u003e\u003cimg src=\"https://img.shields.io/pypi/v/gpt2-client?color=%231dd1a1\u0026logo=%231dd1a1\u0026logoColor=%231dd1a1\" alt=\"Pypi package\"\u003e\u003c/a\u003e\n  \u003c/a\u003e\n  \u003ca\u003e\n    \u003ca style=\"margin: 0 5px\" href=\"https://pepy.tech/project/gpt2-client\"\u003e\u003cimg src=\"https://pepy.tech/badge/gpt2-client\" alt=\"GitHub license\"\u003e\u003c/a\u003e\n  \u003c/a\u003e\n  \u003ca\u003e\n    \u003ca style=\"margin: 0 5px\" href=\"https://opensource.org/licenses/MIT\"\u003e\u003cimg src=\"https://img.shields.io/badge/license-MIT-%23feca57\" alt=\"GitHub license\"\u003e\u003c/a\u003e\n  \u003c/a\u003e\n  \u003ca\u003e\n    \u003ca style=\"margin: 0 5px\" href=\"https://colab.research.google.com/drive/1RZwp1n6XeWxvhBjt1e3ATSOy4Mj9GEEl\"\u003e\u003cimg    src=\"https://colab.research.google.com/assets/colab-badge.svg\"\u003e\u003c/a\u003e\n  \u003c/a\u003e\n\n\u003c/p\u003e\n\n\u003cp align=\"center\"\u003e\n\n  \u003ca\u003e\n    \u003ca href=\"https://www.buymeacoffee.com/qHtxL0S\" target=\"_blank\"\u003e\u003cimg src=\"https://cdn.buymeacoffee.com/buttons/default-green.png\" alt=\"Buy Me A Coffee\" style=\"width: 100px !important; border-radius: 5px !important; box-shadow: 2px 2px 10px rgba(0, 0, 0, 0.1) !important;\" \u003e\u003c/a\u003e\n  \u003c/a\u003e\n\n\u003c/p\u003e\n\n\u003cp align=\"center\"\u003e\n  \u003ca style=\"padding: 0 10px;\" href=\"#what-is-it\"\u003eWhat is it\u003c/a\u003e • \n  \u003ca style=\"padding: 0 10px;\" href=\"#installation\"\u003eInstallation\u003c/a\u003e • \n  \u003ca style=\"padding: 0 10px;\" href=\"#getting-started\"\u003eGetting Started\u003c/a\u003e\n\u003c/p\u003e\n\n\u003cdiv\u003e\u003cimg src=\"https://github.com/rish-16/gpt2client/raw/master/assets/demo.png\" /\u003e\u003c/div\u003e\n\n\u003cp align=\"center\"\u003e\u003cstrong\u003eMade by Rishabh Anand • \u003ca href=\"https://rish-16.github.io\"\u003ehttps://rish-16.github.io\u003c/a\u003e\u003c/strong\u003e\u003c/p\u003e\n\n\u003cp align=\"center\"\u003e\u003ch2 align=\"center\"\u003eWhat is it\u003c/h2\u003e\u003c/p\u003e\n\nGPT-2 is a Natural Language Processing model [developed by OpenAI](https://openai.com/blog/better-language-models/) for text generation. It is the successor to the GPT (Generative Pre-trained Transformer) model trained on 40GB of text from the internet. It features a Transformer model that was brought to light by the [Attention Is All You Need](https://arxiv.org/abs/1706.03762) paper in 2017. The model has 4 versions - `124M`, `345M`, `774M`, and `1558M` - that differ in terms of the amount of training data fed to it and the number of parameters they contain.\n\u003cbr\u003e\n\u003cbr\u003e\nThe 1.5B model is currently the largest available model released by OpenAI.\n\u003cbr\u003e\n\u003cbr\u003e\nFinally, `gpt2-client` is a wrapper around the original [`gpt-2` repository](https://github.com/openai/gpt-2) that features the same functionality but with more accessiblity, comprehensibility, and utilty. You can play around with all four GPT-2 models in less than five lines of code.\n\n\u003e **\\*Note**: This client wrapper is in no way liable to any damage caused directly or indirectly. Any names, places, and objects referenced by the model are fictional and seek no resemblance to real life entities or organisations. Samples are unfiltered and may contain offensive content. User discretion advised.\\*\n\n\u003cp align=\"center\"\u003e\u003ch2 align=\"center\"\u003eInstallation\u003c/h2\u003e\u003c/p\u003e\n\nInstall client via `pip`. Ideally, `gpt2-client` is well supported for \u003cstrong\u003ePython \u003e= 3.5\u003c/strong\u003e and \u003cstrong\u003eTensorFlow \u003e= 1.X\u003c/strong\u003e. Some libraries may need to be reinstalled or upgraded using the `--upgrade` flag via `pip` if _Python 2.X_ is used.\n\n```bash\npip install gpt2-client\n```\n\n\u003e **_Note:_** `gpt2-client` is **not** compatible with TensorFlow 2.0 , try TensorFlow 1.14.0\n\n\u003cp align=\"center\"\u003e\u003ch2 align=\"center\"\u003eGetting started\u003c/h2\u003e\u003c/p\u003e\n\n**1. Download the model weights and checkpoints**\n\n```python\nfrom gpt2_client import GPT2Client\n\ngpt2 = GPT2Client('124M') # This could also be `355M`, `774M`, or `1558M`. Rename `save_dir` to anything.\ngpt2.load_model(force_download=False) # Use cached versions if available.\n```\n\nThis creates a directory called `models` in the current working directory and downloads the weights, checkpoints, model JSON, and hyper-parameters required by the model. Once you have called the `load_model()` function, you need not call it again assuming that the files have finished downloading in the `models` directory.\n\n\u003e **_Note:_** Set `force_download=True` to overwrite the existing cached model weights and checkpoints\n\n**2. Start generating text!**\n\n```python\nfrom gpt2_client import GPT2Client\n\ngpt2 = GPT2Client('124M') # This could also be `355M`, `774M`, or `1558M`\ngpt2.load_model()\n\ngpt2.generate(interactive=True) # Asks user for prompt\ngpt2.generate(n_samples=4) # Generates 4 pieces of text\ntext = gpt2.generate(return_text=True) # Generates text and returns it in an array\ngpt2.generate(interactive=True, n_samples=3) # A different prompt each time\n```\n\nYou can see from the aforementioned sample that the generation options are highly flexible. You can mix and match based on what kind of text you need generated, be it multiple chunks or one at a time with prompts.\n\n**3. Generating text from batch of prompts**\n\n```python\nfrom gpt2_client import GPT2Client\n\ngpt2 = GPT2Client('124M') # This could also be `355M`, `774M`, or `1558M`\ngpt2.load_model()\n\nprompts = [\n  \"This is a prompt 1\",\n  \"This is a prompt 2\",\n  \"This is a prompt 3\",\n  \"This is a prompt 4\"\n]\n\ntext = gpt2.generate_batch_from_prompts(prompts) # returns an array of generated text\n```\n\n**4. Fine-tuning GPT-2 to custom datasets**\n\n```python\nfrom gpt2_client import GPT2Client\n\ngpt2 = GPT2Client('124M') # This could also be `355M`, `774M`, or `1558M`\ngpt2.load_model()\n\nmy_corpus = './data/shakespeare.txt' # path to corpus\ncustom_text = gpt2.finetune(my_corpus, return_text=True) # Load your custom dataset\n```\n\nIn order to fine-tune GPT-2 to your custom corpus or dataset, it's ideal to have a GPU or TPU at hand. [Google Colab](http://colab.research.google.com) is one such tool you can make use of to re-train/fine-tune your custom model.\n\n**5. Encoding and decoding text sequences**\n\n```python\nfrom gpt2_client import GPT2Client\n\ngpt2 = GPT2Client('124M') # This could also be `355M`, `774M`, or `1558M`\ngpt2.load_model()\n\n# encoding a sentence\nencs = gpt2.encode_seq(\"Hello world, this is a sentence\")\n# [15496, 995, 11, 428, 318, 257, 6827]\n\n# decoding an encoded sequence\ndecs = gpt2.decode_seq(encs)\n# Hello world, this is a sentence\n```\n\n\u003cp align=\"center\"\u003e\u003ch2 align=\"center\"\u003eContributing\u003c/h2\u003e\u003c/p\u003e\n\nSuggestions, improvements, and enhancements are always welcome! If you have any issues, please do raise one in the Issues section. If you have an improvement, do file an issue to discuss the suggestion before creating a PR.\n\nAll ideas – no matter how outrageous – welcome!\n\n\u003cp align=\"center\"\u003e\u003ch2 align=\"center\"\u003eDonations\u003c/h2\u003e\u003c/p\u003e\n\nOpen-source is really fun. Your donations motivate me to bring fresh ideas to life. If interested in supporting my open-source endeavours, please do donate – it means a lot to me!\n\n\u003ca href=\"https://www.buymeacoffee.com/qHtxL0S\" target=\"_blank\"\u003e\u003cimg src=\"https://cdn.buymeacoffee.com/buttons/default-green.png\" alt=\"Buy Me A Coffee\" style=\"width: 100px !important; border-radius: 5px !important; box-shadow: 2px 2px 10px rgba(0, 0, 0, 0.1) !important;\" \u003e\u003c/a\u003e\n\n\u003cp align=\"center\"\u003e\u003ch2 align=\"center\"\u003eLicence\u003c/h2\u003e\u003c/p\u003e\n\n[MIT](https://github.com/rish-16/gpt2client/blob/master/LICENSE.txt)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frish-16%2Fgpt2client","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frish-16%2Fgpt2client","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frish-16%2Fgpt2client/lists"}