devoppro commited on
Commit
7900fe8
·
verified ·
1 Parent(s): b2b9f33

Create deploy/app.py

Browse files
Files changed (1) hide show
  1. deploy/app.py +49 -0
deploy/app.py ADDED
@@ -0,0 +1,49 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch, gradio as gr
2
+ from pathlib import Path
3
+ import sys
4
+ sys.path.insert(0, str(Path(__file__).parent.parent))
5
+ from model.architecture import CodeLLM, CodeLLMConfig
6
+ from model.tokenizer import get_gpt2_tokenizer_for_code
7
+
8
+ DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
9
+ config = CodeLLMConfig()
10
+ model = CodeLLM(config)
11
+
12
+ WEIGHTS_PATH = Path("./checkpoints/final/pytorch_model.bin")
13
+ if WEIGHTS_PATH.exists():
14
+ model.load_state_dict(torch.load(WEIGHTS_PATH, map_location=DEVICE))
15
+ print("Loaded trained weights!")
16
+
17
+ model.to(DEVICE).eval()
18
+ tokenizer = get_gpt2_tokenizer_for_code()
19
+
20
+ def generate_code(prompt, language="Python", max_new_tokens=256, temperature=0.8, top_k=50, top_p=0.95):
21
+ lang_map = {"Python":"<|python|>","JavaScript":"<|javascript|>",
22
+ "TypeScript":"<|typescript|>","Rust":"<|rust|>","Go":"<|go|>","C++":"<|cpp|>"}
23
+ full_prompt = f"{lang_map.get(language,'')}{prompt}"
24
+ input_ids = tokenizer.encode(full_prompt, return_tensors="pt").to(DEVICE)
25
+ with torch.no_grad():
26
+ out = model.generate(input_ids, max_new_tokens=max_new_tokens,
27
+ temperature=temperature, top_k=top_k, top_p=top_p)
28
+ return tokenizer.decode(out[0][input_ids.shape[1]:], skip_special_tokens=True)
29
+
30
+ with gr.Blocks(title="CodeLLM", theme=gr.themes.Soft()) as demo:
31
+ gr.Markdown("# CodeLLM — Custom Coding AI\n125M param transformer built from scratch")
32
+ with gr.Row():
33
+ with gr.Column():
34
+ prompt = gr.Textbox(label="Code prompt", lines=5, placeholder="def fibonacci(n):")
35
+ lang = gr.Dropdown(["Python","JavaScript","TypeScript","Rust","Go","C++"], value="Python", label="Language")
36
+ with gr.Row():
37
+ btn = gr.Button("Generate ⚡", variant="primary")
38
+ clear = gr.Button("Clear")
39
+ output = gr.Code(label="Output", language="python", lines=20)
40
+ with gr.Accordion("Settings", open=False):
41
+ with gr.Row():
42
+ max_tok = gr.Slider(32, 512, 256, step=32, label="Max tokens")
43
+ temp = gr.Slider(0.1, 2.0, 0.8, step=0.1, label="Temperature")
44
+ topk = gr.Slider(1, 100, 50, step=1, label="Top-k")
45
+ topp = gr.Slider(0.1, 1.0, 0.95, step=0.05, label="Top-p")
46
+ btn.click(generate_code, inputs=[prompt, lang, max_tok, temp, topk, topp], outputs=output)
47
+ clear.click(lambda: ("", ""), outputs=[prompt, output])
48
+
49
+ demo.launch()