Spaces:
Sleeping
Sleeping
Commit ·
6b7613a
1
Parent(s): 473c7a6
Use Luke's clean_output function
Browse files
main.py
CHANGED
|
@@ -26,6 +26,56 @@ def canonicalize_output(decompiled: str):
|
|
| 26 |
assert False, "Unable to parse code"
|
| 27 |
return df.canonical_code + DECOMPILED_ORIG_SEP
|
| 28 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline #, BitsAndBytesConfig
|
| 30 |
from peft import PeftModel
|
| 31 |
|
|
@@ -88,7 +138,7 @@ def predict(inp):
|
|
| 88 |
except Exception as e:
|
| 89 |
return f"Something went wrong during inference: {str(e)}"
|
| 90 |
print("done!")
|
| 91 |
-
return results[0]['generated_text']
|
| 92 |
|
| 93 |
def run():
|
| 94 |
|
|
|
|
| 26 |
assert False, "Unable to parse code"
|
| 27 |
return df.canonical_code + DECOMPILED_ORIG_SEP
|
| 28 |
|
| 29 |
+
# From Luke to clean up the results
|
| 30 |
+
from prepare import (
|
| 31 |
+
parser,
|
| 32 |
+
FileTypeMapping,
|
| 33 |
+
PreprocessedFunction,
|
| 34 |
+
has_error,
|
| 35 |
+
TypeNotDefinedError,
|
| 36 |
+
TypeNotFoundError,
|
| 37 |
+
UnsupportedFeatureError,
|
| 38 |
+
get_all_user_defined_types
|
| 39 |
+
)
|
| 40 |
+
from evaluator import add_placeholders_for_nonvariable_types
|
| 41 |
+
from tree_sitter import Node
|
| 42 |
+
|
| 43 |
+
class InvalidPredictionError(Exception):
|
| 44 |
+
pass
|
| 45 |
+
|
| 46 |
+
def clean_output(raw_prediction: str) -> str:
|
| 47 |
+
### Parse and sort nodes
|
| 48 |
+
root = parser.parse(bytes(raw_prediction, "utf8")).root_node
|
| 49 |
+
if root.type == "ERROR":
|
| 50 |
+
raise InvalidPredictionError("Prediction is irrecoverably malformed.")
|
| 51 |
+
assert root.type == "translation_unit"
|
| 52 |
+
fn_nodes: list[Node] = []
|
| 53 |
+
udt_nodes: list[Node] = []
|
| 54 |
+
other_nodes: list[Node] = []
|
| 55 |
+
for node in root.children:
|
| 56 |
+
if node.type == "function_definition":
|
| 57 |
+
fn_nodes.append(node)
|
| 58 |
+
elif node.type in {"struct_specifier", "union_specifier", "enum_specifier"}:
|
| 59 |
+
udt_nodes.append(node)
|
| 60 |
+
elif node.type == ";":
|
| 61 |
+
pass
|
| 62 |
+
else:
|
| 63 |
+
other_nodes.append(node)
|
| 64 |
+
|
| 65 |
+
# Process all UDTs
|
| 66 |
+
types = FileTypeMapping()
|
| 67 |
+
for type_node in udt_nodes:
|
| 68 |
+
if not has_error(type_node):
|
| 69 |
+
try:
|
| 70 |
+
types.parse_type(type_node)
|
| 71 |
+
except:
|
| 72 |
+
pass # Ignore the misgenerated type. If it's necessary, we'll get another exception later.
|
| 73 |
+
|
| 74 |
+
if len(fn_nodes) == 0:
|
| 75 |
+
raise InvalidPredictionError("No recognizable function definitions.")
|
| 76 |
+
fn_node = fn_nodes[0]
|
| 77 |
+
assert fn_node.text is not None # To make mypy happy
|
| 78 |
+
|
| 79 |
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline #, BitsAndBytesConfig
|
| 80 |
from peft import PeftModel
|
| 81 |
|
|
|
|
| 138 |
except Exception as e:
|
| 139 |
return f"Something went wrong during inference: {str(e)}"
|
| 140 |
print("done!")
|
| 141 |
+
return clean_output(results[0]['generated_text'])
|
| 142 |
|
| 143 |
def run():
|
| 144 |
|