| from flask import Flask, render_template_string, request, jsonify |
| import requests |
| import threading |
|
|
| app = Flask(__name__) |
|
|
| |
| BACKEND_URL = "http://127.0.0.1:5000/restore" |
|
|
| |
| 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__': |
| |
| app.run(host='0.0.0.0', port=8000, debug=True) |
|
|