qqwjq1981 commited on
Commit
bbf5ede
·
verified ·
1 Parent(s): fda4fd0

Update utils/keyframe_utils.py

Browse files
Files changed (1) hide show
  1. utils/keyframe_utils.py +44 -23
utils/keyframe_utils.py CHANGED
@@ -3,6 +3,7 @@ import random
3
  import os
4
  from diffusers import StableDiffusionPipeline
5
  import torch
 
6
 
7
  # Load and cache the diffusion pipeline (only once)
8
  pipe = StableDiffusionPipeline.from_pretrained(
@@ -12,37 +13,57 @@ pipe = StableDiffusionPipeline.from_pretrained(
12
  pipe = pipe.to("cpu")
13
 
14
 
 
 
 
 
 
15
  def generate_keyframe_prompt(segment):
16
  """
17
- Generates a detailed prompt optimized for Stable Diffusion (low-resolution, preview style)
18
- based on the segment description.
19
  """
20
  description = segment.get("description", "")
21
  speaker = segment.get("speaker", "")
22
  narration = segment.get("narration", "")
23
  segment_id = segment.get("segment_id")
24
 
25
- prompt_parts = []
26
-
27
- if description:
28
- prompt_parts.append(f"Scene: {description}.")
29
-
30
- if speaker and narration:
31
- prompt_parts.append(f"Character '{speaker}' speaking: \"{narration}\".")
32
- elif narration:
33
- prompt_parts.append(f"Narration: \"{narration}\".")
34
-
35
- prompt_parts.append("Style: Simple, cartoonish, line art, sketch, low detail, illustrative, minimal background, focus on main subject.")
36
- prompt_parts.append("Resolution: lowres, 256x256.")
37
- prompt_parts.append("Lighting: Nighttime museum, dim lighting.")
38
- prompt_parts.append("Setting: Museum interior, exhibits.")
39
-
40
- negative_prompt = "blurry, distorted, ugly, tiling, poorly drawn, out of frame, disfigured, deformed, bad anatomy, watermark, text, signature, high detail, realistic, photorealistic, complex"
41
-
42
- return {
43
- "prompt": " ".join(prompt_parts).strip(),
44
- "negative_prompt": negative_prompt
45
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
46
 
47
 
48
  def generate_all_keyframe_images(script_data, output_dir="keyframes"):
 
3
  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
  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
+ # Global story context (in Chinese)
19
+ story_context_cn = "《博物馆的全能ACE》是一部拟人化博物馆文物与AI讲解助手互动的短片,讲述太阳人石刻在闭馆后的博物馆中,遇到了新来的AI助手博小翼,两者展开对话,AI展示了自己的多模态讲解能力与文化知识,最终被文物们认可,并一起展开智慧导览服务的故事。该片融合了文物拟人化、夜间博物馆奇妙氛围、科技感界面与中国地方文化元素,风格活泼、具未来感。"
20
+
21
  def generate_keyframe_prompt(segment):
22
  """
23
+ Calls GPT-4o to generate an image prompt optimized for Stable Diffusion,
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
+
33
+ 【整体故事背景】:\n{story_context_cn}
34
+
35
+ 【当前分镜描述】:\n{description}
36
+ 【角色】:{speaker}\n【台词或画外音】:{narration}
37
+
38
+ 请用英文输出一个简洁但具体的prompt,风格偏草图、线稿、卡通、简洁构图,并指出一个negative prompt。"
39
+
40
+ try:
41
+ response = openai.ChatCompletion.create(
42
+ model="gpt-4o",
43
+ messages=[
44
+ {"role": "system", "content": "You are an expert visual prompt designer for image generation."},
45
+ {"role": "user", "content": input_prompt}
46
+ ],
47
+ temperature=0.7
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
+ return {
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 {
64
+ "prompt": description,
65
+ "negative_prompt": ""
66
+ }
67
 
68
 
69
  def generate_all_keyframe_images(script_data, output_dir="keyframes"):