File size: 2,428 Bytes
7900fe8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import torch, gradio as gr
from pathlib import Path
import sys
sys.path.insert(0, str(Path(__file__).parent.parent))
from model.architecture import CodeLLM, CodeLLMConfig
from model.tokenizer import get_gpt2_tokenizer_for_code

DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
config = CodeLLMConfig()
model  = CodeLLM(config)

WEIGHTS_PATH = Path("./checkpoints/final/pytorch_model.bin")
if WEIGHTS_PATH.exists():
    model.load_state_dict(torch.load(WEIGHTS_PATH, map_location=DEVICE))
    print("Loaded trained weights!")

model.to(DEVICE).eval()
tokenizer = get_gpt2_tokenizer_for_code()

def generate_code(prompt, language="Python", max_new_tokens=256, temperature=0.8, top_k=50, top_p=0.95):
    lang_map = {"Python":"<|python|>","JavaScript":"<|javascript|>",
                "TypeScript":"<|typescript|>","Rust":"<|rust|>","Go":"<|go|>","C++":"<|cpp|>"}
    full_prompt = f"{lang_map.get(language,'')}{prompt}"
    input_ids = tokenizer.encode(full_prompt, return_tensors="pt").to(DEVICE)
    with torch.no_grad():
        out = model.generate(input_ids, max_new_tokens=max_new_tokens,
                             temperature=temperature, top_k=top_k, top_p=top_p)
    return tokenizer.decode(out[0][input_ids.shape[1]:], skip_special_tokens=True)

with gr.Blocks(title="CodeLLM", theme=gr.themes.Soft()) as demo:
    gr.Markdown("# CodeLLM — Custom Coding AI\n125M param transformer built from scratch")
    with gr.Row():
        with gr.Column():
            prompt = gr.Textbox(label="Code prompt", lines=5, placeholder="def fibonacci(n):")
            lang   = gr.Dropdown(["Python","JavaScript","TypeScript","Rust","Go","C++"], value="Python", label="Language")
            with gr.Row():
                btn   = gr.Button("Generate ⚡", variant="primary")
                clear = gr.Button("Clear")
        output = gr.Code(label="Output", language="python", lines=20)
    with gr.Accordion("Settings", open=False):
        with gr.Row():
            max_tok = gr.Slider(32, 512, 256, step=32, label="Max tokens")
            temp    = gr.Slider(0.1, 2.0, 0.8, step=0.1, label="Temperature")
            topk    = gr.Slider(1, 100, 50, step=1, label="Top-k")
            topp    = gr.Slider(0.1, 1.0, 0.95, step=0.05, label="Top-p")
    btn.click(generate_code, inputs=[prompt, lang, max_tok, temp, topk, topp], outputs=output)
    clear.click(lambda: ("", ""), outputs=[prompt, output])

demo.launch()