# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     name: python3
# ---

# + [markdown] id="_seE71WAElyq"
# # GPT-2 Code Generator
# ## This code was taken from publicly-available Huggingface repository
# ## and was provided by SIC98
# ## link: https://huggingface.co/SIC98/GPT2-python-code-generator 

# + colab={"base_uri": "https://localhost:8080/"} id="gkOf4EGDfDGl" executionInfo={"status": "ok", "timestamp": 1626156596666, "user_tz": -60, "elapsed": 7144, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GhvXV91_cxrC4m7SmeaHRr4HdN9pQ5ReaB1LqMgcQ=s64", "userId": "00629242608009283527"}} outputId="7ca2acd0-3394-4789-b5f9-25ffa55830d0"
pip install transformers


# + colab={"base_uri": "https://localhost:8080/", "height": 316, "referenced_widgets": ["34cfe545395849bba152ac59130f52c5", "10cec0dc1415462b9eba8874dc5b5676", "1549e9b9ddea407bbb57fd69830ebcca", "4860a6bd207d4349b818dba8f4ba00a8", "5f70c6a91166488cacd0449e7fa4798c", "<KEY>", "bc2d96644ea3445b904a69253066485f", "<KEY>", "<KEY>", "b7bcde3eab5049ed9f27d91387081437", "<KEY>", "28cd1ec752714810a1aa5d8aaf175a47", "<KEY>", "e5fdf64f5ad44ea0bdb4c5e9d206d93d", "<KEY>", "eefa792808204e89982b40a33a1ff800", "<KEY>", "<KEY>", "<KEY>", "<KEY>", "846db92988d9469f82a5d1fd435d6071", "d0ec6a186adc47a5b7a2c22909394a6a", "c6fde3c7137247399e158ac6d919123c", "ea23060b67424ccab2689c9803cad4cb", "<KEY>", "<KEY>", "<KEY>", "84ba92a89c7f4a38893887080d8b4493", "d8ebe59541ab41ccb2d59e2077f8356e", "<KEY>", "<KEY>", "<KEY>", "c9c1e32374644c1aa670de8ea20a1416", "3f2a47addb244ba3a31e0240d5692021", "b270323fa3d94d068eadaa451d84105a", "<KEY>", "<KEY>", "<KEY>", "<KEY>", "637c300f43ee4feebe1bfe596634d950", "<KEY>", "<KEY>", "2a006771812548dfb73eb04bac2625c0", "adfbe9a519a14d6092ac4b3308b4c733", "9eab5fe626de4b128d25740de2508918", "a57ad55b068b4edc9491af35d46f66fe", "d3234ac59d1142c7a3b761aea8dcc423", "576e748123ba44c0ad2f1a7d56c0a237"]} id="MdE7hzioeglH" executionInfo={"status": "ok", "timestamp": 1626156612811, "user_tz": -60, "elapsed": 16148, "user": {"displayName": "Ave Coders", "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GhvXV91_cxrC4m7SmeaHRr4HdN9pQ5ReaB1LqMgcQ=s64", "userId": "00629242608009283527"}} outputId="dcd4b02e-1bd6-4eae-cf73-3b1e4c1e5a03"
import torch
from transformers import GPT2LMHeadModel, GPT2Tokenizer
tokenizer = GPT2Tokenizer.from_pretrained('SIC98/GPT2-python-code-generator')
model = GPT2LMHeadModel.from_pretrained('SIC98/GPT2-python-code-generator')

# + id="qMH0CVoDeo3v"
# Can use fast tokenizer
from transformers import GPT2TokenizerFast
tokenizer = GPT2TokenizerFast.from_pretrained('SIC98/GPT2-python-code-generator')

# + colab={"base_uri": "https://localhost:8080/"} id="K0yYU_y4ezn4" executionInfo={"status": "ok", "timestamp": 1626156615003, "user_tz": -60, "elapsed": 1743, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GhvXV91_cxrC4m7SmeaHRr4HdN9pQ5ReaB1LqMgcQ=s64", "userId": "00629242608009283527"}} outputId="1745c15b-6c5e-402b-effd-585a0c121d56"
# Test tokenizer
print(tokenizer("Hello world"))
print(tokenizer(" Hello world"))

print(tokenizer.encode("Hello world"))
print(tokenizer.encode(" Hello world"))

print(tokenizer.decode([15496, 995]))
print(tokenizer.decode([18435, 995]))

# + colab={"base_uri": "https://localhost:8080/"} id="qhB9WyVHe0HG" executionInfo={"status": "ok", "timestamp": 1626156793040, "user_tz": -60, "elapsed": 2574, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GhvXV91_cxrC4m7SmeaHRr4HdN9pQ5ReaB1LqMgcQ=s64", "userId": "00629242608009283527"}} outputId="36752241-3f54-4575-ab18-85844e7f4c0c"

sequence = 'def is_palindrome(s):\n    """Check whether a string is a palindrome"""'
    
inputs = tokenizer.encode(sequence, return_tensors='pt')
outputs = model.generate(inputs, max_length=64, do_sample=True, temperature=0.1, top_p=1.0)
text = tokenizer.decode(outputs[0], skip_special_tokens=True)
print("=====================================")
print(text)


# + colab={"base_uri": "https://localhost:8080/"} id="tPdNwcCOe5mf" executionInfo={"status": "ok", "timestamp": 1626156744756, "user_tz": -60, "elapsed": 8237, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GhvXV91_cxrC4m7SmeaHRr4HdN9pQ5ReaB1LqMgcQ=s64", "userId": "00629242608009283527"}} outputId="dff94be6-e4c8-446f-9ff4-a5f17cbbf3e6"

sequence = '@dataclass\nclass Item:\n    name: str\n    price: float\n\n@dataclass\nclass Order\n    id: int\n    items: List[Item]\n    \n    def compute_total_price(self, palindrome_discount=0.2):\n        """\n        Compute the total price and return it.\n        Apply a discount to items whose names are palindromes.\n        """'
    
inputs = tokenizer.encode(sequence, return_tensors='pt')
outputs = model.generate(inputs, max_length=256, do_sample=True, temperature=0.2, top_p=1.0)
text = tokenizer.decode(outputs[0], skip_special_tokens=True)
print("=====================================")
print(text)

# + colab={"base_uri": "https://localhost:8080/"} id="GlC7bG56gCWg" executionInfo={"status": "ok", "timestamp": 1626156859924, "user_tz": -60, "elapsed": 14603, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GhvXV91_cxrC4m7SmeaHRr4HdN9pQ5ReaB1LqMgcQ=s64", "userId": "00629242608009283527"}} outputId="65f19a07-bb13-4947-9e88-44fa7f933e69"
sequence = 'def add_all(a, b, c, d):'
    
inputs = tokenizer.encode(sequence, return_tensors='pt')
outputs = model.generate(inputs, max_length=256, do_sample=True, temperature=0.2, top_p=1.0)
text = tokenizer.decode(outputs[0], skip_special_tokens=True)
print("=====================================")
print(text)

StarcoderdataJupyterScriptsDedupFiltered