GusPuffy commited on
Commit
01f2fb9
1 Parent(s): 05282c1

Upload folder using huggingface_hub

Browse files
Files changed (10) hide show
  1. LICENSE +91 -0
  2. README.md +98 -0
  3. bytecode.py +66 -0
  4. config.json +2 -5
  5. convert_pyc_to_bytecode.py +56 -0
  6. decompile.py +57 -0
  7. op_codes.py +121 -0
  8. tokenizer.json +4 -52
  9. tokenizer_config.json +25 -183
  10. util.py +17 -0
LICENSE ADDED
@@ -0,0 +1,91 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ DEEPSEEK LICENSE AGREEMENT
2
+
3
+ Version 1.0, 23 October 2023
4
+
5
+ Copyright (c) 2023 DeepSeek
6
+
7
+ Section I: PREAMBLE
8
+
9
+ Large generative models are being widely adopted and used, and have the potential to transform the way individuals conceive and benefit from AI or ML technologies.
10
+
11
+ Notwithstanding the current and potential benefits that these artifacts can bring to society at large, there are also concerns about potential misuses of them, either due to their technical limitations or ethical considerations.
12
+
13
+ In short, this license strives for both the open and responsible downstream use of the accompanying model. When it comes to the open character, we took inspiration from open source permissive licenses regarding the grant of IP rights. Referring to the downstream responsible use, we added use-based restrictions not permitting the use of the model in very specific scenarios, in order for the licensor to be able to enforce the license in case potential misuses of the Model may occur. At the same time, we strive to promote open and responsible research on generative models for content generation.
14
+
15
+ Even though downstream derivative versions of the model could be released under different licensing terms, the latter will always have to include - at minimum - the same use-based restrictions as the ones in the original license (this license). We believe in the intersection between open and responsible AI development; thus, this agreement aims to strike a balance between both in order to enable responsible open-science in the field of AI.
16
+
17
+ This License governs the use of the model (and its derivatives) and is informed by the model card associated with the model.
18
+
19
+ NOW THEREFORE, You and DeepSeek agree as follows:
20
+
21
+ 1. Definitions
22
+ "License" means the terms and conditions for use, reproduction, and Distribution as defined in this document.
23
+ "Data" means a collection of information and/or content extracted from the dataset used with the Model, including to train, pretrain, or otherwise evaluate the Model. The Data is not licensed under this License.
24
+ "Output" means the results of operating a Model as embodied in informational content resulting therefrom.
25
+ "Model" means any accompanying machine-learning based assemblies (including checkpoints), consisting of learnt weights, parameters (including optimizer states), corresponding to the model architecture as embodied in the Complementary Material, that have been trained or tuned, in whole or in part on the Data, using the Complementary Material.
26
+ "Derivatives of the Model" means all modifications to the Model, works based on the Model, or any other model which is created or initialized by transfer of patterns of the weights, parameters, activations or output of the Model, to the other model, in order to cause the other model to perform similarly to the Model, including - but not limited to - distillation methods entailing the use of intermediate data representations or methods based on the generation of synthetic data by the Model for training the other model.
27
+ "Complementary Material" means the accompanying source code and scripts used to define, run, load, benchmark or evaluate the Model, and used to prepare data for training or evaluation, if any. This includes any accompanying documentation, tutorials, examples, etc, if any.
28
+ "Distribution" means any transmission, reproduction, publication or other sharing of the Model or Derivatives of the Model to a third party, including providing the Model as a hosted service made available by electronic or other remote means - e.g. API-based or web access.
29
+ "DeepSeek" (or "we") means Beijing DeepSeek Artificial Intelligence Fundamental Technology Research Co., Ltd., Hangzhou DeepSeek Artificial Intelligence Fundamental Technology Research Co., Ltd. and/or any of their affiliates.
30
+ "You" (or "Your") means an individual or Legal Entity exercising permissions granted by this License and/or making use of the Model for whichever purpose and in any field of use, including usage of the Model in an end-use application - e.g. chatbot, translator, etc.
31
+ "Third Parties" means individuals or legal entities that are not under common control with DeepSeek or You.
32
+
33
+ Section II: INTELLECTUAL PROPERTY RIGHTS
34
+
35
+ Both copyright and patent grants apply to the Model, Derivatives of the Model and Complementary Material. The Model and Derivatives of the Model are subject to additional terms as described in Section III.
36
+
37
+ 2. Grant of Copyright License. Subject to the terms and conditions of this License, DeepSeek hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare, publicly display, publicly perform, sublicense, and distribute the Complementary Material, the Model, and Derivatives of the Model.
38
+
39
+ 3. Grant of Patent License. Subject to the terms and conditions of this License and where and as applicable, DeepSeek hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this paragraph) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Model and the Complementary Material, where such license applies only to those patent claims licensable by DeepSeek that are necessarily infringed by its contribution(s). If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Model and/or Complementary Material constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for the Model and/or works shall terminate as of the date such litigation is asserted or filed.
40
+
41
+
42
+ Section III: CONDITIONS OF USAGE, DISTRIBUTION AND REDISTRIBUTION
43
+
44
+ 4. Distribution and Redistribution. You may host for Third Party remote access purposes (e.g. software-as-a-service), reproduce and distribute copies of the Model or Derivatives of the Model thereof in any medium, with or without modifications, provided that You meet the following conditions:
45
+ a. Use-based restrictions as referenced in paragraph 5 MUST be included as an enforceable provision by You in any type of legal agreement (e.g. a license) governing the use and/or distribution of the Model or Derivatives of the Model, and You shall give notice to subsequent users You Distribute to, that the Model or Derivatives of the Model are subject to paragraph 5. This provision does not apply to the use of Complementary Material.
46
+ b. You must give any Third Party recipients of the Model or Derivatives of the Model a copy of this License;
47
+ c. You must cause any modified files to carry prominent notices stating that You changed the files;
48
+ d. You must retain all copyright, patent, trademark, and attribution notices excluding those notices that do not pertain to any part of the Model, Derivatives of the Model.
49
+ e. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions - respecting paragraph 4.a. – for use, reproduction, or Distribution of Your modifications, or for any such Derivatives of the Model as a whole, provided Your use, reproduction, and Distribution of the Model otherwise complies with the conditions stated in this License.
50
+
51
+ 5. Use-based restrictions. The restrictions set forth in Attachment A are considered Use-based restrictions. Therefore You cannot use the Model and the Derivatives of the Model for the specified restricted uses. You may use the Model subject to this License, including only for lawful purposes and in accordance with the License. Use may include creating any content with, finetuning, updating, running, training, evaluating and/or reparametrizing the Model. You shall require all of Your users who use the Model or a Derivative of the Model to comply with the terms of this paragraph (paragraph 5).
52
+
53
+ 6. The Output You Generate. Except as set forth herein, DeepSeek claims no rights in the Output You generate using the Model. You are accountable for the Output you generate and its subsequent uses. No use of the output can contravene any provision as stated in the License.
54
+
55
+ Section IV: OTHER PROVISIONS
56
+
57
+ 7. Updates and Runtime Restrictions. To the maximum extent permitted by law, DeepSeek reserves the right to restrict (remotely or otherwise) usage of the Model in violation of this License.
58
+
59
+ 8. Trademarks and related. Nothing in this License permits You to make use of DeepSeek’ trademarks, trade names, logos or to otherwise suggest endorsement or misrepresent the relationship between the parties; and any rights not expressly granted herein are reserved by DeepSeek.
60
+
61
+ 9. Personal information, IP rights and related. This Model may contain personal information and works with IP rights. You commit to complying with applicable laws and regulations in the handling of personal information and the use of such works. Please note that DeepSeek's license granted to you to use the Model does not imply that you have obtained a legitimate basis for processing the related information or works. As an independent personal information processor and IP rights user, you need to ensure full compliance with relevant legal and regulatory requirements when handling personal information and works with IP rights that may be contained in the Model, and are willing to assume solely any risks and consequences that may arise from that.
62
+
63
+ 10. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, DeepSeek provides the Model and the Complementary Material on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Model, Derivatives of the Model, and the Complementary Material and assume any risks associated with Your exercise of permissions under this License.
64
+
65
+ 11. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall DeepSeek be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Model and the Complementary Material (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if DeepSeek has been advised of the possibility of such damages.
66
+
67
+ 12. Accepting Warranty or Additional Liability. While redistributing the Model, Derivatives of the Model and the Complementary Material thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of DeepSeek, and only if You agree to indemnify, defend, and hold DeepSeek harmless for any liability incurred by, or claims asserted against, DeepSeek by reason of your accepting any such warranty or additional liability.
68
+
69
+ 13. If any provision of this License is held to be invalid, illegal or unenforceable, the remaining provisions shall be unaffected thereby and remain valid as if such provision had not been set forth herein.
70
+
71
+ 14. Governing Law and Jurisdiction. This agreement will be governed and construed under PRC laws without regard to choice of law principles, and the UN Convention on Contracts for the International Sale of Goods does not apply to this agreement. The courts located in the domicile of Hangzhou DeepSeek Artificial Intelligence Fundamental Technology Research Co., Ltd. shall have exclusive jurisdiction of any dispute arising out of this agreement.
72
+
73
+ END OF TERMS AND CONDITIONS
74
+
75
+ Attachment A
76
+
77
+ Use Restrictions
78
+
79
+ You agree not to use the Model or Derivatives of the Model:
80
+
81
+ - In any way that violates any applicable national or international law or regulation or infringes upon the lawful rights and interests of any third party;
82
+ - For military use in any way;
83
+ - For the purpose of exploiting, harming or attempting to exploit or harm minors in any way;
84
+ - To generate or disseminate verifiably false information and/or content with the purpose of harming others;
85
+ - To generate or disseminate inappropriate content subject to applicable regulatory requirements;
86
+ - To generate or disseminate personal identifiable information without due authorization or for unreasonable use;
87
+ - To defame, disparage or otherwise harass others;
88
+ - For fully automated decision making that adversely impacts an individual’s legal rights or otherwise creates or modifies a binding, enforceable obligation;
89
+ - For any use intended to or which has the effect of discriminating against or harming individuals or groups based on online or offline social behavior or known or predicted personal or personality characteristics;
90
+ - To exploit any of the vulnerabilities of a specific group of persons based on their age, social, physical or mental characteristics, in order to materially distort the behavior of a person pertaining to that group in a manner that causes or is likely to cause that person or another person physical or psychological harm;
91
+ - For any use intended to or which has the effect of discriminating against individuals or groups based on legally protected characteristics or categories.
README.md ADDED
@@ -0,0 +1,98 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ license: other
3
+ license_name: deepseek-license
4
+ license_link: LICENSE
5
+ ---
6
+
7
+ <p align="center">
8
+ <img width="120px" alt="Sentient Simulations Plumbob" src="https://www.sentientsimulations.com/transparent-plumbob2.png">
9
+ </p>
10
+ <p align="center"><a href="https://www.sentientsimulations.com/">[🏠Sentient Simulations]</a> | <a href="https://discord.com/invite/JTjbydmUAp">[Discord]</a> | <a href="https://www.patreon.com/SentientSims">[Patreon]</a>
11
+ <hr>
12
+
13
+ # Sentient Simulations AI Python 3.7 Decompiler - 6.7b - v0.9
14
+
15
+ ### 1. Introduction
16
+
17
+ The Sentient Simulations AI Python Decompiler is a [deepseek-ai/deepseek-coder-6.7b-base](https://huggingface.co/deepseek-ai/deepseek-coder-6.7b-base) finetune for the specific task of decompiling Python 3.7 bytecode back to its original Python source code.
18
+
19
+ ### 2. Data Preparation
20
+
21
+ The Sentient Simulations AI Python Decompiler data used Python 3.7 source code that was then compiled to bytecode. The bytecode was used as the input and the source code was used as the output to teach the model how to generate the original source code from Python bytecode. Below are the steps to prepare the data.
22
+
23
+ 1. Grab a ton of Python code, or use something like [The Stack v2](https://huggingface.co/datasets/bigcode/the-stack-v2), and compile it using the version of Python you want to use
24
+ 1. Throw out any code that doesn't compile to that version of Python
25
+ 1. Remove all comments from the code
26
+ 1. Format all the code using [Python black](https://github.com/psf/black) for consistency
27
+ 1. Format the bytecode in a way that reduces tokens and is easier for the AI to read - I tried with custom tokens initially but I got inconsistent results
28
+ 1. Generate input output pairs for the training data
29
+ 1. [Axolotl](https://github.com/OpenAccess-AI-Collective/axolotl) Sample packing was used to prepare the data with a constant context of 16k tokens
30
+
31
+ ### 4. Training
32
+ The model was trained for 4 days on 3x3090s using [Deepspeed Zero 3 Offload](https://www.deepspeed.ai/2021/03/07/zero3-offload.html) at 16k context.
33
+
34
+ ### 5. Prerequisites
35
+ 1. Create a Python 3.7 environment to get the byte code
36
+ 1. Create a Python 3.10 environment to run the decompiler
37
+ 1. Download the python files and GGUF file
38
+ ```
39
+ conda create -n pydecompiler-37 python=3.7 -y
40
+ conda create -n pydecompiler-310 python=3.10 -y
41
+ conda activate pydecompiler-310
42
+ pip install huggingface-hub
43
+ mkdir pydecompiler
44
+ cd pydecompiler
45
+ huggingface-cli download GusPuffy/sentient-simulations-pydecompiler-3.7-6.7b-v0.9 --local-dir . --local-dir-use-symlinks False --include "*.py"
46
+ huggingface-cli download GusPuffy/sentient-simulations-pydecompiler-3.7-6.7b-v0.9-GGUF --local-dir . --local-dir-use-symlinks False --include "sentient-simulations-pydecompiler-3.7-6.7b-v0.9-q8_0.gguf"
47
+ ```
48
+ 4. [Install llama cpp](https://github.com/abetlen/llama-cpp-python?tab=readme-ov-file#installation), make sure to use the prefix during install for whatever backend you want to use if you have GPUs or only want to use CPU
49
+
50
+ ### 6. Test Example
51
+ 1. Convert a python file to source and byte code using Python 3.7
52
+ ```
53
+ conda activate pydecompiler-37
54
+ python bytecode.py bytecode.py > bytecode-decompiled.pycb
55
+ ```
56
+ 1. The bytecode has been written to bytecode-decompiled.pycb, now we need to switch to the Python 3.10 environment to run the decompiler on the test example
57
+ ```
58
+ conda activate pydecompiler-310
59
+ python decompile.py bytecode-decompiled.pycb
60
+ ```
61
+ 3. Compare the contents of the AI decompiled code in 'bytecode-decompiled.py' with the actual contents of 'bytecode.py'
62
+
63
+ ### 7. Decompilation of a Directory of .pyc files
64
+ If you have a bunch of files you want to decompile, you can run the following command to decompile the entire directory.
65
+
66
+ 1. Convert the .pyc files to bytecode strings using Python 3.7
67
+ ```
68
+ conda activate pydecompiler-37
69
+ python convert_pyc_to_bytecode.py directory_with_files
70
+ ```
71
+ 2. Decompile the bytecode strings back to Python source code (This will take some time depending on how many files there are)
72
+ ```
73
+ conda activate pydecompiler-310
74
+ python convert_pyc_to_bytecode.py directory_with_files
75
+ ```
76
+ Note that files over 15k token count are skipped.
77
+ Files over 10k will most likely be truncated due to the context limit.
78
+
79
+ At the end of the decompiled source code is a note if the context limit was reached or the AI decided it was complete:
80
+ ```
81
+ # Finish Reason: stop
82
+ # Finish Reason: length
83
+ ```
84
+
85
+ ### 8. Next Iteration
86
+
87
+ The dataset has shown pretty amazing results for decompiling files under 16k context using only a 7b model.
88
+
89
+ I would like to train a larger context model or the 34b version of deepseek coder.
90
+
91
+ Let me know if you have compute available and you are interested in training a longer context version of this tool!
92
+
93
+ ### 9. License
94
+ This is a fune tune of deepseek so checkout their license [LICENSE-MODEL](https://github.com/deepseek-ai/deepseek-coder/blob/main/LICENSE-MODEL) for more details.
95
+
96
+ ### 10. Contact
97
+
98
+ If you have any questions, please raise an issue or find me on [discord](https://discord.com/invite/JTjbydmUAp)
bytecode.py ADDED
@@ -0,0 +1,66 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import dis
2
+ import io
3
+ import re
4
+ import sys
5
+ import textwrap
6
+ from op_codes import op_codes_37
7
+
8
+
9
+ def fix_spacing(unformatted_decompiled_python):
10
+ decompiled_python = re.sub(r"[ \t]+", " ", unformatted_decompiled_python)
11
+ decompiled_python = re.sub(r"\n ", "\n", decompiled_python)
12
+ return decompiled_python
13
+
14
+
15
+ def get_byte_code(python_code):
16
+ code_object = compile(python_code, "f.py", "exec")
17
+
18
+ original_stdout = sys.stdout
19
+ string_output = io.StringIO()
20
+ sys.stdout = string_output
21
+ dis.dis(code_object)
22
+ sys.stdout = original_stdout
23
+ disassembled_pyc = string_output.getvalue()
24
+ return fix_spacing(textwrap.dedent(str(disassembled_pyc)).strip())
25
+
26
+
27
+ def extract_text_between_parentheses(input_string):
28
+ start_index = input_string.find("(")
29
+ end_index = input_string.rfind(")")
30
+
31
+ if start_index != -1 and end_index != -1 and start_index < end_index:
32
+ return input_string[start_index + 1 : end_index]
33
+ else:
34
+ return ""
35
+
36
+
37
+ def reformat_bytecode_line(input_string):
38
+ if input_string.startswith("Disassembly of <"):
39
+ return None
40
+
41
+ for code in op_codes_37:
42
+ if code in input_string:
43
+ reformatted_string = extract_text_between_parentheses(input_string)
44
+ return f"{code} {reformatted_string}".strip()
45
+
46
+ return input_string
47
+
48
+
49
+ def format_bytecode(bytecode: str):
50
+ formatted_strings = []
51
+ for line in bytecode.split("\n"):
52
+ reformatted_string = reformat_bytecode_line(line)
53
+ if reformatted_string is not None:
54
+ formatted_strings.append(reformatted_string)
55
+ return "\n".join(formatted_strings).strip()
56
+
57
+
58
+ if __name__ == "__main__":
59
+ file_path = sys.argv[1]
60
+ print(f"Converting python source code to byte code {file_path}")
61
+ with open(file_path, "r") as file:
62
+ file_contents = file.read()
63
+ print(file_contents)
64
+ byte_code = get_byte_code(file_contents)
65
+
66
+ print(format_bytecode(bytecode=byte_code))
config.json CHANGED
@@ -1,10 +1,7 @@
1
  {
2
- "_name_or_path": "GusPuffy/sentient-simulations-pydecompiler-3.7-6.7b-v0.9",
3
  "architectures": [
4
  "LlamaForCausalLM"
5
  ],
6
- "attention_bias": false,
7
- "attention_dropout": 0.0,
8
  "bos_token_id": 32013,
9
  "eos_token_id": 32014,
10
  "hidden_act": "silu",
@@ -24,8 +21,8 @@
24
  },
25
  "rope_theta": 100000,
26
  "tie_word_embeddings": false,
27
- "torch_dtype": "float32",
28
- "transformers_version": "4.38.2",
29
  "use_cache": true,
30
  "vocab_size": 32256
31
  }
 
1
  {
 
2
  "architectures": [
3
  "LlamaForCausalLM"
4
  ],
 
 
5
  "bos_token_id": 32013,
6
  "eos_token_id": 32014,
7
  "hidden_act": "silu",
 
21
  },
22
  "rope_theta": 100000,
23
  "tie_word_embeddings": false,
24
+ "torch_dtype": "bfloat16",
25
+ "transformers_version": "4.34.1",
26
  "use_cache": true,
27
  "vocab_size": 32256
28
  }
convert_pyc_to_bytecode.py ADDED
@@ -0,0 +1,56 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import dis
2
+ import io
3
+ import marshal
4
+ import os
5
+ import sys
6
+ import textwrap
7
+
8
+ from bytecode import fix_spacing, format_bytecode
9
+
10
+
11
+ def disassemble_pyc_file(pyc_file) -> str:
12
+ with open(pyc_file, "rb") as f:
13
+ header = f.read(16)
14
+ magic_number = header[:2]
15
+ if 3394 != int.from_bytes(magic_number, "little"):
16
+ print(f"{pyc_file} not compiled with Python 3.7")
17
+ return None
18
+
19
+ bytecode = marshal.load(f)
20
+ original_stdout = sys.stdout
21
+ string_output = io.StringIO()
22
+ sys.stdout = string_output
23
+ dis.dis(bytecode)
24
+ sys.stdout = original_stdout
25
+
26
+ disassembled_pyc = string_output.getvalue()
27
+ byte_code = fix_spacing(textwrap.dedent(str(disassembled_pyc)).strip())
28
+ formatted_bytecode = format_bytecode(bytecode=byte_code)
29
+
30
+ return formatted_bytecode
31
+
32
+
33
+ def convert_pyc_to_bytecode(file_path: str):
34
+ formatted_bytecode = disassemble_pyc_file(file_path)
35
+
36
+ if formatted_bytecode is None:
37
+ return
38
+
39
+ with open(file_path + "b", "w") as bytecode_file:
40
+ print(f"Converted {file_path} to bytecode")
41
+ bytecode_file.write(formatted_bytecode)
42
+
43
+
44
+ if __name__ == "__main__":
45
+ path = sys.argv[1]
46
+ if os.path.isfile(path) and path.endswith(".pyc"):
47
+ convert_pyc_to_bytecode(path)
48
+ elif os.path.isdir(path):
49
+ for root, _, files in os.walk(path):
50
+ for file in files:
51
+ file_path = os.path.join(root, file)
52
+
53
+ if not file.endswith(".pyc"):
54
+ continue
55
+
56
+ convert_pyc_to_bytecode(file_path)
decompile.py ADDED
@@ -0,0 +1,57 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import sys
3
+ from llama_cpp import Llama
4
+
5
+ from util import create_prompt, is_bytecode_empty
6
+
7
+ llm = Llama(
8
+ model_path="/home/guspuffy/projects/output-merged/GGUF/sentient-simulations-pydecompiler-3.7-6.7b-v0.9-q8_0.gguf",
9
+ n_gpu_layers=-1,
10
+ last_n_tokens_size=0,
11
+ n_ctx=16384,
12
+ )
13
+
14
+
15
+ def decompile_pycb(file_path: str, force_overwrite=False):
16
+ source_code_path = file_path[:-2]
17
+
18
+ with open(file_path, "r") as bytecode_file:
19
+ bytecode = bytecode_file.read()
20
+
21
+ if is_bytecode_empty(bytecode):
22
+ return "# Empty file"
23
+
24
+ prompt = create_prompt(bytecode)
25
+
26
+ tokens = llm.tokenize(text=prompt.encode("utf-8"), add_bos=True, special=True)
27
+ if len(tokens) > 15000:
28
+ return None
29
+
30
+ output = llm(prompt, max_tokens=None, echo=False)
31
+ result = output["choices"][0]
32
+ source_code = result.get("text").strip()
33
+ finish_reason = result.get("finish_reason")
34
+
35
+ source_code += f"\n\n# Finish Reason: {finish_reason}\n"
36
+
37
+ with open(source_code_path, "w") as source_code_file:
38
+ source_code_file.write(source_code)
39
+
40
+ return source_code
41
+
42
+
43
+ if __name__ == "__main__":
44
+ path = sys.argv[1]
45
+ if os.path.isfile(path):
46
+ output = decompile_pycb(path)
47
+ print(output)
48
+ elif os.path.isdir(path):
49
+ for root, dirs, files in os.walk(path):
50
+ for file in files:
51
+ if file.endswith(".pycb") and not os.path.exists(
52
+ os.path.join(root, file[:-2])
53
+ ):
54
+ file_path = os.path.join(root, file)
55
+ output = decompile_pycb(file_path)
56
+ if output:
57
+ print(f"Decompiled {file_path}")
op_codes.py ADDED
@@ -0,0 +1,121 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ op_codes_37 = [
2
+ "POP_TOP",
3
+ "ROT_TWO",
4
+ "ROT_THREE",
5
+ "DUP_TOP",
6
+ "DUP_TOP_TWO",
7
+ "NOP",
8
+ "UNARY_POSITIVE",
9
+ "UNARY_NEGATIVE",
10
+ "UNARY_NOT",
11
+ "UNARY_INVERT",
12
+ "BINARY_MATRIX_MULTIPLY",
13
+ "INPLACE_MATRIX_MULTIPLY",
14
+ "BINARY_POWER",
15
+ "BINARY_MULTIPLY",
16
+ "BINARY_MODULO",
17
+ "BINARY_ADD",
18
+ "BINARY_SUBTRACT",
19
+ "BINARY_SUBSCR",
20
+ "BINARY_FLOOR_DIVIDE",
21
+ "BINARY_TRUE_DIVIDE",
22
+ "INPLACE_FLOOR_DIVIDE",
23
+ "INPLACE_TRUE_DIVIDE",
24
+ "GET_AITER",
25
+ "GET_ANEXT",
26
+ "BEFORE_ASYNC_WITH",
27
+ "INPLACE_ADD",
28
+ "INPLACE_SUBTRACT",
29
+ "INPLACE_MULTIPLY",
30
+ "INPLACE_MODULO",
31
+ "STORE_SUBSCR",
32
+ "DELETE_SUBSCR",
33
+ "BINARY_LSHIFT",
34
+ "BINARY_RSHIFT",
35
+ "BINARY_AND",
36
+ "BINARY_XOR",
37
+ "BINARY_OR",
38
+ "INPLACE_POWER",
39
+ "GET_ITER",
40
+ "GET_YIELD_FROM_ITER",
41
+ "PRINT_EXPR",
42
+ "LOAD_BUILD_CLASS",
43
+ "YIELD_FROM",
44
+ "GET_AWAITABLE",
45
+ "INPLACE_LSHIFT",
46
+ "INPLACE_RSHIFT",
47
+ "INPLACE_AND",
48
+ "INPLACE_XOR",
49
+ "INPLACE_OR",
50
+ "BREAK_LOOP",
51
+ "WITH_CLEANUP_START",
52
+ "WITH_CLEANUP_FINISH",
53
+ "RETURN_VALUE",
54
+ "IMPORT_STAR",
55
+ "SETUP_ANNOTATIONS",
56
+ "YIELD_VALUE",
57
+ "POP_BLOCK",
58
+ "END_FINALLY",
59
+ "POP_EXCEPT",
60
+ "STORE_NAME",
61
+ "DELETE_NAME",
62
+ "UNPACK_SEQUENCE",
63
+ "FOR_ITER",
64
+ "UNPACK_EX",
65
+ "STORE_ATTR",
66
+ "DELETE_ATTR",
67
+ "STORE_GLOBAL",
68
+ "DELETE_GLOBAL",
69
+ "LOAD_CONST",
70
+ "LOAD_NAME",
71
+ "BUILD_TUPLE",
72
+ "BUILD_LIST",
73
+ "BUILD_SET",
74
+ "BUILD_MAP",
75
+ "LOAD_ATTR",
76
+ "COMPARE_OP",
77
+ "IMPORT_NAME",
78
+ "IMPORT_FROM",
79
+ "JUMP_FORWARD",
80
+ "JUMP_IF_FALSE_OR_POP",
81
+ "JUMP_IF_TRUE_OR_POP",
82
+ "JUMP_ABSOLUTE",
83
+ "POP_JUMP_IF_FALSE",
84
+ "POP_JUMP_IF_TRUE",
85
+ "LOAD_GLOBAL",
86
+ "CONTINUE_LOOP",
87
+ "SETUP_LOOP",
88
+ "SETUP_EXCEPT",
89
+ "SETUP_FINALLY",
90
+ "LOAD_FAST",
91
+ "STORE_FAST",
92
+ "DELETE_FAST",
93
+ "RAISE_VARARGS",
94
+ "CALL_FUNCTION",
95
+ "MAKE_FUNCTION",
96
+ "BUILD_SLICE",
97
+ "LOAD_CLOSURE",
98
+ "LOAD_DEREF",
99
+ "STORE_DEREF",
100
+ "DELETE_DEREF",
101
+ "CALL_FUNCTION_KW",
102
+ "CALL_FUNCTION_EX",
103
+ "SETUP_WITH",
104
+ "EXTENDED_ARG",
105
+ "LIST_APPEND",
106
+ "SET_ADD",
107
+ "MAP_ADD",
108
+ "LOAD_CLASSDEREF",
109
+ "BUILD_LIST_UNPACK",
110
+ "BUILD_MAP_UNPACK",
111
+ "BUILD_MAP_UNPACK_WITH_CALL",
112
+ "BUILD_TUPLE_UNPACK",
113
+ "BUILD_SET_UNPACK",
114
+ "SETUP_ASYNC_WITH",
115
+ "FORMAT_VALUE",
116
+ "BUILD_CONST_KEY_MAP",
117
+ "BUILD_STRING",
118
+ "BUILD_TUPLE_UNPACK_WITH_CALL",
119
+ "LOAD_METHOD",
120
+ "CALL_METHOD",
121
+ ]
tokenizer.json CHANGED
@@ -254,58 +254,10 @@
254
  ]
255
  },
256
  "post_processor": {
257
- "type": "TemplateProcessing",
258
- "single": [
259
- {
260
- "SpecialToken": {
261
- "id": "<|begin▁of▁sentence|>",
262
- "type_id": 0
263
- }
264
- },
265
- {
266
- "Sequence": {
267
- "id": "A",
268
- "type_id": 0
269
- }
270
- }
271
- ],
272
- "pair": [
273
- {
274
- "SpecialToken": {
275
- "id": "<|begin▁of▁sentence|>",
276
- "type_id": 0
277
- }
278
- },
279
- {
280
- "Sequence": {
281
- "id": "A",
282
- "type_id": 0
283
- }
284
- },
285
- {
286
- "SpecialToken": {
287
- "id": "<|begin▁of▁sentence|>",
288
- "type_id": 1
289
- }
290
- },
291
- {
292
- "Sequence": {
293
- "id": "B",
294
- "type_id": 1
295
- }
296
- }
297
- ],
298
- "special_tokens": {
299
- "<|begin▁of▁sentence|>": {
300
- "id": "<|begin▁of▁sentence|>",
301
- "ids": [
302
- 32013
303
- ],
304
- "tokens": [
305
- "<|begin▁of▁sentence|>"
306
- ]
307
- }
308
- }
309
  },
310
  "decoder": {
311
  "type": "ByteLevel",
 
254
  ]
255
  },
256
  "post_processor": {
257
+ "type": "ByteLevel",
258
+ "add_prefix_space": true,
259
+ "trim_offsets": false,
260
+ "use_regex": true
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
261
  },
262
  "decoder": {
263
  "type": "ByteLevel",
tokenizer_config.json CHANGED
@@ -1,192 +1,34 @@
1
  {
2
  "add_bos_token": true,
3
  "add_eos_token": false,
4
- "added_tokens_decoder": {
5
- "32000": {
6
- "content": "õ",
7
- "lstrip": false,
8
- "normalized": true,
9
- "rstrip": false,
10
- "single_word": false,
11
- "special": false
12
- },
13
- "32001": {
14
- "content": "÷",
15
- "lstrip": false,
16
- "normalized": true,
17
- "rstrip": false,
18
- "single_word": false,
19
- "special": false
20
- },
21
- "32002": {
22
- "content": "Á",
23
- "lstrip": false,
24
- "normalized": true,
25
- "rstrip": false,
26
- "single_word": false,
27
- "special": false
28
- },
29
- "32003": {
30
- "content": "ý",
31
- "lstrip": false,
32
- "normalized": true,
33
- "rstrip": false,
34
- "single_word": false,
35
- "special": false
36
- },
37
- "32004": {
38
- "content": "À",
39
- "lstrip": false,
40
- "normalized": true,
41
- "rstrip": false,
42
- "single_word": false,
43
- "special": false
44
- },
45
- "32005": {
46
- "content": "ÿ",
47
- "lstrip": false,
48
- "normalized": true,
49
- "rstrip": false,
50
- "single_word": false,
51
- "special": false
52
- },
53
- "32006": {
54
- "content": "ø",
55
- "lstrip": false,
56
- "normalized": true,
57
- "rstrip": false,
58
- "single_word": false,
59
- "special": false
60
- },
61
- "32007": {
62
- "content": "ú",
63
- "lstrip": false,
64
- "normalized": true,
65
- "rstrip": false,
66
- "single_word": false,
67
- "special": false
68
- },
69
- "32008": {
70
- "content": "þ",
71
- "lstrip": false,
72
- "normalized": true,
73
- "rstrip": false,
74
- "single_word": false,
75
- "special": false
76
- },
77
- "32009": {
78
- "content": "ü",
79
- "lstrip": false,
80
- "normalized": true,
81
- "rstrip": false,
82
- "single_word": false,
83
- "special": false
84
- },
85
- "32010": {
86
- "content": "ù",
87
- "lstrip": false,
88
- "normalized": true,
89
- "rstrip": false,
90
- "single_word": false,
91
- "special": false
92
- },
93
- "32011": {
94
- "content": "ö",
95
- "lstrip": false,
96
- "normalized": true,
97
- "rstrip": false,
98
- "single_word": false,
99
- "special": false
100
- },
101
- "32012": {
102
- "content": "û",
103
- "lstrip": false,
104
- "normalized": true,
105
- "rstrip": false,
106
- "single_word": false,
107
- "special": false
108
- },
109
- "32013": {
110
- "content": "<|begin▁of▁sentence|>",
111
- "lstrip": false,
112
- "normalized": true,
113
- "rstrip": false,
114
- "single_word": false,
115
- "special": true
116
- },
117
- "32014": {
118
- "content": "<|end▁of▁sentence|>",
119
- "lstrip": false,
120
- "normalized": true,
121
- "rstrip": false,
122
- "single_word": false,
123
- "special": true
124
- },
125
- "32015": {
126
- "content": "<|fim▁hole|>",
127
- "lstrip": false,
128
- "normalized": true,
129
- "rstrip": false,
130
- "single_word": false,
131
- "special": false
132
- },
133
- "32016": {
134
- "content": "<|fim▁begin|>",
135
- "lstrip": false,
136
- "normalized": true,
137
- "rstrip": false,
138
- "single_word": false,
139
- "special": false
140
- },
141
- "32017": {
142
- "content": "<|fim▁end|>",
143
- "lstrip": false,
144
- "normalized": true,
145
- "rstrip": false,
146
- "single_word": false,
147
- "special": false
148
- },
149
- "32018": {
150
- "content": "<pad>",
151
- "lstrip": false,
152
- "normalized": true,
153
- "rstrip": false,
154
- "single_word": false,
155
- "special": false
156
- },
157
- "32019": {
158
- "content": "<|User|>",
159
- "lstrip": false,
160
- "normalized": true,
161
- "rstrip": false,
162
- "single_word": false,
163
- "special": false
164
- },
165
- "32020": {
166
- "content": "<|Assistant|>",
167
- "lstrip": false,
168
- "normalized": true,
169
- "rstrip": false,
170
- "single_word": false,
171
- "special": false
172
- },
173
- "32021": {
174
- "content": "<|EOT|>",
175
- "lstrip": false,
176
- "normalized": true,
177
- "rstrip": false,
178
- "single_word": false,
179
- "special": false
180
- }
181
  },
182
- "bos_token": "<|begin▁of▁sentence|>",
183
  "clean_up_tokenization_spaces": false,
184
- "eos_token": "<|end▁of▁sentence|>",
 
 
 
 
 
 
 
185
  "legacy": true,
186
  "model_max_length": 16384,
187
- "pad_token": "<|end▁of▁sentence|>",
 
 
 
 
 
 
 
188
  "sp_model_kwargs": {},
189
- "tokenizer_class": "LlamaTokenizer",
190
  "unk_token": null,
191
- "use_default_system_prompt": false
192
- }
 
1
  {
2
  "add_bos_token": true,
3
  "add_eos_token": false,
4
+ "bos_token": {
5
+ "__type": "AddedToken",
6
+ "content": "<|begin▁of▁sentence|>",
7
+ "lstrip": false,
8
+ "normalized": true,
9
+ "rstrip": false,
10
+ "single_word": false
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
  },
 
12
  "clean_up_tokenization_spaces": false,
13
+ "eos_token": {
14
+ "__type": "AddedToken",
15
+ "content": "<|end▁of▁sentence|>",
16
+ "lstrip": false,
17
+ "normalized": true,
18
+ "rstrip": false,
19
+ "single_word": false
20
+ },
21
  "legacy": true,
22
  "model_max_length": 16384,
23
+ "pad_token": {
24
+ "__type": "AddedToken",
25
+ "content": "<|end▁of▁sentence|>",
26
+ "lstrip": false,
27
+ "normalized": true,
28
+ "rstrip": false,
29
+ "single_word": false
30
+ },
31
  "sp_model_kwargs": {},
 
32
  "unk_token": null,
33
+ "tokenizer_class": "LlamaTokenizerFast"
34
+ }
util.py ADDED
@@ -0,0 +1,17 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ def create_prompt(bytecode: str):
2
+ return (
3
+ "\n\n".join(
4
+ [
5
+ "### Instruction:",
6
+ "Using the provided Python 3.7 bytecode chunk, continue outputting the Python source code",
7
+ "### Input:",
8
+ bytecode,
9
+ "### Response:",
10
+ ]
11
+ )
12
+ + "\n\n"
13
+ )
14
+
15
+
16
+ def is_bytecode_empty(bytecode: str):
17
+ return bytecode.strip() == "LOAD_CONST None\nRETURN_VALUE"