Update app.py
Browse files
app.py
CHANGED
|
@@ -1,47 +1,339 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import re
|
| 3 |
-
from transformers import pipeline
|
| 4 |
from diffusers import StableDiffusionPipeline
|
| 5 |
from pydub import AudioSegment
|
| 6 |
import zipfile
|
| 7 |
from io import BytesIO
|
| 8 |
import json
|
| 9 |
import torch
|
|
|
|
| 10 |
|
| 11 |
# 初始化模型 (全部使用原始pipeline)
|
| 12 |
@st.cache_resource
|
| 13 |
def load_pipelines():
|
| 14 |
try:
|
|
|
|
|
|
|
|
|
|
| 15 |
# 1. Script generation
|
| 16 |
script_pipe = pipeline(
|
| 17 |
"text2text-generation",
|
| 18 |
model="RUCAIBox/mvp-story",
|
| 19 |
-
device=
|
| 20 |
)
|
| 21 |
|
| 22 |
# 2. Storyboard generation (BART model)
|
| 23 |
storyboard_pipe = pipeline(
|
| 24 |
"text2text-generation",
|
| 25 |
model="Jessiesj/script_gpt2-story",
|
| 26 |
-
device=
|
| 27 |
)
|
| 28 |
|
| 29 |
# 3. Soundtrack generation (MusicGen)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
music_pipe = pipeline(
|
| 31 |
-
"text-to-audio",
|
| 32 |
-
model=
|
| 33 |
-
|
|
|
|
| 34 |
)
|
| 35 |
|
| 36 |
# 4. Storyboard image generation (Stable Diffusion)
|
| 37 |
image_pipe = StableDiffusionPipeline.from_pretrained(
|
| 38 |
"prompthero/openjourney-v4",
|
| 39 |
safety_checker=None,
|
| 40 |
-
torch_dtype=
|
| 41 |
-
)
|
| 42 |
|
| 43 |
-
return script_pipe, storyboard_pipe, image_pipe
|
| 44 |
-
# return script_pipe, storyboard_pipe, music_pipe, image_pipe
|
| 45 |
|
| 46 |
except Exception as e:
|
| 47 |
st.error(f"Failed to load models: {str(e)}")
|
|
@@ -77,8 +369,7 @@ def format_script(text):
|
|
| 77 |
return re.sub(r"(?<!### )Scene\d+", r"### Scene\g<0>", text)
|
| 78 |
|
| 79 |
# Load pipelines
|
| 80 |
-
script_pipe, storyboard_pipe, image_pipe = load_pipelines()
|
| 81 |
-
# script_pipe, storyboard_pipe, music_pipe, image_pipe = load_pipelines()
|
| 82 |
|
| 83 |
# Streamlit interface
|
| 84 |
st.title("🎥 Micro - movie Intelligent Creation System")
|
|
@@ -117,10 +408,10 @@ if user_input and style_or_opening:
|
|
| 117 |
st.subheader("Generated Script")
|
| 118 |
cleaned = format_script(response) # Ensure cleaned is defined here
|
| 119 |
st.markdown(f"```markdown\n{format_script(response)}\n```")
|
| 120 |
-
status.update(label="✅ Generation completed"
|
| 121 |
|
| 122 |
except Exception as e:
|
| 123 |
-
status.update(label="❌ Generation failed"
|
| 124 |
st.error(f"Error details: {str(e)}")
|
| 125 |
st.stop()
|
| 126 |
|
|
@@ -163,120 +454,70 @@ if user_input and style_or_opening:
|
|
| 163 |
}"""
|
| 164 |
|
| 165 |
# 生成分镜
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
|
| 172 |
# 增强的格式修复
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
# )
|
| 179 |
-
# storyboard = re.search(r'\{.*\}', storyboard, re.DOTALL).group()
|
| 180 |
-
# parsed = json.loads(storyboard)
|
| 181 |
-
parsed = json.loads(sample_storyboard)
|
| 182 |
-
except Exception as e:
|
| 183 |
-
# st.warning(f"Using sample storyboard due to error: {str(e)}")
|
| 184 |
-
# parsed = json.loads(sample_storyboard) # 回退到示例数据
|
| 185 |
-
# status.update(label="⚠️ Using sample storyboard", status="warning")
|
| 186 |
-
pass
|
| 187 |
-
|
| 188 |
-
finally:
|
| 189 |
-
# status.update(label="Storyboard ready", status="complete")
|
| 190 |
-
# st.subheader("Storyboard Script")
|
| 191 |
-
# st.json(parsed)
|
| 192 |
-
# st.caption("*Note: If using sample data, edit the JSON before proceeding*")
|
| 193 |
-
pass
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
# 3. 生成配乐
|
| 197 |
-
# with st.spinner("🎵 正在生成配乐..."):
|
| 198 |
-
# audio = music_pipe(
|
| 199 |
-
# "epic sci-fi background music",
|
| 200 |
-
# max_new_tokens=256
|
| 201 |
-
# )
|
| 202 |
-
# audio_buffer = BytesIO()
|
| 203 |
-
# audio["audio"].export(audio_buffer, format="wav")
|
| 204 |
-
|
| 205 |
-
# st.subheader("🎧 背景音乐")
|
| 206 |
-
# st.audio(audio_buffer, format="audio/wav")
|
| 207 |
-
|
| 208 |
-
# 4. 生成分镜图(可选)
|
| 209 |
-
# if st.toggle("🎨 生成分镜预览图(需要2-3分钟)"):
|
| 210 |
-
# with st.spinner("🖼️ 正在渲染电影级画面..."):
|
| 211 |
-
# # 从剧本提取关键描述
|
| 212 |
-
# scene_desc = re.search(r"场景\d+:(.*?)\n", script)
|
| 213 |
-
# if scene_desc:
|
| 214 |
-
# scene_desc = scene_desc.group(1)
|
| 215 |
-
# else:
|
| 216 |
-
# scene_desc = f"movie scene of {user_input}"
|
| 217 |
-
|
| 218 |
-
# # 精细化图片提示词
|
| 219 |
-
# image_prompt = f"""电影剧照,{scene_desc},
|
| 220 |
-
# 超高清8K分辨率,
|
| 221 |
-
# 电影级打光,浅景深效果,
|
| 222 |
-
# Unreal Engine 5渲染,
|
| 223 |
-
# 电影胶片颗粒感"""
|
| 224 |
-
|
| 225 |
-
# # 优化生成参数
|
| 226 |
-
# image = image_pipe(
|
| 227 |
-
# image_prompt,
|
| 228 |
-
# num_inference_steps=50,
|
| 229 |
-
# guidance_scale=9.5,
|
| 230 |
-
# width=1024,
|
| 231 |
-
# height=576 # 16:9电影比例
|
| 232 |
-
# ).images[0]
|
| 233 |
-
|
| 234 |
-
# st.subheader("🎞️ 首场景预览")
|
| 235 |
-
# st.image(image, caption=scene_desc)
|
| 236 |
-
|
| 237 |
-
# except Exception as e:
|
| 238 |
-
# st.error("生成过程中发生错误")
|
| 239 |
-
# st.code(f"错误详情:{str(e)}")
|
| 240 |
-
# st.button("点击查看技术详情",
|
| 241 |
-
# help="将以下信息提供给技术人员:\n" + str(e))
|
| 242 |
-
|
| 243 |
-
st.subheader("🎧 Background Music")
|
| 244 |
-
for i, scene in enumerate(parsed["scenes"]):
|
| 245 |
-
action = scene["action"]
|
| 246 |
-
with st.spinner(f"🎵 Generating soundtrack for scene {i + 1}..."):
|
| 247 |
-
try:
|
| 248 |
-
audio = music_pipe(
|
| 249 |
-
action,
|
| 250 |
-
max_new_tokens=256
|
| 251 |
)
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
|
| 255 |
except Exception as e:
|
| 256 |
-
st.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 257 |
|
| 258 |
-
st.subheader("
|
| 259 |
-
for i, scene in enumerate(parsed["scenes"]):
|
| 260 |
-
|
| 261 |
-
|
| 262 |
-
with st.spinner(f"🖼️ Rendering image for scene {i + 1}..."):
|
| 263 |
try:
|
| 264 |
-
|
| 265 |
-
|
| 266 |
-
|
| 267 |
-
|
| 268 |
-
|
| 269 |
-
|
| 270 |
-
|
| 271 |
-
# 优化生成参数
|
| 272 |
-
image = image_pipe(
|
| 273 |
-
image_prompt,
|
| 274 |
-
num_inference_steps=50,
|
| 275 |
-
guidance_scale=9.5,
|
| 276 |
-
width=1024,
|
| 277 |
-
height=576 # 16:9 movie ratio
|
| 278 |
-
).images[0]
|
| 279 |
-
|
| 280 |
-
st.image(image, caption=f"Preview for scene {i + 1}")
|
| 281 |
except Exception as e:
|
| 282 |
-
st.error(f"Failed to generate
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# import streamlit as st
|
| 2 |
+
# import re
|
| 3 |
+
# from transformers import pipeline
|
| 4 |
+
# from diffusers import StableDiffusionPipeline
|
| 5 |
+
# from pydub import AudioSegment
|
| 6 |
+
# import zipfile
|
| 7 |
+
# from io import BytesIO
|
| 8 |
+
# import json
|
| 9 |
+
# import torch
|
| 10 |
+
|
| 11 |
+
# # 初始化模型 (全部使用原始pipeline)
|
| 12 |
+
# @st.cache_resource
|
| 13 |
+
# def load_pipelines():
|
| 14 |
+
# try:
|
| 15 |
+
# # 1. Script generation
|
| 16 |
+
# script_pipe = pipeline(
|
| 17 |
+
# "text2text-generation",
|
| 18 |
+
# model="RUCAIBox/mvp-story",
|
| 19 |
+
# device=0 if torch.cuda.is_available() else -1
|
| 20 |
+
# )
|
| 21 |
+
|
| 22 |
+
# # 2. Storyboard generation (BART model)
|
| 23 |
+
# storyboard_pipe = pipeline(
|
| 24 |
+
# "text2text-generation",
|
| 25 |
+
# model="Jessiesj/script_gpt2-story",
|
| 26 |
+
# device=0 if torch.cuda.is_available() else -1
|
| 27 |
+
# )
|
| 28 |
+
|
| 29 |
+
# # 3. Soundtrack generation (MusicGen)
|
| 30 |
+
# music_pipe = pipeline(
|
| 31 |
+
# "text-to-audio",
|
| 32 |
+
# model="facebook/musicgen-small",
|
| 33 |
+
# device_map="auto"
|
| 34 |
+
# )
|
| 35 |
+
|
| 36 |
+
# # 4. Storyboard image generation (Stable Diffusion)
|
| 37 |
+
# image_pipe = StableDiffusionPipeline.from_pretrained(
|
| 38 |
+
# "prompthero/openjourney-v4",
|
| 39 |
+
# safety_checker=None,
|
| 40 |
+
# torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32
|
| 41 |
+
# )
|
| 42 |
+
|
| 43 |
+
# return script_pipe, storyboard_pipe, image_pipe
|
| 44 |
+
# # return script_pipe, storyboard_pipe, music_pipe, image_pipe
|
| 45 |
+
|
| 46 |
+
# except Exception as e:
|
| 47 |
+
# st.error(f"Failed to load models: {str(e)}")
|
| 48 |
+
# raise
|
| 49 |
+
|
| 50 |
+
# # Prompt template
|
| 51 |
+
# def build_script_prompt(theme, style_or_opening):
|
| 52 |
+
# return f"""
|
| 53 |
+
# {style_or_opening}
|
| 54 |
+
# Generate a micro - movie script with two scenes, with the theme: {theme}.
|
| 55 |
+
# ### Format requirements (must be strictly followed):
|
| 56 |
+
# - Use Markdown syntax
|
| 57 |
+
# - Each scene starts with ### Scene X: [Location] (INT/EXT. Time)
|
| 58 |
+
# - Include action descriptions (wrapped in square brackets []) and character dialogues
|
| 59 |
+
|
| 60 |
+
# ### Example:
|
| 61 |
+
# ### Scene 1: Spaceship cockpit (INT. Emergency)
|
| 62 |
+
# [Alarm lights are flashing wildly, sparks are coming out of the console]
|
| 63 |
+
# Captain (shouting): Start the backup engine!
|
| 64 |
+
# Co - pilot (typing on the keyboard): The power system has failed!
|
| 65 |
+
|
| 66 |
+
# ### Scene 2: Alien jungle (EXT. Dusk)
|
| 67 |
+
# [The expedition team pushes aside the dense purple bushes]
|
| 68 |
+
# Team member A (pointing into the distance): Look! Is that the entrance to the ruins?
|
| 69 |
+
# """
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
# # Content cleaning
|
| 73 |
+
# def format_script(text):
|
| 74 |
+
# # Remove invalid symbols
|
| 75 |
+
# text = re.sub(r"[():<>]{2,}", "", text)
|
| 76 |
+
# # Standardize scene titles
|
| 77 |
+
# return re.sub(r"(?<!### )Scene\d+", r"### Scene\g<0>", text)
|
| 78 |
+
|
| 79 |
+
# # Load pipelines
|
| 80 |
+
# script_pipe, storyboard_pipe, image_pipe = load_pipelines()
|
| 81 |
+
# # script_pipe, storyboard_pipe, music_pipe, image_pipe = load_pipelines()
|
| 82 |
+
|
| 83 |
+
# # Streamlit interface
|
| 84 |
+
# st.title("🎥 Micro - movie Intelligent Creation System")
|
| 85 |
+
|
| 86 |
+
# # User input
|
| 87 |
+
# user_input = st.text_input(
|
| 88 |
+
# "Enter the movie theme (e.g., Sci - fi space adventure)",
|
| 89 |
+
# help="Recommended format: Genre + Core conflict, example: 'Jungle exploration + Search for the lost golden city'"
|
| 90 |
+
# )
|
| 91 |
+
|
| 92 |
+
# style_or_opening = st.text_input(
|
| 93 |
+
# "Enter the script style or opening",
|
| 94 |
+
# help="Provide a style description or the beginning of the script"
|
| 95 |
+
# )
|
| 96 |
+
|
| 97 |
+
# if user_input and style_or_opening:
|
| 98 |
+
|
| 99 |
+
# # 1. Generate the script
|
| 100 |
+
# with st.status("🖋️ Generating the script...", expanded=True) as status:
|
| 101 |
+
# try:
|
| 102 |
+
# # Generate content
|
| 103 |
+
# prompt = build_script_prompt(user_input, style_or_opening)
|
| 104 |
+
# response = script_pipe(
|
| 105 |
+
# prompt,
|
| 106 |
+
# max_length=600,
|
| 107 |
+
# temperature=0.8,
|
| 108 |
+
# num_beams=4,
|
| 109 |
+
# no_repeat_ngram_size=3
|
| 110 |
+
# )[0]["generated_text"]
|
| 111 |
+
|
| 112 |
+
# # Post - processing
|
| 113 |
+
# def format_script(text):
|
| 114 |
+
# return text
|
| 115 |
+
|
| 116 |
+
# # Display the result
|
| 117 |
+
# st.subheader("Generated Script")
|
| 118 |
+
# cleaned = format_script(response) # Ensure cleaned is defined here
|
| 119 |
+
# st.markdown(f"```markdown\n{format_script(response)}\n```")
|
| 120 |
+
# status.update(label="✅ Generation completed", state="complete")
|
| 121 |
+
|
| 122 |
+
# except Exception as e:
|
| 123 |
+
# status.update(label="❌ Generation failed", status="error")
|
| 124 |
+
# st.error(f"Error details: {str(e)}")
|
| 125 |
+
# st.stop()
|
| 126 |
+
|
| 127 |
+
# # 2. Generate the storyboard
|
| 128 |
+
# with st.status("🎥 Converting to storyboard script...", expanded=True) as status:
|
| 129 |
+
# try:
|
| 130 |
+
# # 示例数据模板
|
| 131 |
+
# sample_storyboard = """
|
| 132 |
+
# {
|
| 133 |
+
# "scenes": [
|
| 134 |
+
# {
|
| 135 |
+
# "scene_number": 1,
|
| 136 |
+
# "location": "Abandoned Space Station (INT. Dystopian Future)",
|
| 137 |
+
# "characters": ["Eve", "R2-D9"],
|
| 138 |
+
# "camera_angle": "Wide shot showing decaying corridors",
|
| 139 |
+
# "action": "Eve scans walls with her holographic projector, R2-D9 beeps nervously"
|
| 140 |
+
# },
|
| 141 |
+
# {
|
| 142 |
+
# "scene_number": 2,
|
| 143 |
+
# "location": "Alien Wasteland (EXT. Toxic Sunset)",
|
| 144 |
+
# "characters": ["Eve", "Mutant Leader"],
|
| 145 |
+
# "camera_angle": "Low-angle shot of Eve aiming plasma rifle",
|
| 146 |
+
# "action": "Mutant leader growls, acid rain hisses on Eve's protective suit"
|
| 147 |
+
# }
|
| 148 |
+
# ]
|
| 149 |
+
# }
|
| 150 |
+
# """
|
| 151 |
+
|
| 152 |
+
# # JSON模板引导
|
| 153 |
+
# json_template = """{
|
| 154 |
+
# "scenes": [
|
| 155 |
+
# {
|
| 156 |
+
# "scene_number": 1,
|
| 157 |
+
# "location": "Scene location (INT/EXT)",
|
| 158 |
+
# "characters": ["Character 1", "Character 2"],
|
| 159 |
+
# "camera_angle": "Camera angle",
|
| 160 |
+
# "action": "Main action description"
|
| 161 |
+
# }
|
| 162 |
+
# ]
|
| 163 |
+
# }"""
|
| 164 |
+
|
| 165 |
+
# # 生成分镜
|
| 166 |
+
# # storyboard = storyboard_pipe(
|
| 167 |
+
# # f"Convert to storyboard JSON: {cleaned[:800]}",
|
| 168 |
+
# # max_length=1500,
|
| 169 |
+
# # temperature=0.3
|
| 170 |
+
# # )[0]["generated_text"]
|
| 171 |
+
|
| 172 |
+
# # 增强的格式修复
|
| 173 |
+
# # storyboard = (
|
| 174 |
+
# # storyboard.replace("'", '"')
|
| 175 |
+
# # .replace("None", '""')
|
| 176 |
+
# # .replace("True", "true")
|
| 177 |
+
# # .replace("False", "false")
|
| 178 |
+
# # )
|
| 179 |
+
# # storyboard = re.search(r'\{.*\}', storyboard, re.DOTALL).group()
|
| 180 |
+
# # parsed = json.loads(storyboard)
|
| 181 |
+
# parsed = json.loads(sample_storyboard)
|
| 182 |
+
# except Exception as e:
|
| 183 |
+
# # st.warning(f"Using sample storyboard due to error: {str(e)}")
|
| 184 |
+
# # parsed = json.loads(sample_storyboard) # 回退到示例数据
|
| 185 |
+
# # status.update(label="⚠️ Using sample storyboard", status="warning")
|
| 186 |
+
# pass
|
| 187 |
+
|
| 188 |
+
# finally:
|
| 189 |
+
# # status.update(label="Storyboard ready", status="complete")
|
| 190 |
+
# # st.subheader("Storyboard Script")
|
| 191 |
+
# # st.json(parsed)
|
| 192 |
+
# # st.caption("*Note: If using sample data, edit the JSON before proceeding*")
|
| 193 |
+
# pass
|
| 194 |
+
|
| 195 |
+
|
| 196 |
+
# # 3. 生成配乐
|
| 197 |
+
# # with st.spinner("🎵 正在生成配乐..."):
|
| 198 |
+
# # audio = music_pipe(
|
| 199 |
+
# # "epic sci-fi background music",
|
| 200 |
+
# # max_new_tokens=256
|
| 201 |
+
# # )
|
| 202 |
+
# # audio_buffer = BytesIO()
|
| 203 |
+
# # audio["audio"].export(audio_buffer, format="wav")
|
| 204 |
+
|
| 205 |
+
# # st.subheader("🎧 背景音乐")
|
| 206 |
+
# # st.audio(audio_buffer, format="audio/wav")
|
| 207 |
+
|
| 208 |
+
# # 4. 生成分镜图(可选)
|
| 209 |
+
# # if st.toggle("🎨 生成分镜预览图(需要2-3分钟)"):
|
| 210 |
+
# # with st.spinner("🖼️ 正在渲染电影级画面..."):
|
| 211 |
+
# # # 从剧本提取关键描述
|
| 212 |
+
# # scene_desc = re.search(r"场景\d+:(.*?)\n", script)
|
| 213 |
+
# # if scene_desc:
|
| 214 |
+
# # scene_desc = scene_desc.group(1)
|
| 215 |
+
# # else:
|
| 216 |
+
# # scene_desc = f"movie scene of {user_input}"
|
| 217 |
+
|
| 218 |
+
# # # 精细化图片提示词
|
| 219 |
+
# # image_prompt = f"""电影剧照,{scene_desc},
|
| 220 |
+
# # 超高清8K分辨率,
|
| 221 |
+
# # 电影级打光,浅景深效果,
|
| 222 |
+
# # Unreal Engine 5渲染,
|
| 223 |
+
# # 电影胶片颗粒感"""
|
| 224 |
+
|
| 225 |
+
# # # 优化生成参数
|
| 226 |
+
# # image = image_pipe(
|
| 227 |
+
# # image_prompt,
|
| 228 |
+
# # num_inference_steps=50,
|
| 229 |
+
# # guidance_scale=9.5,
|
| 230 |
+
# # width=1024,
|
| 231 |
+
# # height=576 # 16:9电影比例
|
| 232 |
+
# # ).images[0]
|
| 233 |
+
|
| 234 |
+
# # st.subheader("🎞️ 首场景预览")
|
| 235 |
+
# # st.image(image, caption=scene_desc)
|
| 236 |
+
|
| 237 |
+
# # except Exception as e:
|
| 238 |
+
# # st.error("生成过程中发生错误")
|
| 239 |
+
# # st.code(f"错误详情:{str(e)}")
|
| 240 |
+
# # st.button("点击查看技术详情",
|
| 241 |
+
# # help="将以下信息提供给技术人员:\n" + str(e))
|
| 242 |
+
|
| 243 |
+
# st.subheader("🎧 Background Music")
|
| 244 |
+
# for i, scene in enumerate(parsed["scenes"]):
|
| 245 |
+
# action = scene["action"]
|
| 246 |
+
# with st.spinner(f"🎵 Generating soundtrack for scene {i + 1}..."):
|
| 247 |
+
# try:
|
| 248 |
+
# audio = music_pipe(
|
| 249 |
+
# action,
|
| 250 |
+
# max_new_tokens=256
|
| 251 |
+
# )
|
| 252 |
+
# audio_buffer = BytesIO()
|
| 253 |
+
# audio["audio"].export(audio_buffer, format="wav")
|
| 254 |
+
# st.audio(audio_buffer, format="audio/wav", caption=f"Soundtrack for scene {i + 1}")
|
| 255 |
+
# except Exception as e:
|
| 256 |
+
# st.error(f"Failed to generate soundtrack for scene {i + 1}: {str(e)}")
|
| 257 |
+
|
| 258 |
+
# st.subheader("🎨 Storyboard Images")
|
| 259 |
+
# for i, scene in enumerate(parsed["scenes"]):
|
| 260 |
+
# location = scene["location"]
|
| 261 |
+
# if st.toggle(f"Generate preview for scene {i + 1} (takes 2 - 3 minutes)"):
|
| 262 |
+
# with st.spinner(f"🖼️ Rendering image for scene {i + 1}..."):
|
| 263 |
+
# try:
|
| 264 |
+
# # 精细化图片提示词
|
| 265 |
+
# image_prompt = f"""Movie still, {location},
|
| 266 |
+
# Ultra - high - definition 8K resolution,
|
| 267 |
+
# Movie - level lighting, shallow depth - of - field effect,
|
| 268 |
+
# Unreal Engine 5 rendering,
|
| 269 |
+
# Movie film graininess"""
|
| 270 |
+
|
| 271 |
+
# # 优化生成参数
|
| 272 |
+
# image = image_pipe(
|
| 273 |
+
# image_prompt,
|
| 274 |
+
# num_inference_steps=50,
|
| 275 |
+
# guidance_scale=9.5,
|
| 276 |
+
# width=1024,
|
| 277 |
+
# height=576 # 16:9 movie ratio
|
| 278 |
+
# ).images[0]
|
| 279 |
+
|
| 280 |
+
# st.image(image, caption=f"Preview for scene {i + 1}")
|
| 281 |
+
# except Exception as e:
|
| 282 |
+
# st.error(f"Failed to generate image for scene {i + 1}: {str(e)}")
|
| 283 |
+
|
| 284 |
import streamlit as st
|
| 285 |
import re
|
| 286 |
+
from transformers import pipeline, AutoModelForTextToWaveform, AutoTokenizer
|
| 287 |
from diffusers import StableDiffusionPipeline
|
| 288 |
from pydub import AudioSegment
|
| 289 |
import zipfile
|
| 290 |
from io import BytesIO
|
| 291 |
import json
|
| 292 |
import torch
|
| 293 |
+
import torchaudio
|
| 294 |
|
| 295 |
# 初始化模型 (全部使用原始pipeline)
|
| 296 |
@st.cache_resource
|
| 297 |
def load_pipelines():
|
| 298 |
try:
|
| 299 |
+
device = "cuda:0" if torch.cuda.is_available() else "cpu"
|
| 300 |
+
torch_dtype = torch.float16 if device == "cuda:0" else torch.float32
|
| 301 |
+
|
| 302 |
# 1. Script generation
|
| 303 |
script_pipe = pipeline(
|
| 304 |
"text2text-generation",
|
| 305 |
model="RUCAIBox/mvp-story",
|
| 306 |
+
device=device
|
| 307 |
)
|
| 308 |
|
| 309 |
# 2. Storyboard generation (BART model)
|
| 310 |
storyboard_pipe = pipeline(
|
| 311 |
"text2text-generation",
|
| 312 |
model="Jessiesj/script_gpt2-story",
|
| 313 |
+
device=device
|
| 314 |
)
|
| 315 |
|
| 316 |
# 3. Soundtrack generation (MusicGen)
|
| 317 |
+
music_model = AutoModelForTextToWaveform.from_pretrained(
|
| 318 |
+
"facebook/musicgen-small",
|
| 319 |
+
torch_dtype=torch_dtype
|
| 320 |
+
).to(device)
|
| 321 |
+
music_tokenizer = AutoTokenizer.from_pretrained("facebook/musicgen-small")
|
| 322 |
music_pipe = pipeline(
|
| 323 |
+
"text-to-audio",
|
| 324 |
+
model=music_model,
|
| 325 |
+
tokenizer=music_tokenizer,
|
| 326 |
+
device=device
|
| 327 |
)
|
| 328 |
|
| 329 |
# 4. Storyboard image generation (Stable Diffusion)
|
| 330 |
image_pipe = StableDiffusionPipeline.from_pretrained(
|
| 331 |
"prompthero/openjourney-v4",
|
| 332 |
safety_checker=None,
|
| 333 |
+
torch_dtype=torch_dtype
|
| 334 |
+
).to(device)
|
| 335 |
|
| 336 |
+
return script_pipe, storyboard_pipe, music_pipe, image_pipe
|
|
|
|
| 337 |
|
| 338 |
except Exception as e:
|
| 339 |
st.error(f"Failed to load models: {str(e)}")
|
|
|
|
| 369 |
return re.sub(r"(?<!### )Scene\d+", r"### Scene\g<0>", text)
|
| 370 |
|
| 371 |
# Load pipelines
|
| 372 |
+
script_pipe, storyboard_pipe, music_pipe, image_pipe = load_pipelines()
|
|
|
|
| 373 |
|
| 374 |
# Streamlit interface
|
| 375 |
st.title("🎥 Micro - movie Intelligent Creation System")
|
|
|
|
| 408 |
st.subheader("Generated Script")
|
| 409 |
cleaned = format_script(response) # Ensure cleaned is defined here
|
| 410 |
st.markdown(f"```markdown\n{format_script(response)}\n```")
|
| 411 |
+
status.update(label="✅ Generation completed")
|
| 412 |
|
| 413 |
except Exception as e:
|
| 414 |
+
status.update(label="❌ Generation failed")
|
| 415 |
st.error(f"Error details: {str(e)}")
|
| 416 |
st.stop()
|
| 417 |
|
|
|
|
| 454 |
}"""
|
| 455 |
|
| 456 |
# 生成分镜
|
| 457 |
+
storyboard = storyboard_pipe(
|
| 458 |
+
f"Convert to storyboard JSON: {cleaned[:800]}",
|
| 459 |
+
max_length=1500,
|
| 460 |
+
temperature=0.3
|
| 461 |
+
)[0]["generated_text"]
|
| 462 |
|
| 463 |
# 增强的格式修复
|
| 464 |
+
storyboard = (
|
| 465 |
+
storyboard.replace("'", '"')
|
| 466 |
+
.replace("None", '""')
|
| 467 |
+
.replace("True", "true")
|
| 468 |
+
.replace("False", "false")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 469 |
)
|
| 470 |
+
storyboard = re.search(r'\{.*\}', storyboard, re.DOTALL).group()
|
| 471 |
+
parsed = json.loads(storyboard)
|
| 472 |
+
|
| 473 |
except Exception as e:
|
| 474 |
+
st.warning(f"Using sample storyboard due to error: {str(e)}")
|
| 475 |
+
parsed = json.loads(sample_storyboard)
|
| 476 |
+
status.update(label="⚠️ Using sample storyboard")
|
| 477 |
+
|
| 478 |
+
status.update(label="Storyboard ready")
|
| 479 |
+
st.subheader("Storyboard Script")
|
| 480 |
+
st.json(parsed)
|
| 481 |
+
st.caption("*Note: If using sample data, edit the JSON before proceeding*")
|
| 482 |
|
| 483 |
+
st.subheader("🎧 Background Music")
|
| 484 |
+
for i, scene in enumerate(parsed["scenes"]):
|
| 485 |
+
action = scene["action"]
|
| 486 |
+
with st.spinner(f"🎵 Generating soundtrack for scene {i + 1}..."):
|
|
|
|
| 487 |
try:
|
| 488 |
+
audio = music_pipe(
|
| 489 |
+
action,
|
| 490 |
+
max_new_tokens=256
|
| 491 |
+
)
|
| 492 |
+
audio_buffer = BytesIO()
|
| 493 |
+
audio["audio"].export(audio_buffer, format="wav")
|
| 494 |
+
st.audio(audio_buffer, format="audio/wav", caption=f"Soundtrack for scene {i + 1}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 495 |
except Exception as e:
|
| 496 |
+
st.error(f"Failed to generate soundtrack for scene {i + 1}: {str(e)}")
|
| 497 |
+
|
| 498 |
+
st.subheader("🎨 Storyboard Images")
|
| 499 |
+
for i, scene in enumerate(parsed["scenes"]):
|
| 500 |
+
location = scene["location"]
|
| 501 |
+
if st.toggle(f"Generate preview for scene {i + 1} (takes 2 - 3 minutes)"):
|
| 502 |
+
with st.spinner(f"🖼️ Rendering image for scene {i + 1}..."):
|
| 503 |
+
try:
|
| 504 |
+
# 精细化图片提示词
|
| 505 |
+
image_prompt = f"""Movie still, {location},
|
| 506 |
+
Ultra - high - definition 8K resolution,
|
| 507 |
+
Movie - level lighting, shallow depth - of - field effect,
|
| 508 |
+
Unreal Engine 5 rendering,
|
| 509 |
+
Movie film graininess"""
|
| 510 |
+
|
| 511 |
+
# 优化生成参数
|
| 512 |
+
image = image_pipe(
|
| 513 |
+
image_prompt,
|
| 514 |
+
num_inference_steps=50,
|
| 515 |
+
guidance_scale=9.5,
|
| 516 |
+
width=1024,
|
| 517 |
+
height=576 # 16:9 movie ratio
|
| 518 |
+
).images[0]
|
| 519 |
+
|
| 520 |
+
st.image(image, caption=f"Preview for scene {i + 1}")
|
| 521 |
+
except Exception as e:
|
| 522 |
+
st.error(f"Failed to generate image for scene {i + 1}: {str(e)}")
|
| 523 |
+
|