sugiv commited on
Commit
0a7a112
·
1 Parent(s): 5879c43

Leetmonkey In Action. Darn LeetMonkey these days

Browse files
Files changed (1) hide show
  1. app.py +45 -31
app.py CHANGED
@@ -4,34 +4,51 @@ from llama_cpp import Llama
4
  import re
5
  from datasets import load_dataset
6
  import random
 
 
7
  import autopep8
8
- import textwrap
9
- import threading
 
 
10
 
11
  # Define the model options
12
  gguf_models = {
 
13
  "Exact Copy": "leetmonkey_peft_exact_copy.gguf",
14
  "F16": "leetmonkey_peft_f16.gguf",
15
- "Q8_0": "leetmonkey_peft__q8_0.gguf",
16
  "Super Block Q6": "leetmonkey_peft_super_block_q6.gguf"
17
  }
18
 
19
- # Global dictionary to store loaded models
20
- loaded_models = {}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
 
22
- # Function to download and load the model
23
- def load_model(model_name):
24
- model_path = hf_hub_download(repo_id="sugiv/leetmonkey-peft-gguf", filename=model_name)
25
- return Llama(model_path=model_path, n_ctx=2048, n_threads=4, n_gpu_layers=0, verbose=False)
26
-
27
- # Function to preload all models
28
- def preload_models():
29
- for name, file in gguf_models.items():
30
- loaded_models[name] = load_model(file)
31
- print("All models loaded successfully!")
32
-
33
- # Start preloading models in a separate thread
34
- threading.Thread(target=preload_models, daemon=True).start()
35
 
36
  # Generation parameters
37
  generation_kwargs = {
@@ -97,20 +114,16 @@ def extract_and_format_code(text):
97
  except:
98
  return formatted_code
99
 
100
- # Load the dataset
101
- dataset = load_dataset("sugiv/leetmonkey_python_dataset")
102
- val_dataset = dataset["train"].train_test_split(test_size=0.1)["test"]
103
-
104
  def update_problem():
105
  sample = random.choice(val_dataset)
106
  return sample['instruction']
107
 
108
- def update_solution(problem, model_name):
109
- model = loaded_models.get(model_name)
110
- if model is None:
111
- return "Model is still loading. Please wait and try again."
112
- generated_output = generate_solution(problem, model)
113
- return extract_and_format_code(generated_output)
114
 
115
  with gr.Blocks() as demo:
116
  gr.Markdown("# LeetCode Problem Solver")
@@ -121,11 +134,12 @@ with gr.Blocks() as demo:
121
  select_problem_btn = gr.Button("Select Random Problem")
122
 
123
  with gr.Column():
124
- model_dropdown = gr.Dropdown(choices=list(gguf_models.keys()), label="Select GGUF Model", value="Exact Copy")
125
  solution_display = gr.Code(label="Generated Solution", language="python", lines=25)
126
  generate_btn = gr.Button("Generate Solution")
127
 
128
  select_problem_btn.click(update_problem, outputs=problem_display)
129
- generate_btn.click(update_solution, inputs=[problem_display, model_dropdown], outputs=solution_display)
130
 
131
- demo.launch(share=True)
 
 
 
4
  import re
5
  from datasets import load_dataset
6
  import random
7
+ import logging
8
+ import os
9
  import autopep8
10
+
11
+ # Set up logging
12
+ logging.basicConfig(level=logging.INFO)
13
+ logger = logging.getLogger(__name__)
14
 
15
  # Define the model options
16
  gguf_models = {
17
+ "Q8_0": "leetmonkey_peft__q8_0.gguf",
18
  "Exact Copy": "leetmonkey_peft_exact_copy.gguf",
19
  "F16": "leetmonkey_peft_f16.gguf",
 
20
  "Super Block Q6": "leetmonkey_peft_super_block_q6.gguf"
21
  }
22
 
23
+ def download_model(model_name):
24
+ logger.info(f"Downloading model: {model_name}")
25
+ model_path = hf_hub_download(
26
+ repo_id="sugiv/leetmonkey-peft-gguf",
27
+ filename=model_name,
28
+ cache_dir="./models",
29
+ force_download=True,
30
+ resume_download=True
31
+ )
32
+ logger.info(f"Model downloaded: {model_path}")
33
+ return model_path
34
+
35
+ # Download the 8-bit model at startup
36
+ q8_model_path = download_model(gguf_models["Q8_0"])
37
+
38
+ # Load the 8-bit model
39
+ llm = Llama(
40
+ model_path=q8_model_path,
41
+ n_ctx=2048,
42
+ n_threads=4,
43
+ n_gpu_layers=0,
44
+ verbose=False
45
+ )
46
+
47
+ logger.info("8-bit model loaded successfully")
48
 
49
+ # Load the dataset
50
+ dataset = load_dataset("sugiv/leetmonkey_python_dataset")
51
+ val_dataset = dataset["train"].train_test_split(test_size=0.1)["test"]
 
 
 
 
 
 
 
 
 
 
52
 
53
  # Generation parameters
54
  generation_kwargs = {
 
114
  except:
115
  return formatted_code
116
 
 
 
 
 
117
  def update_problem():
118
  sample = random.choice(val_dataset)
119
  return sample['instruction']
120
 
121
+ def update_solution(problem):
122
+ logger.info("Generating solution")
123
+ generated_output = generate_solution(problem)
124
+ formatted_code = extract_and_format_code(generated_output)
125
+ logger.info("Solution generated successfully")
126
+ return formatted_code
127
 
128
  with gr.Blocks() as demo:
129
  gr.Markdown("# LeetCode Problem Solver")
 
134
  select_problem_btn = gr.Button("Select Random Problem")
135
 
136
  with gr.Column():
 
137
  solution_display = gr.Code(label="Generated Solution", language="python", lines=25)
138
  generate_btn = gr.Button("Generate Solution")
139
 
140
  select_problem_btn.click(update_problem, outputs=problem_display)
141
+ generate_btn.click(update_solution, inputs=[problem_display], outputs=solution_display)
142
 
143
+ if __name__ == "__main__":
144
+ logger.info("Starting Gradio interface")
145
+ demo.launch(share=True)