Views
No views yet
Refer to our GitHub repo ARiSE-Lab/SemCoder for detailed introduction to SemCoder!
1from transformers import pipeline
2import torch
3
4generator = pipeline(
5 model="semcoder/semcoder_s_1030",
6 task="text-generation",
7 torch_dtype=torch.float16,
8 device_map="auto",
9)
10
11# Generate Code
12
13CODEGEN_REQUEST = """You are an exceptionally intelligent coding assistant that consistently delivers accurate and reliable <Code> according to <NL_Description>
14
15<NL_Description>
16{desc}
17
18<Code>
19"""
20desc = """You are tasked with implementing a Python class that simulates a simple version of a "To-Do List" application. The class should have the following functionalities:
211. Add a new task to the to-do list.
222. Mark a task as completed.
233. Display all tasks in the to-do list.
244. Display only the incomplete tasks in the to-do list.
25"""
26
27prompt = CODEGEN_REQUEST.format(desc=desc)
28result = generator(prompt, max_length=2048, num_return_sequences=1, temperature=0.0)
29code = result[0]["generated_text"].split("```python")[1].split("```")[0]
30print(code)
31
32# Understand Code with Monologues
33
34FWD_MNL_REQUEST = """Simulate the Execution: You are given a Python function and an assertion containing a function input. Complete the assertion containing the execution output corresponding to the given input in [ANSWER] and [/ANSWER] tags.
35{code}
36"""
37
38tests = """
39todo_list = ToDoList()
40todo_list.add_task("Buy groceries")
41todo_list.add_task("Complete assignment")
42todo_list.mark_completed("Buy groceries")
43assert todo_list.tasks == ???
44"""
45code += tests
46prompt = FWD_MNL_REQUEST.format(code=code)
47result = generator(prompt, max_length=2048, num_return_sequences=1, temperature=0.0)
48print(result[0]["generated_text"])1@article{ding2024semcoder,
2 title={SemCoder: Training Code Language Models with Comprehensive Semantics},
3 author={Yangruibo Ding and Jinjun Peng and Marcus J. Min and Gail Kaiser and Junfeng Yang and Baishakhi Ray},
4 journal={arXiv preprint arXiv:2406.01006},
5 year={2024}
6}