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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
import os
|
| 3 |
+
import shutil
|
| 4 |
+
import requests
|
| 5 |
+
|
| 6 |
+
import gradio as gr
|
| 7 |
+
from huggingface_hub import Repository
|
| 8 |
+
from text_generation import Client
|
| 9 |
+
|
| 10 |
+
HF_TOKEN = "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"
|
| 11 |
+
|
| 12 |
+
API_URL = "https://api-inference.huggingface.co/models/bigcode/starcoder"
|
| 13 |
+
API_URL_BASE ="https://api-inference.huggingface.co/models/bigcode/starcoderbase"
|
| 14 |
+
API_URL_PLUS = "https://api-inference.huggingface.co/models/bigcode/starcoderplus"
|
| 15 |
+
|
| 16 |
+
FIM_PREFIX = "<fim_prefix>"
|
| 17 |
+
FIM_MIDDLE = "<fim_middle>"
|
| 18 |
+
FIM_SUFFIX = "<fim_suffix>"
|
| 19 |
+
|
| 20 |
+
FIM_INDICATOR = "<FILL_HERE>"
|
| 21 |
+
|
| 22 |
+
FORMATS = """## Model Formats
|
| 23 |
+
|
| 24 |
+
The model is pretrained on code and is formatted with special tokens in addition to the pure code data,\
|
| 25 |
+
such as prefixes specifying the source of the file or tokens separating code from a commit message.\
|
| 26 |
+
Use these templates to explore the model's capacities:
|
| 27 |
+
|
| 28 |
+
### 1. Prefixes 🏷️
|
| 29 |
+
For pure code files, use any combination of the following prefixes:
|
| 30 |
+
|
| 31 |
+
```
|
| 32 |
+
<reponame>REPONAME<filename>FILENAME<gh_stars>STARS\ncode<|endoftext|>
|
| 33 |
+
```
|
| 34 |
+
STARS can be one of: 0, 1-10, 10-100, 100-1000, 1000+
|
| 35 |
+
|
| 36 |
+
### 2. Commits 💾
|
| 37 |
+
The commits data is formatted as follows:
|
| 38 |
+
|
| 39 |
+
```
|
| 40 |
+
<commit_before>code<commit_msg>text<commit_after>code<|endoftext|>
|
| 41 |
+
```
|
| 42 |
+
|
| 43 |
+
### 3. Jupyter Notebooks 📓
|
| 44 |
+
The model is trained on Jupyter notebooks as Python scripts and structured formats like:
|
| 45 |
+
|
| 46 |
+
```
|
| 47 |
+
<start_jupyter><jupyter_text>text<jupyter_code>code<jupyter_output>output<jupyter_text>
|
| 48 |
+
```
|
| 49 |
+
|
| 50 |
+
### 4. Issues 🐛
|
| 51 |
+
We also trained on GitHub issues using the following formatting:
|
| 52 |
+
```
|
| 53 |
+
<issue_start><issue_comment>text<issue_comment>...<issue_closed>
|
| 54 |
+
```
|
| 55 |
+
|
| 56 |
+
### 5. Fill-in-the-middle 🧩
|
| 57 |
+
Fill in the middle requires rearranging the model inputs. The playground handles this for you - all you need is to specify where to fill:
|
| 58 |
+
```
|
| 59 |
+
code before<FILL_HERE>code after
|
| 60 |
+
```
|
| 61 |
+
"""
|
| 62 |
+
|
| 63 |
+
theme = gr.themes.Monochrome(
|
| 64 |
+
primary_hue="indigo",
|
| 65 |
+
secondary_hue="blue",
|
| 66 |
+
neutral_hue="slate",
|
| 67 |
+
radius_size=gr.themes.sizes.radius_sm,
|
| 68 |
+
font=[
|
| 69 |
+
gr.themes.GoogleFont("Open Sans"),
|
| 70 |
+
"ui-sans-serif",
|
| 71 |
+
"system-ui",
|
| 72 |
+
"sans-serif",
|
| 73 |
+
],
|
| 74 |
+
)
|
| 75 |
+
|
| 76 |
+
client = Client(
|
| 77 |
+
API_URL,
|
| 78 |
+
headers={"Authorization": f"Bearer {HF_TOKEN}"},
|
| 79 |
+
)
|
| 80 |
+
client_base = Client(
|
| 81 |
+
API_URL_BASE, headers={"Authorization": f"Bearer {HF_TOKEN}"},
|
| 82 |
+
)
|
| 83 |
+
client_plus = Client(
|
| 84 |
+
API_URL_PLUS, headers={"Authorization": f"Bearer {HF_TOKEN}"},
|
| 85 |
+
)
|
| 86 |
+
|
| 87 |
+
def generate(
|
| 88 |
+
prompt, temperature=0.9, max_new_tokens=256, top_p=0.95, repetition_penalty=1.0, version="StarCoder",
|
| 89 |
+
):
|
| 90 |
+
|
| 91 |
+
temperature = float(temperature)
|
| 92 |
+
if temperature < 1e-2:
|
| 93 |
+
temperature = 1e-2
|
| 94 |
+
top_p = float(top_p)
|
| 95 |
+
fim_mode = False
|
| 96 |
+
|
| 97 |
+
generate_kwargs = dict(
|
| 98 |
+
temperature=temperature,
|
| 99 |
+
max_new_tokens=max_new_tokens,
|
| 100 |
+
top_p=top_p,
|
| 101 |
+
repetition_penalty=repetition_penalty,
|
| 102 |
+
do_sample=True,
|
| 103 |
+
seed=42,
|
| 104 |
+
)
|
| 105 |
+
|
| 106 |
+
if FIM_INDICATOR in prompt:
|
| 107 |
+
fim_mode = True
|
| 108 |
+
try:
|
| 109 |
+
prefix, suffix = prompt.split(FIM_INDICATOR)
|
| 110 |
+
except:
|
| 111 |
+
raise ValueError(f"Only one {FIM_INDICATOR} allowed in prompt!")
|
| 112 |
+
prompt = f"{FIM_PREFIX}{prefix}{FIM_SUFFIX}{suffix}{FIM_MIDDLE}"
|
| 113 |
+
|
| 114 |
+
if version == "StarCoder":
|
| 115 |
+
stream = client.generate_stream(prompt, **generate_kwargs)
|
| 116 |
+
elif version == "StarCoderPlus":
|
| 117 |
+
stream = client_plus.generate_stream(prompt, **generate_kwargs)
|
| 118 |
+
else:
|
| 119 |
+
stream = client_base.generate_stream(prompt, **generate_kwargs)
|
| 120 |
+
|
| 121 |
+
if fim_mode:
|
| 122 |
+
output = prefix
|
| 123 |
+
else:
|
| 124 |
+
output = prompt
|
| 125 |
+
|
| 126 |
+
previous_token = ""
|
| 127 |
+
for response in stream:
|
| 128 |
+
if response.token.text == "<|endoftext|>":
|
| 129 |
+
if fim_mode:
|
| 130 |
+
output += suffix
|
| 131 |
+
else:
|
| 132 |
+
return output
|
| 133 |
+
else:
|
| 134 |
+
output += response.token.text
|
| 135 |
+
previous_token = response.token.text
|
| 136 |
+
yield output
|
| 137 |
+
return output
|
| 138 |
+
|
| 139 |
+
|
| 140 |
+
examples = [
|
| 141 |
+
"X_train, y_train, X_test, y_test = train_test_split(X, y, test_size=0.1)\n\n# Train a logistic regression model, predict the labels on the test set and compute the accuracy score",
|
| 142 |
+
"// Returns every other value in the array as a new array.\nfunction everyOther(arr) {",
|
| 143 |
+
"Poor English: She no went to the market. Corrected English:",
|
| 144 |
+
"def alternating(list1, list2):\n results = []\n for i in range(min(len(list1), len(list2))):\n results.append(list1[i])\n results.append(list2[i])\n if len(list1) > len(list2):\n <FILL_HERE>\n else:\n results.extend(list2[i+1:])\n return results",
|
| 145 |
+
]
|
| 146 |
+
|
| 147 |
+
|
| 148 |
+
def process_example(args):
|
| 149 |
+
for x in generate(args):
|
| 150 |
+
pass
|
| 151 |
+
return x
|
| 152 |
+
|
| 153 |
+
|
| 154 |
+
css = ".generating {visibility: hidden}"
|
| 155 |
+
|
| 156 |
+
monospace_css = """
|
| 157 |
+
#q-input textarea {
|
| 158 |
+
font-family: monospace, 'Consolas', Courier, monospace;
|
| 159 |
+
}
|
| 160 |
+
"""
|
| 161 |
+
|
| 162 |
+
|
| 163 |
+
css += monospace_css + ".gradio-container {color: black}"
|
| 164 |
+
|
| 165 |
+
|
| 166 |
+
description = """
|
| 167 |
+
<div style="text-align: center;">
|
| 168 |
+
<h1> ⭐ StarCoder <span style='color: #e6b800;'>Models</span> Playground</h1>
|
| 169 |
+
</div>
|
| 170 |
+
<div style="text-align: left;">
|
| 171 |
+
<p>This is a demo to generate text and code with the following StarCoder models:</p>
|
| 172 |
+
<ul>
|
| 173 |
+
<li><a href="https://huggingface.co/bigcode/starcoderplus" style='color: #e6b800;'>StarCoderPlus</a>: A finetuned version of StarCoderBase on English web data, making it strong in both English text and code generation.</li>
|
| 174 |
+
<li><a href="https://huggingface.co/bigcode/starcoderbase" style='color: #e6b800;'>StarCoderBase</a>: A code generation model trained on 80+ programming languages, providing broad language coverage for code generation tasks.</li>
|
| 175 |
+
<li><a href="https://huggingface.co/bigcode/starcoder" style='color: #e6b800;'>StarCoder</a>: A finetuned version of StarCoderBase specifically focused on Python, while also maintaining strong performance on other programming languages.</li>
|
| 176 |
+
</ul>
|
| 177 |
+
<p><b>Please note:</b> These models are not designed for instruction purposes. If you're looking for instruction or want to chat with a fine-tuned model, you can visit the <a href="https://huggingface.co/spaces/HuggingFaceH4/starchat-playground">StarChat Playground</a>.</p>
|
| 178 |
+
</div>
|
| 179 |
+
"""
|
| 180 |
+
disclaimer = """⚠️<b>Any use or sharing of this demo constitues your acceptance of the BigCode [OpenRAIL-M](https://huggingface.co/spaces/bigcode/bigcode-model-license-agreement) License Agreement and the use restrictions included within.</b>\
|
| 181 |
+
<br>**Intended Use**: this app and its [supporting model](https://huggingface.co/bigcode) are provided for demonstration purposes; not to serve as replacement for human expertise. For more details on the model's limitations in terms of factuality and biases, see the [model card.](hf.co/bigcode)"""
|
| 182 |
+
|
| 183 |
+
with gr.Blocks(theme=theme, analytics_enabled=False, css=css) as demo:
|
| 184 |
+
with gr.Column():
|
| 185 |
+
gr.Markdown(description)
|
| 186 |
+
with gr.Row():
|
| 187 |
+
version = gr.Dropdown(
|
| 188 |
+
["StarCoderPlus", "StarCoderBase", "StarCoder"],
|
| 189 |
+
value="StarCoder",
|
| 190 |
+
label="Model",
|
| 191 |
+
info="Choose a model from the list",
|
| 192 |
+
)
|
| 193 |
+
with gr.Row():
|
| 194 |
+
with gr.Column():
|
| 195 |
+
instruction = gr.Textbox(
|
| 196 |
+
placeholder="Enter your code here",
|
| 197 |
+
lines=5,
|
| 198 |
+
label="Input",
|
| 199 |
+
elem_id="q-input",
|
| 200 |
+
)
|
| 201 |
+
submit = gr.Button("Generate", variant="primary")
|
| 202 |
+
output = gr.Code(elem_id="q-output", lines=30, label="Output")
|
| 203 |
+
with gr.Row():
|
| 204 |
+
with gr.Column():
|
| 205 |
+
with gr.Accordion("Advanced settings", open=False):
|
| 206 |
+
with gr.Row():
|
| 207 |
+
column_1, column_2 = gr.Column(), gr.Column()
|
| 208 |
+
with column_1:
|
| 209 |
+
temperature = gr.Slider(
|
| 210 |
+
label="Temperature",
|
| 211 |
+
value=0.2,
|
| 212 |
+
minimum=0.0,
|
| 213 |
+
maximum=1.0,
|
| 214 |
+
step=0.05,
|
| 215 |
+
interactive=True,
|
| 216 |
+
info="Higher values produce more diverse outputs",
|
| 217 |
+
)
|
| 218 |
+
max_new_tokens = gr.Slider(
|
| 219 |
+
label="Max new tokens",
|
| 220 |
+
value=256,
|
| 221 |
+
minimum=0,
|
| 222 |
+
maximum=8192,
|
| 223 |
+
step=64,
|
| 224 |
+
interactive=True,
|
| 225 |
+
info="The maximum numbers of new tokens",
|
| 226 |
+
)
|
| 227 |
+
with column_2:
|
| 228 |
+
top_p = gr.Slider(
|
| 229 |
+
label="Top-p (nucleus sampling)",
|
| 230 |
+
value=0.90,
|
| 231 |
+
minimum=0.0,
|
| 232 |
+
maximum=1,
|
| 233 |
+
step=0.05,
|
| 234 |
+
interactive=True,
|
| 235 |
+
info="Higher values sample more low-probability tokens",
|
| 236 |
+
)
|
| 237 |
+
repetition_penalty = gr.Slider(
|
| 238 |
+
label="Repetition penalty",
|
| 239 |
+
value=1.2,
|
| 240 |
+
minimum=1.0,
|
| 241 |
+
maximum=2.0,
|
| 242 |
+
step=0.05,
|
| 243 |
+
interactive=True,
|
| 244 |
+
info="Penalize repeated tokens",
|
| 245 |
+
)
|
| 246 |
+
|
| 247 |
+
gr.Markdown(disclaimer)
|
| 248 |
+
with gr.Group(elem_id="share-btn-container"):
|
| 249 |
+
share_button = gr.Button(
|
| 250 |
+
"Share to community", elem_id="share-btn", visible=True
|
| 251 |
+
)
|
| 252 |
+
gr.Examples(
|
| 253 |
+
examples=examples,
|
| 254 |
+
inputs=[instruction],
|
| 255 |
+
cache_examples=False,
|
| 256 |
+
fn=process_example,
|
| 257 |
+
outputs=[output],
|
| 258 |
+
)
|
| 259 |
+
gr.Markdown(FORMATS)
|
| 260 |
+
|
| 261 |
+
submit.click(
|
| 262 |
+
generate,
|
| 263 |
+
inputs=[instruction, temperature, max_new_tokens, top_p, repetition_penalty, version],
|
| 264 |
+
outputs=[output],
|
| 265 |
+
)
|
| 266 |
+
demo.queue(concurrency_count=16).launch(debug=True)
|