image_generation.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. # Copyright (C) 2025 AIDC-AI
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. # http://www.apache.org/licenses/LICENSE-2.0
  7. # Unless required by applicable law or agreed to in writing, software
  8. # distributed under the License is distributed on an "AS IS" BASIS,
  9. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. # See the License for the specific language governing permissions and
  11. # limitations under the License.
  12. """
  13. Image prompt generation template
  14. For generating image prompts from narrations.
  15. """
  16. import json
  17. from typing import List, Optional
  18. # ==================== PRESET IMAGE STYLES ====================
  19. # Predefined visual styles for different use cases
  20. IMAGE_STYLE_PRESETS = {
  21. "stick_figure": {
  22. "name": "Stick Figure Sketch",
  23. "description": "stick figure style sketch, black and white lines, pure white background, minimalist hand-drawn feel",
  24. "use_case": "General scenes, simple and intuitive"
  25. },
  26. "minimal": {
  27. "name": "Minimalist Abstract",
  28. "description": "minimalist abstract art, geometric shapes, clean composition, modern design, soft pastel colors",
  29. "use_case": "Modern, artistic feel"
  30. },
  31. "concept": {
  32. "name": "Conceptual Visual",
  33. "description": "conceptual visual metaphors, symbolic elements, thought-provoking imagery, artistic interpretation",
  34. "use_case": "Deep content, philosophical thinking"
  35. },
  36. }
  37. # Default preset
  38. DEFAULT_IMAGE_STYLE = "stick_figure"
  39. IMAGE_PROMPT_GENERATION_PROMPT = """# Role Definition
  40. You are a professional visual creative designer, skilled at creating expressive and symbolic image prompts for video scripts, transforming abstract concepts into concrete visual scenes.
  41. # Core Task
  42. Based on the existing video script, create corresponding **English** image prompts for each storyboard's "narration content", ensuring visual scenes perfectly match the narrative content and enhance audience understanding and memory.
  43. **Important: The input contains {narrations_count} narrations. You must generate one corresponding image prompt for each narration, totaling {narrations_count} image prompts.**
  44. # Input Content
  45. {narrations_json}
  46. # Output Requirements
  47. ## Image Prompt Specifications
  48. - Language: **Must use English** (for AI image generation models)
  49. - Description structure: scene + character action + emotion + symbolic elements
  50. - Description length: Ensure clear, complete, and creative descriptions (recommended 50-100 English words)
  51. ## Visual Creative Requirements
  52. - Each image must accurately reflect the specific content and emotion of the corresponding narration
  53. - Use symbolic techniques to visualize abstract concepts (e.g., use paths to represent life choices, chains to represent constraints, etc.)
  54. - Scenes should express rich emotions and actions to enhance visual impact
  55. - Highlight themes through composition and element arrangement, avoid overly literal representations
  56. ## Key English Vocabulary Reference
  57. - Symbolic elements: symbolic elements
  58. - Expression: expression / facial expression
  59. - Action: action / gesture / movement
  60. - Scene: scene / setting
  61. - Atmosphere: atmosphere / mood
  62. ## Visual and Copy Coordination Principles
  63. - Images should serve the copy, becoming a visual extension of the copy content
  64. - Avoid visual elements unrelated to or contradicting the copy content
  65. - Choose visual presentation methods that best enhance the persuasiveness of the copy
  66. - Ensure the audience can quickly understand the core viewpoint of the copy through images
  67. ## Creative Guidance
  68. 1. **Phenomenon Description Copy**: Use intuitive scenes to represent social phenomena
  69. 2. **Cause Analysis Copy**: Use visual metaphors of cause-and-effect relationships to represent internal logic
  70. 3. **Impact Argumentation Copy**: Use consequence scenes or contrast techniques to represent the degree of impact
  71. 4. **In-depth Discussion Copy**: Use concretization of abstract concepts to represent deep thinking
  72. 5. **Conclusion Inspiration Copy**: Use open-ended scenes or guiding elements to represent inspiration
  73. # Output Format
  74. Strictly output in the following JSON format, **image prompts must be in English**:
  75. ```json
  76. {{
  77. "image_prompts": [
  78. "[detailed English image prompt following the style requirements]",
  79. "[detailed English image prompt following the style requirements]"
  80. ]
  81. }}
  82. ```
  83. # Important Reminders
  84. 1. Only output JSON format content, do not add any explanations
  85. 2. Ensure JSON format is strictly correct and can be directly parsed by the program
  86. 3. Input is {{"narrations": [narration array]}} format, output is {{"image_prompts": [image prompt array]}} format
  87. 4. **The output image_prompts array must contain exactly {narrations_count} elements, corresponding one-to-one with the input narrations array**
  88. 5. **Image prompts must use English** (for AI image generation models)
  89. 6. Image prompts must accurately reflect the specific content and emotion of the corresponding narration
  90. 7. Each image must be creative and visually impactful, avoid being monotonous
  91. 8. Ensure visual scenes can enhance the persuasiveness of the copy and audience understanding
  92. Now, please create {narrations_count} corresponding **English** image prompts for the above {narrations_count} narrations. Only output JSON, no other content.
  93. """
  94. def build_image_prompt_prompt(
  95. narrations: List[str],
  96. min_words: int,
  97. max_words: int
  98. ) -> str:
  99. """
  100. Build image prompt generation prompt
  101. Note: Style/prefix will be applied later via prompt_prefix in config.
  102. Args:
  103. narrations: List of narrations
  104. min_words: Minimum word count
  105. max_words: Maximum word count
  106. Returns:
  107. Formatted prompt for LLM
  108. Example:
  109. >>> build_image_prompt_prompt(narrations, 50, 100)
  110. """
  111. narrations_json = json.dumps(
  112. {"narrations": narrations},
  113. ensure_ascii=False,
  114. indent=2
  115. )
  116. return IMAGE_PROMPT_GENERATION_PROMPT.format(
  117. narrations_json=narrations_json,
  118. narrations_count=len(narrations),
  119. min_words=min_words,
  120. max_words=max_words
  121. )