Leetmonkey In Action via Inference
Browse files
app.py
CHANGED
@@ -13,18 +13,14 @@ import textwrap
|
|
13 |
logging.basicConfig(level=logging.INFO)
|
14 |
logger = logging.getLogger(__name__)
|
15 |
|
16 |
-
#
|
17 |
-
|
18 |
-
|
19 |
-
"Exact Copy": "leetmonkey_peft_exact_copy.gguf",
|
20 |
-
"F16": "leetmonkey_peft_f16.gguf",
|
21 |
-
"Super Block Q6": "leetmonkey_peft_super_block_q6.gguf"
|
22 |
-
}
|
23 |
|
24 |
def download_model(model_name):
|
25 |
logger.info(f"Downloading model: {model_name}")
|
26 |
model_path = hf_hub_download(
|
27 |
-
repo_id=
|
28 |
filename=model_name,
|
29 |
cache_dir="./models",
|
30 |
force_download=True,
|
@@ -34,12 +30,12 @@ def download_model(model_name):
|
|
34 |
return model_path
|
35 |
|
36 |
# Download and load the 8-bit model at startup
|
37 |
-
|
38 |
llm = Llama(
|
39 |
-
model_path=
|
40 |
n_ctx=2048,
|
41 |
n_threads=4,
|
42 |
-
n_gpu_layers
|
43 |
verbose=False
|
44 |
)
|
45 |
logger.info("8-bit model loaded successfully")
|
@@ -59,7 +55,7 @@ generation_kwargs = {
|
|
59 |
"repeat_penalty": 1.1
|
60 |
}
|
61 |
|
62 |
-
def generate_solution(instruction
|
63 |
system_prompt = "You are a Python coding assistant specialized in solving LeetCode problems. Provide only the complete implementation of the given function. Ensure proper indentation and formatting. Do not include any explanations or multiple solutions."
|
64 |
full_prompt = f"""### Instruction:
|
65 |
{system_prompt}
|
@@ -74,8 +70,8 @@ Here's the complete Python function implementation:
|
|
74 |
```python
|
75 |
"""
|
76 |
|
77 |
-
|
78 |
-
|
79 |
|
80 |
def extract_and_format_code(text):
|
81 |
# Extract code between triple backticks
|
@@ -115,44 +111,10 @@ def extract_and_format_code(text):
|
|
115 |
def select_random_problem():
|
116 |
return random.choice(train_dataset)['instruction']
|
117 |
|
118 |
-
def
|
119 |
-
|
120 |
-
model = llm
|
121 |
-
else:
|
122 |
-
model_path = download_model(gguf_models[model_name])
|
123 |
-
model = Llama(model_path=model_path, n_ctx=2048, n_threads=4, n_gpu_layers=0, verbose=False)
|
124 |
-
|
125 |
-
logger.info(f"Generating solution using {model_name} model")
|
126 |
-
generated_output = generate_solution(problem, model)
|
127 |
-
formatted_code = extract_and_format_code(generated_output)
|
128 |
-
logger.info("Solution generated successfully")
|
129 |
-
return formatted_code
|
130 |
-
|
131 |
-
def stream_solution(problem, model_name):
|
132 |
-
if model_name == "Q8_0 (8-bit)":
|
133 |
-
model = llm
|
134 |
-
else:
|
135 |
-
model_path = download_model(gguf_models[model_name])
|
136 |
-
model = Llama(model_path=model_path, n_ctx=2048, n_threads=4, n_gpu_layers=0, verbose=False)
|
137 |
-
|
138 |
-
logger.info(f"Generating solution using {model_name} model")
|
139 |
-
system_prompt = "You are a Python coding assistant specialized in solving LeetCode problems. Provide only the complete implementation of the given function. Ensure proper indentation and formatting. Do not include any explanations or multiple solutions."
|
140 |
-
full_prompt = f"""### Instruction:
|
141 |
-
{system_prompt}
|
142 |
-
|
143 |
-
Implement the following function for the LeetCode problem:
|
144 |
-
|
145 |
-
{problem}
|
146 |
-
|
147 |
-
### Response:
|
148 |
-
Here's the complete Python function implementation:
|
149 |
-
|
150 |
-
```python
|
151 |
-
"""
|
152 |
-
|
153 |
generated_text = ""
|
154 |
-
for
|
155 |
-
token = chunk["choices"][0]["text"]
|
156 |
generated_text += token
|
157 |
yield generated_text
|
158 |
|
@@ -161,7 +123,7 @@ Here's the complete Python function implementation:
|
|
161 |
yield formatted_code
|
162 |
|
163 |
with gr.Blocks() as demo:
|
164 |
-
gr.Markdown("# LeetCode Problem Solver")
|
165 |
|
166 |
with gr.Row():
|
167 |
with gr.Column():
|
@@ -169,12 +131,11 @@ with gr.Blocks() as demo:
|
|
169 |
select_problem_btn = gr.Button("Select Random Problem")
|
170 |
|
171 |
with gr.Column():
|
172 |
-
model_dropdown = gr.Dropdown(choices=list(gguf_models.keys()), label="Select GGUF Model", value="Q8_0 (8-bit)")
|
173 |
solution_display = gr.Code(label="Generated Solution", language="python", lines=25)
|
174 |
generate_btn = gr.Button("Generate Solution")
|
175 |
|
176 |
select_problem_btn.click(select_random_problem, outputs=problem_display)
|
177 |
-
generate_btn.click(stream_solution, inputs=[problem_display
|
178 |
|
179 |
if __name__ == "__main__":
|
180 |
logger.info("Starting Gradio interface")
|
|
|
13 |
logging.basicConfig(level=logging.INFO)
|
14 |
logger = logging.getLogger(__name__)
|
15 |
|
16 |
+
# Model settings
|
17 |
+
MODEL_NAME = "leetmonkey_peft__q8_0.gguf"
|
18 |
+
REPO_ID = "sugiv/leetmonkey-peft-gguf"
|
|
|
|
|
|
|
|
|
19 |
|
20 |
def download_model(model_name):
|
21 |
logger.info(f"Downloading model: {model_name}")
|
22 |
model_path = hf_hub_download(
|
23 |
+
repo_id=REPO_ID,
|
24 |
filename=model_name,
|
25 |
cache_dir="./models",
|
26 |
force_download=True,
|
|
|
30 |
return model_path
|
31 |
|
32 |
# Download and load the 8-bit model at startup
|
33 |
+
model_path = download_model(MODEL_NAME)
|
34 |
llm = Llama(
|
35 |
+
model_path=model_path,
|
36 |
n_ctx=2048,
|
37 |
n_threads=4,
|
38 |
+
n_gpu_layers=-1, # Use all available GPU layers
|
39 |
verbose=False
|
40 |
)
|
41 |
logger.info("8-bit model loaded successfully")
|
|
|
55 |
"repeat_penalty": 1.1
|
56 |
}
|
57 |
|
58 |
+
def generate_solution(instruction):
|
59 |
system_prompt = "You are a Python coding assistant specialized in solving LeetCode problems. Provide only the complete implementation of the given function. Ensure proper indentation and formatting. Do not include any explanations or multiple solutions."
|
60 |
full_prompt = f"""### Instruction:
|
61 |
{system_prompt}
|
|
|
70 |
```python
|
71 |
"""
|
72 |
|
73 |
+
for chunk in llm(full_prompt, stream=True, **generation_kwargs):
|
74 |
+
yield chunk["choices"][0]["text"]
|
75 |
|
76 |
def extract_and_format_code(text):
|
77 |
# Extract code between triple backticks
|
|
|
111 |
def select_random_problem():
|
112 |
return random.choice(train_dataset)['instruction']
|
113 |
|
114 |
+
def stream_solution(problem):
|
115 |
+
logger.info("Generating solution")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
116 |
generated_text = ""
|
117 |
+
for token in generate_solution(problem):
|
|
|
118 |
generated_text += token
|
119 |
yield generated_text
|
120 |
|
|
|
123 |
yield formatted_code
|
124 |
|
125 |
with gr.Blocks() as demo:
|
126 |
+
gr.Markdown("# LeetCode Problem Solver (8-bit GGUF Model)")
|
127 |
|
128 |
with gr.Row():
|
129 |
with gr.Column():
|
|
|
131 |
select_problem_btn = gr.Button("Select Random Problem")
|
132 |
|
133 |
with gr.Column():
|
|
|
134 |
solution_display = gr.Code(label="Generated Solution", language="python", lines=25)
|
135 |
generate_btn = gr.Button("Generate Solution")
|
136 |
|
137 |
select_problem_btn.click(select_random_problem, outputs=problem_display)
|
138 |
+
generate_btn.click(stream_solution, inputs=[problem_display], outputs=solution_display)
|
139 |
|
140 |
if __name__ == "__main__":
|
141 |
logger.info("Starting Gradio interface")
|