Download evaluation/general_benchmarks/MBPP/utils/utils.py from tablegpt/TableGPT2-7B: direct link, hf CLI and curl.
- Browser
- Download file 1.46 kB
-
https://huggingface.co/tablegpt/TableGPT2-7B/resolve/refs%2Fpr%2F6/evaluation/general_benchmarks/MBPP/utils/utils.py
- Command line
-
hf download hf://tablegpt/TableGPT2-7B@refs/pr/6/evaluation/general_benchmarks/MBPP/utils/utils.py
-
curl -L -o utils.py https://huggingface.co/tablegpt/TableGPT2-7B/resolve/refs%2Fpr%2F6/evaluation/general_benchmarks/MBPP/utils/utils.py
1.46 kB
| def cleanup_code( | |
| code: str, | |
| language_type: str = None, | |
| dataset: str = None, | |
| issft: bool = False, | |
| stop_words=[], | |
| ): | |
| """ | |
| Cleans up the generated code. | |
| """ | |
| if language_type.lower() == "python": | |
| if issft: | |
| code = _clean_python_code_for_sft(code) | |
| stop_words = ["\ndef", "\nclass", "\nif", "\n#", "\nprint"] | |
| code = _truncate_code_at_stopwords(code, stop_words) | |
| elif language_type.lower() == "ts": | |
| code = _truncate_code_at_stopwords( | |
| code, | |
| stop_words | |
| + [ | |
| "\nexport", | |
| "\nimport", | |
| "\nexport default", | |
| "\nimport default", | |
| "\nconsole.log", | |
| ], | |
| ) | |
| else: | |
| code = _truncate_code_at_stopwords(code, stop_words) | |
| return code | |
| def _clean_python_code_for_sft(code): | |
| code = code.replace("\r", "") | |
| if "```python" in code: | |
| code_start_idx = code.index("```python") | |
| code = code[code_start_idx:].replace("```python", "").strip() | |
| end_idx = code.find("```") if "```" in code else len(code) | |
| code = code[:end_idx].strip() | |
| return code | |
| def _truncate_code_at_stopwords(code, stop_words): | |
| min_stop_idx = len(code) | |
| for stop_word in stop_words: | |
| stop_index = code.find(stop_word) | |
| if 0 <= stop_index < min_stop_idx: | |
| min_stop_idx = stop_index | |
| return code[:min_stop_idx] | |