Debugging shit
Browse files
app.py
CHANGED
@@ -60,47 +60,6 @@ def verify_token(token: str) -> bool:
|
|
60 |
except jwt.PyJWTError:
|
61 |
return False
|
62 |
|
63 |
-
def stream_solution(instruction: str, token: str):
|
64 |
-
if not verify_token(token):
|
65 |
-
return {"error": "Invalid token"}
|
66 |
-
|
67 |
-
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."
|
68 |
-
full_prompt = f"""### Instruction:
|
69 |
-
{system_prompt}
|
70 |
-
|
71 |
-
Implement the following function for the LeetCode problem:
|
72 |
-
|
73 |
-
{instruction}
|
74 |
-
|
75 |
-
### Response:
|
76 |
-
Here's the complete Python function implementation:
|
77 |
-
|
78 |
-
```python
|
79 |
-
"""
|
80 |
-
|
81 |
-
async def generate():
|
82 |
-
generated_text = ""
|
83 |
-
try:
|
84 |
-
for chunk in llm(full_prompt, stream=True, **generation_kwargs):
|
85 |
-
token = chunk["choices"][0]["text"]
|
86 |
-
generated_text += token
|
87 |
-
yield token
|
88 |
-
|
89 |
-
# Optionally send progress updates:
|
90 |
-
progress = len(generated_text) / len(full_prompt)
|
91 |
-
yield {"progress": progress} # Send a progress update
|
92 |
-
|
93 |
-
formatted_code = extract_and_format_code(generated_text)
|
94 |
-
yield formatted_code
|
95 |
-
except Exception as e:
|
96 |
-
# Handle exceptions and return an error message
|
97 |
-
yield {"error": str(e)}
|
98 |
-
|
99 |
-
logger.info(StreamingResponse(generate(), media_type="application/x-ndjson"))
|
100 |
-
logger.info(StreamingResponse(generate(), media_type="text/plain"))
|
101 |
-
logger.info(StreamingResponse(generate(), media_type="application/json"))
|
102 |
-
return StreamingResponse(generate(), media_type="text/plain")
|
103 |
-
|
104 |
def extract_and_format_code(text):
|
105 |
# Extract code between triple backticks
|
106 |
code_match = re.search(r'```python\s*(.*?)\s*```', text, re.DOTALL)
|
@@ -185,6 +144,7 @@ Here's the complete Python function implementation:
|
|
185 |
formatted_code = extract_and_format_code(generated_text)
|
186 |
yield json.dumps({"complete": True, "formatted_code": formatted_code}) + "\n"
|
187 |
|
|
|
188 |
return StreamingResponse(generate(), media_type="application/x-ndjson")
|
189 |
|
190 |
def random_problem(token: str) -> Dict[str, Any]:
|
|
|
60 |
except jwt.PyJWTError:
|
61 |
return False
|
62 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
63 |
def extract_and_format_code(text):
|
64 |
# Extract code between triple backticks
|
65 |
code_match = re.search(r'```python\s*(.*?)\s*```', text, re.DOTALL)
|
|
|
144 |
formatted_code = extract_and_format_code(generated_text)
|
145 |
yield json.dumps({"complete": True, "formatted_code": formatted_code}) + "\n"
|
146 |
|
147 |
+
logger.info("Streaming response")
|
148 |
return StreamingResponse(generate(), media_type="application/x-ndjson")
|
149 |
|
150 |
def random_problem(token: str) -> Dict[str, Any]:
|