Spaces:
Running on Zero
Running on Zero
Commit ·
15fb9e9
1
Parent(s): 371e795
Live demo: 4-language LeetCode coder suite
Browse files- app.py +78 -0
- requirements.txt +6 -0
app.py
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import spaces
|
| 2 |
+
import torch
|
| 3 |
+
import gradio as gr
|
| 4 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 5 |
+
from peft import PeftModel
|
| 6 |
+
|
| 7 |
+
BASE_MODEL = "unsloth/Qwen2.5-Coder-7B-Instruct"
|
| 8 |
+
ADAPTERS = {
|
| 9 |
+
"Python": "AmareshHebbar/leetcode-python-qwen25-coder-7b",
|
| 10 |
+
"Java": "AmareshHebbar/leetcode-java-qwen25-coder-7b",
|
| 11 |
+
"C++": "AmareshHebbar/leetcode-cpp-qwen25-coder-7b",
|
| 12 |
+
"JavaScript": "AmareshHebbar/leetcode-javascript-qwen25-coder-7b",
|
| 13 |
+
}
|
| 14 |
+
|
| 15 |
+
print("Loading base model...")
|
| 16 |
+
tokenizer = AutoTokenizer.from_pretrained(BASE_MODEL)
|
| 17 |
+
base_model = AutoModelForCausalLM.from_pretrained(
|
| 18 |
+
BASE_MODEL, torch_dtype=torch.bfloat16, device_map="auto",
|
| 19 |
+
)
|
| 20 |
+
|
| 21 |
+
print("Attaching adapters...")
|
| 22 |
+
model = PeftModel.from_pretrained(base_model, ADAPTERS["Python"], adapter_name="Python")
|
| 23 |
+
for lang, repo in ADAPTERS.items():
|
| 24 |
+
if lang != "Python":
|
| 25 |
+
model.load_adapter(repo, adapter_name=lang)
|
| 26 |
+
model.eval()
|
| 27 |
+
|
| 28 |
+
EXAMPLES = [
|
| 29 |
+
["Merge k sorted linked lists into one sorted list.", "Divide and conquer / heap", "Python"],
|
| 30 |
+
["Given a set of non-overlapping intervals, insert a new interval and merge as needed.", "Sorting / interval merge", "JavaScript"],
|
| 31 |
+
["Find the length of the longest increasing path in a matrix.", "DFS + memoization", "Java"],
|
| 32 |
+
]
|
| 33 |
+
|
| 34 |
+
@spaces.GPU(duration=60)
|
| 35 |
+
def solve(problem: str, algorithm_tag: str, language: str):
|
| 36 |
+
model.set_adapter(language)
|
| 37 |
+
lang_name = "C++" if language == "C++" else language
|
| 38 |
+
system_prompt = (
|
| 39 |
+
f"You are an expert {lang_name} competitive programmer. Given a "
|
| 40 |
+
f"LeetCode-style problem statement and an algorithm tag, write a "
|
| 41 |
+
f"correct, efficient {lang_name} solution."
|
| 42 |
+
)
|
| 43 |
+
user_msg = f"Problem: {problem}"
|
| 44 |
+
if algorithm_tag.strip():
|
| 45 |
+
user_msg += f"\nAlgorithm: {algorithm_tag}"
|
| 46 |
+
|
| 47 |
+
messages = [{"role": "system", "content": system_prompt}, {"role": "user", "content": user_msg}]
|
| 48 |
+
prompt = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
|
| 49 |
+
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
| 50 |
+
|
| 51 |
+
outputs = model.generate(
|
| 52 |
+
**inputs, max_new_tokens=512, temperature=0.2, do_sample=True,
|
| 53 |
+
pad_token_id=tokenizer.eos_token_id,
|
| 54 |
+
)
|
| 55 |
+
return tokenizer.decode(outputs[0][inputs["input_ids"].shape[1]:], skip_special_tokens=True)
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
with gr.Blocks(title="LeetCode Multi-Language Coder Suite") as demo:
|
| 59 |
+
gr.Markdown(
|
| 60 |
+
"# LeetCode Multi-Language Coder Suite\n"
|
| 61 |
+
"Qwen2.5-Coder-7B, QDoRA fine-tuned per language on execution-verified "
|
| 62 |
+
"LeetCode solutions. [Models & benchmarks](https://huggingface.co/collections/AmareshHebbar/leetcode-multi-language-coder-suite) · "
|
| 63 |
+
"[GitHub](https://github.com/amareshhebbar)"
|
| 64 |
+
)
|
| 65 |
+
with gr.Row():
|
| 66 |
+
with gr.Column():
|
| 67 |
+
problem = gr.Textbox(label="Problem statement", lines=4,
|
| 68 |
+
placeholder="Given an array of integers nums and an integer target...")
|
| 69 |
+
tag = gr.Textbox(label="Algorithm tag (optional)", placeholder="Hash Map")
|
| 70 |
+
language = gr.Dropdown(list(ADAPTERS.keys()), value="Python", label="Language")
|
| 71 |
+
run = gr.Button("Generate solution", variant="primary")
|
| 72 |
+
with gr.Column():
|
| 73 |
+
output = gr.Code(label="Generated solution", language="python")
|
| 74 |
+
|
| 75 |
+
gr.Examples(examples=EXAMPLES, inputs=[problem, tag, language])
|
| 76 |
+
run.click(solve, inputs=[problem, tag, language], outputs=output)
|
| 77 |
+
|
| 78 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
transformers
|
| 2 |
+
peft
|
| 3 |
+
accelerate
|
| 4 |
+
torch
|
| 5 |
+
spaces
|
| 6 |
+
gradio
|