Fixing generate and random problem API
Browse files- app.py +52 -17
- requirements.txt +2 -1
app.py
CHANGED
@@ -7,6 +7,14 @@ import logging
|
|
7 |
import os
|
8 |
import jwt
|
9 |
from typing import Dict, Any
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
|
11 |
# Set up logging
|
12 |
logging.basicConfig(level=logging.INFO)
|
@@ -44,6 +52,38 @@ def verify_token(token: str) -> bool:
|
|
44 |
return True
|
45 |
except jwt.PyJWTError:
|
46 |
return False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
47 |
|
48 |
def generate_solution(instruction: str, token: str) -> Dict[str, Any]:
|
49 |
if not verify_token(token):
|
@@ -63,17 +103,12 @@ Here's the complete Python function implementation:
|
|
63 |
```python
|
64 |
"""
|
65 |
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
# Extract and format code
|
70 |
-
code_match = re.search(r'```python\s*(.*?)\s*```', generated_text, re.DOTALL)
|
71 |
-
if code_match:
|
72 |
-
code = code_match.group(1)
|
73 |
-
else:
|
74 |
-
code = generated_text
|
75 |
|
76 |
-
|
|
|
77 |
|
78 |
def stream_solution(instruction: str, token: str) -> Dict[str, Any]:
|
79 |
if not verify_token(token):
|
@@ -103,13 +138,13 @@ def random_problem(token: str) -> Dict[str, Any]:
|
|
103 |
if not verify_token(token):
|
104 |
return {"error": "Invalid token"}
|
105 |
|
106 |
-
#
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
return {"problem":
|
113 |
|
114 |
# Create Gradio interfaces for each endpoint
|
115 |
generate_interface = gr.Interface(
|
|
|
7 |
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)
|
16 |
+
dataset = load_dataset("sugiv/leetmonkey_python_dataset")
|
17 |
+
train_dataset = dataset["train"]
|
18 |
|
19 |
# Set up logging
|
20 |
logging.basicConfig(level=logging.INFO)
|
|
|
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
|
59 |
+
code_match = re.search(r'```python\s*(.*?)\s*```', text, re.DOTALL)
|
60 |
+
if code_match:
|
61 |
+
code = code_match.group(1)
|
62 |
+
else:
|
63 |
+
code = text
|
64 |
+
|
65 |
+
# Dedent the code to remove any common leading whitespace
|
66 |
+
code = textwrap.dedent(code)
|
67 |
+
|
68 |
+
# Split the code into lines
|
69 |
+
lines = code.split('\n')
|
70 |
+
|
71 |
+
# Ensure proper indentation
|
72 |
+
indented_lines = []
|
73 |
+
for line in lines:
|
74 |
+
if line.strip().startswith('class') or line.strip().startswith('def'):
|
75 |
+
indented_lines.append(line) # Keep class and function definitions as is
|
76 |
+
elif line.strip(): # If the line is not empty
|
77 |
+
indented_lines.append(' ' + line) # Add 4 spaces of indentation
|
78 |
+
else:
|
79 |
+
indented_lines.append(line) # Keep empty lines as is
|
80 |
+
|
81 |
+
formatted_code = '\n'.join(indented_lines)
|
82 |
+
|
83 |
+
try:
|
84 |
+
return autopep8.fix_code(formatted_code)
|
85 |
+
except:
|
86 |
+
return formatted_code
|
87 |
|
88 |
def generate_solution(instruction: str, token: str) -> Dict[str, Any]:
|
89 |
if not verify_token(token):
|
|
|
103 |
```python
|
104 |
"""
|
105 |
|
106 |
+
generated_text = ""
|
107 |
+
for chunk in llm(full_prompt, stream=True, **generation_kwargs):
|
108 |
+
generated_text += chunk["choices"]["text"]
|
|
|
|
|
|
|
|
|
|
|
|
|
109 |
|
110 |
+
formatted_code = extract_and_format_code(generated_text)
|
111 |
+
return {"solution": formatted_code}
|
112 |
|
113 |
def stream_solution(instruction: str, token: str) -> Dict[str, Any]:
|
114 |
if not verify_token(token):
|
|
|
138 |
if not verify_token(token):
|
139 |
return {"error": "Invalid token"}
|
140 |
|
141 |
+
# Select a random problem from the dataset
|
142 |
+
random_item = random.choice(train_dataset)
|
143 |
+
|
144 |
+
# Extract the instruction (problem statement) from the randomly selected item
|
145 |
+
problem = random_item['instruction']
|
146 |
+
|
147 |
+
return {"problem": problem}
|
148 |
|
149 |
# Create Gradio interfaces for each endpoint
|
150 |
generate_interface = gr.Interface(
|
requirements.txt
CHANGED
@@ -4,4 +4,5 @@ datasets
|
|
4 |
transformers
|
5 |
autopep8
|
6 |
huggingface_hub
|
7 |
-
pyjwt
|
|
|
|
4 |
transformers
|
5 |
autopep8
|
6 |
huggingface_hub
|
7 |
+
pyjwt
|
8 |
+
autopep8
|