Spaces:
Runtime error
Runtime error
Create utils.py
Browse files
utils.py
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import ast
|
| 2 |
+
import glob
|
| 3 |
+
import os
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
def parse_file_content(string: str):
|
| 7 |
+
first_break = string.find("---")
|
| 8 |
+
last_break = string.rfind("---")
|
| 9 |
+
if first_break == -1 and last_break == -1 or first_break == last_break:
|
| 10 |
+
return None, None
|
| 11 |
+
nl_after = string.find("\n", last_break)
|
| 12 |
+
description = string[nl_after:]
|
| 13 |
+
return string[first_break + 4 : last_break], description.strip("\n")
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
def parse_action(string: str):
|
| 17 |
+
assert string.startswith("action:")
|
| 18 |
+
idx = string.find("action_input=")
|
| 19 |
+
if idx == -1:
|
| 20 |
+
return string[8:], None
|
| 21 |
+
return string[8 : idx - 1], string[idx + 13 :].strip("'").strip('"')
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
def extract_imports(file_contents):
|
| 25 |
+
module_ast = ast.parse(file_contents)
|
| 26 |
+
imports = []
|
| 27 |
+
functions = [n for n in module_ast.body if isinstance(n, ast.FunctionDef)]
|
| 28 |
+
classes = [n for n in module_ast.body if isinstance(n, ast.ClassDef)]
|
| 29 |
+
for node in ast.walk(module_ast):
|
| 30 |
+
if isinstance(node, ast.Import):
|
| 31 |
+
for alias in node.names:
|
| 32 |
+
imports.append(alias.name)
|
| 33 |
+
elif isinstance(node, ast.ImportFrom):
|
| 34 |
+
module_name = node.module
|
| 35 |
+
for alias in node.names:
|
| 36 |
+
name = alias.name
|
| 37 |
+
if module_name:
|
| 38 |
+
imports.append(f"{module_name}.{name}")
|
| 39 |
+
else:
|
| 40 |
+
imports.append(name)
|
| 41 |
+
return imports, functions, classes
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
def read_python_module_structure(path):
|
| 45 |
+
file_types = ["*.py"]
|
| 46 |
+
code = []
|
| 47 |
+
for file_type in file_types:
|
| 48 |
+
code += glob.glob(os.path.join(path, "**", file_type), recursive=True)
|
| 49 |
+
|
| 50 |
+
structure_prompt = "Files:\n"
|
| 51 |
+
structure_prompt += "(listing all files and their functions and classes)\n\n"
|
| 52 |
+
|
| 53 |
+
def get_file_name(i):
|
| 54 |
+
return "./{}.py".format(i.replace(".", "/"))
|
| 55 |
+
|
| 56 |
+
content = {}
|
| 57 |
+
internal_imports_map = {}
|
| 58 |
+
for fn in code:
|
| 59 |
+
if os.path.basename(fn) == "gpt.py":
|
| 60 |
+
continue
|
| 61 |
+
with open(fn, "r") as f:
|
| 62 |
+
content[fn] = f.read()
|
| 63 |
+
imports, functions, classes = extract_imports(content[fn])
|
| 64 |
+
internal_imports = list(
|
| 65 |
+
{".".join(i.split(".")[:-1]) for i in imports if i.startswith("app.")}
|
| 66 |
+
)
|
| 67 |
+
internal_imports_map[fn] = [get_file_name(i) for i in internal_imports]
|
| 68 |
+
structure_prompt += f"{fn}\n"
|
| 69 |
+
for function in functions:
|
| 70 |
+
structure_prompt += " {}()\n".format(function.name, function.args.args)
|
| 71 |
+
|
| 72 |
+
for class_ in classes:
|
| 73 |
+
structure_prompt += " {}\n".format(class_.name)
|
| 74 |
+
methods = [n for n in class_.body if isinstance(n, ast.FunctionDef)]
|
| 75 |
+
for method in methods:
|
| 76 |
+
structure_prompt += " {}.{}()\n".format(class_.name, method.name)
|
| 77 |
+
structure_prompt += "\n"
|
| 78 |
+
|
| 79 |
+
return structure_prompt, content, internal_imports_map
|