之前玩完llama。cpp后,发现很好用,就想着加上一个web界面。 但是本人对于前端非常不懂,就想着用ai自己来做一个。 于是问了chatgpt,大概耗时10分钟就跑完了前端和后端,你别说还像个样子 我的问题: 想做一个基于python,实现类似chatgpt的风格的网页。1。用户输入提示词;2。后台调用shell命令3。将shell命令的结果展示到网页上 chagpt的返回的代码 要创建一个类似于ChatGPT风格的网页,您可以使用Python的Web框架Flask和后端库如subprocess来调用shell命令。以下是一个简单的示例:安装Flask库:bashCopycodepipinstallFlask创建一个名为app。py的文件,然后添加以下代码:pythonCopycodefromflaskimportFlask,rendertemplate,requestimportsubprocessappFlask(name)app。route(,methods〔GET,POST〕)defindex():ifrequest。methodPOST:userinputrequest。form〔prompt〕resultrunshellcommand(userinput)returnrendertemplate(index。html,resultresult)returnrendertemplate(index。html,resultNone)defrunshellcommand(command):processsubprocess。Popen(command,stdoutsubprocess。PIPE,stderrsubprocess。PIPE,shellTrue)output,errorprocess。communicate()iferror:returnerror。decode(utf8)returnoutput。decode(utf8)ifnamemain:app。run(debugTrue)创建一个名为templates的文件夹,并在其中创建一个名为index。html的文件。将以下代码添加到index。html:htmlCopycode!DOCTYPEhtmlhtmllangenheadmetacharsetUTF8metanameviewportcontentwidthdevicewidth,initialscale1。0titlePythonChatGPTlikeWebApptitleheadbodyh1PythonChatGPTlikeWebApph1{ifresult}h2Result:h2pre{{result}}pre{endif}bodyhtml运行app。py文件启动Web应用:bashCopycodepythonapp。py 现在,您可以访问http:127。0。0。1:5000以查看您的基于Python的类似于ChatGPT风格的网页。在输入框中输入提示词,然后单击提交按钮,它将调用shell命令并将结果显示在网页上。 请注意,该示例允许用户执行任意shell命令,可能存在安全风险。在实际部署中,您需要对用户输入进行严格的验证以防止潜在的安全问题。