File size: 3,610 Bytes
06f2b0d | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 | from flask import Flask, render_template_string, request, jsonify
import requests
import threading
app = Flask(__name__)
# 你自己的后端API地址
BACKEND_URL = "http://127.0.0.1:5000/restore"
# HTML 模板
HTML_PAGE = """
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<title>图像复原API测试前端</title>
<style>
body { font-family: "Segoe UI", sans-serif; background: #f8f9fa; padding: 40px; }
h2 { text-align: center; }
.box {
background: white;
border-radius: 10px;
padding: 20px;
margin: 15px auto;
width: 600px;
box-shadow: 0 2px 8px rgba(0,0,0,0.1);
}
.box input, .box textarea {
width: 100%;
margin: 8px 0;
padding: 8px;
border: 1px solid #ccc;
border-radius: 6px;
font-size: 14px;
}
.btn {
background-color: #007bff;
color: white;
border: none;
padding: 8px 16px;
border-radius: 6px;
cursor: pointer;
font-size: 14px;
}
.btn:disabled {
background-color: #999;
cursor: not-allowed;
}
.result {
margin-top: 10px;
background: #f1f3f5;
padding: 10px;
border-radius: 6px;
white-space: pre-wrap;
font-family: monospace;
}
</style>
</head>
<body>
<h2>🧪 图像复原 API 测试界面</h2>
<div id="boxes"></div>
<script>
const NUM_BOXES = 4; // 测试4个并发请求
const boxesDiv = document.getElementById("boxes");
for (let i = 0; i < NUM_BOXES; i++) {
const box = document.createElement("div");
box.className = "box";
box.innerHTML = `
<h3>任务 #${i+1}</h3>
<label>Image ID:</label>
<input id="image_${i}" type="text" placeholder="例如 image1.png" />
<label>Models (JSON数组):</label>
<textarea id="models_${i}" rows="2">["restormer.derain", "x-restormer.dehaze"]</textarea>
<button class="btn" id="btn_${i}" onclick="sendRequest(${i})">发送请求</button>
<div class="result" id="result_${i}">等待输入...</div>
`;
boxesDiv.appendChild(box);
}
function sendRequest(idx) {
const image_id = document.getElementById(`image_${idx}`).value.trim();
const models = document.getElementById(`models_${idx}`).value.trim();
const btn = document.getElementById(`btn_${idx}`);
const resultDiv = document.getElementById(`result_${idx}`);
if (!image_id || !models) {
alert("请输入 image_id 和 models");
return;
}
let modelsArray;
try {
modelsArray = JSON.parse(models);
} catch (e) {
alert("Models 必须是合法的 JSON 数组,如 ['restormer.derain']");
return;
}
btn.disabled = true;
resultDiv.textContent = "⏳ 请求中,请稍候...";
fetch("/test_api", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ image_id, models: modelsArray })
})
.then(res => res.json())
.then(data => {
resultDiv.textContent = "✅ 返回结果:\\n" + JSON.stringify(data, null, 2);
})
.catch(err => {
resultDiv.textContent = "❌ 请求失败:\\n" + err;
})
.finally(() => {
btn.disabled = false;
});
}
</script>
</body>
</html>
"""
@app.route('/')
def index():
return render_template_string(HTML_PAGE)
@app.route('/test_api', methods=['POST'])
def test_api():
"""代理前端请求到后端 /restore 接口"""
data = request.get_json()
try:
resp = requests.post(BACKEND_URL, json=data, timeout=600)
return jsonify(resp.json())
except Exception as e:
return jsonify({"error": str(e)}), 500
if __name__ == '__main__':
# 注意:该前端跑在不同端口(例如 8000)
app.run(host='0.0.0.0', port=8000, debug=True)
|