Changing the code extract logic to preserve all code
Browse files
app.py
CHANGED
@@ -83,22 +83,18 @@ def extract_and_format_code(text):
|
|
83 |
else:
|
84 |
code = text
|
85 |
|
86 |
-
# Remove any text before the function definition
|
87 |
-
code = re.sub(r'^.*?(?=def\s+\w+\s*\()', '', code, flags=re.DOTALL)
|
88 |
-
|
89 |
# Dedent the code to remove any common leading whitespace
|
90 |
code = textwrap.dedent(code)
|
91 |
|
92 |
# Split the code into lines
|
93 |
lines = code.split('\n')
|
94 |
|
95 |
-
# Find the function definition line
|
96 |
-
func_def_index = next((i for i, line in enumerate(lines) if line.strip().startswith('def ')), 0)
|
97 |
-
|
98 |
# Ensure proper indentation
|
99 |
-
indented_lines = [
|
100 |
-
for line in lines
|
101 |
-
if line.strip()
|
|
|
|
|
102 |
indented_lines.append(' ' + line) # Add 4 spaces of indentation
|
103 |
else:
|
104 |
indented_lines.append(line) # Keep empty lines as is
|
@@ -110,6 +106,7 @@ def extract_and_format_code(text):
|
|
110 |
except:
|
111 |
return formatted_code
|
112 |
|
|
|
113 |
def select_random_problem():
|
114 |
return random.choice(train_dataset)['instruction']
|
115 |
|
|
|
83 |
else:
|
84 |
code = text
|
85 |
|
|
|
|
|
|
|
86 |
# Dedent the code to remove any common leading whitespace
|
87 |
code = textwrap.dedent(code)
|
88 |
|
89 |
# Split the code into lines
|
90 |
lines = code.split('\n')
|
91 |
|
|
|
|
|
|
|
92 |
# Ensure proper indentation
|
93 |
+
indented_lines = []
|
94 |
+
for line in lines:
|
95 |
+
if line.strip().startswith('class') or line.strip().startswith('def'):
|
96 |
+
indented_lines.append(line) # Keep class and function definitions as is
|
97 |
+
elif line.strip(): # If the line is not empty
|
98 |
indented_lines.append(' ' + line) # Add 4 spaces of indentation
|
99 |
else:
|
100 |
indented_lines.append(line) # Keep empty lines as is
|
|
|
106 |
except:
|
107 |
return formatted_code
|
108 |
|
109 |
+
|
110 |
def select_random_problem():
|
111 |
return random.choice(train_dataset)['instruction']
|
112 |
|