mirror of
https://github.com/deepseek-ai/DeepSeek-Coder
synced 2025-06-26 18:25:53 +00:00
add instruction model eval script
This commit is contained in:
@@ -1,3 +1,109 @@
|
||||
import re
|
||||
|
||||
languge_settings = {
|
||||
'python': {
|
||||
'full_name': 'Python',
|
||||
'indent': 4,
|
||||
},
|
||||
'cpp': {
|
||||
'full_name': 'cpp',
|
||||
'indent': 0,
|
||||
'main': "int main()",
|
||||
},
|
||||
'java': {
|
||||
'full_name': 'Java',
|
||||
'indent': 4,
|
||||
'main': "public static void main",
|
||||
},
|
||||
'cs': {
|
||||
'full_name': "csharp",
|
||||
'indent': 0,
|
||||
'main': "public static void Main",
|
||||
},
|
||||
'php': {
|
||||
'full_name': "PHP",
|
||||
'indent': 0,
|
||||
},
|
||||
'ts': {
|
||||
'full_name': "TypeScript",
|
||||
'indent': 0,
|
||||
},
|
||||
'js': {
|
||||
'full_name': "JavaScript",
|
||||
'indent': 0
|
||||
},
|
||||
'sh': {
|
||||
'full_name': "Bash",
|
||||
'indent': 0
|
||||
}
|
||||
}
|
||||
|
||||
def get_function_name(question: str, lang: str):
|
||||
func_lines = [x for x in question.strip().split('\n') if x.strip()]
|
||||
|
||||
if lang.lower() == 'python':
|
||||
func_idx = [i for i in range(len(func_lines)) if func_lines[i].startswith("def ")][-1]
|
||||
func_name = func_lines[func_idx].split('(')[0].strip()
|
||||
func_prefix = "\n".join(func_lines[:func_idx])
|
||||
return func_name, func_prefix
|
||||
|
||||
func_name = func_lines[-1].split('{')[0].strip()
|
||||
func_prefix = "\n".join(func_lines[:-1])
|
||||
return func_name, func_prefix
|
||||
|
||||
def extract_generation_code(example: str, lang_code: str, verbose: bool=False):
|
||||
task_id = example['task_id']
|
||||
output = example.get('output', example.get("gpt_completion"))
|
||||
question = example["prompt"].strip()
|
||||
setting = languge_settings[lang_code]
|
||||
lang = setting['full_name']
|
||||
indent = setting['indent']
|
||||
|
||||
try:
|
||||
code_block: str = re.findall(f'```{lang.lower()}\n(.*?)```', output, re.DOTALL | re.IGNORECASE)[0]
|
||||
if verbose:
|
||||
print(">>> Task: {}\n{}".format(task_id, code_block))
|
||||
|
||||
# Remove main
|
||||
if setting.get('main', None) and setting['main'] in code_block:
|
||||
main_start = code_block.index(setting['main'])
|
||||
code_block = code_block[:main_start]
|
||||
|
||||
func_name, func_prefix = get_function_name(question, lang)
|
||||
|
||||
try:
|
||||
start = code_block.lower().index(func_name.lower())
|
||||
indent = 0
|
||||
while start - indent >= 0 and code_block[start - indent-1] == ' ':
|
||||
indent += 1
|
||||
|
||||
try:
|
||||
end = code_block.rindex('\n' + ' '*indent + '}')
|
||||
except:
|
||||
end = len(code_block)
|
||||
except:
|
||||
start = 0
|
||||
try:
|
||||
end = code_block.rindex('\n' + ' '*indent + '}')
|
||||
except:
|
||||
end = len(code_block)
|
||||
|
||||
body = code_block[start:end]
|
||||
|
||||
if lang_code.lower() in ['php', 'ts', 'js']:
|
||||
body += '\n' + ' '*indent + '}'
|
||||
|
||||
generation = func_prefix + '\n' + body + '\n'
|
||||
example['generation'] = generation
|
||||
|
||||
except Exception as ex:
|
||||
print("Failed to extract code block with error `{}`:\n>>> Task: {}\n>>> Output:\n{}".format(
|
||||
ex, task_id, output
|
||||
))
|
||||
example['generation'] = example['prompt'] + '\n' + output
|
||||
|
||||
return example
|
||||
|
||||
def cleanup_code(
|
||||
code: str,
|
||||
language_type: str = None,
|
||||
|
||||
Reference in New Issue
Block a user