Leetmonkey In Action via Inference
Browse files- app.py +71 -51
- requirements.txt +4 -1
app.py
CHANGED
@@ -8,6 +8,9 @@ from huggingface_hub import hf_hub_download
|
|
8 |
from llama_cpp import Llama
|
9 |
import jwt
|
10 |
from typing import Generator
|
|
|
|
|
|
|
11 |
|
12 |
# Set up logging
|
13 |
logging.basicConfig(level=logging.INFO)
|
@@ -15,6 +18,8 @@ logger = logging.getLogger(__name__)
|
|
15 |
|
16 |
# JWT settings
|
17 |
JWT_SECRET = os.environ.get("JWT_SECRET")
|
|
|
|
|
18 |
JWT_ALGORITHM = "HS256"
|
19 |
|
20 |
# Model settings
|
@@ -108,73 +113,88 @@ def extract_and_format_code(text: str) -> str:
|
|
108 |
except:
|
109 |
return formatted_code
|
110 |
|
111 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
112 |
try:
|
113 |
-
jwt.decode(
|
114 |
return True
|
115 |
except jwt.PyJWTError:
|
116 |
-
|
117 |
|
118 |
-
|
119 |
-
|
120 |
-
return "Invalid token. Please provide a valid JWT token."
|
121 |
-
|
122 |
logger.info("Generating solution")
|
123 |
-
generated_output = generate_solution(instruction)
|
124 |
formatted_code = extract_and_format_code(generated_output)
|
125 |
logger.info("Solution generated successfully")
|
126 |
-
return formatted_code
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
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."
|
135 |
-
full_prompt = f"""### Instruction:
|
136 |
{system_prompt}
|
137 |
|
138 |
Implement the following function for the LeetCode problem:
|
139 |
|
140 |
-
{instruction}
|
141 |
|
142 |
### Response:
|
143 |
Here's the complete Python function implementation:
|
144 |
|
145 |
```python
|
146 |
"""
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
def
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
-
|
175 |
-
|
176 |
-
|
177 |
-
|
|
|
|
|
|
|
|
|
|
|
178 |
|
179 |
if __name__ == "__main__":
|
180 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
from llama_cpp import Llama
|
9 |
import jwt
|
10 |
from typing import Generator
|
11 |
+
from fastapi import FastAPI, HTTPException, Depends
|
12 |
+
from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials
|
13 |
+
from pydantic import BaseModel
|
14 |
|
15 |
# Set up logging
|
16 |
logging.basicConfig(level=logging.INFO)
|
|
|
18 |
|
19 |
# JWT settings
|
20 |
JWT_SECRET = os.environ.get("JWT_SECRET")
|
21 |
+
if not JWT_SECRET:
|
22 |
+
raise ValueError("JWT_SECRET environment variable is not set")
|
23 |
JWT_ALGORITHM = "HS256"
|
24 |
|
25 |
# Model settings
|
|
|
113 |
except:
|
114 |
return formatted_code
|
115 |
|
116 |
+
security = HTTPBearer()
|
117 |
+
app = FastAPI()
|
118 |
+
|
119 |
+
class ProblemRequest(BaseModel):
|
120 |
+
instruction: str
|
121 |
+
|
122 |
+
def verify_token(credentials: HTTPAuthorizationCredentials = Depends(security)):
|
123 |
try:
|
124 |
+
jwt.decode(credentials.credentials, JWT_SECRET, algorithms=[JWT_ALGORITHM])
|
125 |
return True
|
126 |
except jwt.PyJWTError:
|
127 |
+
raise HTTPException(status_code=401, detail="Invalid token")
|
128 |
|
129 |
+
@app.post("/generate_solution")
|
130 |
+
async def generate_solution_api(request: ProblemRequest, authorized: bool = Depends(verify_token)):
|
|
|
|
|
131 |
logger.info("Generating solution")
|
132 |
+
generated_output = generate_solution(request.instruction)
|
133 |
formatted_code = extract_and_format_code(generated_output)
|
134 |
logger.info("Solution generated successfully")
|
135 |
+
return {"solution": formatted_code}
|
136 |
+
|
137 |
+
@app.post("/stream_solution")
|
138 |
+
async def stream_solution_api(request: ProblemRequest, authorized: bool = Depends(verify_token)):
|
139 |
+
async def generate():
|
140 |
+
logger.info("Streaming solution")
|
141 |
+
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."
|
142 |
+
full_prompt = f"""### Instruction:
|
|
|
|
|
143 |
{system_prompt}
|
144 |
|
145 |
Implement the following function for the LeetCode problem:
|
146 |
|
147 |
+
{request.instruction}
|
148 |
|
149 |
### Response:
|
150 |
Here's the complete Python function implementation:
|
151 |
|
152 |
```python
|
153 |
"""
|
154 |
+
|
155 |
+
generated_text = ""
|
156 |
+
for chunk in llm(full_prompt, stream=True, **generation_kwargs):
|
157 |
+
token = chunk["choices"][0]["text"]
|
158 |
+
generated_text += token
|
159 |
+
yield token
|
160 |
+
|
161 |
+
formatted_code = extract_and_format_code(generated_text)
|
162 |
+
logger.info("Solution generated successfully")
|
163 |
+
yield formatted_code
|
164 |
+
|
165 |
+
return generate()
|
166 |
+
|
167 |
+
# Gradio wrapper for FastAPI
|
168 |
+
def gradio_wrapper(app):
|
169 |
+
def inference(instruction, token):
|
170 |
+
import requests
|
171 |
+
url = "http://localhost:8000/generate_solution"
|
172 |
+
headers = {"Authorization": f"Bearer {token}"}
|
173 |
+
response = requests.post(url, json={"instruction": instruction}, headers=headers)
|
174 |
+
if response.status_code == 200:
|
175 |
+
return response.json()["solution"]
|
176 |
+
else:
|
177 |
+
return f"Error: {response.status_code}, {response.text}"
|
178 |
+
|
179 |
+
iface = gr.Interface(
|
180 |
+
fn=inference,
|
181 |
+
inputs=[
|
182 |
+
gr.Textbox(label="LeetCode Problem Instruction"),
|
183 |
+
gr.Textbox(label="JWT Token")
|
184 |
+
],
|
185 |
+
outputs=gr.Code(label="Generated Solution"),
|
186 |
+
title="LeetCode Problem Solver API",
|
187 |
+
description="Enter a LeetCode problem instruction and your JWT token to generate a solution."
|
188 |
+
)
|
189 |
+
return iface
|
190 |
|
191 |
if __name__ == "__main__":
|
192 |
+
import uvicorn
|
193 |
+
from threading import Thread
|
194 |
+
|
195 |
+
# Start FastAPI in a separate thread
|
196 |
+
Thread(target=lambda: uvicorn.run(app, host="0.0.0.0", port=8000)).start()
|
197 |
+
|
198 |
+
# Launch Gradio interface
|
199 |
+
iface = gradio_wrapper(app)
|
200 |
+
iface.launch(share=True)
|
requirements.txt
CHANGED
@@ -2,4 +2,7 @@ gradio
|
|
2 |
llama-cpp-python
|
3 |
huggingface_hub
|
4 |
pyjwt
|
5 |
-
autopep8
|
|
|
|
|
|
|
|
2 |
llama-cpp-python
|
3 |
huggingface_hub
|
4 |
pyjwt
|
5 |
+
autopep8
|
6 |
+
fastapi
|
7 |
+
uvicorn
|
8 |
+
pydantic
|