Spaces:
Sleeping
Sleeping
Duplicate from bigcode/santacoder-demo
Browse filesCo-authored-by: Leandro von Werra <lvwerra@users.noreply.huggingface.co>
- .gitattributes +34 -0
- README.md +13 -0
- app.py +129 -0
- requirements.txt +2 -0
.gitattributes
ADDED
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
*.7z filter=lfs diff=lfs merge=lfs -text
|
2 |
+
*.arrow filter=lfs diff=lfs merge=lfs -text
|
3 |
+
*.bin filter=lfs diff=lfs merge=lfs -text
|
4 |
+
*.bz2 filter=lfs diff=lfs merge=lfs -text
|
5 |
+
*.ckpt filter=lfs diff=lfs merge=lfs -text
|
6 |
+
*.ftz filter=lfs diff=lfs merge=lfs -text
|
7 |
+
*.gz filter=lfs diff=lfs merge=lfs -text
|
8 |
+
*.h5 filter=lfs diff=lfs merge=lfs -text
|
9 |
+
*.joblib filter=lfs diff=lfs merge=lfs -text
|
10 |
+
*.lfs.* filter=lfs diff=lfs merge=lfs -text
|
11 |
+
*.mlmodel filter=lfs diff=lfs merge=lfs -text
|
12 |
+
*.model filter=lfs diff=lfs merge=lfs -text
|
13 |
+
*.msgpack filter=lfs diff=lfs merge=lfs -text
|
14 |
+
*.npy filter=lfs diff=lfs merge=lfs -text
|
15 |
+
*.npz filter=lfs diff=lfs merge=lfs -text
|
16 |
+
*.onnx filter=lfs diff=lfs merge=lfs -text
|
17 |
+
*.ot filter=lfs diff=lfs merge=lfs -text
|
18 |
+
*.parquet filter=lfs diff=lfs merge=lfs -text
|
19 |
+
*.pb filter=lfs diff=lfs merge=lfs -text
|
20 |
+
*.pickle filter=lfs diff=lfs merge=lfs -text
|
21 |
+
*.pkl filter=lfs diff=lfs merge=lfs -text
|
22 |
+
*.pt filter=lfs diff=lfs merge=lfs -text
|
23 |
+
*.pth filter=lfs diff=lfs merge=lfs -text
|
24 |
+
*.rar filter=lfs diff=lfs merge=lfs -text
|
25 |
+
*.safetensors filter=lfs diff=lfs merge=lfs -text
|
26 |
+
saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
27 |
+
*.tar.* filter=lfs diff=lfs merge=lfs -text
|
28 |
+
*.tflite filter=lfs diff=lfs merge=lfs -text
|
29 |
+
*.tgz filter=lfs diff=lfs merge=lfs -text
|
30 |
+
*.wasm filter=lfs diff=lfs merge=lfs -text
|
31 |
+
*.xz filter=lfs diff=lfs merge=lfs -text
|
32 |
+
*.zip filter=lfs diff=lfs merge=lfs -text
|
33 |
+
*.zst filter=lfs diff=lfs merge=lfs -text
|
34 |
+
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
README.md
ADDED
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
---
|
2 |
+
title: SantaCoder Demo
|
3 |
+
emoji: 🎅
|
4 |
+
colorFrom: blue
|
5 |
+
colorTo: red
|
6 |
+
sdk: gradio
|
7 |
+
sdk_version: 3.21.0
|
8 |
+
app_file: app.py
|
9 |
+
pinned: false
|
10 |
+
duplicated_from: bigcode/santacoder-demo
|
11 |
+
---
|
12 |
+
|
13 |
+
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
app.py
ADDED
@@ -0,0 +1,129 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
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:0"
|
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/santacoder", use_auth_token=token, 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=token)
|
32 |
+
model = AutoModelForCausalLM.from_pretrained("bigcode/christmas-models", trust_remote_code=True, use_auth_token=token).to(device)
|
33 |
+
pipe = pipeline("text-generation", model=model, tokenizer=tokenizer, device=device)
|
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='font-size: 12px'><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='font-size: 12px'><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(
|
93 |
+
css=".gradio-container {background-color: #20233fff; color:white}"
|
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.Code(lines=5, language="python", label="Input code", value="def all_odd_elements(sequence):\n \"\"\"Returns every odd element of the sequence.\"\"\"")
|
101 |
+
|
102 |
+
with gr.Accordion("Advanced settings", open=False):
|
103 |
+
max_new_tokens= gr.Slider(
|
104 |
+
minimum=8,
|
105 |
+
maximum=1024,
|
106 |
+
step=1,
|
107 |
+
value=48,
|
108 |
+
label="Number of tokens to generate",
|
109 |
+
)
|
110 |
+
temperature = gr.Slider(
|
111 |
+
minimum=0.1,
|
112 |
+
maximum=2.5,
|
113 |
+
step=0.1,
|
114 |
+
value=0.2,
|
115 |
+
label="Temperature",
|
116 |
+
)
|
117 |
+
seed = gr.Slider(
|
118 |
+
minimum=0,
|
119 |
+
maximum=1000,
|
120 |
+
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, api_name="predict")
|
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()
|
requirements.txt
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
torch
|
2 |
+
transformers
|