Spaces:
Runtime error
Runtime error
Download agents/error_fixer.py from hugging2021/CodeFixPro: direct link, hf CLI and curl.
- Browser
- Download file 696 Bytes
-
https://huggingface.co/spaces/hugging2021/CodeFixPro/resolve/51e799a82ecf6bc2d4aa2dcc527d17846d0f3a49/agents/error_fixer.py
- Command line
-
hf download hf://spaces/hugging2021/CodeFixPro@51e799a82ecf6bc2d4aa2dcc527d17846d0f3a49/agents/error_fixer.py
-
curl -L -o error_fixer.py https://huggingface.co/spaces/hugging2021/CodeFixPro/resolve/51e799a82ecf6bc2d4aa2dcc527d17846d0f3a49/agents/error_fixer.py
696 Bytes
| from transformers import pipeline | |
| import re | |
| # Load once globally to save time | |
| code_fix_pipeline = pipeline("text2text-generation", model="bigcode/starcoder", max_length=512) | |
| def clean_code(code): | |
| """Basic code cleanup (removes weird chars, extra spaces).""" | |
| return re.sub(r'[^\x00-\x7F]+', '', code).strip() | |
| def fix_code(code: str) -> str: | |
| """ | |
| Uses Hugging Face starcoder to attempt fixing the code. | |
| """ | |
| code = clean_code(code) | |
| prompt = f"### Fix the following code:\n{code}\n### Fixed code:" | |
| try: | |
| result = code_fix_pipeline(prompt)[0]["generated_text"] | |
| return result.strip() | |
| except Exception as e: | |
| return f"Error during fixing: {e}" |