Spaces:
Sleeping
Sleeping
havinashpatil commited on
Commit ·
271cc02
1
Parent(s): 402970c
Add AI coding system with local Hugging Face LLM integration
Browse filesFiles changed (4)
- README.md+54
- ai_fix.batAdded+5
- ai_fix.pyAdded+91
- requirements.txt+2
118 unmodified lines119120121122123124118 unmodified linespython create_tasks.py```## Usage### 0. Training with TRL (Colab)118 unmodified lines119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178118 unmodified linespython create_tasks.py```## AI Coding System (Local Hugging Face LLM)CodeArena now includes a built-in AI code fixer using Hugging Face Transformers for local, offline code repair.### Features- **Local LLM**: No API keys or internet required- **Fast Fixes**: Uses TinyLlama-1.1B for quick code corrections- **Command Line**: Simple stdin/stdout interface- **Optimized Prompts**: Engineered for code repair tasks### Setup1. **Install Dependencies:**```bashpip install accelerate bitsandbytes # Added to requirements.txt```2. **First Run (Model Download):**```bashpython ai_fix.py < any_code.py```This will download the model (~600MB) on first use.### Usage**Fix a Python file:**```bashcat buggy_code.py | python ai_fix.py```**Interactive fixing:**```bash# Windowstype buggy_code.py | ai_fix.bat# Linux/Maccat buggy_code.py | python ai_fix.py```**Example:**```bashecho "def hello()print('world')" | python ai_fix.py# Output: def hello():# print('world')```### Model Options- **Default**: `TinyLlama/TinyLlama-1.1B-Chat-v1.0` (fast, lightweight)- **Change model**: Edit `MODEL_NAME` in `ai_fix.py`### Performance- **CPU**: ~10-30 seconds per fix- **GPU**: ~2-5 seconds per fix- **Memory**: ~2GB RAM minimum## Usage### 0. Training with TRL (Colab)
12345@echo offREM AI Code Fixer Batch ScriptREM Usage: type code.py | ai_fix.bat
python ai_fix.py12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091#!/usr/bin/env python3"""AI Code Fixer using Hugging Face TransformersReads code from stdin, fixes it using TinyLlama, outputs fixed code."""
import sysimport osfrom transformers import AutoModelForCausalLM, AutoTokenizerimport torch
# Model configurationMODEL_NAME = "TinyLlama/TinyLlama-1.1B-Chat-v1.0"
def load_model(): """Load the model and tokenizer.""" print("Loading model...", file=sys.stderr) tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
# Try to use GPU if available, fallback to CPU device = "cuda" if torch.cuda.is_available() else "cpu" print(f"Using device: {device}", file=sys.stderr)
model = AutoModelForCausalLM.from_pretrained( MODEL_NAME, torch_dtype=torch.float16 if device == "cuda" else torch.float32, device_map="auto" if device == "cuda" else None, low_cpu_mem_usage=True )
if device == "cpu": model = model.to(device)
return model, tokenizer
def generate_fix(model, tokenizer, code): """Generate fixed code using the model.""" prompt = f"""You are an expert competitive programmer.
Fix the following Python code:- Remove syntax errors- Ensure correct logic- Optimize to O(n) if possible
Code:{code}
Return ONLY corrected code."""
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
with torch.no_grad(): output = model.generate( **inputs, max_new_tokens=500, temperature=0.3, # Lower temperature for more deterministic fixes do_sample=True, top_p=0.9, pad_token_id=tokenizer.eos_token_id )
# Decode and extract only the code part full_output = tokenizer.decode(output[0], skip_special_tokens=True)
# Try to extract just the code after the prompt if "Return ONLY corrected code." in full_output: code_part = full_output.split("Return ONLY corrected code.")[-1].strip() else: code_part = full_output.replace(prompt, "").strip()
return code_part
def main(): # Read code from stdin code = sys.stdin.read().strip()
if not code: print("No code provided", file=sys.stderr) sys.exit(1)
try: model, tokenizer = load_model() fixed_code = generate_fix(model, tokenizer, code) print(fixed_code) except Exception as e: print(f"Error: {e}", file=sys.stderr) sys.exit(1)
if __name__ == "__main__": main()8 unmodified lines910118 unmodified linestorchdatasetstrl8 unmodified lines9101112138 unmodified linestorchdatasetstrlacceleratebitsandbytes
README.md+54
ai_fix.batAdded+5
ai_fix.pyAdded+91
requirements.txt+2