Spaces:
Running
Running
Update agents/error_fixer.py
Browse files- agents/error_fixer.py +8 -15
agents/error_fixer.py
CHANGED
@@ -1,21 +1,14 @@
|
|
1 |
-
from transformers import pipeline
|
2 |
-
import re
|
3 |
|
4 |
-
# Load
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
"""Basic code cleanup (removes weird chars, extra spaces)."""
|
9 |
-
return re.sub(r'[^\x00-\x7F]+', '', code).strip()
|
10 |
|
11 |
def fix_code(code: str) -> str:
|
12 |
-
""
|
13 |
-
Uses Hugging Face starcoder to attempt fixing the code.
|
14 |
-
"""
|
15 |
-
code = clean_code(code)
|
16 |
-
prompt = f"### Fix the following code:\n{code}\n### Fixed code:"
|
17 |
try:
|
18 |
-
result = code_fix_pipeline(prompt)[0]["generated_text"]
|
19 |
return result.strip()
|
20 |
except Exception as e:
|
21 |
-
return f"Error during fixing: {e}"
|
|
|
1 |
+
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, pipeline
|
|
|
2 |
|
3 |
+
# Load CodeT5 model (public and free)
|
4 |
+
tokenizer = AutoTokenizer.from_pretrained("Salesforce/codet5-base")
|
5 |
+
model = AutoModelForSeq2SeqLM.from_pretrained("Salesforce/codet5-base")
|
6 |
+
code_fix_pipeline = pipeline("text2text-generation", model=model, tokenizer=tokenizer)
|
|
|
|
|
7 |
|
8 |
def fix_code(code: str) -> str:
|
9 |
+
prompt = f"Fix the following code:\n{code}"
|
|
|
|
|
|
|
|
|
10 |
try:
|
11 |
+
result = code_fix_pipeline(prompt, max_length=256)[0]["generated_text"]
|
12 |
return result.strip()
|
13 |
except Exception as e:
|
14 |
+
return f"Error during fixing: {e}"
|