File size: 2,421 Bytes
af71584
 
 
4c0932e
6e1e776
ec9d819
cce0122
 
9bbdc3b
b425eea
cce0122
9bbdc3b
b425eea
6e1e776
947480c
af71584
 
 
 
f7c14e4
af71584
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e067c58
af71584
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import gradio as gr
from transformers import AutoTokenizer, AutoModelForCausalLM, set_seed, pipeline

title = "SantaCoder 🎅 bash/shell 🐚 Completion"
description = "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)"
EXAMPLE_0 = "#!/bin/bash\n# This script removes files larger than 2MB in the current folder\nfind ."
EXAMPLE_1 = "#!/bin/bash\n\n# This script send an email\nto=”admin@example.com”\nsubject=”Greeting”\nmsg=”Welcome to our site”\n"
EXAMPLE_3 = "#!/bin/bash\n# This script convert avi files to mp4\nfor filename in $(ls *.avi); do\n"
EXAMPLE_4 = "#!/bin/bash\nsource=$1\ndest=$2\n# copy source on dest\n"
EXAMPLE_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")"""


examples = [[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]]
tokenizer = AutoTokenizer.from_pretrained("mrm8488/santacoder-finetuned-the-stack-bash-4")
model = AutoModelForCausalLM.from_pretrained("mrm8488/santacoder-finetuned-the-stack-bash-4", trust_remote_code=True)


def code_generation(gen_prompt, max_tokens, temperature=0.6, seed=42):
    set_seed(seed)
    pipe = pipeline("text-generation", model=model, tokenizer=tokenizer)
    generated_text = pipe(gen_prompt, do_sample=True, top_p=0.95, temperature=temperature, max_new_tokens=max_tokens)[0]['generated_text']
    return generated_text


iface = gr.Interface(
    fn=code_generation, 
    inputs=[
        gr.Textbox(lines=10, label="Input code"),
        gr.inputs.Slider(
            minimum=8,
            maximum=256,
            step=1,
            default=8,
            label="Number of tokens to generate",
        ),
        gr.inputs.Slider(
            minimum=0,
            maximum=2,
            step=0.1,
            default=0.6,
            label="Temperature",
        ),
        gr.inputs.Slider(
            minimum=0,
            maximum=1000,
            step=1,
            default=42,
            label="Random seed to use for the generation"
        )
    ],
    outputs=gr.Textbox(label="Predicted code", lines=10),
    examples=examples,
    layout="horizontal",
    theme="peach",
    description=description,
    title=title
)
iface.launch()