File size: 3,443 Bytes
5e2d95e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import gradio as gr
from transformers import AutoTokenizer, AutoModelForCausalLM, set_seed, pipeline


title = "🎅 Santa Explains Code"
description = "This space converts Python code into English text that explains its function using [SantaCoder-Code-To-Text](https://huggingface.co/loubnabnl/santacoder-code-to-text), \
a code generation model that was fine-tuned on the [github-jupyter-code-to-text](https://huggingface.co/datasets/codeparrot/github-jupyter-code-to-text) dataset. \
This dataset includes Python code accompanied by docstrings that explain it. The data was sourced from Jupyter notebooks.\n\n\
Limitations: The model was fine-tuned on a small dataset from Jupyter Notebooks, so it can only explain simple, \
common functions that are found in these notebooks, in a similar fashion to the text in markdown cells. It might also be sensitive to function names and comments."

EXAMPLE_0 = "def function(sequence):\n    return [x for x in sequence if x % 2 == 0]"
EXAMPLE_1 =  "from sklearn import model_selection\nX_train, X_test, Y_train, Y_test = model_selection.train_test_split(X, Y, test_size=0.2)"
EXAMPLE_2 = "def load_text(file)\n    with open(filename, 'r') as f:\n        text = f.read()\n    return text"
EXAMPLE_3 = "net.zero_grad()\nloss.backward()"
EXAMPLE_4 = "net.zero_grad()\nloss.backward()\n\nnoptimizer.step()"
EXAMPLE_5 = "def sort_function(arr):\n    n = len(arr)\n \n    # Traverse through all array elements\n    for i in range(n):\n \n        # Last i elements are already in place\n        for j in range(0, n-i-1):\n \n            # traverse the array from 0 to n-i-1\n            # Swap if the element found is greater\n            # than the next element\n            if arr[j] > arr[j+1]:\n                arr[j], arr[j+1] = arr[j+1], arr[j]"

example = [
    [EXAMPLE_0, 32, 0.6, 42],
    [EXAMPLE_1, 34, 0.4, 42],
    [EXAMPLE_2, 11, 0.6, 42],
    [EXAMPLE_3, 30, 0.6, 42],
    [EXAMPLE_4, 46, 0.6, 42],
    [EXAMPLE_5, 32, 0.6, 42],
    ]


tokenizer = AutoTokenizer.from_pretrained("loubnabnl/santacoder-code-to-text")
model = AutoModelForCausalLM.from_pretrained("loubnabnl/santacoder-code-to-text", trust_remote_code=True)
pipe = pipeline("text-generation", model=model, tokenizer=tokenizer)

def make_doctring(gen_prompt):
    return gen_prompt + f"\n\n\"\"\"\nExplanation:"

def code_generation(gen_prompt, max_tokens, temperature=0.6, seed=42):
    set_seed(seed)
    prompt = make_doctring(gen_prompt)
    generated_text = pipe(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="Python 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.5,
            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 explanation", lines=10),
    examples=example,
    layout="horizontal",
    description=description,
    title=title
)
iface.launch()