CoolFace
Apppublic

codeparrot/codeparrot-subspace

sourceHugging Faceupdated 4y agoView on Hugging Face
9likes
app.py57 linesDownload Raw Back to root
1import gradio as gr2from transformers import AutoTokenizer, AutoModelForCausalLM, set_seed, pipeline3 4#https://huggingface.co/spaces/lvwerra/codeparrot-generation5 6title = "CodeParrot Generator 🦜"7description = "This is a subspace to make code generation with [CodeParrot](https://huggingface.co/lvwerra/codeparrot), it is used in a larger [space](https://huggingface.co/spaces/loubnabnl/Code-generation-models-v1) for model comparison. For more flexibilty in sampling, you can find another demo for CodeParrot [here](https://huggingface.co/spaces/lvwerra/codeparrot-generation)."8example = [9    ["def print_hello_world():", 8, 0.6, 42],10    ["def get_file_size(filepath):", 40, 0.6, 42],11    ["def count_lines(filename):", 40, 0.6, 42],12    ["def count_words(filename):", 40, 0.6, 42]]13tokenizer = AutoTokenizer.from_pretrained("codeparrot/codeparrot")14model = AutoModelForCausalLM.from_pretrained("codeparrot/codeparrot", low_cpu_mem_usage=True)15 16 17def code_generation(gen_prompt, max_tokens, temperature=0.6, seed=42):18    set_seed(seed)19    pipe = pipeline("text-generation", model=model, tokenizer=tokenizer)20    generated_text = pipe(gen_prompt, do_sample=True, top_p=0.95, temperature=temperature, max_new_tokens=max_tokens)[0]['generated_text']21    return generated_text22 23 24iface = gr.Interface(25    fn=code_generation, 26    inputs=[27        gr.Textbox(lines=10, label="Input code"),28        gr.inputs.Slider(29            minimum=8,30            maximum=256,31            step=1,32            default=8,33            label="Number of tokens to generate",34        ),35        gr.inputs.Slider(36            minimum=0,37            maximum=2,38            step=0.1,39            default=0.6,40            label="Temperature",41        ),42        gr.inputs.Slider(43            minimum=0,44            maximum=1000,45            step=1,46            default=42,47            label="Random seed to use for the generation"48        )49    ],50    outputs=gr.Textbox(label="Predicted code", lines=10),51    examples=example,52    layout="horizontal",53    theme="peach",54    description=description,55    title=title56)57iface.launch()