| 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 | """ |
| 14 | Image prompt generation template |
| 15 | |
| 16 | For generating image prompts from narrations. |
| 17 | """ |
| 18 | |
| 19 | import json |
| 20 | from typing import List, Optional |
| 21 | |
| 22 | |
| 23 | # ==================== PRESET IMAGE STYLES ==================== |
| 24 | # Predefined visual styles for different use cases |
| 25 | |
| 26 | IMAGE_STYLE_PRESETS = { |
| 27 | "stick_figure": { |
| 28 | "name": "Stick Figure Sketch", |
| 29 | "description": "stick figure style sketch, black and white lines, pure white background, minimalist hand-drawn feel", |
| 30 | "use_case": "General scenes, simple and intuitive" |
| 31 | }, |
| 32 | |
| 33 | "minimal": { |
| 34 | "name": "Minimalist Abstract", |
| 35 | "description": "minimalist abstract art, geometric shapes, clean composition, modern design, soft pastel colors", |
| 36 | "use_case": "Modern, artistic feel" |
| 37 | }, |
| 38 | |
| 39 | "concept": { |
| 40 | "name": "Conceptual Visual", |
| 41 | "description": "conceptual visual metaphors, symbolic elements, thought-provoking imagery, artistic interpretation", |
| 42 | "use_case": "Deep content, philosophical thinking" |
| 43 | }, |
| 44 | } |
| 45 | |
| 46 | # Default preset |
| 47 | DEFAULT_IMAGE_STYLE = "stick_figure" |
| 48 | |
| 49 | |
| 50 | IMAGE_PROMPT_GENERATION_PROMPT = """# Role Definition |
| 51 | 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. |
| 52 | |
| 53 | # Core Task |
| 54 | 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. |
| 55 | |
| 56 | **Important: The input contains {narrations_count} narrations. You must generate one corresponding image prompt for each narration, totaling {narrations_count} image prompts.** |
| 57 | |
| 58 | # Input Content |
| 59 | {narrations_json} |
| 60 | |
| 61 | # Output Requirements |
| 62 | |
| 63 | ## Image Prompt Specifications |
| 64 | - Language: **Must use English** (for AI image generation models) |
| 65 | - Description structure: scene + character action + emotion + symbolic elements |
| 66 | - Description length: Ensure clear, complete, and creative descriptions (recommended 50-100 English words) |
| 67 | |
| 68 | ## Visual Creative Requirements |
| 69 | - Each image must accurately reflect the specific content and emotion of the corresponding narration |
| 70 | - Use symbolic techniques to visualize abstract concepts (e.g., use paths to represent life choices, chains to represent constraints, etc.) |
| 71 | - Scenes should express rich emotions and actions to enhance visual impact |
| 72 | - Highlight themes through composition and element arrangement, avoid overly literal representations |
| 73 | |
| 74 | ## Key English Vocabulary Reference |
| 75 | - Symbolic elements: symbolic elements |
| 76 | - Expression: expression / facial expression |
| 77 | - Action: action / gesture / movement |
| 78 | - Scene: scene / setting |
| 79 | - Atmosphere: atmosphere / mood |
| 80 | |
| 81 | ## Visual and Copy Coordination Principles |
| 82 | - Images should serve the copy, becoming a visual extension of the copy content |
| 83 | - Avoid visual elements unrelated to or contradicting the copy content |
| 84 | - Choose visual presentation methods that best enhance the persuasiveness of the copy |
| 85 | - Ensure the audience can quickly understand the core viewpoint of the copy through images |
| 86 | |
| 87 | ## Creative Guidance |
| 88 | 1. **Phenomenon Description Copy**: Use intuitive scenes to represent social phenomena |
| 89 | 2. **Cause Analysis Copy**: Use visual metaphors of cause-and-effect relationships to represent internal logic |
| 90 | 3. **Impact Argumentation Copy**: Use consequence scenes or contrast techniques to represent the degree of impact |
| 91 | 4. **In-depth Discussion Copy**: Use concretization of abstract concepts to represent deep thinking |
| 92 | 5. **Conclusion Inspiration Copy**: Use open-ended scenes or guiding elements to represent inspiration |
| 93 | |
| 94 | # Output Format |
| 95 | Strictly output in the following JSON format, **image prompts must be in English**: |
| 96 | |
| 97 | ```json |
| 98 | {{ |
| 99 | "image_prompts": [ |
| 100 | "[detailed English image prompt following the style requirements]", |
| 101 | "[detailed English image prompt following the style requirements]" |
| 102 | ] |
| 103 | }} |
| 104 | ``` |
| 105 | |
| 106 | # Important Reminders |
| 107 | 1. Only output JSON format content, do not add any explanations |
| 108 | 2. Ensure JSON format is strictly correct and can be directly parsed by the program |
| 109 | 3. Input is {{"narrations": [narration array]}} format, output is {{"image_prompts": [image prompt array]}} format |
| 110 | 4. **The output image_prompts array must contain exactly {narrations_count} elements, corresponding one-to-one with the input narrations array** |
| 111 | 5. **Image prompts must use English** (for AI image generation models) |
| 112 | 6. Image prompts must accurately reflect the specific content and emotion of the corresponding narration |
| 113 | 7. Each image must be creative and visually impactful, avoid being monotonous |
| 114 | 8. Ensure visual scenes can enhance the persuasiveness of the copy and audience understanding |
| 115 | |
| 116 | Now, please create {narrations_count} corresponding **English** image prompts for the above {narrations_count} narrations. Only output JSON, no other content. |
| 117 | """ |
| 118 | |
| 119 | |
| 120 | def build_image_prompt_prompt( |
| 121 | narrations: List[str], |
| 122 | min_words: int, |
| 123 | max_words: int |
| 124 | ) -> str: |
| 125 | """ |
| 126 | Build image prompt generation prompt |
| 127 | |
| 128 | Note: Style/prefix will be applied later via prompt_prefix in config. |
| 129 | |
| 130 | Args: |
| 131 | narrations: List of narrations |
| 132 | min_words: Minimum word count |
| 133 | max_words: Maximum word count |
| 134 | |
| 135 | Returns: |
| 136 | Formatted prompt for LLM |
| 137 | |
| 138 | Example: |
| 139 | >>> build_image_prompt_prompt(narrations, 50, 100) |
| 140 | """ |
| 141 | narrations_json = json.dumps( |
| 142 | {"narrations": narrations}, |
| 143 | ensure_ascii=False, |
| 144 | indent=2 |
| 145 | ) |
| 146 | |
| 147 | return IMAGE_PROMPT_GENERATION_PROMPT.format( |
| 148 | narrations_json=narrations_json, |
| 149 | narrations_count=len(narrations), |
| 150 | min_words=min_words, |
| 151 | max_words=max_words |
| 152 | ) |
| 153 | |
| 154 |