ejschwartz commited on
Commit
7e6be15
·
1 Parent(s): 09e6242

Install codealign

Browse files
Files changed (2) hide show
  1. Dockerfile +2 -0
  2. main.py +26 -0
Dockerfile CHANGED
@@ -40,6 +40,8 @@ RUN --mount=type=secret,id=GH_TOKEN,mode=0444,required=true \
40
 
41
  RUN conda env create -f idioms/env.yml
42
 
 
 
43
  ENV PATH="/home/user/miniconda3/envs/idioms/bin:${PATH}"
44
  ENV PYTHONPATH="${PYTHONPATH}:${HOME}/app/idioms"
45
 
 
40
 
41
  RUN conda env create -f idioms/env.yml
42
 
43
+ RUN git clone https://github.com/squaresLab/codealign.git && pip install codealign/.
44
+
45
  ENV PATH="/home/user/miniconda3/envs/idioms/bin:${PATH}"
46
  ENV PYTHONPATH="${PYTHONPATH}:${HOME}/app/idioms"
47
 
main.py CHANGED
@@ -30,8 +30,14 @@ def canonicalize_output(decompiled: str):
30
  from prepare import (
31
  parser,
32
  FileTypeMapping,
 
33
  has_error,
 
 
 
 
34
  )
 
35
  from tree_sitter import Node
36
 
37
  class InvalidPredictionError(Exception):
@@ -70,6 +76,26 @@ def clean_output(raw_prediction: str) -> str:
70
  fn_node = fn_nodes[0]
71
  assert fn_node.text is not None # To make mypy happy
72
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
73
  from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline #, BitsAndBytesConfig
74
  from peft import PeftModel
75
 
 
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):
 
76
  fn_node = fn_nodes[0]
77
  assert fn_node.text is not None # To make mypy happy
78
 
79
+ # We don't track or measure the accuracy of types not associated with variables (though this is taken into account when computing an alignment).
80
+ # However PreprocessedFunction will throw an exception if it encounters an unrecognized type in a typecast.
81
+ # To prevent this, we add generic placholder types.
82
+ predicted_body = fn_node.child_by_field_name("body")
83
+ assert predicted_body is not None
84
+ try: # Add placholders in a try-except in case a non-variable type is unparsable.
85
+ add_placeholders_for_nonvariable_types(predicted_body, types)
86
+ predicted_fn = PreprocessedFunction(fn_node, types)
87
+ except (TypeNotFoundError, TypeNotDefinedError, UnsupportedFeatureError, AssertionError):
88
+ # We need the variable-type mapping that PreprocessedFunction provides to standardize the field names
89
+ # for the alignment. We don't return here, however, because PreprocessedFunction is very strict about
90
+ # types; a function may be syntatically, or arguably, semantically valid but fail here. Thus, we
91
+ # assign predicted_fn to None here and then do the early return later.
92
+ raise InvalidPredictionError("Invalid type used in predicted function.")
93
+
94
+ predicted_code = fn_node.text.decode()
95
+ predicted_udts = get_all_user_defined_types(predicted_fn)
96
+
97
+ return predicted_code + "\n" + "\n".join(udt.declaration("") for udt in predicted_udts)
98
+
99
  from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline #, BitsAndBytesConfig
100
  from peft import PeftModel
101