debug_generate_ai_material.py 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. """手动调试 AI 图片生成和 OSS 上传。
  2. 默认只生成图片并上传 OSS,打印 JSON 输出;不会创建广告/创意。
  3. 示例:
  4. python debug_generate_ai_material.py --video-id 71187017
  5. 需要环境变量:
  6. OPENROUTER_API_KEY
  7. ALIYUN_OSS_ENDPOINT
  8. ALIYUN_OSS_BUCKET
  9. ALIYUN_OSS_ACCESS_KEY_ID
  10. ALIYUN_OSS_ACCESS_KEY_SECRET
  11. AI_IMAGE_PUBLIC_BASE_URL
  12. """
  13. from __future__ import annotations
  14. import argparse
  15. import json
  16. import sys
  17. from pathlib import Path
  18. from dotenv import load_dotenv
  19. _HERE = Path(__file__).parent
  20. load_dotenv(_HERE / ".env")
  21. sys.path.insert(0, str(_HERE.parent.parent))
  22. sys.path.insert(0, str(_HERE))
  23. from tools.ai_generated_material import ( # noqa: E402
  24. OPENROUTER_IMAGE_MODEL,
  25. build_ai_image_object_key,
  26. build_generation_prompts,
  27. generate_image_bytes,
  28. sanitize_video_description,
  29. upload_image_to_oss,
  30. )
  31. from tools.video_feature_query import read_cached_video_element_features # noqa: E402
  32. def main() -> int:
  33. parser = argparse.ArgumentParser()
  34. parser.add_argument("--video-id", type=int, default=0)
  35. parser.add_argument("--title", default="")
  36. parser.add_argument("--category", default="")
  37. parser.add_argument("--model", default=OPENROUTER_IMAGE_MODEL)
  38. parser.add_argument("--limit", type=int, default=1)
  39. args = parser.parse_args()
  40. if not args.video_id:
  41. raise SystemExit("必须提供 --video-id")
  42. features_by_vid = read_cached_video_element_features([args.video_id])
  43. features = features_by_vid.get(args.video_id) or []
  44. topic = next((f.standard_element for f in features if f.element_dimension == "解构选题"), "")
  45. sanitized_description = sanitize_video_description(topic) if topic else ""
  46. prompts = [
  47. (p.prompt_type, p.prompt_text)
  48. for p in build_generation_prompts(
  49. video_id=args.video_id,
  50. title=args.title,
  51. category=args.category,
  52. features=features,
  53. sanitized_description=sanitized_description,
  54. )
  55. ][: max(1, args.limit)]
  56. outputs = []
  57. for prompt_type, prompt_text in prompts:
  58. image_bytes, content_type, raw = generate_image_bytes(prompt_text, model=args.model)
  59. ext = ".png" if content_type == "image/png" else ".jpg"
  60. object_key = build_ai_image_object_key(
  61. account_id="debug",
  62. landing_video_id=args.video_id,
  63. prompt_type=prompt_type,
  64. extension=ext,
  65. debug=True,
  66. )
  67. oss_url = upload_image_to_oss(image_bytes, content_type, object_key)
  68. outputs.append({
  69. "prompt_type": prompt_type,
  70. "model": args.model,
  71. "content_type": content_type,
  72. "oss_url": oss_url,
  73. "object_key": object_key,
  74. "prompt_text": prompt_text,
  75. "raw_response_id": raw.get("id"),
  76. })
  77. print(json.dumps(outputs, ensure_ascii=False, indent=2))
  78. return 0
  79. if __name__ == "__main__":
  80. raise SystemExit(main())