Spaces:
Paused
Paused
Upload folder using huggingface_hub
Browse files- README.md +13 -5
- app.py +58 -0
- requirements.txt +3 -0
README.md
CHANGED
|
@@ -1,13 +1,21 @@
|
|
| 1 |
---
|
| 2 |
-
title:
|
| 3 |
-
emoji:
|
| 4 |
colorFrom: blue
|
| 5 |
-
colorTo:
|
| 6 |
sdk: gradio
|
| 7 |
sdk_version: 6.19.0
|
| 8 |
-
python_version: '3.13'
|
| 9 |
app_file: app.py
|
| 10 |
pinned: false
|
| 11 |
---
|
| 12 |
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
---
|
| 2 |
+
title: CodeLM
|
| 3 |
+
emoji: π
|
| 4 |
colorFrom: blue
|
| 5 |
+
colorTo: green
|
| 6 |
sdk: gradio
|
| 7 |
sdk_version: 6.19.0
|
|
|
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
---
|
| 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)
|
app.py
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 3 |
+
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,
|
| 10 |
+
dtype=torch.float16,
|
| 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())
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
transformers>=4.40
|
| 2 |
+
torch>=2.0
|
| 3 |
+
accelerate>=1.0
|