mohanchinnappan commited on
Commit
7af15a0
·
1 Parent(s): 5195dae

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +254 -0
app.py ADDED
@@ -0,0 +1,254 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ from share_btn import community_icon_html, loading_icon_html, share_js, share_btn_css
11
+
12
+ HF_TOKEN = os.environ.get("HF_TOKEN", None)
13
+ API_URL = "https://api-inference.huggingface.co/models/bigcode/starcoder"
14
+ API_URL_BASE ="https://api-inference.huggingface.co/models/bigcode/starcoderbase"
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
+
84
+ def generate(
85
+ prompt, temperature=0.9, max_new_tokens=256, top_p=0.95, repetition_penalty=1.0, version="StarCoder",
86
+ ):
87
+
88
+ temperature = float(temperature)
89
+ if temperature < 1e-2:
90
+ temperature = 1e-2
91
+ top_p = float(top_p)
92
+ fim_mode = False
93
+
94
+ generate_kwargs = dict(
95
+ temperature=temperature,
96
+ max_new_tokens=max_new_tokens,
97
+ top_p=top_p,
98
+ repetition_penalty=repetition_penalty,
99
+ do_sample=True,
100
+ seed=42,
101
+ )
102
+
103
+ if FIM_INDICATOR in prompt:
104
+ fim_mode = True
105
+ try:
106
+ prefix, suffix = prompt.split(FIM_INDICATOR)
107
+ except:
108
+ raise ValueError(f"Only one {FIM_INDICATOR} allowed in prompt!")
109
+ prompt = f"{FIM_PREFIX}{prefix}{FIM_SUFFIX}{suffix}{FIM_MIDDLE}"
110
+
111
+ if version == "StarCoder":
112
+ stream = client.generate_stream(prompt, **generate_kwargs)
113
+ else:
114
+ stream = client_base.generate_stream(prompt, **generate_kwargs)
115
+
116
+ if fim_mode:
117
+ output = prefix
118
+ else:
119
+ output = prompt
120
+
121
+ previous_token = ""
122
+ for response in stream:
123
+ if response.token.text == "<|endoftext|>":
124
+ if fim_mode:
125
+ output += suffix
126
+ else:
127
+ return output
128
+ else:
129
+ output += response.token.text
130
+ previous_token = response.token.text
131
+ yield output
132
+ return output
133
+
134
+
135
+ examples = [
136
+ "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",
137
+ "// Returns every other value in the array as a new array.\nfunction everyOther(arr) {",
138
+ "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",
139
+ ]
140
+
141
+
142
+ def process_example(args):
143
+ for x in generate(args):
144
+ pass
145
+ return x
146
+
147
+
148
+ css = ".generating {visibility: hidden}"
149
+
150
+ monospace_css = """
151
+ #q-input textarea {
152
+ font-family: monospace, 'Consolas', Courier, monospace;
153
+ }
154
+ """
155
+
156
+
157
+ css += share_btn_css + monospace_css + ".gradio-container {color: black}"
158
+
159
+
160
+ description = """
161
+ <div style="text-align: center;">
162
+ <h1> 💫 StarCoder<span style='color: #e6b800;'> - </span>Code Completion Playground 🪐</h1>
163
+ <p>This is a demo to generate code with <a href="https://huggingface.co/bigcode/starcoder" style='color: #e6b800;'>StarCoder</a>, a 15B parameter model for code generation in 86 programming languages.
164
+ <b>This is not an instruction model</b>. For instruction and chatting you can chat with a fine-tuned version of the model at <a href="https://huggingface.co/spaces/HuggingFaceH4/starchat-playground">StarChat Playground</a></p>
165
+ </div>
166
+ """
167
+ 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>\
168
+ <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)"""
169
+
170
+ with gr.Blocks(theme=theme, analytics_enabled=False, css=css) as demo:
171
+ with gr.Column():
172
+ gr.Markdown(description)
173
+ with gr.Row():
174
+ with gr.Column():
175
+ instruction = gr.Textbox(
176
+ placeholder="Enter your code here",
177
+ label="Code",
178
+ elem_id="q-input",
179
+ )
180
+ submit = gr.Button("Generate", variant="primary")
181
+ output = gr.Code(elem_id="q-output", lines=30)
182
+ with gr.Row():
183
+ with gr.Column():
184
+ with gr.Accordion("Advanced settings", open=False):
185
+ with gr.Row():
186
+ column_1, column_2 = gr.Column(), gr.Column()
187
+ with column_1:
188
+ temperature = gr.Slider(
189
+ label="Temperature",
190
+ value=0.2,
191
+ minimum=0.0,
192
+ maximum=1.0,
193
+ step=0.05,
194
+ interactive=True,
195
+ info="Higher values produce more diverse outputs",
196
+ )
197
+ max_new_tokens = gr.Slider(
198
+ label="Max new tokens",
199
+ value=256,
200
+ minimum=0,
201
+ maximum=8192,
202
+ step=64,
203
+ interactive=True,
204
+ info="The maximum numbers of new tokens",
205
+ )
206
+ with column_2:
207
+ top_p = gr.Slider(
208
+ label="Top-p (nucleus sampling)",
209
+ value=0.90,
210
+ minimum=0.0,
211
+ maximum=1,
212
+ step=0.05,
213
+ interactive=True,
214
+ info="Higher values sample more low-probability tokens",
215
+ )
216
+ repetition_penalty = gr.Slider(
217
+ label="Repetition penalty",
218
+ value=1.2,
219
+ minimum=1.0,
220
+ maximum=2.0,
221
+ step=0.05,
222
+ interactive=True,
223
+ info="Penalize repeated tokens",
224
+ )
225
+ with gr.Column():
226
+ version = gr.Dropdown(
227
+ ["StarCoderBase", "StarCoder"],
228
+ value="StarCoder",
229
+ label="Version",
230
+ info="",
231
+ )
232
+ gr.Markdown(disclaimer)
233
+ with gr.Group(elem_id="share-btn-container"):
234
+ community_icon = gr.HTML(community_icon_html, visible=True)
235
+ loading_icon = gr.HTML(loading_icon_html, visible=True)
236
+ share_button = gr.Button(
237
+ "Share to community", elem_id="share-btn", visible=True
238
+ )
239
+ gr.Examples(
240
+ examples=examples,
241
+ inputs=[instruction],
242
+ cache_examples=False,
243
+ fn=process_example,
244
+ outputs=[output],
245
+ )
246
+ gr.Markdown(FORMATS)
247
+
248
+ submit.click(
249
+ generate,
250
+ inputs=[instruction, temperature, max_new_tokens, top_p, repetition_penalty, version],
251
+ outputs=[output],
252
+ )
253
+ share_button.click(None, [], [], _js=share_js)
254
+ demo.queue(concurrency_count=16).launch(debug=True)