Spaces:
Running
Running
Update utils/keyframe_utils.py
Browse files- utils/keyframe_utils.py +30 -8
utils/keyframe_utils.py
CHANGED
@@ -4,6 +4,7 @@ import os
|
|
4 |
from diffusers import StableDiffusionPipeline
|
5 |
import torch
|
6 |
import openai
|
|
|
7 |
|
8 |
# Load and cache the diffusion pipeline (only once)
|
9 |
pipe = StableDiffusionPipeline.from_pretrained(
|
@@ -13,20 +14,32 @@ pipe = StableDiffusionPipeline.from_pretrained(
|
|
13 |
pipe = pipe.to("cpu")
|
14 |
|
15 |
|
16 |
-
openai.api_key = os.getenv("OPENAI_API_KEY") # Make sure this is set in your environment
|
17 |
|
18 |
-
|
|
|
|
|
19 |
story_context_cn = "《博物馆的全能ACE》是一部拟人化博物馆文物与AI讲解助手互动的短片,讲述太阳人石刻在闭馆后的博物馆中,遇到了新来的AI助手博小翼,两者展开对话,AI展示了自己的多模态讲解能力与文化知识,最终被文物们认可,并一起展开智慧导览服务的故事。该片融合了文物拟人化、夜间博物馆奇妙氛围、科技感界面与中国地方文化元素,风格活泼、具未来感。"
|
20 |
|
|
|
|
|
|
|
|
|
|
|
21 |
def generate_keyframe_prompt(segment):
|
22 |
"""
|
23 |
-
|
24 |
-
based on segment content and full story context.
|
25 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
description = segment.get("description", "")
|
27 |
speaker = segment.get("speaker", "")
|
28 |
narration = segment.get("narration", "")
|
29 |
-
segment_id = segment.get("segment_id")
|
30 |
|
31 |
input_prompt = f"你是一个擅长视觉脚本设计的AI,请基于以下故事整体背景与分镜内容,帮我生成一个适合用于Stable Diffusion图像生成的英文提示词(image prompt),用于生成低分辨率草图风格的关键帧。请注意突出主要角色、镜头氛围、光影、构图、动作,避免复杂背景和细节。
|
32 |
|
@@ -48,16 +61,26 @@ def generate_keyframe_prompt(segment):
|
|
48 |
)
|
49 |
output_text = response["choices"][0]["message"]["content"]
|
50 |
|
51 |
-
# Split response into prompt + negative if possible
|
52 |
if "Negative prompt:" in output_text:
|
53 |
prompt, negative = output_text.split("Negative prompt:", 1)
|
54 |
else:
|
55 |
prompt, negative = output_text, "blurry, distorted, low quality, text, watermark"
|
56 |
|
57 |
-
|
58 |
"prompt": prompt.strip(),
|
59 |
"negative_prompt": negative.strip()
|
60 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
61 |
except Exception as e:
|
62 |
print(f"[Error] GPT-4o prompt generation failed for segment {segment_id}: {e}")
|
63 |
return {
|
@@ -65,7 +88,6 @@ def generate_keyframe_prompt(segment):
|
|
65 |
"negative_prompt": ""
|
66 |
}
|
67 |
|
68 |
-
|
69 |
def generate_all_keyframe_images(script_data, output_dir="keyframes"):
|
70 |
"""
|
71 |
Generates 3 keyframe images per segment using Stable Diffusion,
|
|
|
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(
|
|
|
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 directory for prompts
|
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)
|
39 |
+
|
40 |
description = segment.get("description", "")
|
41 |
speaker = segment.get("speaker", "")
|
42 |
narration = segment.get("narration", "")
|
|
|
43 |
|
44 |
input_prompt = f"你是一个擅长视觉脚本设计的AI,请基于以下故事整体背景与分镜内容,帮我生成一个适合用于Stable Diffusion图像生成的英文提示词(image prompt),用于生成低分辨率草图风格的关键帧。请注意突出主要角色、镜头氛围、光影、构图、动作,避免复杂背景和细节。
|
45 |
|
|
|
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:
|
67 |
prompt, negative = output_text, "blurry, distorted, low quality, text, watermark"
|
68 |
|
69 |
+
result = {
|
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 {
|
|
|
88 |
"negative_prompt": ""
|
89 |
}
|
90 |
|
|
|
91 |
def generate_all_keyframe_images(script_data, output_dir="keyframes"):
|
92 |
"""
|
93 |
Generates 3 keyframe images per segment using Stable Diffusion,
|