hugging-fellows/santacoder-bash-completion
0
1import gradio as gr2from transformers import AutoTokenizer, AutoModelForCausalLM, set_seed, pipeline3 4title = "SantaCoder 🎅 bash/shell 🐚 Completion"5description = "This is a subspace to make code generation with [SantaCoder fine-tuned on The Stack bash/shell](https://huggingface.co/mrm8488/santacoder-finetuned-the-stack-bash-4)"6EXAMPLE_0 = "#!/bin/bash\n# This script removes files larger than 2MB in the current folder\nfind ."7EXAMPLE_1 = "#!/bin/bash\n\n# This script send an email\nto=”admin@example.com”\nsubject=”Greeting”\nmsg=”Welcome to our site”\n"8EXAMPLE_3 = "#!/bin/bash\n# This script convert avi files to mp4\nfor filename in $(ls *.avi); do\n"9EXAMPLE_4 = "#!/bin/bash\nsource=$1\ndest=$2\n# copy source on dest\n"10EXAMPLE_5 = """#!/bin/bash\n\n# This script check if the arg passed as first arg is a founder of huggingface\nfounders_array=("julien" "thom" "clem")"""11 12 13examples = [[EXAMPLE_0, 14, 0.6, 42], [EXAMPLE_1, 28, 0.6, 42], [EXAMPLE_3, 46, 0.6, 42], [EXAMPLE_4, 35, 0.6, 43], [EXAMPLE_5, 70, 0.6, 43]]14tokenizer = AutoTokenizer.from_pretrained("mrm8488/santacoder-finetuned-the-stack-bash-4")15model = AutoModelForCausalLM.from_pretrained("mrm8488/santacoder-finetuned-the-stack-bash-4", trust_remote_code=True)16 17 18def code_generation(gen_prompt, max_tokens, temperature=0.6, seed=42):19 set_seed(seed)20 pipe = pipeline("text-generation", model=model, tokenizer=tokenizer)21 generated_text = pipe(gen_prompt, do_sample=True, top_p=0.95, temperature=temperature, max_new_tokens=max_tokens)[0]['generated_text']22 return generated_text23 24 25iface = gr.Interface(26 fn=code_generation, 27 inputs=[28 gr.Textbox(lines=10, label="Input code"),29 gr.inputs.Slider(30 minimum=8,31 maximum=256,32 step=1,33 default=8,34 label="Number of tokens to generate",35 ),36 gr.inputs.Slider(37 minimum=0,38 maximum=2,39 step=0.1,40 default=0.6,41 label="Temperature",42 ),43 gr.inputs.Slider(44 minimum=0,45 maximum=1000,46 step=1,47 default=42,48 label="Random seed to use for the generation"49 )50 ],51 outputs=gr.Textbox(label="Predicted code", lines=10),52 examples=examples,53 layout="horizontal",54 theme="peach",55 description=description,56 title=title57)58iface.launch()