Spaces:
Running on Zero
Running on Zero
Redesign demo UI
Browse files
app.py
CHANGED
|
@@ -5,36 +5,125 @@ from transformers import AutoModelForCausalLM, AutoTokenizer
|
|
| 5 |
from peft import PeftModel
|
| 6 |
|
| 7 |
BASE_MODEL = "unsloth/Qwen2.5-Coder-7B-Instruct"
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
"
|
| 11 |
-
"
|
| 12 |
-
"
|
|
|
|
| 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 |
-
|
| 22 |
-
|
| 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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
model.set_adapter(language)
|
| 37 |
-
lang_name =
|
| 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 "
|
|
@@ -52,27 +141,71 @@ def solve(problem: str, algorithm_tag: str, language: str):
|
|
| 52 |
**inputs, max_new_tokens=512, temperature=0.2, do_sample=True,
|
| 53 |
pad_token_id=tokenizer.eos_token_id,
|
| 54 |
)
|
| 55 |
-
|
|
|
|
| 56 |
|
| 57 |
|
| 58 |
-
|
| 59 |
-
gr.
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
tag = gr.Textbox(label="Algorithm tag (optional)", placeholder="Hash Map")
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
|
| 75 |
-
|
| 76 |
-
run.click(solve, inputs=[problem, tag, language], outputs=output)
|
| 77 |
|
| 78 |
demo.launch()
|
|
|
|
| 5 |
from peft import PeftModel
|
| 6 |
|
| 7 |
BASE_MODEL = "unsloth/Qwen2.5-Coder-7B-Instruct"
|
| 8 |
+
|
| 9 |
+
LANG_META = {
|
| 10 |
+
"Python": {"repo": "AmareshHebbar/leetcode-python-qwen25-coder-7b", "icon": "π", "code_lang": "python"},
|
| 11 |
+
"Java": {"repo": "AmareshHebbar/leetcode-java-qwen25-coder-7b", "icon": "β", "code_lang": "java"},
|
| 12 |
+
"C++": {"repo": "AmareshHebbar/leetcode-cpp-qwen25-coder-7b", "icon": "βοΈ", "code_lang": "cpp"},
|
| 13 |
+
"JavaScript": {"repo": "AmareshHebbar/leetcode-javascript-qwen25-coder-7b", "icon": "π¨", "code_lang": "javascript"},
|
| 14 |
}
|
| 15 |
|
|
|
|
| 16 |
tokenizer = AutoTokenizer.from_pretrained(BASE_MODEL)
|
| 17 |
+
base_model = AutoModelForCausalLM.from_pretrained(BASE_MODEL, torch_dtype=torch.bfloat16)
|
|
|
|
|
|
|
| 18 |
|
| 19 |
+
model = PeftModel.from_pretrained(base_model, LANG_META["Python"]["repo"], adapter_name="Python")
|
| 20 |
+
for lang, meta in LANG_META.items():
|
|
|
|
| 21 |
if lang != "Python":
|
| 22 |
+
model.load_adapter(meta["repo"], adapter_name=lang)
|
| 23 |
model.eval()
|
| 24 |
|
| 25 |
+
_on_gpu = False
|
| 26 |
+
|
| 27 |
EXAMPLES = [
|
| 28 |
["Merge k sorted linked lists into one sorted list.", "Divide and conquer / heap", "Python"],
|
| 29 |
["Given a set of non-overlapping intervals, insert a new interval and merge as needed.", "Sorting / interval merge", "JavaScript"],
|
| 30 |
["Find the length of the longest increasing path in a matrix.", "DFS + memoization", "Java"],
|
| 31 |
+
["Given the root of a binary tree, return the maximum path sum between any two nodes.", "Tree DFS / post-order", "C++"],
|
| 32 |
]
|
| 33 |
|
| 34 |
+
CSS = """
|
| 35 |
+
:root {
|
| 36 |
+
--lc-bg: #0b0f14;
|
| 37 |
+
--lc-panel: #121820;
|
| 38 |
+
--lc-border: #1f2833;
|
| 39 |
+
--lc-accent: #34d399;
|
| 40 |
+
--lc-accent-dim: #34d39933;
|
| 41 |
+
--lc-text: #e6edf3;
|
| 42 |
+
--lc-text-dim: #8b98a5;
|
| 43 |
+
}
|
| 44 |
+
.gradio-container {
|
| 45 |
+
background: var(--lc-bg) !important;
|
| 46 |
+
font-family: 'Inter', -apple-system, sans-serif !important;
|
| 47 |
+
}
|
| 48 |
+
#lc-header {
|
| 49 |
+
text-align: center;
|
| 50 |
+
padding: 28px 0 8px 0;
|
| 51 |
+
}
|
| 52 |
+
#lc-header h1 {
|
| 53 |
+
font-size: 2.1rem;
|
| 54 |
+
font-weight: 700;
|
| 55 |
+
background: linear-gradient(90deg, #34d399, #60a5fa);
|
| 56 |
+
-webkit-background-clip: text;
|
| 57 |
+
-webkit-text-fill-color: transparent;
|
| 58 |
+
margin-bottom: 4px;
|
| 59 |
+
}
|
| 60 |
+
#lc-header p {
|
| 61 |
+
color: var(--lc-text-dim);
|
| 62 |
+
font-size: 0.95rem;
|
| 63 |
+
}
|
| 64 |
+
#lc-badges {
|
| 65 |
+
display: flex;
|
| 66 |
+
justify-content: center;
|
| 67 |
+
gap: 8px;
|
| 68 |
+
margin-top: 10px;
|
| 69 |
+
flex-wrap: wrap;
|
| 70 |
+
}
|
| 71 |
+
.lc-badge {
|
| 72 |
+
background: var(--lc-panel);
|
| 73 |
+
border: 1px solid var(--lc-border);
|
| 74 |
+
color: var(--lc-text-dim);
|
| 75 |
+
padding: 4px 12px;
|
| 76 |
+
border-radius: 999px;
|
| 77 |
+
font-size: 0.78rem;
|
| 78 |
+
}
|
| 79 |
+
#lc-panel-left, #lc-panel-right {
|
| 80 |
+
background: var(--lc-panel) !important;
|
| 81 |
+
border: 1px solid var(--lc-border) !important;
|
| 82 |
+
border-radius: 14px !important;
|
| 83 |
+
padding: 18px !important;
|
| 84 |
+
}
|
| 85 |
+
#lc-generate {
|
| 86 |
+
background: linear-gradient(90deg, #34d399, #22c55e) !important;
|
| 87 |
+
border: none !important;
|
| 88 |
+
color: #04120a !important;
|
| 89 |
+
font-weight: 600 !important;
|
| 90 |
+
border-radius: 10px !important;
|
| 91 |
+
}
|
| 92 |
+
#lc-lang-radio label {
|
| 93 |
+
border-radius: 10px !important;
|
| 94 |
+
}
|
| 95 |
+
#lc-output-code {
|
| 96 |
+
border-radius: 10px !important;
|
| 97 |
+
}
|
| 98 |
+
footer { display: none !important; }
|
| 99 |
+
"""
|
| 100 |
+
|
| 101 |
+
THEME = gr.themes.Base(
|
| 102 |
+
primary_hue="emerald",
|
| 103 |
+
neutral_hue="slate",
|
| 104 |
+
font=[gr.themes.GoogleFont("Inter"), "sans-serif"],
|
| 105 |
+
).set(
|
| 106 |
+
body_background_fill="#0b0f14",
|
| 107 |
+
block_background_fill="#121820",
|
| 108 |
+
block_border_color="#1f2833",
|
| 109 |
+
body_text_color="#e6edf3",
|
| 110 |
+
input_background_fill="#0b0f14",
|
| 111 |
+
button_primary_background_fill="#34d399",
|
| 112 |
+
button_primary_text_color="#04120a",
|
| 113 |
+
)
|
| 114 |
+
|
| 115 |
+
|
| 116 |
@spaces.GPU(duration=60)
|
| 117 |
+
def solve(problem, algorithm_tag, language):
|
| 118 |
+
global _on_gpu
|
| 119 |
+
if not _on_gpu:
|
| 120 |
+
model.to("cuda")
|
| 121 |
+
_on_gpu = True
|
| 122 |
+
if not problem.strip():
|
| 123 |
+
return "", "Enter a problem statement first."
|
| 124 |
+
|
| 125 |
model.set_adapter(language)
|
| 126 |
+
lang_name = language
|
| 127 |
system_prompt = (
|
| 128 |
f"You are an expert {lang_name} competitive programmer. Given a "
|
| 129 |
f"LeetCode-style problem statement and an algorithm tag, write a "
|
|
|
|
| 141 |
**inputs, max_new_tokens=512, temperature=0.2, do_sample=True,
|
| 142 |
pad_token_id=tokenizer.eos_token_id,
|
| 143 |
)
|
| 144 |
+
code = tokenizer.decode(outputs[0][inputs["input_ids"].shape[1]:], skip_special_tokens=True)
|
| 145 |
+
return code, f"{LANG_META[language]['icon']} generated with the {language} QDoRA adapter"
|
| 146 |
|
| 147 |
|
| 148 |
+
def on_lang_change(language):
|
| 149 |
+
return gr.Code(language=LANG_META[language]["code_lang"])
|
| 150 |
+
|
| 151 |
+
|
| 152 |
+
with gr.Blocks(css=CSS, theme=THEME, title="LeetCode Multi-Language Coder Suite") as demo:
|
| 153 |
+
with gr.Column(elem_id="lc-header"):
|
| 154 |
+
gr.Markdown("# LeetCode Multi-Language Coder Suite")
|
| 155 |
+
gr.Markdown("Qwen2.5-Coder-7B Β· QDoRA fine-tuned per language Β· execution-verified training data")
|
| 156 |
+
gr.HTML(
|
| 157 |
+
"""
|
| 158 |
+
<div id="lc-badges">
|
| 159 |
+
<span class="lc-badge">π Python</span>
|
| 160 |
+
<span class="lc-badge">β Java</span>
|
| 161 |
+
<span class="lc-badge">βοΈ C++</span>
|
| 162 |
+
<span class="lc-badge">π¨ JavaScript</span>
|
| 163 |
+
<span class="lc-badge">π 4 QDoRA adapters, 1 base model</span>
|
| 164 |
+
</div>
|
| 165 |
+
"""
|
| 166 |
+
)
|
| 167 |
+
|
| 168 |
+
with gr.Row(equal_height=True):
|
| 169 |
+
with gr.Column(scale=5, elem_id="lc-panel-left"):
|
| 170 |
+
language = gr.Radio(
|
| 171 |
+
choices=list(LANG_META.keys()),
|
| 172 |
+
value="Python",
|
| 173 |
+
label="Language",
|
| 174 |
+
elem_id="lc-lang-radio",
|
| 175 |
+
)
|
| 176 |
+
problem = gr.Textbox(
|
| 177 |
+
label="Problem statement",
|
| 178 |
+
lines=5,
|
| 179 |
+
placeholder="Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.",
|
| 180 |
+
)
|
| 181 |
tag = gr.Textbox(label="Algorithm tag (optional)", placeholder="Hash Map")
|
| 182 |
+
run = gr.Button("Generate solution", elem_id="lc-generate", size="lg")
|
| 183 |
+
gr.Examples(
|
| 184 |
+
examples=EXAMPLES,
|
| 185 |
+
inputs=[problem, tag, language],
|
| 186 |
+
label="Try an example",
|
| 187 |
+
)
|
| 188 |
+
|
| 189 |
+
with gr.Column(scale=6, elem_id="lc-panel-right"):
|
| 190 |
+
status = gr.Markdown("")
|
| 191 |
+
output = gr.Code(
|
| 192 |
+
label="Generated solution",
|
| 193 |
+
language="python",
|
| 194 |
+
elem_id="lc-output-code",
|
| 195 |
+
lines=22,
|
| 196 |
+
)
|
| 197 |
+
|
| 198 |
+
gr.HTML(
|
| 199 |
+
"""
|
| 200 |
+
<div style="text-align:center; color:#8b98a5; font-size:0.82rem; margin-top:18px;">
|
| 201 |
+
<a href="https://huggingface.co/collections/AmareshHebbar/leetcode-multi-language-coder-suite" style="color:#34d399;">Models & benchmarks</a>
|
| 202 |
+
Β·
|
| 203 |
+
<a href="https://github.com/amareshhebbar" style="color:#34d399;">GitHub</a>
|
| 204 |
+
</div>
|
| 205 |
+
"""
|
| 206 |
+
)
|
| 207 |
|
| 208 |
+
language.change(on_lang_change, inputs=language, outputs=output)
|
| 209 |
+
run.click(solve, inputs=[problem, tag, language], outputs=[output, status])
|
| 210 |
|
| 211 |
demo.launch()
|