|
# 安装Flask
pip install flask
# 创建web_ui.py
from flask import Flask, request, render_template_string
import requests
app = Flask(__name__)
HTML_TEMPLATE = """
<!DOCTYPE html>
<html>
<head>
<title>Hermes Agent Mobile</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
body { font-family: Arial, sans-serif; padding: 20px; }
#chat { height: 300px; overflow-y: auto; border: 1px solid #ccc; padding: 10px; margin-bottom: 10px; }
#input { width: 100%; padding: 10px; box-sizing: border-box; }
button { width: 100%; padding: 10px; margin-top: 10px; }
</style>
</head>
<body>
<div id="chat" id="chat"></div>
<input type="text" id="input" placeholder="输入任务...">
<button onclick="sendMessage()">发送</button>
<script>
function sendMessage() {
const input = document.getElementById('input');
const message = input.value;
if (!message) return;
// 添加用户消息
document.getElementById('chat').innerHTML += `<div><strong>你:</strong> ${message}</div>`;
input.value = '';
// 调用API
fetch('/api/chat', {
method: 'POST',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify({message: message})
})
.then(response => response.json())
.then(data => {
document.getElementById('chat').innerHTML += `<div><strong>Hermes:</strong> ${data.response}</div>`;
document.getElementById('chat').scrollTop = document.getElementById('chat').scrollHeight;
});
}
</script>
</body>
</html>
"""
@app.route('/')
def home():
return render_template_string(HTML_TEMPLATE)
@app.route('/api/chat', methods=['POST'])
def chat():
data = request.json
response = requests.post(
"http://localhost:8080/execute",
json={"task": data["message"]}
)
return {"response": response.json()["result"]}
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000, threaded=True)
|