Spaces:
Sleeping
Sleeping
Update utils/keyframe_utils.py
Browse files- utils/keyframe_utils.py +5 -47
utils/keyframe_utils.py
CHANGED
@@ -1,12 +1,12 @@
|
|
1 |
-
import openai
|
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 |
-
|
10 |
|
11 |
# Global story context
|
12 |
story_context_cn = "《博物馆的全能ACE》是一部拟人化博物馆文物与AI讲解助手互动的短片,讲述太阳人石刻在闭馆后的博物馆中,遇到了新来的AI助手博小翼,两者展开对话,AI展示了自己的多模态讲解能力与文化知识,最终被文物们认可,并一起展开智慧导览服务的故事。该片融合了文物拟人化、夜间博物馆奇妙氛围、科技感界面与中国地方文化元素,风格活泼、具未来感。"
|
@@ -40,10 +40,10 @@ def generate_keyframe_prompt(segment):
|
|
40 |
speaker = segment.get("speaker", "")
|
41 |
narration = segment.get("narration", "")
|
42 |
|
43 |
-
input_prompt = f"你是一个擅长视觉脚本设计的AI,请基于以下故事整体背景与分镜内容,帮我生成一个适合用于Stable Diffusion图像生成的英文提示词(image prompt
|
44 |
|
45 |
try:
|
46 |
-
response =
|
47 |
model="gpt-4o",
|
48 |
messages=[
|
49 |
{"role": "system", "content": "You are an expert visual prompt designer for image generation."},
|
@@ -51,7 +51,7 @@ def generate_keyframe_prompt(segment):
|
|
51 |
],
|
52 |
temperature=0.7
|
53 |
)
|
54 |
-
output_text = response
|
55 |
if "Negative prompt:" in output_text:
|
56 |
prompt, negative = output_text.split("Negative prompt:", 1)
|
57 |
else:
|
@@ -72,45 +72,3 @@ def generate_keyframe_prompt(segment):
|
|
72 |
"prompt": description,
|
73 |
"negative_prompt": ""
|
74 |
}
|
75 |
-
|
76 |
-
def generate_all_keyframe_images(script_data, output_dir="keyframes"):
|
77 |
-
os.makedirs(output_dir, exist_ok=True)
|
78 |
-
keyframe_outputs = []
|
79 |
-
|
80 |
-
for segment in script_data:
|
81 |
-
sd_prompts = generate_keyframe_prompt(segment)
|
82 |
-
prompt = sd_prompts["prompt"]
|
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)
|
103 |
-
|
104 |
-
keyframe_outputs.append({
|
105 |
-
"segment_id": segment_id,
|
106 |
-
"prompt": prompt,
|
107 |
-
"negative_prompt": negative_prompt,
|
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
|
|
|
|
|
1 |
import os
|
2 |
import json
|
3 |
from pathlib import Path
|
4 |
from diffusers import StableDiffusionPipeline, StableDiffusionImg2ImgPipeline
|
5 |
import torch
|
6 |
from PIL import Image
|
7 |
+
from openai import OpenAI
|
8 |
|
9 |
+
client = OpenAI()
|
10 |
|
11 |
# Global story context
|
12 |
story_context_cn = "《博物馆的全能ACE》是一部拟人化博物馆文物与AI讲解助手互动的短片,讲述太阳人石刻在闭馆后的博物馆中,遇到了新来的AI助手博小翼,两者展开对话,AI展示了自己的多模态讲解能力与文化知识,最终被文物们认可,并一起展开智慧导览服务的故事。该片融合了文物拟人化、夜间博物馆奇妙氛围、科技感界面与中国地方文化元素,风格活泼、具未来感。"
|
|
|
40 |
speaker = segment.get("speaker", "")
|
41 |
narration = segment.get("narration", "")
|
42 |
|
43 |
+
input_prompt = f"你是一个擅长视觉脚本设计的AI,请基于以下故事整体背景与分镜内容,帮我生成一个适合用于Stable Diffusion图像生成的英文提示词(image prompt),用于生成低分辨率草图风格的关键帧。请注意突出主要角色、镜头氛围、光影、构图、动作,避免复杂背景和细节。提示词长度不应超过80词,以防止超出Stable Diffusion的token限制。\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 = client.chat.completions.create(
|
47 |
model="gpt-4o",
|
48 |
messages=[
|
49 |
{"role": "system", "content": "You are an expert visual prompt designer for image generation."},
|
|
|
51 |
],
|
52 |
temperature=0.7
|
53 |
)
|
54 |
+
output_text = response.choices[0].message.content
|
55 |
if "Negative prompt:" in output_text:
|
56 |
prompt, negative = output_text.split("Negative prompt:", 1)
|
57 |
else:
|
|
|
72 |
"prompt": description,
|
73 |
"negative_prompt": ""
|
74 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|