File size: 696 Bytes
60370a2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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}"