Spaces:
Runtime error
Runtime error
Commit
·
36031cb
1
Parent(s):
4740e27
Update app.py
Browse files
app.py
CHANGED
@@ -6,7 +6,7 @@ from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
|
6 |
from prettytable import PrettyTable
|
7 |
import streamlit as st
|
8 |
|
9 |
-
st.title('Code Generation on the CoNaLa Dataset')
|
10 |
|
11 |
class CodeGenerator:
|
12 |
def __init__(self):
|
@@ -110,6 +110,66 @@ class CodeGenerator:
|
|
110 |
|
111 |
|
112 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
113 |
code_generator = CodeGenerator()
|
114 |
|
115 |
|
@@ -130,6 +190,15 @@ def main():
|
|
130 |
st.write(error_message)
|
131 |
|
132 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
133 |
if __name__ == '__main__':
|
134 |
|
135 |
main()
|
|
|
6 |
from prettytable import PrettyTable
|
7 |
import streamlit as st
|
8 |
|
9 |
+
#st.title('Code Generation on the CoNaLa Dataset')
|
10 |
|
11 |
class CodeGenerator:
|
12 |
def __init__(self):
|
|
|
110 |
|
111 |
|
112 |
|
113 |
+
|
114 |
+
|
115 |
+
|
116 |
+
import autopep8
|
117 |
+
import black
|
118 |
+
import isort
|
119 |
+
import pylint.lint
|
120 |
+
import autoimport
|
121 |
+
|
122 |
+
|
123 |
+
class PythonCodeFormatter:
|
124 |
+
def __init__(self, code):
|
125 |
+
self.code = code.replace('▁', ' ').strip()
|
126 |
+
|
127 |
+
|
128 |
+
def load_code_from_file(self, filename):
|
129 |
+
# Load the code to be fixed
|
130 |
+
with open(filename, 'r') as f:
|
131 |
+
self.code = f.read()
|
132 |
+
|
133 |
+
def format(self):
|
134 |
+
try:
|
135 |
+
# Use isort to sort and organize the imports
|
136 |
+
formatted_code = isort.code(self.code)
|
137 |
+
|
138 |
+
# Use black to format the code
|
139 |
+
formatted_code = black.format_str(formatted_code, mode=black.Mode())
|
140 |
+
|
141 |
+
# Use autoimport to add a missing import statement
|
142 |
+
formatted_code = autoimport.fix_code(formatted_code)
|
143 |
+
|
144 |
+
# Use autopep8 to fix any remaining issues
|
145 |
+
formatted_code = autopep8.fix_code(formatted_code)
|
146 |
+
|
147 |
+
except RuntimeError as error:
|
148 |
+
if str(error) == 'Project root not found':
|
149 |
+
return formatted_code
|
150 |
+
else:
|
151 |
+
raise # re-raise the error if it's not the one we're looking for
|
152 |
+
except ValueError as error:
|
153 |
+
if str(error) == 'Cannot parse: 2:0: EOF in multi-line statement':
|
154 |
+
# add )) to the end of the line to fix the error
|
155 |
+
self.code += '))'
|
156 |
+
# retry the format() function with the corrected code
|
157 |
+
formatted_code = self.format()
|
158 |
+
else:
|
159 |
+
raise # re-raise the error if it's not the one we're looking for
|
160 |
+
|
161 |
+
return formatted_code
|
162 |
+
|
163 |
+
|
164 |
+
def save(self, filename):
|
165 |
+
# Save the fixed code to a file
|
166 |
+
with open(filename, 'w') as f:
|
167 |
+
f.write(self.code)
|
168 |
+
|
169 |
+
|
170 |
+
|
171 |
+
|
172 |
+
|
173 |
code_generator = CodeGenerator()
|
174 |
|
175 |
|
|
|
190 |
st.write(error_message)
|
191 |
|
192 |
|
193 |
+
st.subheader('Error Correction')
|
194 |
+
formatter = PythonCodeFormatter(output_code)
|
195 |
+
formatted_code = formatter.format()
|
196 |
+
st.write('Code after correction:')
|
197 |
+
st.write(formatted_code)
|
198 |
+
|
199 |
+
|
200 |
+
|
201 |
+
|
202 |
if __name__ == '__main__':
|
203 |
|
204 |
main()
|