Spaces:
Sleeping
Sleeping
Update utils/keyframe_utils.py
Browse files- utils/keyframe_utils.py +29 -7
utils/keyframe_utils.py
CHANGED
@@ -2,8 +2,9 @@ import openai
|
|
2 |
import os
|
3 |
import json
|
4 |
from pathlib import Path
|
5 |
-
from diffusers import StableDiffusionPipeline
|
6 |
import torch
|
|
|
7 |
|
8 |
openai.api_key = os.getenv("OPENAI_API_KEY")
|
9 |
|
@@ -15,8 +16,18 @@ CACHE_DIR = Path("prompt_cache")
|
|
15 |
CACHE_DIR.mkdir(exist_ok=True)
|
16 |
LOG_PATH = Path("prompt_log.jsonl")
|
17 |
|
18 |
-
|
19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
|
21 |
def generate_keyframe_prompt(segment):
|
22 |
segment_id = segment.get("segment_id")
|
@@ -29,7 +40,7 @@ def generate_keyframe_prompt(segment):
|
|
29 |
speaker = segment.get("speaker", "")
|
30 |
narration = segment.get("narration", "")
|
31 |
|
32 |
-
input_prompt = f"你是一个擅长视觉脚本设计的AI,请基于以下故事整体背景与分镜内容,帮我生成一个适合用于Stable Diffusion图像生成的英文提示词(image prompt),用于生成低分辨率草图风格的关键帧。请注意突出主要角色、镜头氛围、光影、构图、动作,避免复杂背景和细节。\n\n【整体故事背景】:\n{story_context_cn}\n\n【当前分镜描述】:\n{description}\n【角色】:{speaker}\n【台词或画外音】:{narration}\n\n请用英文输出一个简洁但具体的prompt,风格偏草图、线稿、卡通、简洁构图,并指出一个negative prompt。"
|
33 |
|
34 |
try:
|
35 |
response = openai.ChatCompletion.create(
|
@@ -72,9 +83,20 @@ def generate_all_keyframe_images(script_data, output_dir="keyframes"):
|
|
72 |
negative_prompt = sd_prompts["negative_prompt"]
|
73 |
segment_id = segment.get("segment_id")
|
74 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
75 |
frame_images = []
|
76 |
for i in range(3):
|
77 |
-
|
|
|
|
|
|
|
|
|
78 |
image_path = os.path.join(output_dir, f"segment_{segment_id}_v{i+1}.png")
|
79 |
image.save(image_path)
|
80 |
frame_images.append(image_path)
|
@@ -86,9 +108,9 @@ def generate_all_keyframe_images(script_data, output_dir="keyframes"):
|
|
86 |
"frame_images": frame_images
|
87 |
})
|
88 |
|
89 |
-
print(f"✓ Generated 3 images for Segment {segment_id}")
|
90 |
|
91 |
with open("all_prompts_output.json", "w", encoding="utf-8") as f:
|
92 |
json.dump(keyframe_outputs, f, ensure_ascii=False, indent=2)
|
93 |
|
94 |
-
return keyframe_outputs
|
|
|
2 |
import os
|
3 |
import json
|
4 |
from pathlib import Path
|
5 |
+
from diffusers import StableDiffusionPipeline, StableDiffusionImg2ImgPipeline
|
6 |
import torch
|
7 |
+
from PIL import Image
|
8 |
|
9 |
openai.api_key = os.getenv("OPENAI_API_KEY")
|
10 |
|
|
|
16 |
CACHE_DIR.mkdir(exist_ok=True)
|
17 |
LOG_PATH = Path("prompt_log.jsonl")
|
18 |
|
19 |
+
# Pipelines
|
20 |
+
pipe_txt2img = StableDiffusionPipeline.from_pretrained("CompVis/stable-diffusion-v1-4", torch_dtype=torch.float16).to("cuda")
|
21 |
+
pipe_img2img = StableDiffusionImg2ImgPipeline.from_pretrained("CompVis/stable-diffusion-v1-4", torch_dtype=torch.float16).to("cuda")
|
22 |
+
|
23 |
+
# Reference image context for characters
|
24 |
+
REFERENCE_CONTEXT = "参考角色视觉信息:'太阳人石刻' 是带有放射状头饰、佩戴墨镜的新石器时代人物形象,风格庄严中略带潮流感。图像见 assets/sunman.png。'博小翼' 是一个圆头圆眼、漂浮型的可爱AI机器人助手,风格拟人、语气亲切,图像见 assets/boxiaoyi.png。"
|
25 |
+
|
26 |
+
# Reference image map
|
27 |
+
ASSET_IMAGES = {
|
28 |
+
"太阳人": "assets/sunman.png",
|
29 |
+
"博小翼": "assets/boxiaoyi.png"
|
30 |
+
}
|
31 |
|
32 |
def generate_keyframe_prompt(segment):
|
33 |
segment_id = segment.get("segment_id")
|
|
|
40 |
speaker = segment.get("speaker", "")
|
41 |
narration = segment.get("narration", "")
|
42 |
|
43 |
+
input_prompt = f"你是一个擅长视觉脚本设计的AI,请基于以下故事整体背景与分镜内容,帮我生成一个适合用于Stable Diffusion图像生成的英文提示词(image prompt),用于生成低分辨率草图风格的关键帧。请注意突出主要角色、镜头氛围、光影、构图、动作,避免复杂背景和细节。\n\n【整体故事背景】:\n{story_context_cn}\n\n【当前分镜描述】:\n{description}\n【角色】:{speaker}\n【台词或画外音】:{narration}\n\n{REFERENCE_CONTEXT}\n\n请用英文输出一个简洁但具体的prompt,风格偏草图、线稿、卡通、简洁构图,并指出一个negative prompt。"
|
44 |
|
45 |
try:
|
46 |
response = openai.ChatCompletion.create(
|
|
|
83 |
negative_prompt = sd_prompts["negative_prompt"]
|
84 |
segment_id = segment.get("segment_id")
|
85 |
|
86 |
+
description = segment.get("description", "")
|
87 |
+
use_reference = any(name in description for name in ASSET_IMAGES)
|
88 |
+
|
89 |
+
if use_reference:
|
90 |
+
ref_key = next(k for k in ASSET_IMAGES if k in description)
|
91 |
+
init_image = Image.open(ASSET_IMAGES[ref_key]).convert("RGB").resize((512, 512))
|
92 |
+
|
93 |
frame_images = []
|
94 |
for i in range(3):
|
95 |
+
if use_reference:
|
96 |
+
image = pipe_img2img(prompt=prompt, image=init_image, negative_prompt=negative_prompt, strength=0.6, guidance_scale=7.5).images[0]
|
97 |
+
else:
|
98 |
+
image = pipe_txt2img(prompt, negative_prompt=negative_prompt, num_inference_steps=20, guidance_scale=7.5, height=256, width=256).images[0]
|
99 |
+
|
100 |
image_path = os.path.join(output_dir, f"segment_{segment_id}_v{i+1}.png")
|
101 |
image.save(image_path)
|
102 |
frame_images.append(image_path)
|
|
|
108 |
"frame_images": frame_images
|
109 |
})
|
110 |
|
111 |
+
print(f"✓ Generated 3 images for Segment {segment_id} ({'img2img' if use_reference else 'txt2img'})")
|
112 |
|
113 |
with open("all_prompts_output.json", "w", encoding="utf-8") as f:
|
114 |
json.dump(keyframe_outputs, f, ensure_ascii=False, indent=2)
|
115 |
|
116 |
+
return keyframe_outputs
|