Spaces:
Sleeping
Sleeping
Update utils/keyframe_utils.py
Browse files- utils/keyframe_utils.py +11 -38
utils/keyframe_utils.py
CHANGED
@@ -1,38 +1,26 @@
|
|
1 |
-
import
|
2 |
-
import random
|
3 |
import os
|
|
|
|
|
4 |
from diffusers import StableDiffusionPipeline
|
5 |
import torch
|
6 |
-
import openai
|
7 |
-
from pathlib import Path
|
8 |
-
|
9 |
-
# Load and cache the diffusion pipeline (only once)
|
10 |
-
pipe = StableDiffusionPipeline.from_pretrained(
|
11 |
-
"CompVis/stable-diffusion-v1-4",
|
12 |
-
torch_dtype=torch.float16
|
13 |
-
)
|
14 |
-
pipe = pipe.to("cpu")
|
15 |
-
|
16 |
-
|
17 |
|
18 |
openai.api_key = os.getenv("OPENAI_API_KEY")
|
19 |
|
20 |
# Global story context
|
21 |
story_context_cn = "《博物馆的全能ACE》是一部拟人化博物馆文物与AI讲解助手互动的短片,讲述太阳人石刻在闭馆后的博物馆中,遇到了新来的AI助手博小翼,两者展开对话,AI展示了自己的多模态讲解能力与文化知识,最终被文物们认可,并一起展开智慧导览服务的故事。该片融合了文物拟人化、夜间博物馆奇妙氛围、科技感界面与中国地方文化元素,风格活泼、具未来感。"
|
22 |
|
23 |
-
# Cache
|
24 |
CACHE_DIR = Path("prompt_cache")
|
25 |
CACHE_DIR.mkdir(exist_ok=True)
|
26 |
LOG_PATH = Path("prompt_log.jsonl")
|
27 |
|
|
|
|
|
|
|
28 |
def generate_keyframe_prompt(segment):
|
29 |
-
"""
|
30 |
-
Generates and caches image prompts using GPT-4o for a given segment.
|
31 |
-
"""
|
32 |
segment_id = segment.get("segment_id")
|
33 |
cache_file = CACHE_DIR / f"segment_{segment_id}.json"
|
34 |
-
|
35 |
-
# Return cached result if exists
|
36 |
if cache_file.exists():
|
37 |
with open(cache_file, "r", encoding="utf-8") as f:
|
38 |
return json.load(f)
|
@@ -41,14 +29,7 @@ def generate_keyframe_prompt(segment):
|
|
41 |
speaker = segment.get("speaker", "")
|
42 |
narration = segment.get("narration", "")
|
43 |
|
44 |
-
input_prompt = f"你是一个擅长视觉脚本设计的AI,请基于以下故事整体背景与分镜内容,帮我生成一个适合用于Stable Diffusion图像生成的英文提示词(image prompt
|
45 |
-
|
46 |
-
【整体故事背景】:\n{story_context_cn}
|
47 |
-
|
48 |
-
【当前分镜描述】:\n{description}
|
49 |
-
【角色】:{speaker}\n【台词或画外音】:{narration}
|
50 |
-
|
51 |
-
请用英文输出一个简洁但具体的prompt,风格偏草图、线稿、卡通、简洁构图,并指出一个negative prompt。"
|
52 |
|
53 |
try:
|
54 |
response = openai.ChatCompletion.create(
|
@@ -60,7 +41,6 @@ def generate_keyframe_prompt(segment):
|
|
60 |
temperature=0.7
|
61 |
)
|
62 |
output_text = response["choices"][0]["message"]["content"]
|
63 |
-
|
64 |
if "Negative prompt:" in output_text:
|
65 |
prompt, negative = output_text.split("Negative prompt:", 1)
|
66 |
else:
|
@@ -70,17 +50,11 @@ def generate_keyframe_prompt(segment):
|
|
70 |
"prompt": prompt.strip(),
|
71 |
"negative_prompt": negative.strip()
|
72 |
}
|
73 |
-
|
74 |
-
# Save to cache
|
75 |
with open(cache_file, "w", encoding="utf-8") as f:
|
76 |
json.dump(result, f, ensure_ascii=False, indent=2)
|
77 |
-
|
78 |
-
# Log to JSONL for review
|
79 |
with open(LOG_PATH, "a", encoding="utf-8") as logf:
|
80 |
logf.write(json.dumps({"segment_id": segment_id, **result}, ensure_ascii=False) + "\n")
|
81 |
-
|
82 |
return result
|
83 |
-
|
84 |
except Exception as e:
|
85 |
print(f"[Error] GPT-4o prompt generation failed for segment {segment_id}: {e}")
|
86 |
return {
|
@@ -89,10 +63,6 @@ def generate_keyframe_prompt(segment):
|
|
89 |
}
|
90 |
|
91 |
def generate_all_keyframe_images(script_data, output_dir="keyframes"):
|
92 |
-
"""
|
93 |
-
Generates 3 keyframe images per segment using Stable Diffusion,
|
94 |
-
stores them in the given output directory.
|
95 |
-
"""
|
96 |
os.makedirs(output_dir, exist_ok=True)
|
97 |
keyframe_outputs = []
|
98 |
|
@@ -118,4 +88,7 @@ def generate_all_keyframe_images(script_data, output_dir="keyframes"):
|
|
118 |
|
119 |
print(f"✓ Generated 3 images for Segment {segment_id}")
|
120 |
|
|
|
|
|
|
|
121 |
return keyframe_outputs
|
|
|
1 |
+
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 |
|
10 |
# Global story context
|
11 |
story_context_cn = "《博物馆的全能ACE》是一部拟人化博物馆文物与AI讲解助手互动的短片,讲述太阳人石刻在闭馆后的博物馆中,遇到了新来的AI助手博小翼,两者展开对话,AI展示了自己的多模态讲解能力与文化知识,最终被文物们认可,并一起展开智慧导览服务的故事。该片融合了文物拟人化、夜间博物馆奇妙氛围、科技感界面与中国地方文化元素,风格活泼、具未来感。"
|
12 |
|
13 |
+
# Cache and log directories
|
14 |
CACHE_DIR = Path("prompt_cache")
|
15 |
CACHE_DIR.mkdir(exist_ok=True)
|
16 |
LOG_PATH = Path("prompt_log.jsonl")
|
17 |
|
18 |
+
pipe = StableDiffusionPipeline.from_pretrained("CompVis/stable-diffusion-v1-4", torch_dtype=torch.float16)
|
19 |
+
pipe = pipe.to("cuda")
|
20 |
+
|
21 |
def generate_keyframe_prompt(segment):
|
|
|
|
|
|
|
22 |
segment_id = segment.get("segment_id")
|
23 |
cache_file = CACHE_DIR / f"segment_{segment_id}.json"
|
|
|
|
|
24 |
if cache_file.exists():
|
25 |
with open(cache_file, "r", encoding="utf-8") as f:
|
26 |
return json.load(f)
|
|
|
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(
|
|
|
41 |
temperature=0.7
|
42 |
)
|
43 |
output_text = response["choices"][0]["message"]["content"]
|
|
|
44 |
if "Negative prompt:" in output_text:
|
45 |
prompt, negative = output_text.split("Negative prompt:", 1)
|
46 |
else:
|
|
|
50 |
"prompt": prompt.strip(),
|
51 |
"negative_prompt": negative.strip()
|
52 |
}
|
|
|
|
|
53 |
with open(cache_file, "w", encoding="utf-8") as f:
|
54 |
json.dump(result, f, ensure_ascii=False, indent=2)
|
|
|
|
|
55 |
with open(LOG_PATH, "a", encoding="utf-8") as logf:
|
56 |
logf.write(json.dumps({"segment_id": segment_id, **result}, ensure_ascii=False) + "\n")
|
|
|
57 |
return result
|
|
|
58 |
except Exception as e:
|
59 |
print(f"[Error] GPT-4o prompt generation failed for segment {segment_id}: {e}")
|
60 |
return {
|
|
|
63 |
}
|
64 |
|
65 |
def generate_all_keyframe_images(script_data, output_dir="keyframes"):
|
|
|
|
|
|
|
|
|
66 |
os.makedirs(output_dir, exist_ok=True)
|
67 |
keyframe_outputs = []
|
68 |
|
|
|
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
|