diff --git "a/\347\250\213\345\272\217/ni2_zh_beta.py" "b/\347\250\213\345\272\217/ni2_zh_beta.py" new file mode 100644 index 0000000..d51b2c9 --- /dev/null +++ "b/\347\250\213\345\272\217/ni2_zh_beta.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("N 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