Update app.py
Browse files
app.py
CHANGED
|
@@ -12,74 +12,95 @@ import torch
|
|
| 12 |
@st.cache_resource
|
| 13 |
def load_pipelines():
|
| 14 |
try:
|
| 15 |
-
# 1.
|
| 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.
|
| 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.
|
| 30 |
# # music_pipe = pipeline(
|
| 31 |
# # "text-to-audio",
|
| 32 |
# # model="facebook/musicgen-small",
|
| 33 |
# # device_map="auto"
|
| 34 |
# # )
|
| 35 |
-
|
| 36 |
-
# 4.
|
| 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"
|
| 48 |
raise
|
| 49 |
|
| 50 |
-
#
|
| 51 |
-
def build_script_prompt(theme):
|
| 52 |
-
return f"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
|
| 55 |
-
|
|
|
|
| 56 |
def format_script(text):
|
| 57 |
-
#
|
| 58 |
text = re.sub(r"[():<>]{2,}", "", text)
|
| 59 |
-
#
|
| 60 |
-
return re.sub(r"(?<!### )
|
| 61 |
|
| 62 |
-
#
|
| 63 |
script_pipe, storyboard_pipe, image_pipe = load_pipelines()
|
| 64 |
# script_pipe, storyboard_pipe, music_pipe, image_pipe = load_pipelines()
|
| 65 |
|
| 66 |
-
# Streamlit
|
| 67 |
st.title("🎥 Micro - movie Intelligent Creation System")
|
| 68 |
-
|
| 69 |
-
#
|
| 70 |
user_input = st.text_input(
|
| 71 |
-
"Enter the movie theme
|
| 72 |
help="Recommended format: Genre + Core conflict, example: 'Jungle exploration + Search for the lost golden city'"
|
| 73 |
)
|
| 74 |
|
| 75 |
-
|
| 76 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
|
| 78 |
-
# 1.
|
| 79 |
with st.status("🖋️ Generating the script...", expanded=True) as status:
|
| 80 |
try:
|
| 81 |
-
#
|
| 82 |
-
prompt = build_script_prompt(user_input)
|
| 83 |
response = script_pipe(
|
| 84 |
prompt,
|
| 85 |
max_length=600,
|
|
@@ -87,34 +108,33 @@ if user_input:
|
|
| 87 |
num_beams=4,
|
| 88 |
no_repeat_ngram_size=3
|
| 89 |
)[0]["generated_text"]
|
| 90 |
-
|
| 91 |
-
#
|
| 92 |
def format_script(text):
|
| 93 |
-
# #
|
| 94 |
-
# text = re.sub(r"
|
| 95 |
-
# #
|
| 96 |
# text = re.sub(r"[<>()]{2,}|\[?\]?", "", text)
|
| 97 |
-
# #
|
| 98 |
# return re.sub(r"(?<=\n)\s*([^\[\n]+?)\s*(?=\n)", r"[\1]", text)
|
| 99 |
-
return
|
| 100 |
-
|
| 101 |
-
#
|
| 102 |
st.subheader("Generated Script")
|
| 103 |
cleaned = format_script(response) # Ensure cleaned is defined here
|
| 104 |
st.markdown(f"```markdown\n{format_script(response)}\n```")
|
| 105 |
status.update(label="✅ Generation completed", state="complete")
|
| 106 |
-
|
| 107 |
except Exception as e:
|
| 108 |
status.update(label="❌ Generation failed", state="error")
|
| 109 |
st.error(f"Error details: {str(e)}")
|
| 110 |
st.stop()
|
| 111 |
|
| 112 |
-
|
| 113 |
-
#2. 生成分镜
|
| 114 |
with st.status("🎥 Converting to a storyboard script...", expanded=True) as status:
|
| 115 |
try:
|
| 116 |
-
# JSON
|
| 117 |
-
|
| 118 |
"scenes": [
|
| 119 |
{
|
| 120 |
"scene_number": 1,
|
|
@@ -125,7 +145,7 @@ if user_input:
|
|
| 125 |
}
|
| 126 |
]
|
| 127 |
}"""
|
| 128 |
-
|
| 129 |
# Generate the storyboard
|
| 130 |
storyboard = storyboard_pipe(
|
| 131 |
f"Convert the following script into a storyboard JSON, strictly following the template:\n{json_template}\nScript content: {cleaned[:800]}", # Key modification point
|
|
|
|
| 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,
|
|
|
|
| 108 |
num_beams=4,
|
| 109 |
no_repeat_ngram_size=3
|
| 110 |
)[0]["generated_text"]
|
| 111 |
+
|
| 112 |
+
# Post - processing
|
| 113 |
def format_script(text):
|
| 114 |
+
# # Force - correct the scene title format (e.g., "Scene 1" -> "### Scene 1")
|
| 115 |
+
# text = re.sub(r"Scene(\d+)", r"### Scene\1", text)
|
| 116 |
+
# # Remove extra symbols
|
| 117 |
# text = re.sub(r"[<>()]{2,}|\[?\]?", "", text)
|
| 118 |
+
# # Ensure action descriptions are wrapped in square brackets
|
| 119 |
# return re.sub(r"(?<=\n)\s*([^\[\n]+?)\s*(?=\n)", r"[\1]", text)
|
| 120 |
+
return text
|
| 121 |
+
|
| 122 |
+
# Display the result
|
| 123 |
st.subheader("Generated Script")
|
| 124 |
cleaned = format_script(response) # Ensure cleaned is defined here
|
| 125 |
st.markdown(f"```markdown\n{format_script(response)}\n```")
|
| 126 |
status.update(label="✅ Generation completed", state="complete")
|
| 127 |
+
|
| 128 |
except Exception as e:
|
| 129 |
status.update(label="❌ Generation failed", state="error")
|
| 130 |
st.error(f"Error details: {str(e)}")
|
| 131 |
st.stop()
|
| 132 |
|
| 133 |
+
# 2. Generate the storyboard
|
|
|
|
| 134 |
with st.status("🎥 Converting to a storyboard script...", expanded=True) as status:
|
| 135 |
try:
|
| 136 |
+
# JSON template guidance
|
| 137 |
+
json_template = """{
|
| 138 |
"scenes": [
|
| 139 |
{
|
| 140 |
"scene_number": 1,
|
|
|
|
| 145 |
}
|
| 146 |
]
|
| 147 |
}"""
|
| 148 |
+
|
| 149 |
# Generate the storyboard
|
| 150 |
storyboard = storyboard_pipe(
|
| 151 |
f"Convert the following script into a storyboard JSON, strictly following the template:\n{json_template}\nScript content: {cleaned[:800]}", # Key modification point
|