test_liblib_pipeline.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. import sys
  2. import os
  3. import json
  4. sys.stdout.reconfigure(encoding='utf-8')
  5. script_dir = os.path.dirname(os.path.abspath(__file__))
  6. tools_dir = os.path.join(script_dir, '..', 'tools', 'local', 'liblibai_controlnet')
  7. sys.path.append(tools_dir)
  8. try:
  9. from liblibai_client import LibLibAIClient, TEMPLATE_UUID, INSTANT_ID_TEMPLATE_UUID
  10. except ImportError:
  11. print(f"Failed to import LibLibAIClient. Make sure the path {tools_dir} is correct.")
  12. sys.exit(1)
  13. def main():
  14. client = LibLibAIClient()
  15. # 1. Search for a checkpoint's UUID
  16. # Because liblibai_client defaults to XL controlnet models (self.sdxl_canny),
  17. # we search for an XL model to ensure the baseType matches (Rule 1).
  18. keyword = "Juggernaut XL"
  19. print(f"1. Searching for Checkpoint matching '{keyword}'...")
  20. search_res = client.search_models(keyword)
  21. if not search_res or search_res.get('code') != 0 or not search_res.get('data', {}).get('data'):
  22. print("Search failed or no models found.")
  23. print(search_res)
  24. return
  25. # Get the first result
  26. first_model = search_res['data']['data'][0]
  27. uuid = first_model['uuid']
  28. version_uuid = first_model['versionUuid']
  29. model_name = first_model.get('name')
  30. base_types = first_model.get('baseType', [])
  31. print(f" -> Found Checkpoint: '{model_name}'")
  32. print(f" -> UUID: {uuid}")
  33. print(f" -> Version UUID: {version_uuid}")
  34. print(f" -> baseType: {base_types} (1=1.5, 2=XL etc.)\n")
  35. # 2. Match local template_id
  36. # According to our docs: `e10adc3949ba59abbe56e057f20f883e` is for 1.5/XL.
  37. print(f"2. Matching local template_id based on baseType ...")
  38. print(f" -> Client is currently configured to use TEMPLATE_UUID: {TEMPLATE_UUID}")
  39. print(f" -> Client INSTANT_ID_TEMPLATE_UUID: {INSTANT_ID_TEMPLATE_UUID}\n")
  40. # 3. Request ControlNet Image Generation
  41. print("3. Executing controlnet generation (using Canny + found Checkpoint)...")
  42. # We use a valid test image (has to be standard or accessible)
  43. # This is an image URL hosted on liblib's OSS or accessible publicly.
  44. # Using a clean realistic portrait to avoid Canny extracting text/watermarks.
  45. test_image_url = "https://images.unsplash.com/photo-1544005313-94ddf0286df2?ixlib=rb-4.0.3&auto=format&fit=crop&w=1024&q=80"
  46. try:
  47. # Request advanced generation with Canny controlnet
  48. gen_res = client.generate_advanced(
  49. mode="canny",
  50. prompt="A masterpiece, best quality, beautiful girl, highly detailed, photorealistic, 8k resolution",
  51. image=test_image_url,
  52. base_model_uuid=version_uuid, # Override CheckPointId with the found version_uuid
  53. width=1024,
  54. height=1024,
  55. steps=5,
  56. cfg_scale=1.5
  57. )
  58. print("\n=== Generation Task Success ===")
  59. print(json.dumps(gen_res, indent=2, ensure_ascii=False))
  60. except Exception as e:
  61. print(f"\n=== Generation Error ===")
  62. print(f"Error: {e}")
  63. if __name__ == '__main__':
  64. main()