Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

generate python

from transformers import AutoTokenizer, AutoModelWithLMHead


tokenizer = AutoTokenizer.from_pretrained("Sentdex/GPyT")
model = AutoModelWithLMHead.from_pretrained("Sentdex/GPyT").to("cuda")


def generate(code, max_length=100):
    '''Takes input code, replaces newline chars with <N>, 
    tokenizes, feeds thru model, decodes, 
    then reformats the newlines back in'''
    newlinechar = "<N>"
    converted = code.replace("
", newlinechar)
    tokenized = tokenizer.encode(converted, return_tensors='pt').to("cuda")
    resp = model.generate(tokenized, max_length=max_length).to("cuda")

    decoded = tokenizer.decode(resp[0])
    reformatted = decoded.replace("<N>","
")
    return reformatted


print(generate("import"))
Source by pythonprogramming.net #
 
PREVIOUS NEXT
Tagged: #generate #python
ADD COMMENT
Topic
Name
2+9 =