Jessiesj commited on
Commit
6b20e72
·
verified ·
1 Parent(s): 359038c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +71 -104
app.py CHANGED
@@ -1,111 +1,78 @@
1
  import streamlit as st
2
- from transformers import pipeline, AutoTokenizer, AutoModelForSeq2SeqLM
3
- from PIL import Image
4
- import json
5
- import io
6
- from zipfile import ZipFile
7
- from musicgen.model import MusicGen
8
-
9
- # 模型加载(使用pipeline)
10
- script_generator = pipeline(
11
- "text2text-generation",
12
- model="mrm8488/t5-base-finetuned-common-gen",
13
- tokenizer="t5-base"
14
- )
15
-
16
- shot_generator = pipeline(
17
- "text2text-generation",
18
- model="philschmid/bart-large-cnn-samsum",
19
- tokenizer="facebook/bart-large"
20
- )
21
-
22
- # 音乐生成器(需要单独处理)
23
- musicgen = MusicGen.get_pretrained("facebook/musicgen-small")
24
-
25
- # 图片生成器(需要diffusers)
26
  from diffusers import StableDiffusionPipeline
27
- image_generator = StableDiffusionPipeline.from_pretrained(
28
- "prompthero/openjourney-v4",
29
- torch_dtype=torch.float16
30
- ).to("cuda") # 或"cpu"
31
-
32
-
33
- def generate_script(topic):
34
- """生成剧本"""
35
- input_text = f"generate script: {topic}"
36
- output = script_generator(input_text, max_length=200)[0]['generated_text']
37
- return output
 
 
 
 
 
 
 
 
 
 
 
 
38
 
 
 
39
 
40
- def generate_shots(script):
41
- """生成分镜"""
42
- input_text = f"summarize shot: {script}"
43
- output = shot_generator(input_text, max_length=200)[0]['generated_text']
 
 
 
 
 
 
 
 
 
 
44
 
45
- # 简单解析JSON(实际需更健壮的解析)
 
 
 
 
 
 
 
46
  try:
47
- return json.loads(output.replace("```json", "").replace("```", ""))
48
  except:
49
- return {"镜头1": {"类型": "中景", "角度": "水平视角", "时长": "5秒"}}
50
-
51
-
52
- def generate_music(shot_description):
53
- """生成音乐(简化版)"""
54
- descriptions = [f"background music for {shot_description} scene"]
55
- wav = musicgen.generate(descriptions, duration=10) # 10秒示例
56
- return wav[0]
57
-
58
-
59
- def generate_image(prompt):
60
- """生成图片"""
61
- image = image_generator(prompt).images[0]
62
- return image
63
-
64
-
65
- # Streamlit界面
66
- st.title("电影分镜生成器")
67
-
68
- # 输入区
69
- user_topic = st.text_input("请输入场景主题(例如:咖啡馆相遇场景)")
70
-
71
- if st.button("生成内容"):
72
- if not user_topic:
73
- st.warning("请输入场景主题!")
74
- else:
75
- with st.spinner("生成中..."):
76
- # 并行生成(简化为顺序执行)
77
- script = generate_script(user_topic)
78
- shots = generate_shots(script)
79
-
80
- # 生成示例图片和音乐
81
- sample_image = generate_image(f"电影分镜:{user_topic}")
82
- sample_audio = generate_music(user_topic)
83
-
84
- # 结果展示
85
- st.header("生成剧本")
86
- st.markdown(script)
87
-
88
- st.header("分镜脚本")
89
- st.json(shots)
90
-
91
- st.header("配乐预览")
92
- audio_bytes = io.BytesIO()
93
- sample_audio.save(audio_bytes, format="WAV")
94
- st.audio(audio_bytes)
95
-
96
- st.header("分镜图片预览")
97
- st.image(sample_image, caption="示例分镜画面")
98
-
99
- # 下载功能
100
- with ZipFile("output.zip", "w") as zipf:
101
- zipf.writestr("script.md", script)
102
- zipf.writestr("shots.json", json.dumps(shots))
103
- sample_image.save(io.BytesIO(), format="PNG")
104
-
105
- with open("output.zip", "rb") as f:
106
- st.download_button(
107
- label="下载所有文件",
108
- data=f,
109
- file_name="movie_scenes.zip",
110
- mime="application/zip"
111
- )
 
1
  import streamlit as st
2
+ from transformers import pipeline
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3
  from diffusers import StableDiffusionPipeline
4
+ import torch
5
+ from pydub import AudioSegment
6
+ import base64
7
+ import json
8
+ import zipfile
9
+ from io import BytesIO
10
+
11
+ # 初始化模型Pipeline(全部使用Hugging Face预训练模型)
12
+ @st.cache_resource
13
+ def load_models():
14
+ # 剧本生成(T5模型)
15
+ script_pipe = pipeline("text2text-generation", model="mrm8488/t5-base-finetuned-common-gen")
16
+
17
+ # 分镜生成(BART模型)
18
+ storyboard_pipe = pipeline("text-generation", model="philschmid/bart-large-cnn-samsum")
19
+
20
+ # 配乐生成(MusicGen)
21
+ music_pipe = pipeline("text-to-audio", model="facebook/musicgen-small")
22
+
23
+ # 分镜图片生成(Stable Diffusion)
24
+ image_pipe = StableDiffusionPipeline.from_pretrained("prompthero/openjourney-v4")
25
+
26
+ return script_pipe, storyboard_pipe, music_pipe, image_pipe
27
 
28
+ # 加载模型
29
+ script_pipe, storyboard_pipe, music_pipe, image_pipe = load_models()
30
 
31
+ # Streamlit界面
32
+ st.title("🎬 微电影创作助手(基础版)")
33
+ user_input = st.text_input("输入电影主题或关键词(例如:浪漫的咖啡馆相遇)")
34
+
35
+ if user_input:
36
+ # 生成剧本
37
+ with st.spinner("正在生成剧本..."):
38
+ script = script_pipe(
39
+ f"Generate a movie script about: {user_input}",
40
+ max_length=300
41
+ )[0]["generated_text"]
42
+
43
+ st.subheader("📜 生成的剧本")
44
+ st.markdown(f"```\n{script}\n```")
45
 
46
+ # 生成分镜
47
+ with st.spinner("正在生成分镜脚本..."):
48
+ storyboard = storyboard_pipe(
49
+ f"Convert this script to storyboard JSON: {script}",
50
+ max_length=500
51
+ )[0]["generated_text"]
52
+
53
+ st.subheader("🎥 分镜脚本")
54
  try:
55
+ st.json(json.loads(storyboard))
56
  except:
57
+ st.error("生成格式错误,请重试!")
58
+
59
+ # 生成配乐
60
+ with st.spinner("正在生成背景音乐..."):
61
+ audio = music_pipe("Calm background music for a romantic scene", max_new_tokens=200)
62
+ audio_buffer = BytesIO()
63
+ audio["audio"].export(audio_buffer, format="wav")
64
+ st.audio(audio_buffer, format="audio/wav")
65
+
66
+ # 生成分镜图片(可选)
67
+ if st.checkbox("生成分镜预览图"):
68
+ with st.spinner("正在生成图片..."):
69
+ image = image_pipe("cinematic scene, 4k film still, " + user_input).images[0]
70
+ st.image(image)
71
+
72
+ # 打包下载功能
73
+ zip_buffer = BytesIO()
74
+ with zipfile.ZipFile(zip_buffer, "w") as zf:
75
+ zf.writestr("script.txt", script)
76
+ zf.writestr("storyboard.json", storyboard)
77
+ zf.writestr("bgm.wav", audio_buffer.getvalue())
78
+ st.download_button("📥 下载完整包", data=zip_buffer.getvalue(), file_name="film_package.zip")