Spaces:
Paused
Paused
Upload folder using huggingface_hub
Browse files
README.md
CHANGED
|
@@ -11,11 +11,11 @@ pinned: false
|
|
| 11 |
|
| 12 |
# CodeLM — Python Code Generation
|
| 13 |
|
| 14 |
-
Fine-tuned
|
| 15 |
|
| 16 |
-
**
|
| 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
|
| 15 |
-
|
|
|
|
|
|
|
| 16 |
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
|
|
|
| 26 |
|
| 27 |
-
with gr.Blocks(title="CodeLM
|
| 28 |
-
gr.Markdown(
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
""")
|
| 35 |
-
|
| 36 |
with gr.Row():
|
| 37 |
-
with gr.Column(scale=
|
| 38 |
-
|
| 39 |
-
label="What
|
| 40 |
-
placeholder="e.g.,
|
| 41 |
-
lines=3
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
)
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
code_output = gr.Code(
|
| 47 |
-
label="Generated Python Code",
|
| 48 |
language="python",
|
| 49 |
-
lines=20
|
| 50 |
)
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
|
|
|
|
|
|
| 56 |
)
|
| 57 |
|
| 58 |
-
|
|
|
|
|
|
| 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 • Fine-tune: LoRA (r=8) • Trained on Kaggle P100
|
| 72 |
+
</div>
|
| 73 |
+
"""
|
| 74 |
)
|
| 75 |
|
| 76 |
+
if __name__ == "__main__":
|
| 77 |
+
demo.launch(theme=theme)
|