Gosh this Leetmonkey
Browse files
app.py
CHANGED
@@ -1,14 +1,15 @@
|
|
1 |
-
import os
|
2 |
-
import re
|
3 |
-
import logging
|
4 |
-
import textwrap
|
5 |
-
import autopep8
|
6 |
import gradio as gr
|
7 |
from huggingface_hub import hf_hub_download
|
8 |
from llama_cpp import Llama
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
import jwt
|
10 |
from typing import Dict, Any
|
11 |
-
import datetime
|
12 |
|
13 |
# Set up logging
|
14 |
logging.basicConfig(level=logging.INFO)
|
@@ -24,16 +25,9 @@ JWT_ALGORITHM = "HS256"
|
|
24 |
MODEL_NAME = "leetmonkey_peft__q8_0.gguf"
|
25 |
REPO_ID = "sugiv/leetmonkey-peft-gguf"
|
26 |
|
27 |
-
#
|
28 |
-
|
29 |
-
|
30 |
-
"stop": ["```", "### Instruction:", "### Response:"],
|
31 |
-
"echo": False,
|
32 |
-
"temperature": 0.05,
|
33 |
-
"top_k": 10,
|
34 |
-
"top_p": 0.9,
|
35 |
-
"repeat_penalty": 1.1
|
36 |
-
}
|
37 |
|
38 |
def download_model(model_name):
|
39 |
logger.info(f"Downloading model: {model_name}")
|
@@ -60,7 +54,18 @@ llm = Llama(
|
|
60 |
)
|
61 |
logger.info("8-bit model loaded successfully")
|
62 |
|
63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
64 |
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."
|
65 |
full_prompt = f"""### Instruction:
|
66 |
{system_prompt}
|
@@ -75,27 +80,32 @@ Here's the complete Python function implementation:
|
|
75 |
```python
|
76 |
"""
|
77 |
|
78 |
-
|
79 |
-
|
80 |
|
81 |
-
def extract_and_format_code(text
|
|
|
82 |
code_match = re.search(r'```python\s*(.*?)\s*```', text, re.DOTALL)
|
83 |
if code_match:
|
84 |
code = code_match.group(1)
|
85 |
else:
|
86 |
code = text
|
87 |
|
|
|
88 |
code = textwrap.dedent(code)
|
|
|
|
|
89 |
lines = code.split('\n')
|
90 |
|
|
|
91 |
indented_lines = []
|
92 |
for line in lines:
|
93 |
if line.strip().startswith('class') or line.strip().startswith('def'):
|
94 |
-
indented_lines.append(line)
|
95 |
-
elif line.strip():
|
96 |
-
indented_lines.append(' ' + line)
|
97 |
else:
|
98 |
-
indented_lines.append(line)
|
99 |
|
100 |
formatted_code = '\n'.join(indented_lines)
|
101 |
|
@@ -104,6 +114,9 @@ def extract_and_format_code(text: str) -> str:
|
|
104 |
except:
|
105 |
return formatted_code
|
106 |
|
|
|
|
|
|
|
107 |
def verify_token(token: str) -> bool:
|
108 |
try:
|
109 |
jwt.decode(token, JWT_SECRET, algorithms=[JWT_ALGORITHM])
|
@@ -111,62 +124,67 @@ def verify_token(token: str) -> bool:
|
|
111 |
except jwt.PyJWTError:
|
112 |
return False
|
113 |
|
|
|
|
|
114 |
def api_generate_solution(instruction: str, token: str) -> Dict[str, Any]:
|
115 |
if not verify_token(token):
|
116 |
return {"error": "Invalid token"}
|
117 |
|
118 |
-
|
119 |
-
|
120 |
-
|
|
|
121 |
|
122 |
-
def
|
123 |
if not verify_token(token):
|
124 |
return {"error": "Invalid token"}
|
125 |
|
126 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
127 |
explanation = llm(explanation_prompt, max_tokens=256)["choices"][0]["text"]
|
128 |
return {"explanation": explanation}
|
129 |
|
130 |
-
def
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
return
|
135 |
|
136 |
-
# Gradio
|
137 |
-
|
138 |
-
fn=api_generate_solution,
|
139 |
inputs=[
|
140 |
-
gr.Textbox(label="LeetCode Problem
|
141 |
gr.Textbox(label="JWT Token")
|
142 |
],
|
143 |
-
outputs=
|
144 |
-
|
145 |
-
|
146 |
-
)
|
147 |
-
|
148 |
-
iface_explain = gr.Interface(
|
149 |
-
fn=api_explain_solution,
|
150 |
-
inputs=[
|
151 |
-
gr.Textbox(label="Code to Explain"),
|
152 |
-
gr.Textbox(label="JWT Token")
|
153 |
],
|
154 |
-
|
155 |
-
|
156 |
-
description="Provide a code snippet and a valid JWT token to get an explanation."
|
157 |
)
|
158 |
|
159 |
-
iface_token = gr.Interface(
|
160 |
-
fn=generate_token,
|
161 |
-
inputs=[],
|
162 |
-
outputs=gr.Textbox(label="Generated JWT Token"),
|
163 |
-
title="Generate JWT Token",
|
164 |
-
description="Generate a new JWT token for API authentication."
|
165 |
-
)
|
166 |
-
|
167 |
-
# Combine interfaces
|
168 |
-
demo = gr.TabbedInterface([iface_generate, iface_explain, iface_token], ["Generate Solution", "Explain Solution", "Generate Token"])
|
169 |
-
|
170 |
if __name__ == "__main__":
|
171 |
logger.info("Starting Gradio API")
|
172 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
from huggingface_hub import hf_hub_download
|
3 |
from llama_cpp import Llama
|
4 |
+
import re
|
5 |
+
from datasets import load_dataset
|
6 |
+
import random
|
7 |
+
import logging
|
8 |
+
import os
|
9 |
+
import autopep8
|
10 |
+
import textwrap
|
11 |
import jwt
|
12 |
from typing import Dict, Any
|
|
|
13 |
|
14 |
# Set up logging
|
15 |
logging.basicConfig(level=logging.INFO)
|
|
|
25 |
MODEL_NAME = "leetmonkey_peft__q8_0.gguf"
|
26 |
REPO_ID = "sugiv/leetmonkey-peft-gguf"
|
27 |
|
28 |
+
# Load the dataset
|
29 |
+
dataset = load_dataset("sugiv/leetmonkey_python_dataset")
|
30 |
+
train_dataset = dataset["train"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
31 |
|
32 |
def download_model(model_name):
|
33 |
logger.info(f"Downloading model: {model_name}")
|
|
|
54 |
)
|
55 |
logger.info("8-bit model loaded successfully")
|
56 |
|
57 |
+
# Generation parameters
|
58 |
+
generation_kwargs = {
|
59 |
+
"max_tokens": 512,
|
60 |
+
"stop": ["```", "### Instruction:", "### Response:"],
|
61 |
+
"echo": False,
|
62 |
+
"temperature": 0.05,
|
63 |
+
"top_k": 10,
|
64 |
+
"top_p": 0.9,
|
65 |
+
"repeat_penalty": 1.1
|
66 |
+
}
|
67 |
+
|
68 |
+
def generate_solution(instruction):
|
69 |
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."
|
70 |
full_prompt = f"""### Instruction:
|
71 |
{system_prompt}
|
|
|
80 |
```python
|
81 |
"""
|
82 |
|
83 |
+
for chunk in llm(full_prompt, stream=True, **generation_kwargs):
|
84 |
+
yield chunk["choices"][0]["text"]
|
85 |
|
86 |
+
def extract_and_format_code(text):
|
87 |
+
# Extract code between triple backticks
|
88 |
code_match = re.search(r'```python\s*(.*?)\s*```', text, re.DOTALL)
|
89 |
if code_match:
|
90 |
code = code_match.group(1)
|
91 |
else:
|
92 |
code = text
|
93 |
|
94 |
+
# Dedent the code to remove any common leading whitespace
|
95 |
code = textwrap.dedent(code)
|
96 |
+
|
97 |
+
# Split the code into lines
|
98 |
lines = code.split('\n')
|
99 |
|
100 |
+
# Ensure proper indentation
|
101 |
indented_lines = []
|
102 |
for line in lines:
|
103 |
if line.strip().startswith('class') or line.strip().startswith('def'):
|
104 |
+
indented_lines.append(line) # Keep class and function definitions as is
|
105 |
+
elif line.strip(): # If the line is not empty
|
106 |
+
indented_lines.append(' ' + line) # Add 4 spaces of indentation
|
107 |
else:
|
108 |
+
indented_lines.append(line) # Keep empty lines as is
|
109 |
|
110 |
formatted_code = '\n'.join(indented_lines)
|
111 |
|
|
|
114 |
except:
|
115 |
return formatted_code
|
116 |
|
117 |
+
def select_random_problem():
|
118 |
+
return random.choice(train_dataset)['instruction']
|
119 |
+
|
120 |
def verify_token(token: str) -> bool:
|
121 |
try:
|
122 |
jwt.decode(token, JWT_SECRET, algorithms=[JWT_ALGORITHM])
|
|
|
124 |
except jwt.PyJWTError:
|
125 |
return False
|
126 |
|
127 |
+
last_generated_solution = ""
|
128 |
+
|
129 |
def api_generate_solution(instruction: str, token: str) -> Dict[str, Any]:
|
130 |
if not verify_token(token):
|
131 |
return {"error": "Invalid token"}
|
132 |
|
133 |
+
global last_generated_solution
|
134 |
+
generated_text = "".join(list(generate_solution(instruction)))
|
135 |
+
last_generated_solution = extract_and_format_code(generated_text)
|
136 |
+
return {"solution": last_generated_solution}
|
137 |
|
138 |
+
def api_stream_solution(instruction: str, token: str) -> Dict[str, Any]:
|
139 |
if not verify_token(token):
|
140 |
return {"error": "Invalid token"}
|
141 |
|
142 |
+
def generate():
|
143 |
+
global last_generated_solution
|
144 |
+
generated_text = ""
|
145 |
+
for token in generate_solution(instruction):
|
146 |
+
generated_text += token
|
147 |
+
yield {"token": token}
|
148 |
+
|
149 |
+
last_generated_solution = extract_and_format_code(generated_text)
|
150 |
+
yield {"solution": last_generated_solution}
|
151 |
+
|
152 |
+
return generate()
|
153 |
+
|
154 |
+
def api_explain_solution(token: str) -> Dict[str, Any]:
|
155 |
+
if not verify_token(token):
|
156 |
+
return {"error": "Invalid token"}
|
157 |
+
|
158 |
+
if not last_generated_solution:
|
159 |
+
return {"error": "No solution has been generated yet"}
|
160 |
+
|
161 |
+
explanation_prompt = f"Explain the following Python code:\n\n{last_generated_solution}\n\nExplanation:"
|
162 |
explanation = llm(explanation_prompt, max_tokens=256)["choices"][0]["text"]
|
163 |
return {"explanation": explanation}
|
164 |
|
165 |
+
def api_random_problem(token: str) -> Dict[str, Any]:
|
166 |
+
if not verify_token(token):
|
167 |
+
return {"error": "Invalid token"}
|
168 |
+
|
169 |
+
return {"problem": select_random_problem()}
|
170 |
|
171 |
+
# Gradio interface
|
172 |
+
iface = gr.Interface(
|
173 |
+
fn=[api_generate_solution, api_stream_solution, api_explain_solution, api_random_problem],
|
174 |
inputs=[
|
175 |
+
gr.Textbox(label="LeetCode Problem"),
|
176 |
gr.Textbox(label="JWT Token")
|
177 |
],
|
178 |
+
outputs=[
|
179 |
+
gr.JSON(label="Generated Solution"),
|
180 |
+
gr.JSON(label="Streamed Solution"),
|
181 |
+
gr.JSON(label="Explanation"),
|
182 |
+
gr.JSON(label="Random Problem")
|
|
|
|
|
|
|
|
|
|
|
183 |
],
|
184 |
+
title="LeetCode Problem Solver API",
|
185 |
+
description="Provide a LeetCode problem instruction and a valid JWT token to generate a solution, get an explanation, or retrieve a random problem."
|
|
|
186 |
)
|
187 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
188 |
if __name__ == "__main__":
|
189 |
logger.info("Starting Gradio API")
|
190 |
+
iface.launch(share=True)
|