test_liblib_dual_pipeline.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. import sys
  2. import os
  3. import json
  4. import base64
  5. sys.stdout.reconfigure(encoding='utf-8')
  6. script_dir = os.path.dirname(os.path.abspath(__file__))
  7. tools_dir = os.path.join(script_dir, '..', 'tools', 'local', 'liblibai_controlnet')
  8. sys.path.append(tools_dir)
  9. try:
  10. from liblibai_client import LibLibAIClient
  11. except ImportError:
  12. print(f"Failed to import LibLibAIClient. Make sure the path {tools_dir} is correct.")
  13. sys.exit(1)
  14. def load_and_upload(client, filepath):
  15. with open(filepath, "rb") as f:
  16. image_bytes = f.read()
  17. b64_image = base64.b64encode(image_bytes).decode('utf-8')
  18. image_payload = f"data:image/png;base64,{b64_image}"
  19. return client.process_image_url(image_payload)
  20. def main():
  21. client = LibLibAIClient()
  22. print("1. Searching for Juggernaut XL Checkpoint...")
  23. search_res = client.search_models('Juggernaut XL')
  24. first_model = search_res['data']['data'][0]
  25. version_uuid = first_model['versionUuid']
  26. print(f" -> Version UUID: {version_uuid}")
  27. print("2. Uploading local images to LibLib OSS...")
  28. depth_map_path = os.path.join(script_dir, "..", "depth_map.png")
  29. character_path = os.path.join(script_dir, "..", "character_ref_main.png")
  30. depth_url = load_and_upload(client, depth_map_path)
  31. print(f" -> Depth Map uploaded: {depth_url}")
  32. char_url = load_and_upload(client, character_path)
  33. print(f" -> Character Ref uploaded: {char_url}")
  34. print("3. Submitting Dual ControlNet (Depth + IP-Adapter) Task...")
  35. payload = {
  36. 'templateUuid': 'e10adc3949ba59abbe56e057f20f883e',
  37. 'generateParams': {
  38. 'checkPointId': version_uuid,
  39. 'prompt': 'A masterpiece, best quality, a beautiful girl on the right holding a clipboard, an easel on the left, photorealistic, 8k resolution, cinematic lighting',
  40. 'width': 1024,
  41. 'height': 1024,
  42. 'steps': 5,
  43. 'cfgScale': 1.5,
  44. 'imgCount': 1,
  45. 'controlNet': [
  46. {
  47. 'unitOrder': 1,
  48. 'sourceImage': depth_url,
  49. 'width': 1024,
  50. 'height': 1024,
  51. 'preprocessor': 3,
  52. 'model': '6349e9dae8814084bd9c1585d335c24c', # SDXL Depth Model
  53. 'annotationParameters': {
  54. 'depthLeres': {
  55. 'preprocessorResolution': 1024,
  56. 'removeNear': 0,
  57. 'removeBackground': 0
  58. }
  59. },
  60. 'controlWeight': 1.0,
  61. 'startingControlStep': 0.0,
  62. 'endingControlStep': 1.0,
  63. 'pixelPerfect': 1,
  64. 'controlMode': 0
  65. },
  66. {
  67. 'unitOrder': 2,
  68. 'sourceImage': char_url,
  69. 'width': 1024,
  70. 'height': 1024,
  71. 'preprocessor': 0,
  72. 'model': '8ea2538fdd7dcdea52b2da6b5151f875', # SDXL IP-Adapter Model
  73. 'annotationParameters': {}, # Usually empty for preprocessor=0
  74. 'controlWeight': 0.8,
  75. 'startingControlStep': 0.0,
  76. 'endingControlStep': 0.8,
  77. 'pixelPerfect': 1,
  78. 'controlMode': 0
  79. }
  80. ]
  81. }
  82. }
  83. try:
  84. task_id = client.submit_task_payload(payload)
  85. print(f" -> Task submitted successfully! Task ID: {task_id}")
  86. print("4. Waiting for generation result...")
  87. res = client.wait_for_result(task_id)
  88. print("\n=== Generation Task Success ===")
  89. print(json.dumps(res, indent=2, ensure_ascii=False))
  90. except Exception as e:
  91. print(f"\n=== Generation Error ===")
  92. print(f"Error: {e}")
  93. if __name__ == '__main__':
  94. main()