Views
No views yet
1from unsloth import FastLanguageModel
2max_seq_length = 8192
3dtype = None
4load_in_4bit = True
5model, tokenizer = FastLanguageModel.from_pretrained(
6 model_name = "patched-codes/Llama-3.2-1B-FastApply",
7 max_seq_length = max_seq_length,
8 dtype = dtype,
9 load_in_4bit = load_in_4bit,
10)
11FastLanguageModel.for_inference(model) # Enable native 2x faster inference1original_code = """import React from 'react';
2import { Loader } from 'lucide-react';
3
4interface ButtonProps {
5 text: string;
6 onClick?: () => void;
7 loading?: boolean;
8 disabled?: boolean;
9 icon?: React.ReactNode;
10}
11
12const Button: React.FC<ButtonProps> = ({
13 text,
14 onClick,
15 loading = false,
16 disabled = false,
17 icon
18}) => (
19 <button
20 className="bg-blue-500 text-white p-2 rounded flex items-center gap-2"
21 onClick={onClick}
22 disabled={disabled || loading}
23 >
24 {loading ? <Loader className="animate-spin" /> : icon}
25 {text}
26 </button>
27);
28
29export default Button;
30"""
31
32update_snippet = """interface ButtonProps {
33 variant?: 'primary' | 'secondary' | 'danger';
34 size?: 'small' | 'medium' | 'large';
35 // ... other props
36}
37
38const Button: React.FC<ButtonProps> = ({
39 variant = 'primary',
40 size = 'medium',
41 // ... other props
42}) => (
43 <button
44 className={`flex items-center gap-2 rounded ${
45 size === 'small' ? 'p-1 text-sm' :
46 size === 'large' ? 'p-3 text-lg' :
47 'p-2 text-md'
48 } ${
49 variant === 'primary' ? 'bg-blue-500 text-white' :
50 variant === 'secondary' ? 'bg-gray-500 text-white' :
51 'bg-red-500 text-white'
52 }`}
53 // ... other attributes
54 >
55 // ... existing code ...
56 </button>
57);
58"""1input_text = f"""
2Merge all changes from the <update> snippet into the <code> below.
3- Preserve the code's structure, order, comments, and indentation exactly.
4- Output only the updated code, enclosed within <updated-code> and </updated-code> tags.
5- Do not include any additional text, explanations, placeholders, ellipses, or code fences.
6
7<code>{original_code}</code>
8
9<update>{update_snippet}</update>
10
11Provide the complete updated code.
12"""
13
14messages = [
15 {"role": "system", "content": "You are a coding assistant that helps merge code updates, ensuring every modification is fully integrated."},
16 {"role": "user", "content": input_text.strip()},
17]
18
19inputs = tokenizer.apply_chat_template(
20 messages,
21 tokenize = True,
22 add_generation_prompt = True, # Must add for generation
23 return_tensors = "pt",
24).to("cuda")
25
26from transformers import TextStreamer
27text_streamer = TextStreamer(tokenizer, skip_prompt = True)
28output = model.generate(input_ids = inputs, streamer = text_streamer, max_new_tokens = 8192,
29 use_cache = True, temperature = 1.5, min_p = 0.1)
30
31response = tokenizer.decode(output[0][len(inputs[0]):])
32
33updated_code = response.split("<updated-code>")[1].split("</updated-code>")[0]