Fixing generate and random problem API
Browse files
app.py
CHANGED
@@ -8,8 +8,10 @@ import os
|
|
8 |
import jwt
|
9 |
from typing import Dict, Any
|
10 |
import autopep8
|
|
|
11 |
|
12 |
from datasets import load_dataset
|
|
|
13 |
import random
|
14 |
|
15 |
# Load the dataset (you might want to do this once at the start of your script)
|
@@ -52,7 +54,36 @@ def verify_token(token: str) -> bool:
|
|
52 |
return True
|
53 |
except jwt.PyJWTError:
|
54 |
return False
|
|
|
|
|
|
|
|
|
55 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
56 |
|
57 |
def extract_and_format_code(text):
|
58 |
# Extract code between triple backticks
|
|
|
8 |
import jwt
|
9 |
from typing import Dict, Any
|
10 |
import autopep8
|
11 |
+
import textwrap
|
12 |
|
13 |
from datasets import load_dataset
|
14 |
+
from fastapi.responses import StreamingResponse
|
15 |
import random
|
16 |
|
17 |
# Load the dataset (you might want to do this once at the start of your script)
|
|
|
54 |
return True
|
55 |
except jwt.PyJWTError:
|
56 |
return False
|
57 |
+
|
58 |
+
def stream_solution(instruction: str, token: str):
|
59 |
+
if not verify_token(token):
|
60 |
+
return {"error": "Invalid token"}
|
61 |
|
62 |
+
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."
|
63 |
+
full_prompt = f"""### Instruction:
|
64 |
+
{system_prompt}
|
65 |
+
|
66 |
+
Implement the following function for the LeetCode problem:
|
67 |
+
|
68 |
+
{instruction}
|
69 |
+
|
70 |
+
### Response:
|
71 |
+
Here's the complete Python function implementation:
|
72 |
+
|
73 |
+
```python
|
74 |
+
"""
|
75 |
+
|
76 |
+
def generate():
|
77 |
+
generated_text = ""
|
78 |
+
for chunk in llm(full_prompt, stream=True, **generation_kwargs):
|
79 |
+
token = chunk["choices"][0]["text"]
|
80 |
+
generated_text += token
|
81 |
+
yield token
|
82 |
+
|
83 |
+
formatted_code = extract_and_format_code(generated_text)
|
84 |
+
yield formatted_code
|
85 |
+
|
86 |
+
return StreamingResponse(generate(), media_type="text/plain")
|
87 |
|
88 |
def extract_and_format_code(text):
|
89 |
# Extract code between triple backticks
|