loubnabnl HF staff commited on
Commit
e212587
1 Parent(s): d471812

stylistic changes

Browse files

- use monospace font for the prompt
- make code block larger
- hide advanced settings + add disclaimer
- update title and model formats

Left:
- improve examples
- remove eos token after this [issue](https://github.com/gradio-app/gradio/issues/3839) is fixed

Files changed (1) hide show
  1. app.py +111 -93
app.py CHANGED
@@ -12,8 +12,6 @@ from share_btn import community_icon_html, loading_icon_html, share_js, share_bt
12
  HF_TOKEN = os.environ.get("HF_TOKEN", None)
13
  API_URL = os.environ.get("API_URL")
14
 
15
- with open("./HHH_prompt.txt", "r") as f:
16
- HHH_PROMPT = f.read() + "\n\n"
17
 
18
  FIM_PREFIX = "<fim_prefix>"
19
  FIM_MIDDLE = "<fim_middle>"
@@ -23,39 +21,40 @@ FIM_INDICATOR = "<FILL_HERE>"
23
 
24
  FORMATS = """## Model formats
25
 
26
- The model is pretrained on code and in addition to the pure code data it is formatted with special tokens. E.g. prefixes specifying the source of the file or special tokens separating code from a commit message. See below:
 
 
27
 
28
- ### Chat mode
29
- Chat mode prepends the [HHH prompt](https://gist.github.com/jareddk/2509330f8ef3d787fc5aaac67aab5f11#file-hhh_prompt-txt) from Anthropic to the request which conditions the model to be an assistant.
30
-
31
- ### Prefixes
32
- Any combination of the three following prefixes can be found in pure code files:
33
 
34
  ```
35
  <reponame>REPONAME<filename>FILENAME<gh_stars>STARS\ncode<|endoftext|>
36
  ```
37
  STARS can be one of: 0, 1-10, 10-100, 100-1000, 1000+
38
 
39
- ### Commits
40
  The commits data is formatted as follows:
 
41
  ```
42
  <commit_before>code<commit_msg>text<commit_after>code<|endoftext|>
43
  ```
44
 
45
- ### Jupyter structure
46
- Jupyter notebooks were both trained in form of Python scripts as well as the following structured format:
 
47
  ```
48
  <start_jupyter><jupyter_text>text<jupyter_code>code<jupyter_output>output<jupyter_text>
49
  ```
50
 
51
- ### Issues
52
  We also trained on GitHub issues using the following formatting:
53
  ```
54
  <issue_start><issue_comment>text<issue_comment>...<issue_closed>
55
  ```
56
 
57
- ### Fill-in-the-middle
58
- Fill in the middle requires rearranging the model inputs. The playground does this for you - all you need is to specify where to fill:
59
  ```
60
  code before<FILL_HERE>code after
61
  ```
@@ -66,21 +65,30 @@ theme = gr.themes.Monochrome(
66
  secondary_hue="blue",
67
  neutral_hue="slate",
68
  radius_size=gr.themes.sizes.radius_sm,
69
- font=[gr.themes.GoogleFont("Open Sans"), "ui-sans-serif", "system-ui", "sans-serif"],
 
 
 
 
 
70
  )
71
 
72
  client = Client(
73
- API_URL, headers={"Authorization": f"Bearer {HF_TOKEN}"},
 
74
  )
75
 
76
- def generate(prompt, temperature=0.9, max_new_tokens=256, top_p=0.95, repetition_penalty=1.0, chat_mode=False):
 
 
 
77
 
78
  temperature = float(temperature)
79
  if temperature < 1e-2:
80
  temperature = 1e-2
81
  top_p = float(top_p)
82
  fim_mode = False
83
-
84
  generate_kwargs = dict(
85
  temperature=temperature,
86
  max_new_tokens=max_new_tokens,
@@ -89,16 +97,7 @@ def generate(prompt, temperature=0.9, max_new_tokens=256, top_p=0.95, repetition
89
  do_sample=True,
90
  seed=42,
91
  )
92
- if chat_mode:
93
- generate_kwargs.update({"stop_sequences": ["\nHuman", "\n-----"]})
94
-
95
- if chat_mode and FIM_INDICATOR in prompt:
96
- raise ValueError("Chat mode and FIM are mutually exclusive. Choose one or the other.")
97
 
98
- if chat_mode:
99
- chat_prompt = "Human: " + prompt + "\n\nAssistant:"
100
- prompt = HHH_PROMPT + chat_prompt
101
-
102
  if FIM_INDICATOR in prompt:
103
  fim_mode = True
104
  try:
@@ -111,17 +110,13 @@ def generate(prompt, temperature=0.9, max_new_tokens=256, top_p=0.95, repetition
111
 
112
  if fim_mode:
113
  output = prefix
114
- elif chat_mode:
115
- output = chat_prompt
116
  else:
117
  output = prompt
118
 
119
  previous_token = ""
120
  for response in stream:
121
- if fim_mode and response.token.text =="<|endoftext|>":
122
  output += (suffix + "\n" + response.token.text)
123
- elif chat_mode and response.token.text in ["Human", "-----"] and previous_token=="\n":
124
- return output
125
  else:
126
  output += response.token.text
127
  previous_token = response.token.text
@@ -133,7 +128,7 @@ examples = [
133
  "def print_hello_world():",
134
  'def fibonacci(n: int) -> int:\n """ Compute the n-th Fibonacci number. """',
135
  'from typing import List, Tuple\n\ndef sum_and_product(numbers: List[int]) -> Tuple[int, int]:\n """ Return the sum and the product of the integers in the list as a tuple. Here is the answer of the exercise"""',
136
- "class ComplexNumbers:"
137
  ]
138
 
139
 
@@ -142,29 +137,92 @@ def process_example(args):
142
  pass
143
  return x
144
 
145
- css = ".generating {visibility: hidden}" + share_btn_css
146
 
147
- with gr.Blocks(theme=theme, analytics_enabled=False, css=css) as demo:
148
- with gr.Column():
149
- gr.Markdown(
150
- """\
151
- # BigCode - Playground
152
 
153
- _Note:_ this is an internal playground - please do not share. The deployment can also change and thus the space not work as we continue development.\
 
 
 
 
 
 
 
 
154
  """
155
 
156
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
157
  with gr.Row():
158
- with gr.Column(scale=3):
159
- instruction = gr.Textbox(placeholder="Enter your prompt here", label="Prompt", elem_id="q-input")
 
 
 
 
160
  submit = gr.Button("Generate", variant="primary")
161
- output = gr.Code(elem_id="q-output")
162
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
163
  with gr.Group(elem_id="share-btn-container"):
164
  community_icon = gr.HTML(community_icon_html, visible=True)
165
  loading_icon = gr.HTML(loading_icon_html, visible=True)
166
- share_button = gr.Button("Share to community", elem_id="share-btn", visible=True)
167
-
 
168
  gr.Examples(
169
  examples=examples,
170
  inputs=[instruction],
@@ -174,50 +232,10 @@ _Note:_ this is an internal playground - please do not share. The deployment can
174
  )
175
  gr.Markdown(FORMATS)
176
 
177
- with gr.Column(scale=1):
178
- chat_mode = gr.Checkbox(
179
- value=False,
180
- label="Chat mode",
181
- info="Uses Anthropic's HHH prompt to turn the model into an assistant."
182
- )
183
- temperature = gr.Slider(
184
- label="Temperature",
185
- value=0.2,
186
- minimum=0.0,
187
- maximum=1.0,
188
- step=0.05,
189
- interactive=True,
190
- info="Higher values produce more diverse outputs",
191
- )
192
- max_new_tokens = gr.Slider(
193
- label="Max new tokens",
194
- value=256,
195
- minimum=0,
196
- maximum=8192,
197
- step=64,
198
- interactive=True,
199
- info="The maximum numbers of new tokens",
200
- )
201
- top_p = gr.Slider(
202
- label="Top-p (nucleus sampling)",
203
- value=0.90,
204
- minimum=0.0,
205
- maximum=1,
206
- step=0.05,
207
- interactive=True,
208
- info="Higher values sample more low-probability tokens",
209
- )
210
- repetition_penalty = gr.Slider(
211
- label="Repetition penalty",
212
- value=1.2,
213
- minimum=1.0,
214
- maximum=2.0,
215
- step=0.05,
216
- interactive=True,
217
- info="Penalize repeated tokens",
218
- )
219
-
220
- submit.click(generate, inputs=[instruction, temperature, max_new_tokens, top_p, repetition_penalty, chat_mode], outputs=[output])
221
- # instruction.submit(generate, inputs=[instruction, temperature, max_new_tokens, top_p, repetition_penalty, chat_mode], outputs=[output])
222
  share_button.click(None, [], [], _js=share_js)
223
- demo.queue(concurrency_count=16).launch(debug=True)
 
12
  HF_TOKEN = os.environ.get("HF_TOKEN", None)
13
  API_URL = os.environ.get("API_URL")
14
 
 
 
15
 
16
  FIM_PREFIX = "<fim_prefix>"
17
  FIM_MIDDLE = "<fim_middle>"
 
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
  ```
 
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
 
81
+
82
+ def generate(
83
+ prompt, temperature=0.9, max_new_tokens=256, top_p=0.95, repetition_penalty=1.0
84
+ ):
85
 
86
  temperature = float(temperature)
87
  if temperature < 1e-2:
88
  temperature = 1e-2
89
  top_p = float(top_p)
90
  fim_mode = False
91
+
92
  generate_kwargs = dict(
93
  temperature=temperature,
94
  max_new_tokens=max_new_tokens,
 
97
  do_sample=True,
98
  seed=42,
99
  )
 
 
 
 
 
100
 
 
 
 
 
101
  if FIM_INDICATOR in prompt:
102
  fim_mode = True
103
  try:
 
110
 
111
  if fim_mode:
112
  output = prefix
 
 
113
  else:
114
  output = prompt
115
 
116
  previous_token = ""
117
  for response in stream:
118
+ if fim_mode and response.token.text == "<|endoftext|>":
119
  output += (suffix + "\n" + response.token.text)
 
 
120
  else:
121
  output += response.token.text
122
  previous_token = response.token.text
 
128
  "def print_hello_world():",
129
  'def fibonacci(n: int) -> int:\n """ Compute the n-th Fibonacci number. """',
130
  'from typing import List, Tuple\n\ndef sum_and_product(numbers: List[int]) -> Tuple[int, int]:\n """ Return the sum and the product of the integers in the list as a tuple. Here is the answer of the exercise"""',
131
+ "class ComplexNumbers:",
132
  ]
133
 
134
 
 
137
  pass
138
  return x
139
 
 
140
 
141
+ css = ".generating {visibility: hidden}"
 
 
 
 
142
 
143
+ monospace_css = """
144
+ #q-input textarea {
145
+ font-family: monospace, 'Consolas', Courier, monospace;
146
+ }
147
+ """
148
+ custom_output_css = """
149
+ #q-output textarea {
150
+ min-height: 800px;
151
+ }
152
  """
153
 
154
+ css += share_btn_css + monospace_css + custom_output_css + ".gradio-container {color: black}"
155
+
156
+
157
+ description = """
158
+ <div style="text-align: center;">
159
+ <h1 style='color: black;'> 💫 StarCoder<span style='color: #e6b800;'> - </span>Playground</h1>
160
+ <p style='color: black;'>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.</p>
161
+ </div>
162
+ """
163
+ disclaimer = """⚠️ **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)"""
164
+
165
+ with gr.Blocks(theme=theme, analytics_enabled=False, css=css) as demo:
166
+ with gr.Column():
167
+ gr.Markdown(description)
168
  with gr.Row():
169
+ with gr.Column():
170
+ instruction = gr.Textbox(
171
+ placeholder="Enter your prompt here",
172
+ label="Prompt",
173
+ elem_id="q-input",
174
+ )
175
  submit = gr.Button("Generate", variant="primary")
176
+ output = gr.Code(elem_id="q-output", lines=30)
177
+
178
+ with gr.Accordion("Advanced settings", open=False):
179
+ with gr.Row():
180
+ column_1, column_2 = gr.Column(), gr.Column()
181
+ with column_1:
182
+ temperature = gr.Slider(
183
+ label="Temperature",
184
+ value=0.2,
185
+ minimum=0.0,
186
+ maximum=1.0,
187
+ step=0.05,
188
+ interactive=True,
189
+ info="Higher values produce more diverse outputs",
190
+ )
191
+ max_new_tokens = gr.Slider(
192
+ label="Max new tokens",
193
+ value=256,
194
+ minimum=0,
195
+ maximum=8192,
196
+ step=64,
197
+ interactive=True,
198
+ info="The maximum numbers of new tokens",
199
+ )
200
+ with column_2:
201
+ top_p = gr.Slider(
202
+ label="Top-p (nucleus sampling)",
203
+ value=0.90,
204
+ minimum=0.0,
205
+ maximum=1,
206
+ step=0.05,
207
+ interactive=True,
208
+ info="Higher values sample more low-probability tokens",
209
+ )
210
+ repetition_penalty = gr.Slider(
211
+ label="Repetition penalty",
212
+ value=1.2,
213
+ minimum=1.0,
214
+ maximum=2.0,
215
+ step=0.05,
216
+ interactive=True,
217
+ info="Penalize repeated tokens",
218
+ )
219
+ gr.Markdown(disclaimer)
220
  with gr.Group(elem_id="share-btn-container"):
221
  community_icon = gr.HTML(community_icon_html, visible=True)
222
  loading_icon = gr.HTML(loading_icon_html, visible=True)
223
+ share_button = gr.Button(
224
+ "Share to community", elem_id="share-btn", visible=True
225
+ )
226
  gr.Examples(
227
  examples=examples,
228
  inputs=[instruction],
 
232
  )
233
  gr.Markdown(FORMATS)
234
 
235
+ submit.click(
236
+ generate,
237
+ inputs=[instruction, temperature, max_new_tokens, top_p, repetition_penalty],
238
+ outputs=[output],
239
+ )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
240
  share_button.click(None, [], [], _js=share_js)
241
+ demo.queue(concurrency_count=16).launch(debug=True)