diff --git "a/\347\250\213\345\272\217/LICENSE.txt" "b/\347\250\213\345\272\217/LICENSE.txt" new file mode 100644 index 0000000..261eeb9 --- /dev/null +++ "b/\347\250\213\345\272\217/LICENSE.txt" @@ -0,0 +1,201 @@ + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) 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 Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. 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 any Contributor 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 + Work (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 such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works 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 any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. diff --git "a/\347\250\213\345\272\217/ni_en_2.0.py" "b/\347\250\213\345\272\217/ni_en_2.0.py" new file mode 100644 index 0000000..a78bda8 --- /dev/null +++ "b/\347\250\213\345\272\217/ni_en_2.0.py" @@ -0,0 +1,543 @@ +''' + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) 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 Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. 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 any Contributor 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 + Work (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 such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works 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 any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +''' + +#Translated from ni2_zh.py by CodeGeeX. +#All rights reserved. No commercial use, personal use only. + +# Import necessary modules +import sys +import os +import subprocess +import json +from zhipuai import ZhipuAI + +# Define configuration file path +CONFIG_FILE = 'config.json' + +# Load API Key +def load_api_key(): + # If the configuration file exists + if os.path.exists(CONFIG_FILE): + # Open the configuration file + with open(CONFIG_FILE, 'r') as file: + # Load the configuration file content + config = json.load(file) + # Return API Key, API choice, and whether the terms have been agreed to + return config.get('api_key'), config.get('api_choice'), config.get('agreed', False) + # If the configuration file does not exist, return None + return None, None, False + +# Save API Key +def save_api_key(api_key, api_choice, agreed): + # Create a configuration dictionary + config = { + 'api_key': api_key, + 'api_choice': api_choice, + 'agreed': agreed + } + # Open the configuration file + with open(CONFIG_FILE, 'w') as file: + # Write the configuration dictionary to the configuration file + json.dump(config, file) + +# Clear configuration file +def clear_config(): + # If the configuration file exists + if os.path.exists(CONFIG_FILE): + # Delete the configuration file + os.remove(CONFIG_FILE) + # Print a prompt message + print("Configuration file has been cleared") + else: + # Print a prompt message + print("Configuration file does not exist") + +# Create project folder +def create_project(address, project_name): + # Concatenate project path + path = os.path.join(address, project_name) + # Create project folder + os.makedirs(path, exist_ok=True) + # Print a prompt message + print(f"Project folder created successfully, path: {path}") + +# Create .n file +def create_n_file(address, file_name): + # Concatenate file path + path = os.path.join(address, file_name + '.n') + # Create file + with open(path, 'w', encoding='utf-8') as file: + file.write('') + # Print a prompt message + print(f"File created successfully, path: {path}") + +# Open .n file with Notepad +def open_n_file_with_notepad(address): + # Use subprocess module to run Notepad + subprocess.run(['notepad.exe', address]) + +# Convert natural language to Python code +def transform_code(address, key, choose): + # Open file + with open(address, 'r', encoding='utf-8') as file: + code = file.read() + + # Construct prompt + prompt = f"Please convert the following natural language to Python statements, without any explanation: {code}" + + # Initialize response + response = None + # Call different APIs based on the choice + if choose == "1": + # Call ZhipuAI API + client = ZhipuAI(api_key=key) + model_name = "glm-4" + try: + response = client.chat.completions.create( + model=model_name, + messages=[{"role": "user", "content": prompt}] + ) + except Exception as e: + print(f"API call failed: {e}") + return + elif choose == "2": + # Call OpenAI API + import openai + openai.api_key = key + try: + response = openai.Completion.create( + engine="davinci-codex", + prompt=prompt, + ) + except Exception as e: + print(f"API call failed: {e}") + return + + # If response is None, print a prompt message + if response is None: + print("API call did not return a result, please check your API Key and network connection") + return + + # Print response + print(response) + + # Process response + try: + if hasattr(response, 'choices') and response.choices: + choice = response.choices[0] + if hasattr(choice, 'message') and hasattr(choice.message, 'content'): + message_content = choice.message.content.replace("`", "").replace("python", "") + else: + message_content = choice.text.replace("`", "").replace("python", "") + + # Concatenate new file path + new_address = os.path.join(os.path.dirname(address), os.path.splitext(os.path.basename(address))[0] + '.py') + # Write to new file + with open(new_address, 'w', encoding='utf-8') as file: + file.write(message_content.strip()) + # Print a prompt message + print(f"Code conversion successful, path: {new_address}") + else: + print("The object returned by the API does not have a 'choices' attribute or is empty") + except (AttributeError, KeyError) as e: + print(f"Error processing API response: {e}") + +# Run Python script +def run_python_script(address): + # Concatenate new file path + new_path = address.replace('.n', '.py') + # Use subprocess module to run Python script + subprocess.run(['python', new_path]) + +# Install Python package +def install_package(package_name): + # Create bat folder + bat_folder = 'bat' + if not os.path.exists(bat_folder): + os.makedirs(bat_folder) + + # Create bat file + bat_file_path = os.path.join(bat_folder, 'install.bat') + with open(bat_file_path, 'w') as bat_file: + bat_file.write(f'pip install {package_name}') + + # Use subprocess module to run bat file + subprocess.run([bat_file_path], shell=True) + +# Uninstall Python package +def uninstall_package(package_name): + # Create bat folder + bat_folder = 'bat' + if not os.path.exists(bat_folder): + os.makedirs(bat_folder) + + # Create bat file + bat_file_path = os.path.join(bat_folder, 'uninstall.bat') + with open(bat_file_path, 'w') as bat_file: + bat_file.write(f'pip uninstall {package_name}') + + # Use subprocess module to run bat file + subprocess.run([bat_file_path], shell=True) + +# Update pip +def up_pip(): + # Create bat folder + bat_folder = 'bat' + if not os.path.exists(bat_folder): + os.makedirs(bat_folder) + + # Create bat file + bat_file_path = os.path.join(bat_folder, 'up.bat') + with open(bat_file_path, 'w') as bat_file: + bat_file.write("pip install --upgrade pip") + + # Use subprocess module to run bat file + subprocess.run([bat_file_path], shell=True) + +# List installed Python packages +def blist(): + # Create bat folder + bat_folder = 'bat' + if not os.path.exists(bat_folder): + os.makedirs(bat_folder) + + # Create bat file + bat_file_path = os.path.join(bat_folder, 'list.bat') + with open(bat_file_path, 'w') as bat_file: + bat_file.write("pip list") + + # Use subprocess module to run bat file + subprocess.run([bat_file_path], shell=True) + +# Show information of specified Python package +def bshow(package_name): + # Create bat folder + bat_folder = 'bat' + if not os.path.exists(bat_folder): + os.makedirs(bat_folder) + + # Create bat file + bat_file_path = os.path.join(bat_folder, 'show.bat') + with open(bat_file_path, 'w') as bat_file: + bat_file.write(f'pip show {package_name}') + + # Use subprocess module to run bat file + subprocess.run([bat_file_path], shell=True) + +# Revoke agreement +def clear_agreement(): + # If the configuration file exists + if os.path.exists(CONFIG_FILE): + # Open the configuration file + with open(CONFIG_FILE, 'r') as file: + # Load the configuration file content + config = json.load(file) + # Set whether the terms have been agreed to as False + config['agreed'] = False + # Open the configuration file + with open(CONFIG_FILE, 'w') as file: + # Write the configuration dictionary to the configuration file + json.dump(config, file) + # Print a prompt message + print("Agreement has been revoked") + else: + # Print a prompt message + print("Configuration file does not exist") + +# Print terms and conditions +def print_terms_and_conditions(): + # Define terms and conditions + terms = "Open Source Program Terms of Use\n1. License Grant\nThis program is released under the Apache License Version 2.0 (hereinafter referred to as 'Apache 2.0'). According to Apache 2.0, you can freely use, copy, modify and distribute this program, regardless of whether it has been modified, but must comply with the following conditions:\na. You must retain the copyright notice and disclaimer of this program.\nb. You must provide clear statements and/or appropriate documentation indicating your modifications to the original work.\nc. You must provide appropriate legal statements and copyright statements indicating the source and use of this program under the Apache 2.0 license.\n2. API Use\nThis program supports two types of APIs: ZhipuAI and OpenAI. Before using this program, you need to agree to the terms of use of the API provider and obtain the API Key by yourself. The author of this program does not provide API Keys and is not responsible for any consequences of API use.\n3. Software Function\nThis program is mainly used to convert natural language to Python code. You can use this program to create projects, files, and perform code conversion, running, and installation of dependencies, etc. However, please note that this program does not guarantee the correctness of the conversion results, and you need to check and verify the correctness of the code by yourself.\n4. Privacy Policy\nThis program does not collect any personal information of yours. However, please note that the API provider may collect information related to your use of the API, please refer to the privacy policy of the API provider for details.\n5. Disclaimer\nThis program is provided 'as is', and the author does not provide any form of express or implied warranty, including but not limited to the warranties of merchantability, suitability for a particular purpose, and non-infringement. In the process of using this program, the author does not assume any direct, indirect, incidental, special, or consequential losses or damages arising from the use of this program.\n6. Modification and Distribution\nYou can freely modify and distribute the source code of this program, but must comply with the requirements of the Apache 2.0 license, including retaining the copyright notice, disclaimer, and contributor rights notice.\n7. Applicable Law and Dispute Resolution\nThe interpretation, validity, and enforcement of these terms and conditions shall be governed by the laws of the People's Republic of China. In the event of any disputes arising from these terms and conditions, the parties shall attempt to resolve the disputes amicably; if the disputes cannot be resolved amicably, either party may bring a lawsuit to the people's court in the location where the author of this program is located.\n8. Other\nAll rights of this program belong to the author, and all rights are reserved. If you have any questions or suggestions about the terms of use of this program, please contact the author.\nBy agreeing to the above, you can continue to use this program." + + # Print terms and conditions + print(terms) + +# Main function +def main(): + # Print terms and conditions + print_terms_and_conditions() + # Load API Key + api_key, api_choice, agreed = load_api_key() + # If the terms have not been agreed to + if not agreed: + # Input agreement + agree_input = input("Enter 'agree' to continue: ") + # If the input is not 'agree' + if agree_input != "agree": + # Print a prompt message + print("You must agree to the terms to continue using the program.") + # Exit the program + sys.exit() + # Input API choice + api_choice = input("Choose API: 1. ZhipuAI 2. OpenAI: ") + # Input API Key + api_key = input("Enter API Key: ") + # Set as agreed + agreed = True + # Save API Key + save_api_key(api_key, api_choice, agreed) + + # Loop to execute commands + while True: + # Input command + cmd = input(">>> ") + # Split command + cmd_parts = cmd.split() + + # Check command + if cmd == "check+": + # Print version information + print("Ni_en_2.0, NITSC all rights reserved") + elif "creat+" in cmd: + # Check if the command is to create a project + if "project+" in cmd: + # Create project + create_project(cmd_parts[2], cmd_parts[3]) + # Check if the command is to create a file + elif "n+" in cmd: + # Create file + create_n_file(cmd_parts[2], cmd_parts[3]) + else: + # Print a prompt message + print("Invalid command, please check if the syntax of the command is correct") + elif "open+" in cmd: + # Open file + open_n_file_with_notepad(cmd_parts[1]) + elif "trans+" in cmd: + # Convert code + transform_code(cmd_parts[1], api_key, api_choice) + elif "run+" in cmd: + # Run Python script + run_python_script(cmd_parts[1]) + elif "install+" in cmd: + # Install Python package + install_package(cmd_parts[1]) + elif "uninstall+" in cmd: + # Uninstall Python package + uninstall_package(cmd_parts[1]) + elif "up+" in cmd: + # Update pip + up_pip() + elif "blist+" in cmd: + # List installed Python packages + blist() + elif "bshow+" in cmd: + # Show information of specified Python package + bshow(cmd_parts[1]) + elif "clean+" in cmd: + # Clear configuration file + if "agree" in cmd_parts: + clear_agreement() + elif "key" in cmd_parts: + clear_config() + elif "exit+" in cmd: + # Exit program + sys.exit() + else: + # Print a prompt message + print("Invalid command, please check if the syntax of the command is correct") + +# If it is the main program, execute the main function +if __name__ == "__main__": + main() diff --git "a/\347\250\213\345\272\217/ni_en_2.1.1.20240706_beta.py" "b/\347\250\213\345\272\217/ni_en_2.1.1.20240706_beta.py" new file mode 100644 index 0000000..4c99638 --- /dev/null +++ "b/\347\250\213\345\272\217/ni_en_2.1.1.20240706_beta.py" @@ -0,0 +1,543 @@ +''' + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) 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 Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. 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 any Contributor 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 + Work (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 such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works 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 any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +''' + +#Translated from ni2_zh.py by CodeGeeX. +#All rights reserved. No commercial use, personal use only. + +# Import necessary modules +import sys +import os +import subprocess +import json +from zhipuai import ZhipuAI + +# Define configuration file path +CONFIG_FILE = 'config.json' + +# Load API Key +def load_api_key(): + # If the configuration file exists + if os.path.exists(CONFIG_FILE): + # Open the configuration file + with open(CONFIG_FILE, 'r') as file: + # Load the configuration file content + config = json.load(file) + # Return API Key, API choice, and whether the terms have been agreed to + return config.get('api_key'), config.get('api_choice'), config.get('agreed', False) + # If the configuration file does not exist, return None + return None, None, False + +# Save API Key +def save_api_key(api_key, api_choice, agreed): + # Create a configuration dictionary + config = { + 'api_key': api_key, + 'api_choice': api_choice, + 'agreed': agreed + } + # Open the configuration file + with open(CONFIG_FILE, 'w') as file: + # Write the configuration dictionary to the configuration file + json.dump(config, file) + +# Clear configuration file +def clear_config(): + # If the configuration file exists + if os.path.exists(CONFIG_FILE): + # Delete the configuration file + os.remove(CONFIG_FILE) + # Print a prompt message + print("Configuration file has been cleared") + else: + # Print a prompt message + print("Configuration file does not exist") + +# Create project folder +def create_project(address, project_name): + # Concatenate project path + path = os.path.join(address, project_name) + # Create project folder + os.makedirs(path, exist_ok=True) + # Print a prompt message + print(f"Project folder created successfully, path: {path}") + +# Create .n file +def create_n_file(address, file_name): + # Concatenate file path + path = os.path.join(address, file_name + '.n') + # Create file + with open(path, 'w', encoding='utf-8') as file: + file.write('') + # Print a prompt message + print(f"File created successfully, path: {path}") + +# Open .n file with Notepad +def open_n_file_with_notepad(address): + # Use subprocess module to run Notepad + subprocess.run(['notepad.exe', address]) + +# Convert natural language to Python code +def transform_code(address, key, choose): + # Open file + with open(address, 'r', encoding='utf-8') as file: + code = file.read() + + # Construct prompt + prompt = f"Please convert the following natural language to Python statements, without any explanation: {code}" + + # Initialize response + response = None + # Call different APIs based on the choice + if choose == "1": + # Call ZhipuAI API + client = ZhipuAI(api_key=key) + model_name = "glm-4" + try: + response = client.chat.completions.create( + model=model_name, + messages=[{"role": "user", "content": prompt}] + ) + except Exception as e: + print(f"API call failed: {e}") + return + elif choose == "2": + # Call OpenAI API + import openai + openai.api_key = key + try: + response = openai.Completion.create( + engine="davinci-codex", + prompt=prompt, + ) + except Exception as e: + print(f"API call failed: {e}") + return + + # If response is None, print a prompt message + if response is None: + print("API call did not return a result, please check your API Key and network connection") + return + + # Print response + print(response) + + # Process response + try: + if hasattr(response, 'choices') and response.choices: + choice = response.choices[0] + if hasattr(choice, 'message') and hasattr(choice.message, 'content'): + message_content = choice.message.content.replace("`", "").replace("python", "") + else: + message_content = choice.text.replace("`", "").replace("python", "") + + # Concatenate new file path + new_address = os.path.join(os.path.dirname(address), os.path.splitext(os.path.basename(address))[0] + '.py') + # Write to new file + with open(new_address, 'w', encoding='utf-8') as file: + file.write(message_content.strip()) + # Print a prompt message + print(f"Code conversion successful, path: {new_address}") + else: + print("The object returned by the API does not have a 'choices' attribute or is empty") + except (AttributeError, KeyError) as e: + print(f"Error processing API response: {e}") + +# Run Python script +def run_python_script(address): + # Concatenate new file path + new_path = address.replace('.n', '.py') + # Use subprocess module to run Python script + subprocess.run(['python', new_path]) + +# Install Python package +def install_package(package_name): + # Create bat folder + bat_folder = 'bat' + if not os.path.exists(bat_folder): + os.makedirs(bat_folder) + + # Create bat file + bat_file_path = os.path.join(bat_folder, 'install.bat') + with open(bat_file_path, 'w') as bat_file: + bat_file.write(f'pip install {package_name}') + + # Use subprocess module to run bat file + subprocess.run([bat_file_path], shell=True) + +# Uninstall Python package +def uninstall_package(package_name): + # Create bat folder + bat_folder = 'bat' + if not os.path.exists(bat_folder): + os.makedirs(bat_folder) + + # Create bat file + bat_file_path = os.path.join(bat_folder, 'uninstall.bat') + with open(bat_file_path, 'w') as bat_file: + bat_file.write(f'pip uninstall {package_name}') + + # Use subprocess module to run bat file + subprocess.run([bat_file_path], shell=True) + +# Update pip +def up_pip(): + # Create bat folder + bat_folder = 'bat' + if not os.path.exists(bat_folder): + os.makedirs(bat_folder) + + # Create bat file + bat_file_path = os.path.join(bat_folder, 'up.bat') + with open(bat_file_path, 'w') as bat_file: + bat_file.write("pip install --upgrade pip") + + # Use subprocess module to run bat file + subprocess.run([bat_file_path], shell=True) + +# List installed Python packages +def blist(): + # Create bat folder + bat_folder = 'bat' + if not os.path.exists(bat_folder): + os.makedirs(bat_folder) + + # Create bat file + bat_file_path = os.path.join(bat_folder, 'list.bat') + with open(bat_file_path, 'w') as bat_file: + bat_file.write("pip list") + + # Use subprocess module to run bat file + subprocess.run([bat_file_path], shell=True) + +# Show information of specified Python package +def bshow(package_name): + # Create bat folder + bat_folder = 'bat' + if not os.path.exists(bat_folder): + os.makedirs(bat_folder) + + # Create bat file + bat_file_path = os.path.join(bat_folder, 'show.bat') + with open(bat_file_path, 'w') as bat_file: + bat_file.write(f'pip show {package_name}') + + # Use subprocess module to run bat file + subprocess.run([bat_file_path], shell=True) + +# Revoke agreement +def clear_agreement(): + # If the configuration file exists + if os.path.exists(CONFIG_FILE): + # Open the configuration file + with open(CONFIG_FILE, 'r') as file: + # Load the configuration file content + config = json.load(file) + # Set whether the terms have been agreed to as False + config['agreed'] = False + # Open the configuration file + with open(CONFIG_FILE, 'w') as file: + # Write the configuration dictionary to the configuration file + json.dump(config, file) + # Print a prompt message + print("Agreement has been revoked") + else: + # Print a prompt message + print("Configuration file does not exist") + +# Print terms and conditions +def print_terms_and_conditions(): + # Define terms and conditions + terms = "Open Source Program Terms of Use\n1. License Grant\nThis program is released under the Apache License Version 2.0 (hereinafter referred to as 'Apache 2.0'). According to Apache 2.0, you can freely use, copy, modify and distribute this program, regardless of whether it has been modified, but must comply with the following conditions:\na. You must retain the copyright notice and disclaimer of this program.\nb. You must provide clear statements and/or appropriate documentation indicating your modifications to the original work.\nc. You must provide appropriate legal statements and copyright statements indicating the source and use of this program under the Apache 2.0 license.\n2. API Use\nThis program supports two types of APIs: ZhipuAI and OpenAI. Before using this program, you need to agree to the terms of use of the API provider and obtain the API Key by yourself. The author of this program does not provide API Keys and is not responsible for any consequences of API use.\n3. Software Function\nThis program is mainly used to convert natural language to Python code. You can use this program to create projects, files, and perform code conversion, running, and installation of dependencies, etc. However, please note that this program does not guarantee the correctness of the conversion results, and you need to check and verify the correctness of the code by yourself.\n4. Privacy Policy\nThis program does not collect any personal information of yours. However, please note that the API provider may collect information related to your use of the API, please refer to the privacy policy of the API provider for details.\n5. Disclaimer\nThis program is provided 'as is', and the author does not provide any form of express or implied warranty, including but not limited to the warranties of merchantability, suitability for a particular purpose, and non-infringement. In the process of using this program, the author does not assume any direct, indirect, incidental, special, or consequential losses or damages arising from the use of this program.\n6. Modification and Distribution\nYou can freely modify and distribute the source code of this program, but must comply with the requirements of the Apache 2.0 license, including retaining the copyright notice, disclaimer, and contributor rights notice.\n7. Applicable Law and Dispute Resolution\nThe interpretation, validity, and enforcement of these terms and conditions shall be governed by the laws of the People's Republic of China. In the event of any disputes arising from these terms and conditions, the parties shall attempt to resolve the disputes amicably; if the disputes cannot be resolved amicably, either party may bring a lawsuit to the people's court in the location where the author of this program is located.\n8. Other\nAll rights of this program belong to the author, and all rights are reserved. If you have any questions or suggestions about the terms of use of this program, please contact the author.\nBy agreeing to the above, you can continue to use this program." + + # Print terms and conditions + print(terms) + +# Main function +def main(): + # Print terms and conditions + print_terms_and_conditions() + # Load API Key + api_key, api_choice, agreed = load_api_key() + # If the terms have not been agreed to + if not agreed: + # Input agreement + agree_input = input("Enter 'agree' to continue: ") + # If the input is not 'agree' + if agree_input != "agree": + # Print a prompt message + print("You must agree to the terms to continue using the program.") + # Exit the program + sys.exit() + # Input API choice + api_choice = input("Choose API: 1. ZhipuAI 2. OpenAI: ") + # Input API Key + api_key = input("Enter API Key: ") + # Set as agreed + agreed = True + # Save API Key + save_api_key(api_key, api_choice, agreed) + + # Loop to execute commands + while True: + # Input command + cmd = input(">>> ") + # Split command + cmd_parts = cmd.split() + + # Check command + if cmd == "check+": + # Print version information + print("ni_en_2.1.1.20240706_beta, NITSC all rights reserved") + elif "creat+" in cmd: + # Check if the command is to create a project + if "project+" in cmd: + # Create project + create_project(cmd_parts[2], cmd_parts[3]) + # Check if the command is to create a file + elif "n+" in cmd: + # Create file + create_n_file(cmd_parts[2], cmd_parts[3]) + else: + # Print a prompt message + print("Invalid command, please check if the syntax of the command is correct") + elif "open+" in cmd: + # Open file + open_n_file_with_notepad(cmd_parts[1]) + elif "trans+" in cmd: + # Convert code + transform_code(cmd_parts[1], api_key, api_choice) + elif "run+" in cmd: + # Run Python script + run_python_script(cmd_parts[1]) + elif "install+" in cmd: + # Install Python package + install_package(cmd_parts[1]) + elif "uninstall+" in cmd: + # Uninstall Python package + uninstall_package(cmd_parts[1]) + elif "up+" in cmd: + # Update pip + up_pip() + elif "blist+" in cmd: + # List installed Python packages + blist() + elif "bshow+" in cmd: + # Show information of specified Python package + bshow(cmd_parts[1]) + elif "clean+" in cmd: + # Clear configuration file + if "agree" in cmd_parts: + clear_agreement() + elif "key" in cmd_parts: + clear_config() + elif "exit+" in cmd: + # Exit program + sys.exit() + else: + # Print a prompt message + print("Invalid command, please check if the syntax of the command is correct") + +# If it is the main program, execute the main function +if __name__ == "__main__": + main() diff --git "a/\347\250\213\345\272\217/ni_zh_2.0.py" "b/\347\250\213\345\272\217/ni_zh_2.0.py" new file mode 100644 index 0000000..ed550ea --- /dev/null +++ "b/\347\250\213\345\272\217/ni_zh_2.0.py" @@ -0,0 +1,542 @@ +''' + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) 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 Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. 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 any Contributor 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 + Work (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 such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works 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 any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +''' + +#周所有,保留所有权利,请勿用于商业用途,仅限个人使用 + +# 导入必要的模块 +import sys +import os +import subprocess +import json +from zhipuai import ZhipuAI + +# 定义配置文件路径 +CONFIG_FILE = 'config.json' + +# 加载API Key +def load_api_key(): + # 如果配置文件存在 + if os.path.exists(CONFIG_FILE): + # 打开配置文件 + with open(CONFIG_FILE, 'r') as file: + # 加载配置文件内容 + config = json.load(file) + # 返回API Key、API选择和是否同意条款 + return config.get('api_key'), config.get('api_choice'), config.get('agreed', False) + # 如果配置文件不存在,返回None + return None, None, False + +# 保存API Key +def save_api_key(api_key, api_choice, agreed): + # 创建配置字典 + config = { + 'api_key': api_key, + 'api_choice': api_choice, + 'agreed': agreed + } + # 打开配置文件 + with open(CONFIG_FILE, 'w') as file: + # 将配置字典写入配置文件 + json.dump(config, file) + +# 清空配置文件 +def clear_config(): + # 如果配置文件存在 + if os.path.exists(CONFIG_FILE): + # 删除配置文件 + os.remove(CONFIG_FILE) + # 打印提示信息 + print("配置文件已清空") + else: + # 打印提示信息 + print("配置文件不存在") + +# 创建项目文件夹 +def create_project(address, project_name): + # 拼接项目路径 + path = os.path.join(address, project_name) + # 创建项目文件夹 + os.makedirs(path, exist_ok=True) + # 打印提示信息 + print(f"项目文件夹创建成功,路径为:{path}") + +# 创建.n文件 +def create_n_file(address, file_name): + # 拼接文件路径 + path = os.path.join(address, file_name + '.n') + # 创建文件 + with open(path, 'w', encoding='utf-8') as file: + file.write('') + # 打印提示信息 + print(f"文件创建成功,路径为:{path}") + +# 使用记事本打开.n文件 +def open_n_file_with_notepad(address): + # 使用subprocess模块运行记事本 + subprocess.run(['notepad.exe', address]) + +# 将自然语言转换为Python代码 +def transform_code(address, key, choose): + # 打开文件 + with open(address, 'r', encoding='utf-8') as file: + code = file.read() + + # 构造提示 + prompt = f"请将以下给出的自然语言转为python语句,不需要任何解释:{code}" + + # 初始化响应 + response = None + # 根据选择调用不同的API + if choose == "1": + # 调用智谱AI API + client = ZhipuAI(api_key=key) + model_name = "glm-4" + try: + response = client.chat.completions.create( + model=model_name, + messages=[{"role": "user", "content": prompt}] + ) + except Exception as e: + print(f"API调用失败: {e}") + return + elif choose == "2": + # 调用OpenAI API + import openai + openai.api_key = key + try: + response = openai.Completion.create( + engine="davinci-codex", + prompt=prompt, + ) + except Exception as e: + print(f"API调用失败: {e}") + return + + # 如果响应为空,打印提示信息 + if response is None: + print("API调用未能返回结果,请检查您的API Key和网络连接") + return + + # 打印响应 + print(response) + + # 处理响应 + try: + if hasattr(response, 'choices') and response.choices: + choice = response.choices[0] + if hasattr(choice, 'message') and hasattr(choice.message, 'content'): + message_content = choice.message.content.replace("`", "").replace("python", "") + else: + message_content = choice.text.replace("`", "").replace("python", "") + + # 拼接新文件路径 + new_address = os.path.join(os.path.dirname(address), os.path.splitext(os.path.basename(address))[0] + '.py') + # 写入新文件 + with open(new_address, 'w', encoding='utf-8') as file: + file.write(message_content.strip()) + # 打印提示信息 + print(f"代码转换成功,路径为:{new_address}") + else: + print("API返回的对象没有choices属性或为空") + except (AttributeError, KeyError) as e: + print(f"处理API返回结果时出错: {e}") + +# 运行Python脚本 +def run_python_script(address): + # 拼接新文件路径 + new_path = address.replace('.n', '.py') + # 使用subprocess模块运行Python脚本 + subprocess.run(['python', new_path]) + +# 安装Python包 +def install_package(package_name): + # 创建bat文件夹 + bat_folder = 'bat' + if not os.path.exists(bat_folder): + os.makedirs(bat_folder) + + # 创建bat文件 + bat_file_path = os.path.join(bat_folder, 'install.bat') + with open(bat_file_path, 'w') as bat_file: + bat_file.write(f'pip install {package_name}') + + # 使用subprocess模块运行bat文件 + subprocess.run([bat_file_path], shell=True) + +# 卸载Python包 +def uninstall_package(package_name): + # 创建bat文件夹 + bat_folder = 'bat' + if not os.path.exists(bat_folder): + os.makedirs(bat_folder) + + # 创建bat文件 + bat_file_path = os.path.join(bat_folder, 'uninstall.bat') + with open(bat_file_path, 'w') as bat_file: + bat_file.write(f'pip uninstall {package_name}') + + # 使用subprocess模块运行bat文件 + subprocess.run([bat_file_path], shell=True) + +# 更新pip +def up_pip(): + # 创建bat文件夹 + bat_folder = 'bat' + if not os.path.exists(bat_folder): + os.makedirs(bat_folder) + + # 创建bat文件 + bat_file_path = os.path.join(bat_folder, 'up.bat') + with open(bat_file_path, 'w') as bat_file: + bat_file.write("pip install --upgrade pip") + + # 使用subprocess模块运行bat文件 + subprocess.run([bat_file_path], shell=True) + +# 列出已安装的Python包 +def blist(): + # 创建bat文件夹 + bat_folder = 'bat' + if not os.path.exists(bat_folder): + os.makedirs(bat_folder) + + # 创建bat文件 + bat_file_path = os.path.join(bat_folder, 'list.bat') + with open(bat_file_path, 'w') as bat_file: + bat_file.write("pip list") + + # 使用subprocess模块运行bat文件 + subprocess.run([bat_file_path], shell=True) + +# 显示指定Python包的信息 +def bshow(package_name): + # 创建bat文件夹 + bat_folder = 'bat' + if not os.path.exists(bat_folder): + os.makedirs(bat_folder) + + # 创建bat文件 + bat_file_path = os.path.join(bat_folder, 'show.bat') + with open(bat_file_path, 'w') as bat_file: + bat_file.write(f'pip show {package_name}') + + # 使用subprocess模块运行bat文件 + subprocess.run([bat_file_path], shell=True) + +# 撤销同意条款 +def clear_agreement(): + # 如果配置文件存在 + if os.path.exists(CONFIG_FILE): + # 打开配置文件 + with open(CONFIG_FILE, 'r') as file: + # 加载配置文件内容 + config = json.load(file) + # 将是否同意条款设置为False + config['agreed'] = False + # 打开配置文件 + with open(CONFIG_FILE, 'w') as file: + # 将配置字典写入配置文件 + json.dump(config, file) + # 打印提示信息 + print("已撤销同意") + else: + # 打印提示信息 + print("配置文件不存在") + +# 打印使用条款 +def print_terms_and_conditions(): + # 定义使用条款 + terms = "开源程序使用条款\n1. 授权许可\n本程序基于Apache License Version 2.0(以下简称“Apache 2.0”)许可证发布。根据Apache 2.0,您可以自由地使用、复制、修改和分发本程序,无论是否进行了修改,但必须遵守以下条件:\na. 您必须保留本程序的版权声明和免责声明。\nb. 您必须提供明确的声明和/或适当的文档,表明您对原始作品的修改。\nc. 您必须提供适当的法律声明和版权声明,表明本程序的来源和使用Apache 2.0许可证。\n2. API使用\n本程序支持智谱AI和OpenAI两种API。在使用本程序之前,您需要同意API提供商的使用条款,并自行获取API Key。本程序作者不提供API Key,也不对API的使用后果承担任何责任。\n3. 软件功能\n本程序主要用于将自然语言转换为Python代码。您可以使用本程序创建项目、文件,以及进行代码转换、运行和安装依赖等操作。但请注意,本程序不保证转换结果的正确性,您需要自行检查和验证代码的正确性。\n4. 隐私政策\n本程序不会收集您的任何个人信息。但请注意,API提供商可能会收集与您使用API相关的信息,具体请参考API提供商的隐私政策。\n5. 免责声明\n本程序按“现状”提供,作者不提供任何形式的明示或暗示的保证,包括但不限于对适销性、特定用途的适用性和非侵权性的保证。在使用本程序过程中,作者不承担因使用本程序而产生的任何直接、间接、偶然、特殊或后果性的损失或损害。\n6. 修改和分发\n您可以自由修改和分发本程序的源代码,但必须遵守Apache 2.0许可证的要求,包括保留版权声明、免责声明和贡献者权利声明。\n7. 法律适用和争议解决\n本条款的解释、有效性和执行均适用中华人民共和国法律。如因本条款产生任何争议,双方应友好协商解决;协商不成的,任何一方均有权向本程序作者所在地的人民法院提起诉讼。\n8. 其他\n本程序的版权归作者所有,保留所有权利。如果您对本程序的使用条款有任何疑问或建议,请联系作者。\n同意以上,即可继续使用本程序。" + + # 打印使用条款 + print(terms) + +# 主函数 +def main(): + # 打印使用条款 + print_terms_and_conditions() + # 加载API Key + api_key, api_choice, agreed = load_api_key() + # 如果没有同意条款 + if not agreed: + # 输入同意条款 + agree_input = input("输入“同意以上”以继续: ") + # 如果输入的不是“同意以上” + if agree_input != "同意以上": + # 打印提示信息 + print("您必须同意条款才能继续使用程序。") + # 退出程序 + sys.exit() + # 输入API选择 + api_choice = input("选择API: 1.智谱AI 2.OpenAI: ") + # 输入API Key + api_key = input("输入API Key: ") + # 设置为已同意 + agreed = True + # 保存API Key + save_api_key(api_key, api_choice, agreed) + + # 循环执行命令 + while True: + # 输入命令 + cmd = input(">>> ") + # 分割命令 + cmd_parts = cmd.split() + + # 检查命令 + if cmd == "check+": + # 打印版本信息 + print("ni_zh_2.0, NITSC所有") + elif "creat+" in cmd: + # 检查命令是否为创建项目 + if "project+" in cmd: + # 创建项目 + create_project(cmd_parts[2], cmd_parts[3]) + # 检查命令是否为创建文件 + elif "n+" in cmd: + # 创建文件 + create_n_file(cmd_parts[2], cmd_parts[3]) + else: + # 打印提示信息 + print("命令无效,请检查命令的语法是否存在错误") + elif "open+" in cmd: + # 打开文件 + open_n_file_with_notepad(cmd_parts[1]) + elif "trans+" in cmd: + # 转换代码 + transform_code(cmd_parts[1], api_key, api_choice) + elif "run+" in cmd: + # 运行Python脚本 + run_python_script(cmd_parts[1]) + elif "install+" in cmd: + # 安装Python包 + install_package(cmd_parts[1]) + elif "uninstall+" in cmd: + # 卸载Python包 + uninstall_package(cmd_parts[1]) + elif "up+" in cmd: + # 更新pip + up_pip() + elif "blist+" in cmd: + # 列出已安装的Python包 + blist() + elif "bshow+" in cmd: + # 显示指定Python包的信息 + bshow(cmd_parts[1]) + elif "clean+" in cmd: + # 清空配置文件 + if "agree" in cmd_parts: + clear_agreement() + elif "key" in cmd_parts: + clear_config() + elif "exit+" in cmd: + # 退出程序 + sys.exit() + else: + # 打印提示信息 + print("命令无效,请检查命令的语法是否存在错误") + +# 如果是主程序,执行主函数 +if __name__ == "__main__": + main() \ No newline at end of file diff --git "a/\347\250\213\345\272\217/ni_zh_2.2.1.20240707_beta.py" "b/\347\250\213\345\272\217/ni_zh_2.2.1.20240707_beta.py" new file mode 100644 index 0000000..e6c71e2 --- /dev/null +++ "b/\347\250\213\345\272\217/ni_zh_2.2.1.20240707_beta.py" @@ -0,0 +1,561 @@ +''' + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) 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 Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. 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 any Contributor 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 + Work (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 such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works 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 any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +''' + +#周所有,保留所有权利,请勿用于商业用途,仅限个人使用 + +# 导入必要的模块 +import sys +import os +import subprocess +import json +from zhipuai import ZhipuAI +import timeit +import textwrap +import re + +# 定义配置文件路径 +CONFIG_FILE = 'config.json' + +# 加载API Key +def load_api_key(): + # 如果配置文件存在 + if os.path.exists(CONFIG_FILE): + # 打开配置文件 + with open(CONFIG_FILE, 'r') as file: + # 加载配置文件内容 + config = json.load(file) + # 返回API Key、API选择和是否同意条款 + return config.get('api_key'), config.get('api_choice'), config.get('agreed', False) + # 如果配置文件不存在,返回None + return None, None, False + +# 保存API Key +def save_api_key(api_key, api_choice, agreed): + # 创建配置字典 + config = { + 'api_key': api_key, + 'api_choice': api_choice, + 'agreed': agreed + } + # 打开配置文件 + with open(CONFIG_FILE, 'w') as file: + # 将配置字典写入配置文件 + json.dump(config, file) + +# 清空配置文件 +def clear_config(): + # 如果配置文件存在 + if os.path.exists(CONFIG_FILE): + # 删除配置文件 + os.remove(CONFIG_FILE) + # 打印提示信息 + print("配置文件已清空") + else: + # 打印提示信息 + print("配置文件不存在") + +# 创建项目文件夹 +def create_project(address, project_name): + # 拼接项目路径 + path = os.path.join(address, project_name) + # 创建项目文件夹 + os.makedirs(path, exist_ok=True) + # 打印提示信息 + print(f"项目文件夹创建成功,路径为:{path}") + +# 创建.n文件 +def create_n_file(address, file_name): + # 拼接文件路径 + path = os.path.join(address, file_name + '.n') + # 创建文件 + with open(path, 'w', encoding='utf-8') as file: + file.write('') + # 打印提示信息 + print(f"文件创建成功,路径为:{path}") + +# 使用记事本打开.n文件 +def open_n_file_with_notepad(address): + # 使用subprocess模块运行记事本 + subprocess.run(['notepad.exe', address]) + +# 将自然语言转换为Python代码 +def transform_code(address, key, choose): + # 打开文件 + with open(address, 'r', encoding='utf-8') as file: + code = file.read() + + # 构造提示 + prompt = f"请将以下给出的自然语言转为python语句,不需要任何解释:{code}" + + # 初始化响应 + response = None + # 根据选择调用不同的API + if choose == "1": + # 调用智谱AI API + client = ZhipuAI(api_key=key) + model_name = "glm-4" + try: + response = client.chat.completions.create( + model=model_name, + messages=[{"role": "user", "content": prompt}] + ) + except Exception as e: + print(f"API调用失败: {e}") + return + elif choose == "2": + # 调用OpenAI API + import openai + openai.api_key = key + try: + response = openai.Completion.create( + engine="davinci-codex", + prompt=prompt, + ) + except Exception as e: + print(f"API调用失败: {e}") + return + + # 如果响应为空,打印提示信息 + if response is None: + print("API调用未能返回结果,请检查您的API Key和网络连接") + return + + # 打印响应 + print(response) + + # 处理响应 + try: + if hasattr(response, 'choices') and response.choices: + choice = response.choices[0] + if hasattr(choice, 'message') and hasattr(choice.message, 'content'): + message_content = choice.message.content.replace("`", "").replace("python", "") + else: + message_content = choice.text.replace("`", "").replace("python", "") + + # 拼接新文件路径 + new_address = os.path.join(os.path.dirname(address), os.path.splitext(os.path.basename(address))[0] + '.py') + # 写入新文件 + with open(new_address, 'w', encoding='utf-8') as file: + file.write(message_content.strip()) + # 打印提示信息 + print(f"代码转换成功,路径为:{new_address}") + else: + print("API返回的对象没有choices属性或为空") + except (AttributeError, KeyError) as e: + print(f"处理API返回结果时出错: {e}") + +# 运行Python脚本 +def run_python_script(address): + # 拼接新文件路径 + new_path = address.replace('.n', '.py') + # 使用subprocess模块运行Python脚本 + subprocess.run(['python', new_path]) + +# 安装Python包 +def install_package(package_name): + # 创建bat文件夹 + bat_folder = 'bat' + if not os.path.exists(bat_folder): + os.makedirs(bat_folder) + + # 创建bat文件 + bat_file_path = os.path.join(bat_folder, 'install.bat') + with open(bat_file_path, 'w') as bat_file: + bat_file.write(f'pip install {package_name}') + + # 使用subprocess模块运行bat文件 + subprocess.run([bat_file_path], shell=True) + +# 卸载Python包 +def uninstall_package(package_name): + # 创建bat文件夹 + bat_folder = 'bat' + if not os.path.exists(bat_folder): + os.makedirs(bat_folder) + + # 创建bat文件 + bat_file_path = os.path.join(bat_folder, 'uninstall.bat') + with open(bat_file_path, 'w') as bat_file: + bat_file.write(f'pip uninstall {package_name}') + + # 使用subprocess模块运行bat文件 + subprocess.run([bat_file_path], shell=True) + +# 更新pip +def up_pip(): + # 创建bat文件夹 + bat_folder = 'bat' + if not os.path.exists(bat_folder): + os.makedirs(bat_folder) + + # 创建bat文件 + bat_file_path = os.path.join(bat_folder, 'up.bat') + with open(bat_file_path, 'w') as bat_file: + bat_file.write("pip install --upgrade pip") + + # 使用subprocess模块运行bat文件 + subprocess.run([bat_file_path], shell=True) + +# 列出已安装的Python包 +def blist(): + # 创建bat文件夹 + bat_folder = 'bat' + if not os.path.exists(bat_folder): + os.makedirs(bat_folder) + + # 创建bat文件 + bat_file_path = os.path.join(bat_folder, 'list.bat') + with open(bat_file_path, 'w') as bat_file: + bat_file.write("pip list") + + # 使用subprocess模块运行bat文件 + subprocess.run([bat_file_path], shell=True) + +# 显示指定Python包的信息 +def bshow(package_name): + # 创建bat文件夹 + bat_folder = 'bat' + if not os.path.exists(bat_folder): + os.makedirs(bat_folder) + + # 创建bat文件 + bat_file_path = os.path.join(bat_folder, 'show.bat') + with open(bat_file_path, 'w') as bat_file: + bat_file.write(f'pip show {package_name}') + + # 使用subprocess模块运行bat文件 + subprocess.run([bat_file_path], shell=True) + +# 执行bash命令 +def bush_code(code): + # 创建bat文件夹 + bat_folder = 'bat' + if not os.path.exists(bat_folder): + os.makedirs(bat_folder) + + # 创建bat文件 + bat_file_path = os.path.join(bat_folder, 'bush_code.bat') + with open(bat_file_path, 'w') as bat_file: + bat_file.write(code) + + # 使用subprocess模块运行bat文件 + subprocess.run([bat_file_path], shell=True) + +# 撤销同意条款 +def clear_agreement(): + # 如果配置文件存在 + if os.path.exists(CONFIG_FILE): + # 打开配置文件 + with open(CONFIG_FILE, 'r') as file: + # 加载配置文件内容 + config = json.load(file) + # 将是否同意条款设置为False + config['agreed'] = False + # 打开配置文件 + with open(CONFIG_FILE, 'w') as file: + # 将配置字典写入配置文件 + json.dump(config, file) + # 打印提示信息 + print("已撤销同意") + else: + # 打印提示信息 + print("配置文件不存在") + +# 打印使用条款 +def print_terms_and_conditions(): + # 定义使用条款 + terms = "开源程序使用条款\n1. 授权许可\n本程序基于Apache License Version 2.0(以下简称“Apache 2.0”)许可证发布。根据Apache 2.0,您可以自由地使用、复制、修改和分发本程序,无论是否进行了修改,但必须遵守以下条件:\na. 您必须保留本程序的版权声明和免责声明。\nb. 您必须提供明确的声明和/或适当的文档,表明您对原始作品的修改。\nc. 您必须提供适当的法律声明和版权声明,表明本程序的来源和使用Apache 2.0许可证。\n2. API使用\n本程序支持智谱AI和OpenAI两种API。在使用本程序之前,您需要同意API提供商的使用条款,并自行获取API Key。本程序作者不提供API Key,也不对API的使用后果承担任何责任。\n3. 软件功能\n本程序主要用于将自然语言转换为Python代码。您可以使用本程序创建项目、文件,以及进行代码转换、运行和安装依赖等操作。但请注意,本程序不保证转换结果的正确性,您需要自行检查和验证代码的正确性。\n4. 隐私政策\n本程序不会收集您的任何个人信息。但请注意,API提供商可能会收集与您使用API相关的信息,具体请参考API提供商的隐私政策。\n5. 免责声明\n本程序按“现状”提供,作者不提供任何形式的明示或暗示的保证,包括但不限于对适销性、特定用途的适用性和非侵权性的保证。在使用本程序过程中,作者不承担因使用本程序而产生的任何直接、间接、偶然、特殊或后果性的损失或损害。\n6. 修改和分发\n您可以自由修改和分发本程序的源代码,但必须遵守Apache 2.0许可证的要求,包括保留版权声明、免责声明和贡献者权利声明。\n7. 法律适用和争议解决\n本条款的解释、有效性和执行均适用中华人民共和国法律。如因本条款产生任何争议,双方应友好协商解决;协商不成的,任何一方均有权向本程序作者所在地的人民法院提起诉讼。\n8. 其他\n本程序的版权归作者所有,保留所有权利。如果您对本程序的使用条款有任何疑问或建议,请联系作者。\n同意以上,即可继续使用本程序。" + # 打印使用条款 + print(terms) + +# 主函数 +def main(): + # 打印使用条款 + print_terms_and_conditions() + # 加载API Key + api_key, api_choice, agreed = load_api_key() + # 如果没有同意条款 + if not agreed: + # 输入同意条款 + agree_input = input("输入“同意以上”以继续: ") + # 如果输入的不是“同意以上” + if agree_input != "同意以上": + # 打印提示信息 + print("您必须同意条款才能继续使用程序。") + # 退出程序 + sys.exit() + # 输入API选择 + api_choice = input("选择API: 1.智谱AI 2.OpenAI: ") + # 输入API Key + api_key = input("输入API Key: ") + # 设置为已同意 + agreed = True + # 保存API Key + save_api_key(api_key, api_choice, agreed) + + # 循环执行命令 + while True: + # 输入命令 + cmd = input(">>> ") + # 分割命令 + cmd_parts = cmd.split() + + # 检查命令 + if cmd == "check+": + # 打印版本信息 + print("ni_zh_2.2.1.20240707_beta, NITSC所有") + elif "creat+" in cmd: + # 检查命令是否为创建项目 + if "project+" in cmd: + # 创建项目 + create_project(cmd_parts[2].replace('"',''), cmd_parts[3].replace('"','')) + # 检查命令是否为创建文件 + elif "n+" in cmd: + # 创建文件 + create_n_file(cmd_parts[2].replace('"',''), cmd_parts[3].replace('"','')) + else: + # 打印提示信息 + print("命令无效,请检查命令的语法是否存在错误") + elif "open+" in cmd: + # 打开文件 + open_n_file_with_notepad(cmd_parts[1].replace('"','')) + elif "trans+" in cmd: + # 转换代码 + transform_code(cmd_parts[1].replace('"',''), api_key, api_choice) + elif "run+" in cmd: + # 运行Python脚本 + run_python_script(cmd_parts[1].replace('"','')) + elif "install+" in cmd: + # 安装Python包 + install_package(cmd_parts[1]) + elif "uninstall+" in cmd: + # 卸载Python包 + uninstall_package(cmd_parts[1]) + elif "up+" in cmd: + # 更新pip + up_pip() + elif "blist+" in cmd: + # 列出已安装的Python包 + blist() + elif "bshow+" in cmd: + # 显示指定Python包的信息 + bshow(cmd_parts[1]) + elif "bush+" in cmd: + bush_code(cmd_parts[1]) + elif "clean+" in cmd: + # 清空配置文件 + if "agree" in cmd_parts: + clear_agreement() + elif "key" in cmd_parts: + clear_config() + elif "exit+" in cmd: + # 退出程序 + sys.exit() + else: + # 打印提示信息 + print("命令无效,请检查命令的语法是否存在错误") + +# 如果是主程序,执行主函数 +if __name__ == "__main__": + main() \ No newline at end of file diff --git "a/\347\250\213\345\272\217/\345\256\211\350\243\205\344\276\235\350\265\226\351\241\271.py" "b/\347\250\213\345\272\217/\345\256\211\350\243\205\344\276\235\350\265\226\351\241\271.py" new file mode 100644 index 0000000..70efcd0 --- /dev/null +++ "b/\347\250\213\345\272\217/\345\256\211\350\243\205\344\276\235\350\265\226\351\241\271.py" @@ -0,0 +1,243 @@ +''' + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) 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 Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. 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 any Contributor 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 + Work (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 such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works 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 any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +''' + +import os,subprocess + +def up_pip(): + bat_folder = 'bat' + if not os.path.exists(bat_folder): + os.makedirs(bat_folder) + + bat_file_path = os.path.join(bat_folder, 'up.bat') + with open(bat_file_path, 'w') as bat_file: + bat_file.write("pip install --upgrade pip") + subprocess.run([bat_file_path], shell=True) + +def install_package(package_name): + bat_folder = 'bat' + if not os.path.exists(bat_folder): + os.makedirs(bat_folder) + bat_file_path = os.path.join(bat_folder, 'install.bat') + with open(bat_file_path, 'w') as bat_file: + bat_file.write(f'pip install {package_name}') + subprocess.run([bat_file_path], shell=True) + +def list(): + bat_folder = 'bat' + if not os.path.exists(bat_folder): + os.makedirs(bat_folder) + bat_file_path = os.path.join(bat_folder, 'plist.bat') + with open(bat_file_path, 'w') as bat_file: + bat_file.write("pip list") + subprocess.run([bat_file_path], shell=True) + +def install_dependency(): + up_pip() + install_package("zhipuai") + install_package("openai") + list() + print("安装依赖库成功!接下来您就可以正常使用 Ni2 语言来编写程序啦") + +if __name__ == "__main__": + install_dependency() \ No newline at end of file