之前玩完llama.cpp后,发现很好用,就想着加上一个web界面。
但是本人对于前端非常不懂,就想着用ai自己来做一个。
于是问了chatgpt,大概耗时10分钟就跑完了前端和后端,你别说还像个样子
我的问题:
想做一个基于python,实现类似chatgpt的风格的网页。1.用户输入提示词;2.后台调用shell命令3.将shell命令的结果展示到网页上
chagpt的返回的代码
要创建一个类似于ChatGPT风格的网页,您可以使用Python的Web框架Flask和后端库如subprocess来调用shell命令。以下是一个简单的示例:
bashCopy codepip install Flask
pythonCopy codefrom flask import Flask, render_template, request
import subprocess
app = Flask(__name__)
@app.route('/', methods=['GET', 'POST'])
def index():
if request.method == 'POST':
user_input = request.form['prompt']
result = run_shell_command(user_input)
return render_template('index.html', result=result)
return render_template('index.html', result=None)
def run_shell_command(command):
process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
output, error = process.communicate()
if error:
return error.decode('utf-8')
return output.decode('utf-8')
if __name__ == '__main__':
app.run(debug=True)
htmlCopy code
Python ChatGPT-like Web App
Python ChatGPT-like Web App
{% if result %}
Result:
{{ result }}
{% endif %}
bashCopy codepython app.py
现在,您可以访问http://127.0.0.1:5000/以查看您的基于Python的类似于ChatGPT风格的网页。在输入框中输入提示词,然后单击提交按钮,它将调用shell命令并将结果显示在网页上。
请注意,该示例允许用户执行任意shell命令,可能存在安全风险。在实际部署中,您需要对用户输入进行严格的验证以防止潜在的安全问题。
页面更新:2024-03-02
本站资料均由网友自行发布提供,仅用于学习交流。如有版权问题,请与我联系,QQ:4156828
© CopyRight 2008-2024 All Rights Reserved. Powered By bs178.com 闽ICP备11008920号-3
闽公网安备35020302034844号