Spaces:
Running
on
Zero
Running
on
Zero
update
Browse files
app.py
CHANGED
@@ -241,15 +241,7 @@ def infilling_dream(
|
|
241 |
x = F.pad(input_ids, (0, max_tokens - input_ids.shape[1]), value = MASK_ID)
|
242 |
|
243 |
# ------ Visualization Setup
|
244 |
-
previous_tokens_vis = None
|
245 |
initial_generated_tokens = input_ids[0, prefix_len: prefix_len + num_generation_tokens]
|
246 |
-
vis_data_initial = []
|
247 |
-
for tok_id in initial_generated_tokens.tolist():
|
248 |
-
display_token = MASK_TOKEN
|
249 |
-
color = '#4444'
|
250 |
-
vis_data_initial.append((display_token, color))
|
251 |
-
|
252 |
-
previous_tokens_vis = initial_generated_tokens
|
253 |
#yield vis_data_initial
|
254 |
yield tokenizer.decode(initial_generated_tokens.tolist()), ''
|
255 |
time.sleep(visualization_delay)
|
@@ -330,32 +322,6 @@ def infilling_dream(
|
|
330 |
|
331 |
## Visualize Denoise Step
|
332 |
cur_generated_tokens = x[0, prefix_len: prefix_len + num_generation_tokens]
|
333 |
-
print(f"Current generated tokens: {tokenizer.decode(cur_generated_tokens)}")
|
334 |
-
vis_data = []
|
335 |
-
for j in range(num_generation_tokens):
|
336 |
-
current_tok_id = cur_generated_tokens[j].item()
|
337 |
-
previous_tok_id = previous_tokens_vis[j].item() if previous_tokens_vis is not None and j < len(previous_tokens_vis) else MASK_ID
|
338 |
-
try:
|
339 |
-
decoded_token = tokenizer.decode([current_tok_id], skip_special_tokens=False)
|
340 |
-
display_token = MASK_TOKEN if current_tok_id == MASK_ID else decoded_token
|
341 |
-
except Exception: display_token = f"[ID:{current_tok_id}]"
|
342 |
-
vis_data = []
|
343 |
-
#print(f"Step {i+1}/{steps}")
|
344 |
-
#print(f"Current generated tokens: {tokenizer.decode(cur_generated_tokens)}")
|
345 |
-
# [Visualization formatting logic remains the same]
|
346 |
-
for j in range(num_generation_tokens):
|
347 |
-
current_tok_id = cur_generated_tokens[j].item()
|
348 |
-
previous_tok_id = previous_tokens_vis[j].item() if previous_tokens_vis is not None and j < len(previous_tokens_vis) else MASK_ID
|
349 |
-
try:
|
350 |
-
decoded_token = tokenizer.decode([current_tok_id], skip_special_tokens=False)
|
351 |
-
display_token = MASK_TOKEN if current_tok_id == MASK_ID else decoded_token
|
352 |
-
except Exception: display_token = f"[ID:{current_tok_id}]"
|
353 |
-
|
354 |
-
color = None; token_to_display = display_token
|
355 |
-
if current_tok_id == MASK_ID: color = "#444444"
|
356 |
-
else: color = "#6699CC"
|
357 |
-
|
358 |
-
vis_data.append((token_to_display, color))
|
359 |
cur_tokens = tokenizer.decode(cur_generated_tokens.tolist())
|
360 |
## replace all <|endoftext|> with <|delete|>
|
361 |
cur_tokens = cur_tokens.replace("<|endoftext|>", "<|delete|>")
|
@@ -408,26 +374,12 @@ def infilling_dream(
|
|
408 |
num_generation_tokens -= 1
|
409 |
|
410 |
cur_generated_tokens = x[0, prefix_len: prefix_len + num_generation_tokens]
|
411 |
-
#vis_data = []
|
412 |
-
#for j in range(num_generation_tokens):
|
413 |
-
# current_tok_id = cur_generated_tokens[j].item()
|
414 |
-
# try:
|
415 |
-
# decoded_token = tokenizer.decode([current_tok_id], skip_special_tokens=False)
|
416 |
-
# display_token = MASK_TOKEN if current_tok_id == MASK_ID else decoded_token
|
417 |
-
# except Exception: display_token = f"[ID:{current_tok_id}]"
|
418 |
-
# color = None; token_to_display = display_token
|
419 |
-
# if current_tok_id == MASK_ID: color = "#444444"
|
420 |
-
# else: color = "#6699CC"
|
421 |
-
|
422 |
-
# vis_data.append((token_to_display, color))
|
423 |
yield tokenizer.decode(cur_generated_tokens.tolist()), ''
|
424 |
-
#yield vis_data
|
425 |
time.sleep(visualization_delay)
|
426 |
|
427 |
generated_code = tokenizer.decode(x[0, prefix_len: prefix_len + num_generation_tokens].tolist())
|
428 |
yield generated_code, ''
|
429 |
-
|
430 |
-
yield generated_code, result
|
431 |
def get_example_input():
|
432 |
### this functions samples a case from humaneval-infilling as prefix and suffix
|
433 |
task_id = random.choice(list(problems.keys()))
|
@@ -454,8 +406,8 @@ def create_chatbot_demo():
|
|
454 |
with gr.Blocks(css=css) as demo:
|
455 |
gr.Markdown("# DreamOn: Diffusion Language Models For Code Infilling Beyond Fixed-size Canvas\nClick **Example Prompt** to get a prefix and suffix, then click **Generate** to generate code. Have fun!")
|
456 |
gr.Markdown(
|
457 |
-
"[[Model Card
|
458 |
-
"[[Blog
|
459 |
)
|
460 |
|
461 |
with gr.Column():
|
@@ -506,6 +458,7 @@ def create_chatbot_demo():
|
|
506 |
with gr.Row():
|
507 |
generate_btn = gr.Button("Generate", variant="primary")
|
508 |
clear_btn = gr.Button("Clear")
|
|
|
509 |
|
510 |
# Generation Settings
|
511 |
with gr.Accordion("Generation Settings"):
|
@@ -612,6 +565,12 @@ def create_chatbot_demo():
|
|
612 |
queue=False
|
613 |
)
|
614 |
|
|
|
|
|
|
|
|
|
|
|
|
|
615 |
sample_btn.click(
|
616 |
fn=get_example_input,
|
617 |
outputs=[prefix_input, suffix_input, test_case_input, task_id_input],
|
|
|
241 |
x = F.pad(input_ids, (0, max_tokens - input_ids.shape[1]), value = MASK_ID)
|
242 |
|
243 |
# ------ Visualization Setup
|
|
|
244 |
initial_generated_tokens = input_ids[0, prefix_len: prefix_len + num_generation_tokens]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
245 |
#yield vis_data_initial
|
246 |
yield tokenizer.decode(initial_generated_tokens.tolist()), ''
|
247 |
time.sleep(visualization_delay)
|
|
|
322 |
|
323 |
## Visualize Denoise Step
|
324 |
cur_generated_tokens = x[0, prefix_len: prefix_len + num_generation_tokens]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
325 |
cur_tokens = tokenizer.decode(cur_generated_tokens.tolist())
|
326 |
## replace all <|endoftext|> with <|delete|>
|
327 |
cur_tokens = cur_tokens.replace("<|endoftext|>", "<|delete|>")
|
|
|
374 |
num_generation_tokens -= 1
|
375 |
|
376 |
cur_generated_tokens = x[0, prefix_len: prefix_len + num_generation_tokens]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
377 |
yield tokenizer.decode(cur_generated_tokens.tolist()), ''
|
|
|
378 |
time.sleep(visualization_delay)
|
379 |
|
380 |
generated_code = tokenizer.decode(x[0, prefix_len: prefix_len + num_generation_tokens].tolist())
|
381 |
yield generated_code, ''
|
382 |
+
|
|
|
383 |
def get_example_input():
|
384 |
### this functions samples a case from humaneval-infilling as prefix and suffix
|
385 |
task_id = random.choice(list(problems.keys()))
|
|
|
406 |
with gr.Blocks(css=css) as demo:
|
407 |
gr.Markdown("# DreamOn: Diffusion Language Models For Code Infilling Beyond Fixed-size Canvas\nClick **Example Prompt** to get a prefix and suffix, then click **Generate** to generate code. Have fun!")
|
408 |
gr.Markdown(
|
409 |
+
"[[Model Card](https://huggingface.co/Dream-org/DreamOn-v0-7B)] "
|
410 |
+
"[[Blog](https://hkunlp.github.io/blog/2025/dreamon/)]"
|
411 |
)
|
412 |
|
413 |
with gr.Column():
|
|
|
458 |
with gr.Row():
|
459 |
generate_btn = gr.Button("Generate", variant="primary")
|
460 |
clear_btn = gr.Button("Clear")
|
461 |
+
check_btn = gr.Button("Run test case")
|
462 |
|
463 |
# Generation Settings
|
464 |
with gr.Accordion("Generation Settings"):
|
|
|
565 |
queue=False
|
566 |
)
|
567 |
|
568 |
+
check_btn.click(
|
569 |
+
fn=check_result,
|
570 |
+
inputs=[output_vis, task_id_input],
|
571 |
+
outputs=[result_output]
|
572 |
+
)
|
573 |
+
|
574 |
sample_btn.click(
|
575 |
fn=get_example_input,
|
576 |
outputs=[prefix_input, suffix_input, test_case_input, task_id_input],
|
eval.py
CHANGED
@@ -10,8 +10,34 @@ import tempfile
|
|
10 |
from typing import Callable, Dict, Optional
|
11 |
|
12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
def unsafe_execute(problem, completion, timeout=3):
|
14 |
-
with
|
15 |
# Construct the check program
|
16 |
check_program = (
|
17 |
problem["prompt"]
|
@@ -23,30 +49,27 @@ def unsafe_execute(problem, completion, timeout=3):
|
|
23 |
+ f"check({problem['entry_point']})"
|
24 |
)
|
25 |
|
26 |
-
#
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
if process.is_alive():
|
46 |
-
process.terminate()
|
47 |
result = "timed out"
|
48 |
else:
|
49 |
-
result =
|
50 |
|
51 |
return result
|
52 |
|
|
|
10 |
from typing import Callable, Dict, Optional
|
11 |
|
12 |
|
13 |
+
import subprocess
|
14 |
+
import time
|
15 |
+
from multiprocessing import Process, Queue
|
16 |
+
|
17 |
+
def write_code_to_file(code, filename):
|
18 |
+
with open(filename, 'w') as f:
|
19 |
+
f.write(code)
|
20 |
+
|
21 |
+
def read_result_from_file(filename):
|
22 |
+
with open(filename, 'r') as f:
|
23 |
+
return f.read().strip()
|
24 |
+
|
25 |
+
def child_process(script_path, result_path, timeout):
|
26 |
+
try:
|
27 |
+
# Construct the command to run the script and redirect output to the result file
|
28 |
+
command = f'python3 {script_path} > {result_path} 2>&1'
|
29 |
+
subprocess.run(command, shell=True, timeout=timeout)
|
30 |
+
except subprocess.TimeoutExpired:
|
31 |
+
result = "timed out"
|
32 |
+
else:
|
33 |
+
result = read_result_from_file(result_path)
|
34 |
+
finally:
|
35 |
+
# Write the result to a file and exit
|
36 |
+
with open(result_path, 'w') as f:
|
37 |
+
f.write(result)
|
38 |
+
|
39 |
def unsafe_execute(problem, completion, timeout=3):
|
40 |
+
with tempfile.TemporaryDirectory() as temp_dir:
|
41 |
# Construct the check program
|
42 |
check_program = (
|
43 |
problem["prompt"]
|
|
|
49 |
+ f"check({problem['entry_point']})"
|
50 |
)
|
51 |
|
52 |
+
# Write the check program to a temporary file
|
53 |
+
script_path = os.path.join(temp_dir, 'script.py')
|
54 |
+
write_code_to_file(check_program, script_path)
|
55 |
+
|
56 |
+
# Create a temporary file to store the result
|
57 |
+
result_path = os.path.join(temp_dir, 'result.txt')
|
58 |
+
|
59 |
+
# Create a queue to communicate between parent and child processes
|
60 |
+
result_queue = Queue()
|
61 |
+
|
62 |
+
# Create a child process
|
63 |
+
p = Process(target=child_process, args=(script_path, result_path, timeout))
|
64 |
+
p.start()
|
65 |
+
p.join(timeout)
|
66 |
+
|
67 |
+
if p.is_alive():
|
68 |
+
p.terminate()
|
69 |
+
p.join()
|
|
|
|
|
|
|
70 |
result = "timed out"
|
71 |
else:
|
72 |
+
result = read_result_from_file(result_path)
|
73 |
|
74 |
return result
|
75 |
|