sakuragolden commited on
Commit
f8266ef
·
verified ·
1 Parent(s): 7e15f52

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +4 -10
app.py CHANGED
@@ -160,16 +160,12 @@ def generate_code_task(task: str, hf_token: str, hf_model: str, language: str, s
160
  # extract code fence content if present
161
  if "```" in code:
162
  parts = code.split("```")
163
- # parts may be: before, ```lang\ncode\n```, ...
164
- # find the first non-empty code-like chunk
165
  candidate = None
166
  for i in range(1, len(parts), 2):
167
  chunk = parts[i].strip()
168
  if len(chunk) > 0:
169
- # if chunk starts with language id like "python\n", remove the first line
170
  lines = chunk.splitlines()
171
  if len(lines) > 1 and len(lines[0]) <= 20 and not lines[0].strip().startswith(("#", "//")) and not "(" in lines[0]:
172
- # treat as lang tag
173
  candidate = "\n".join(lines[1:]).strip()
174
  else:
175
  candidate = chunk
@@ -177,16 +173,13 @@ def generate_code_task(task: str, hf_token: str, hf_model: str, language: str, s
177
  if candidate:
178
  code = candidate
179
 
180
- # try to strip instruction echoes
181
  if instruction.strip() and code.startswith(instruction.strip()):
182
  code = code[len(instruction.strip()):].strip()
183
 
184
- # fallback: ensure code not empty
185
  if not code:
186
  code = gen
187
 
188
  ext = ext_for_language(language)
189
- fname = f"code_{language.lower().replace(' ', '_')}{ext}"
190
  path = save_code_to_tempfile(code, filename_hint="generated_code", ext=ext)
191
  return code, path
192
 
@@ -199,7 +192,6 @@ EXAMPLES = [
199
  ]
200
 
201
  def load_example_to_inputs(example):
202
- # returns values matching the UI inputs order we will set
203
  (task_v, hf_mod, lang, src_lang, desc, temperature, max_new_tokens, top_k) = example
204
  return hf_mod, task_v, lang, src_lang, desc, temperature, max_new_tokens, top_k
205
 
@@ -270,9 +262,11 @@ def build_ui():
270
 
271
  hf_model.change(fn=update_model_info, inputs=[hf_model], outputs=[model_info])
272
 
273
- # Example buttons wiring
274
  for btn, ex in example_buttons:
275
- btn.click(fn=load_example_to_inputs, inputs=None, outputs=[hf_model, task, language, src_language, description, temp, max_tokens, top_k], _js=None, _preprocess=False, _postprocess=False, args=[ex])
 
 
276
 
277
  # Generate
278
  gen_btn.click(fn=do_generate, inputs=[task, hf_token, hf_model, language, src_language, description, temp, max_tokens, top_k], outputs=[code_out, download_file])
 
160
  # extract code fence content if present
161
  if "```" in code:
162
  parts = code.split("```")
 
 
163
  candidate = None
164
  for i in range(1, len(parts), 2):
165
  chunk = parts[i].strip()
166
  if len(chunk) > 0:
 
167
  lines = chunk.splitlines()
168
  if len(lines) > 1 and len(lines[0]) <= 20 and not lines[0].strip().startswith(("#", "//")) and not "(" in lines[0]:
 
169
  candidate = "\n".join(lines[1:]).strip()
170
  else:
171
  candidate = chunk
 
173
  if candidate:
174
  code = candidate
175
 
 
176
  if instruction.strip() and code.startswith(instruction.strip()):
177
  code = code[len(instruction.strip()):].strip()
178
 
 
179
  if not code:
180
  code = gen
181
 
182
  ext = ext_for_language(language)
 
183
  path = save_code_to_tempfile(code, filename_hint="generated_code", ext=ext)
184
  return code, path
185
 
 
192
  ]
193
 
194
  def load_example_to_inputs(example):
 
195
  (task_v, hf_mod, lang, src_lang, desc, temperature, max_new_tokens, top_k) = example
196
  return hf_mod, task_v, lang, src_lang, desc, temperature, max_new_tokens, top_k
197
 
 
262
 
263
  hf_model.change(fn=update_model_info, inputs=[hf_model], outputs=[model_info])
264
 
265
+ # Example buttons wiring (use closure to bind example)
266
  for btn, ex in example_buttons:
267
+ def make_loader(example):
268
+ return lambda: load_example_to_inputs(example)
269
+ btn.click(fn=make_loader(ex), inputs=None, outputs=[hf_model, task, language, src_language, description, temp, max_tokens, top_k])
270
 
271
  # Generate
272
  gen_btn.click(fn=do_generate, inputs=[task, hf_token, hf_model, language, src_language, description, temp, max_tokens, top_k], outputs=[code_out, download_file])