File size: 2,531 Bytes
7ffa9ab
 
 
 
68062c6
7ffa9ab
ed865eb
 
 
 
 
7ffa9ab
 
 
 
 
 
 
ed865eb
 
 
 
7ffa9ab
ed865eb
 
 
 
 
 
 
 
 
 
7ffa9ab
ed865eb
 
 
 
 
 
 
7ffa9ab
ed865eb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7ffa9ab
ed865eb
 
 
7ffa9ab
ed865eb
7ffa9ab
ed865eb
 
 
 
 
 
 
7ffa9ab
 
ed865eb
 
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import gradio as gr
from transformers import AutoModelForCausalLM, AutoTokenizer
import torch

MODEL_ID = "arinbalyan/qwen-coder-python"

theme = (
    gr.themes.Soft(primary_hue="indigo", neutral_hue="slate")
    .set(button_primary_background_fill_hover="#4f46e5")
)

tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
model = AutoModelForCausalLM.from_pretrained(
    MODEL_ID,
    dtype=torch.float16,
    device_map="auto",
)

def generate(instruction: str):
    if not instruction.strip():
        return "// Describe what you want to code, then hit Generate."
    prompt = f"### Instruction:\n{instruction.strip()}\n\n### Python Code:\n"
    inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
    with torch.no_grad():
        outputs = model.generate(
            **inputs,
            max_new_tokens=256,
            temperature=0.7,
            do_sample=True,
            pad_token_id=tokenizer.eos_token_id,
        )
    text = tokenizer.decode(outputs[0], skip_special_tokens=True)
    return text.split("### Python Code:\n")[-1].strip()

with gr.Blocks(theme=theme, title="CodeLM") as demo:
    gr.Markdown(
        """
        # 🐍 CodeLM
        Fine-tuned code model that writes Python from plain English.
        """
    )
    with gr.Row():
        with gr.Column(scale=1):
            instruction = gr.Textbox(
                label="What should it code?",
                placeholder="e.g., merge two sorted lists",
                lines=3,
            )
            run = gr.Button("Generate", variant="primary", size="lg")
            gr.Examples(
                examples=[
                    "Write a function to reverse a string",
                    "Check if a number is prime",
                    "Flatten a nested list recursively",
                    "Merge two sorted lists",
                    "Compute factorial of a number",
                ],
                inputs=instruction,
                label="Try one of these",
            )
        with gr.Column(scale=1):
            code = gr.Code(
                label="Python",
                language="python",
                lines=20,
            )
    run.click(generate, inputs=instruction, outputs=code)
    gr.Markdown(
        """
        <div style="text-align:center; margin-top:1rem; color:gray; font-size:0.9rem;">
        Base: StarCoder2-1B &nbsp;•&nbsp; Fine-tune: LoRA (r=8) &nbsp;•&nbsp; Trained on Kaggle P100
        </div>
        """
    )

if __name__ == "__main__":
    demo.launch(theme=theme)