masanorihirano commited on
Commit
ecf7aeb
1 Parent(s): a1a5519

limit token

Browse files
Files changed (1) hide show
  1. app.py +13 -11
app.py CHANGED
@@ -159,14 +159,16 @@ def evaluate(
159
  instruction,
160
  input=None,
161
  temperature=0.7,
162
- max_new_tokens=256,
163
  ):
164
  num_beams: int = 1
165
  top_p: float = 1.0
166
  top_k: int = 0
167
  prompt = generate_prompt(instruction, input)
168
  inputs = tokenizer(prompt, return_tensors="pt")
169
- input_ids = inputs["input_ids"][:256].to(device)
 
 
170
  generation_config = GenerationConfig(
171
  temperature=temperature,
172
  top_p=top_p,
@@ -181,7 +183,7 @@ def evaluate(
181
  generation_config=generation_config,
182
  return_dict_in_generate=True,
183
  output_scores=True,
184
- max_new_tokens=max_new_tokens,
185
  )
186
  s = generation_output.sequences[0]
187
  output = tokenizer.decode(s, skip_special_tokens=True)
@@ -205,7 +207,7 @@ def evaluate(
205
  "top_p": top_p,
206
  "top_k": top_k,
207
  "num_beams": num_beams,
208
- "max_new_tokens": max_new_tokens,
209
  },
210
  )
211
  except Exception as e:
@@ -255,13 +257,13 @@ with gr.Blocks(
255
  interactive=True,
256
  label="Temperature",
257
  )
258
- max_new_tokens = gr.Slider(
259
- minimum=1,
260
- maximum=128,
261
- value=64,
262
  step=1,
263
  interactive=True,
264
- label="Max length",
265
  )
266
 
267
  with gr.Column(elem_id="user_consent_container") as user_consent_block:
@@ -305,13 +307,13 @@ with gr.Blocks(
305
  inputs.submit(no_interactive, [], [submit_button, clear_button])
306
  inputs.submit(
307
  evaluate,
308
- [instruction, inputs, temperature, max_new_tokens],
309
  [outputs, submit_button, clear_button],
310
  )
311
  submit_button.click(no_interactive, [], [submit_button, clear_button])
312
  submit_button.click(
313
  evaluate,
314
- [instruction, inputs, temperature, max_new_tokens],
315
  [outputs, submit_button, clear_button],
316
  )
317
  clear_button.click(reset_textbox, [], [instruction, inputs, outputs], queue=False)
 
159
  instruction,
160
  input=None,
161
  temperature=0.7,
162
+ max_tokens=384,
163
  ):
164
  num_beams: int = 1
165
  top_p: float = 1.0
166
  top_k: int = 0
167
  prompt = generate_prompt(instruction, input)
168
  inputs = tokenizer(prompt, return_tensors="pt")
169
+ if len(inputs["input_ids"][0]) > max_tokens:
170
+ return f"please reduce length. Currently, {len(inputs['input_ids'][0])} token are used.", gr.update(interactive=True), gr.update(interactive=True)
171
+ input_ids = inputs["input_ids"].to(device)
172
  generation_config = GenerationConfig(
173
  temperature=temperature,
174
  top_p=top_p,
 
183
  generation_config=generation_config,
184
  return_dict_in_generate=True,
185
  output_scores=True,
186
+ max_new_tokens=max_tokens-len(input_ids),
187
  )
188
  s = generation_output.sequences[0]
189
  output = tokenizer.decode(s, skip_special_tokens=True)
 
207
  "top_p": top_p,
208
  "top_k": top_k,
209
  "num_beams": num_beams,
210
+ "max_tokens": max_tokens,
211
  },
212
  )
213
  except Exception as e:
 
257
  interactive=True,
258
  label="Temperature",
259
  )
260
+ max_tokens = gr.Slider(
261
+ minimum=20,
262
+ maximum=384,
263
+ value=128,
264
  step=1,
265
  interactive=True,
266
+ label="Max length (Pre-prompt + instruction + input + output))",
267
  )
268
 
269
  with gr.Column(elem_id="user_consent_container") as user_consent_block:
 
307
  inputs.submit(no_interactive, [], [submit_button, clear_button])
308
  inputs.submit(
309
  evaluate,
310
+ [instruction, inputs, temperature, max_tokens],
311
  [outputs, submit_button, clear_button],
312
  )
313
  submit_button.click(no_interactive, [], [submit_button, clear_button])
314
  submit_button.click(
315
  evaluate,
316
+ [instruction, inputs, temperature, max_tokens],
317
  [outputs, submit_button, clear_button],
318
  )
319
  clear_button.click(reset_textbox, [], [instruction, inputs, outputs], queue=False)