arinbalyan commited on
Commit
ed865eb
·
verified ·
1 Parent(s): 7ffa9ab

Upload folder using huggingface_hub

Browse files
Files changed (2) hide show
  1. README.md +3 -3
  2. app.py +56 -37
README.md CHANGED
@@ -11,11 +11,11 @@ pinned: false
11
 
12
  # CodeLM — Python Code Generation
13
 
14
- Fine-tuned StarCoder2-1B on Python code instructions.
15
 
16
- **Base:** bigcode/starcoder2-1B | **Fine-tune:** LoRA (r=8)
17
 
18
  ## Links
19
  - **Trained model:** [arinbalyan/starcoder2-code-lm](https://huggingface.co/arinbalyan/starcoder2-code-lm)
20
- - **Training notebook:** [Kaggle](https://www.kaggle.com/code/arinbalyan/fine-tune-starcoder2-python-code)
21
  - **HF Profile:** [arinbalyan](https://huggingface.co/arinbalyan)
 
11
 
12
  # CodeLM — Python Code Generation
13
 
14
+ Fine-tuned Qwen2.5-Coder-1.5B on Python code instructions.
15
 
16
+ **Model:** Qwen/Qwen2.5-Coder-1.5B-Instruct | **Fine-tune:** LoRA (r=8)
17
 
18
  ## Links
19
  - **Trained model:** [arinbalyan/starcoder2-code-lm](https://huggingface.co/arinbalyan/starcoder2-code-lm)
20
+ - **Training notebook:** [Kaggle](https://www.kaggle.com/code/arinbalyan/fine-tune-starcoder2-1b-for-python-code)
21
  - **HF Profile:** [arinbalyan](https://huggingface.co/arinbalyan)
app.py CHANGED
@@ -4,6 +4,11 @@ import torch
4
 
5
  MODEL_ID = "arinbalyan/starcoder-code-lm"
6
 
 
 
 
 
 
7
  tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
8
  model = AutoModelForCausalLM.from_pretrained(
9
  MODEL_ID,
@@ -11,48 +16,62 @@ model = AutoModelForCausalLM.from_pretrained(
11
  device_map="auto",
12
  )
13
 
14
- def generate_code(instruction):
15
- prompt = f"### Instruction:\n{instruction}\n\n### Python Code:\n"
 
 
16
  inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
17
- outputs = model.generate(
18
- **inputs,
19
- max_new_tokens=256,
20
- temperature=0.7,
21
- do_sample=True,
22
- pad_token_id=tokenizer.eos_token_id,
23
- )
24
- response = tokenizer.decode(outputs[0], skip_special_tokens=True)
25
- return response.split("### Python Code:\n")[-1].strip()
 
26
 
27
- with gr.Blocks(title="CodeLM - Python Code Generation") as demo:
28
- gr.Markdown("""
29
- # CodeLM - Python Code Generation
30
-
31
- Fine-tuned StarCoder2-1B with LoRA on Python code instructions.
32
-
33
- Enter a description of what you want to code, and the model will generate Python code.
34
- """)
35
-
36
  with gr.Row():
37
- with gr.Column(scale=2):
38
- instruction_input = gr.Textbox(
39
- label="What do you want to code?",
40
- placeholder="e.g., Write a function to check if a string is a palindrome",
41
- lines=3
 
 
 
 
 
 
 
 
 
 
 
 
42
  )
43
- generate_btn = gr.Button("Generate Code", variant="primary")
44
-
45
- with gr.Column(scale=3):
46
- code_output = gr.Code(
47
- label="Generated Python Code",
48
  language="python",
49
- lines=20
50
  )
51
-
52
- generate_btn.click(
53
- fn=generate_code,
54
- inputs=instruction_input,
55
- outputs=code_output
 
 
56
  )
57
 
58
- demo.launch(theme=gr.themes.Soft())
 
 
4
 
5
  MODEL_ID = "arinbalyan/starcoder-code-lm"
6
 
7
+ theme = (
8
+ gr.themes.Soft(primary_hue="indigo", neutral_hue="slate")
9
+ .set(button_primary_background_fill_hover="#4f46e5")
10
+ )
11
+
12
  tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
13
  model = AutoModelForCausalLM.from_pretrained(
14
  MODEL_ID,
 
16
  device_map="auto",
17
  )
18
 
19
+ def generate(instruction: str):
20
+ if not instruction.strip():
21
+ return "// Describe what you want to code, then hit Generate."
22
+ prompt = f"### Instruction:\n{instruction.strip()}\n\n### Python Code:\n"
23
  inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
24
+ with torch.no_grad():
25
+ outputs = model.generate(
26
+ **inputs,
27
+ max_new_tokens=256,
28
+ temperature=0.7,
29
+ do_sample=True,
30
+ pad_token_id=tokenizer.eos_token_id,
31
+ )
32
+ text = tokenizer.decode(outputs[0], skip_special_tokens=True)
33
+ return text.split("### Python Code:\n")[-1].strip()
34
 
35
+ with gr.Blocks(theme=theme, title="CodeLM") as demo:
36
+ gr.Markdown(
37
+ """
38
+ # 🐍 CodeLM
39
+ Fine-tuned code model that writes Python from plain English.
40
+ """
41
+ )
 
 
42
  with gr.Row():
43
+ with gr.Column(scale=1):
44
+ instruction = gr.Textbox(
45
+ label="What should it code?",
46
+ placeholder="e.g., merge two sorted lists",
47
+ lines=3,
48
+ )
49
+ run = gr.Button("Generate", variant="primary", size="lg")
50
+ gr.Examples(
51
+ examples=[
52
+ "Write a function to reverse a string",
53
+ "Check if a number is prime",
54
+ "Flatten a nested list recursively",
55
+ "Merge two sorted lists",
56
+ "Compute factorial of a number",
57
+ ],
58
+ inputs=instruction,
59
+ label="Try one of these",
60
  )
61
+ with gr.Column(scale=1):
62
+ code = gr.Code(
63
+ label="Python",
 
 
64
  language="python",
65
+ lines=20,
66
  )
67
+ run.click(generate, inputs=instruction, outputs=code)
68
+ gr.Markdown(
69
+ """
70
+ <div style="text-align:center; margin-top:1rem; color:gray; font-size:0.9rem;">
71
+ Base: StarCoder2-1B &nbsp;•&nbsp; Fine-tune: LoRA (r=8) &nbsp;•&nbsp; Trained on Kaggle P100
72
+ </div>
73
+ """
74
  )
75
 
76
+ if __name__ == "__main__":
77
+ demo.launch(theme=theme)