Spaces:
Running
on
T4
Running
on
T4
Update app.py
Browse files
app.py
CHANGED
@@ -2,23 +2,91 @@ import gradio as gr
|
|
2 |
from transformers import AutoTokenizer, AutoModelForCausalLM, set_seed
|
3 |
from transformers import pipeline
|
4 |
import os
|
|
|
5 |
|
6 |
description = """# <p style="text-align: center; color: white;"> π
<span style='color: #ff75b3;'>SantaCoder:</span> Code Generation </p>
|
7 |
-
<span style='color: white;'>This is a demo to generate code with <a href="https://huggingface.co/bigcode/santacoder" style="color: #ff75b3;">SantaCoder</a>,
|
|
|
|
|
8 |
|
9 |
token = os.environ["HUB_TOKEN"]
|
10 |
-
device="cuda
|
11 |
-
revision = "dedup-alt-comments"
|
12 |
|
13 |
-
tokenizer = AutoTokenizer.from_pretrained("bigcode/christmas-models", use_auth_token=token)
|
14 |
-
model = AutoModelForCausalLM.from_pretrained("bigcode/christmas-models", revision=revision, trust_remote_code=True, use_auth_token=token)
|
15 |
|
|
|
|
|
|
|
|
|
|
|
16 |
|
17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
set_seed(seed)
|
19 |
-
|
20 |
-
|
21 |
-
|
|
|
|
|
|
|
|
|
22 |
|
23 |
|
24 |
demo = gr.Blocks(
|
@@ -26,13 +94,13 @@ demo = gr.Blocks(
|
|
26 |
)
|
27 |
with demo:
|
28 |
with gr.Row():
|
29 |
-
gr.
|
30 |
-
|
31 |
-
|
32 |
-
code = gr.Textbox(lines=
|
33 |
|
34 |
-
with gr.Accordion("Advanced settings"):
|
35 |
-
|
36 |
minimum=8,
|
37 |
maximum=1024,
|
38 |
step=1,
|
@@ -53,9 +121,9 @@ with demo:
|
|
53 |
label="Random seed to use for the generation"
|
54 |
)
|
55 |
run = gr.Button()
|
56 |
-
output = gr.
|
57 |
|
58 |
-
event = run.click(code_generation, [code,
|
59 |
gr.HTML(label="Contact", value="<img src='https://huggingface.co/datasets/bigcode/admin/resolve/main/bigcode_contact.png' alt='contact' style='display: block; margin: auto; max-width: 800px;'>")
|
60 |
|
61 |
demo.launch()
|
|
|
2 |
from transformers import AutoTokenizer, AutoModelForCausalLM, set_seed
|
3 |
from transformers import pipeline
|
4 |
import os
|
5 |
+
import torch
|
6 |
|
7 |
description = """# <p style="text-align: center; color: white;"> π
<span style='color: #ff75b3;'>SantaCoder:</span> Code Generation </p>
|
8 |
+
<span style='color: white;'>This is a demo to generate code with <a href="https://huggingface.co/bigcode/santacoder" style="color: #ff75b3;">SantaCoder</a>,
|
9 |
+
a 1.1B parameter model for code generation in Python, Java & JavaScript. The model can also do infilling, just specify where you would like the model to complete code
|
10 |
+
with the <span style='color: #ff75b3;'><FILL-HERE></span> token.</span>"""
|
11 |
|
12 |
token = os.environ["HUB_TOKEN"]
|
13 |
+
device="cuda"
|
|
|
14 |
|
|
|
|
|
15 |
|
16 |
+
FIM_PREFIX = "<fim-prefix>"
|
17 |
+
FIM_MIDDLE = "<fim-middle>"
|
18 |
+
FIM_SUFFIX = "<fim-suffix>"
|
19 |
+
FIM_PAD = "<fim-pad>"
|
20 |
+
EOD = "<|endoftext|>"
|
21 |
|
22 |
+
GENERATION_TITLE= "<p style='font-size: 16px; color: white;'>Generated code:</p>"
|
23 |
+
|
24 |
+
tokenizer_fim = AutoTokenizer.from_pretrained("bigcode/christmas-models", use_auth_token=True, padding_side="left")
|
25 |
+
|
26 |
+
tokenizer_fim.add_special_tokens({
|
27 |
+
"additional_special_tokens": [EOD, FIM_PREFIX, FIM_MIDDLE, FIM_SUFFIX, FIM_PAD],
|
28 |
+
"pad_token": EOD,
|
29 |
+
})
|
30 |
+
|
31 |
+
tokenizer = AutoTokenizer.from_pretrained("bigcode/christmas-models", use_auth_token=True)
|
32 |
+
model = AutoModelForCausalLM.from_pretrained("bigcode/christmas-models", trust_remote_code=True, use_auth_token=True).to(device)
|
33 |
+
pipe = pipeline("text-generation", model=model, tokenizer=tokenizer)
|
34 |
+
|
35 |
+
def post_processing(prompt, completion):
|
36 |
+
completion = "<span style='color: #ff75b3;'>" + completion + "</span>"
|
37 |
+
prompt = "<span style='color: #727cd6;'>" + prompt + "</span>"
|
38 |
+
code_html = f"<br><hr><br><pre style='max-width: 500px; margin: 0 auto; display: block;'><code>{prompt}{completion}</code></pre><br><hr>"
|
39 |
+
return GENERATION_TITLE + code_html
|
40 |
+
|
41 |
+
def post_processing_fim(prefix, middle, suffix):
|
42 |
+
prefix = "<span style='color: #727cd6;'>" + prefix + "</span>"
|
43 |
+
middle = "<span style='color: #ff75b3;'>" + middle + "</span>"
|
44 |
+
suffix = "<span style='color: #727cd6;'>" + suffix + "</span>"
|
45 |
+
code_html = f"<br><hr><br><pre style='max-width: 500px; margin: 0 auto; display: block;'><code>{prefix}{middle}{suffix}</code></pre><br><hr>"
|
46 |
+
return GENERATION_TITLE + code_html
|
47 |
+
|
48 |
+
def fim_generation(prompt, max_new_tokens, temperature):
|
49 |
+
prefix = prompt.split("<FILL-HERE>")[0]
|
50 |
+
suffix = prompt.split("<FILL-HERE>")[1]
|
51 |
+
[middle] = infill((prefix, suffix), max_new_tokens, temperature)
|
52 |
+
return post_processing_fim(prefix, middle, suffix)
|
53 |
+
|
54 |
+
def extract_fim_part(s: str):
|
55 |
+
# Find the index of
|
56 |
+
start = s.find(FIM_MIDDLE) + len(FIM_MIDDLE)
|
57 |
+
stop = s.find(EOD, start) or len(s)
|
58 |
+
return s[start:stop]
|
59 |
+
|
60 |
+
def infill(prefix_suffix_tuples, max_new_tokens, temperature):
|
61 |
+
if type(prefix_suffix_tuples) == tuple:
|
62 |
+
prefix_suffix_tuples = [prefix_suffix_tuples]
|
63 |
+
|
64 |
+
prompts = [f"{FIM_PREFIX}{prefix}{FIM_SUFFIX}{suffix}{FIM_MIDDLE}" for prefix, suffix in prefix_suffix_tuples]
|
65 |
+
# `return_token_type_ids=False` is essential, or we get nonsense output.
|
66 |
+
inputs = tokenizer_fim(prompts, return_tensors="pt", padding=True, return_token_type_ids=False).to(device)
|
67 |
+
with torch.no_grad():
|
68 |
+
outputs = model.generate(
|
69 |
+
**inputs,
|
70 |
+
do_sample=True,
|
71 |
+
temperature=temperature,
|
72 |
+
max_new_tokens=max_new_tokens,
|
73 |
+
pad_token_id=tokenizer.pad_token_id
|
74 |
+
)
|
75 |
+
# WARNING: cannot use skip_special_tokens, because it blows away the FIM special tokens.
|
76 |
+
return [
|
77 |
+
extract_fim_part(tokenizer_fim.decode(tensor, skip_special_tokens=False)) for tensor in outputs
|
78 |
+
]
|
79 |
+
|
80 |
+
|
81 |
+
def code_generation(prompt, max_new_tokens, temperature=0.2, seed=42):
|
82 |
set_seed(seed)
|
83 |
+
|
84 |
+
if "<FILL-HERE>" in prompt:
|
85 |
+
return fim_generation(prompt, max_new_tokens, temperature=0.2)
|
86 |
+
else:
|
87 |
+
completion = pipe(prompt, do_sample=True, top_p=0.95, temperature=temperature, max_new_tokens=max_new_tokens)[0]['generated_text']
|
88 |
+
completion = completion[len(prompt):]
|
89 |
+
return post_processing(prompt, completion)
|
90 |
|
91 |
|
92 |
demo = gr.Blocks(
|
|
|
94 |
)
|
95 |
with demo:
|
96 |
with gr.Row():
|
97 |
+
_, colum_2, _ = gr.Column(scale=1), gr.Column(scale=6), gr.Column(scale=1)
|
98 |
+
with colum_2:
|
99 |
+
gr.Markdown(value=description)
|
100 |
+
code = gr.Textbox(lines=5, label="Input code")
|
101 |
|
102 |
+
with gr.Accordion("Advanced settings", open=False):
|
103 |
+
max_new_tokens= gr.Slider(
|
104 |
minimum=8,
|
105 |
maximum=1024,
|
106 |
step=1,
|
|
|
121 |
label="Random seed to use for the generation"
|
122 |
)
|
123 |
run = gr.Button()
|
124 |
+
output = gr.HTML(label="Generated code")
|
125 |
|
126 |
+
event = run.click(code_generation, [code, max_new_tokens, temperature, seed], output)
|
127 |
gr.HTML(label="Contact", value="<img src='https://huggingface.co/datasets/bigcode/admin/resolve/main/bigcode_contact.png' alt='contact' style='display: block; margin: auto; max-width: 800px;'>")
|
128 |
|
129 |
demo.launch()
|