test_inpainting_endpoint.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #include <iostream>
  2. #include <string>
  3. #include <thread>
  4. #include <chrono>
  5. #include <nlohmann/json.hpp>
  6. #include <httplib.h>
  7. #include "include/utils.h"
  8. using json = nlohmann::json;
  9. // Simple test for the inpainting endpoint
  10. int main() {
  11. const std::string serverUrl = "http://localhost:8080";
  12. httplib::Client client(serverUrl.c_str());
  13. std::cout << "Testing inpainting endpoint..." << std::endl;
  14. // Create a simple 1x1 white PNG (base64 encoded)
  15. // This is a minimal PNG header for a 1x1 white pixel
  16. const std::string whitePixelBase64 =
  17. "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8"
  18. "/5/hHgAHggJ/PchI7wAAAABJRU5ErkJggg==";
  19. // Create a simple 1x1 black PNG (base64 encoded) for mask
  20. const std::string blackPixelBase64 =
  21. "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk"
  22. "Y+h8DwADwA/wAA9AAB7BDQAAAABJRU5ErkJggg==";
  23. // Test inpainting request
  24. json inpaintingRequest = {
  25. {"prompt", "a red apple"},
  26. {"negative_prompt", "blurry, low quality"},
  27. {"source_image", whitePixelBase64},
  28. {"mask_image", blackPixelBase64},
  29. {"width", 512},
  30. {"height", 512},
  31. {"steps", 5}, // Use fewer steps for faster testing
  32. {"cfg_scale", 7.5},
  33. {"seed", "42"},
  34. {"sampling_method", "euler_a"},
  35. {"strength", 0.8}
  36. };
  37. std::cout << "Sending inpainting request..." << std::endl;
  38. auto res = client.Post("/api/generate/inpainting", inpaintingRequest.dump(), "application/json");
  39. if (res && res->status == 202) {
  40. std::cout << "✓ Inpainting request accepted (status 202)" << std::endl;
  41. try {
  42. json response = json::parse(res->body);
  43. std::string requestId = response.value("request_id", "");
  44. if (!requestId.empty()) {
  45. std::cout << "✓ Request ID: " << requestId << std::endl;
  46. // Poll for job completion
  47. std::cout << "Polling for job completion..." << std::endl;
  48. for (int i = 0; i < 60; ++i) { // Poll for up to 60 seconds
  49. std::this_thread::sleep_for(std::chrono::seconds(1));
  50. auto statusRes = client.Get(("/api/queue/job/" + requestId).c_str());
  51. if (statusRes && statusRes->status == 200) {
  52. json statusResponse = json::parse(statusRes->body);
  53. std::string status = statusResponse["job"]["status"];
  54. std::cout << "Job status: " << status << std::endl;
  55. if (status == "completed") {
  56. std::cout << "✓ Inpainting job completed successfully!" << std::endl;
  57. if (statusResponse["job"].contains("outputs") &&
  58. statusResponse["job"]["outputs"].is_array() &&
  59. statusResponse["job"]["outputs"].size() > 0) {
  60. std::cout << "✓ Generated " << statusResponse["job"]["outputs"].size()
  61. << " image(s)" << std::endl;
  62. }
  63. return 0; // Success
  64. } else if (status == "failed") {
  65. std::string error = statusResponse["job"].value("error_message", "Unknown error");
  66. std::cerr << "✗ Inpainting job failed: " << error << std::endl;
  67. return 1;
  68. }
  69. }
  70. }
  71. std::cerr << "✗ Job polling timeout" << std::endl;
  72. return 1;
  73. } else {
  74. std::cerr << "✗ No request ID in response" << std::endl;
  75. return 1;
  76. }
  77. } catch (const std::exception& e) {
  78. std::cerr << "✗ Failed to parse response: " << e.what() << std::endl;
  79. return 1;
  80. }
  81. } else {
  82. std::cerr << "✗ Failed to send inpainting request" << std::endl;
  83. if (res) {
  84. std::cerr << "Status: " << res->status << std::endl;
  85. std::cerr << "Response: " << res->body << std::endl;
  86. } else {
  87. std::cerr << "No response received" << std::endl;
  88. }
  89. return 1;
  90. }
  91. }