Update app.py
Browse files
app.py
CHANGED
@@ -1,47 +1,73 @@
|
|
1 |
import streamlit as st
|
2 |
from transformers import pipeline
|
3 |
from PIL import Image
|
4 |
-
import numpy as np
|
5 |
-
import tempfile
|
6 |
import os
|
|
|
|
|
7 |
from modelscope.pipelines import pipeline as modelscope_pipeline
|
8 |
from modelscope.outputs import OutputKeys
|
|
|
9 |
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
try:
|
15 |
if progress_bar:
|
16 |
progress_bar.progress(0.1, "Generating image caption...")
|
17 |
-
|
18 |
-
# Setup image captioning
|
19 |
-
caption_pipe = pipeline("image-to-text", model="Salesforce/blip-image-captioning-base")
|
20 |
|
21 |
# Generate caption
|
22 |
-
caption =
|
23 |
st.write(f"Generated caption: *{caption}*")
|
24 |
|
25 |
if progress_bar:
|
26 |
-
progress_bar.progress(0.3, "
|
27 |
-
|
28 |
-
#
|
29 |
-
|
30 |
-
'text-to-video-synthesis',
|
31 |
-
model='damo/text-to-video-synthesis'
|
32 |
-
)
|
33 |
|
34 |
-
if progress_bar:
|
35 |
-
progress_bar.progress(0.5, "Generating video...")
|
36 |
-
|
37 |
# Generate video
|
38 |
-
output =
|
39 |
video_path = output[OutputKeys.OUTPUT_VIDEO]
|
40 |
|
|
|
|
|
|
|
41 |
if progress_bar:
|
42 |
progress_bar.progress(1.0, "Video generation complete!")
|
43 |
-
|
44 |
-
return
|
45 |
|
46 |
except Exception as e:
|
47 |
st.error(f"Error generating video: {str(e)}")
|
@@ -50,13 +76,26 @@ def generate_video_from_image(image, duration_seconds=10, progress_bar=None):
|
|
50 |
def main():
|
51 |
st.set_page_config(page_title="AI Video Generator", page_icon="🎥")
|
52 |
|
53 |
-
st.title("🎥
|
54 |
st.write("""
|
55 |
-
Upload an image to generate a video
|
56 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
57 |
""")
|
58 |
|
59 |
-
|
|
|
|
|
60 |
|
61 |
# File uploader
|
62 |
uploaded_file = st.file_uploader("Choose an image", type=['png', 'jpg', 'jpeg'])
|
@@ -81,23 +120,47 @@ def main():
|
|
81 |
with open(video_path, 'rb') as video_file:
|
82 |
video_bytes = video_file.read()
|
83 |
|
84 |
-
# Create download
|
85 |
-
st.
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
91 |
|
92 |
# Display video
|
93 |
st.video(video_bytes)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
94 |
else:
|
95 |
st.error("Failed to generate video. Please try again.")
|
96 |
|
97 |
except Exception as e:
|
98 |
st.error(f"An error occurred: {str(e)}")
|
99 |
-
st.error("Full error message for debugging:")
|
100 |
-
st.error(e)
|
101 |
|
102 |
if __name__ == "__main__":
|
103 |
main()
|
|
|
1 |
import streamlit as st
|
2 |
from transformers import pipeline
|
3 |
from PIL import Image
|
|
|
|
|
4 |
import os
|
5 |
+
import pathlib
|
6 |
+
from huggingface_hub import snapshot_download
|
7 |
from modelscope.pipelines import pipeline as modelscope_pipeline
|
8 |
from modelscope.outputs import OutputKeys
|
9 |
+
import shutil
|
10 |
|
11 |
+
# Create a downloads directory if it doesn't exist
|
12 |
+
if not os.path.exists('downloads'):
|
13 |
+
os.makedirs('downloads')
|
14 |
+
|
15 |
+
def initialize_models():
|
16 |
+
"""Initialize and cache the models to avoid reloading."""
|
17 |
+
if 'caption_pipeline' not in st.session_state:
|
18 |
+
st.session_state.caption_pipeline = pipeline("image-to-text", model="Salesforce/blip-image-captioning-base")
|
19 |
+
|
20 |
+
if 'video_pipeline' not in st.session_state:
|
21 |
+
# Download and cache the model
|
22 |
+
model_dir = pathlib.Path('weights')
|
23 |
+
snapshot_download(
|
24 |
+
'damo-vilab/modelscope-damo-text-to-video-synthesis',
|
25 |
+
repo_type='model',
|
26 |
+
local_dir=model_dir
|
27 |
+
)
|
28 |
+
st.session_state.video_pipeline = modelscope_pipeline(
|
29 |
+
'text-to-video-synthesis',
|
30 |
+
model_dir.as_posix()
|
31 |
+
)
|
32 |
+
|
33 |
+
def save_video(video_path, caption):
|
34 |
+
"""Save video to downloads directory with a meaningful name."""
|
35 |
+
# Create a filename from the caption
|
36 |
+
safe_caption = "".join(x for x in caption[:30] if x.isalnum() or x in (' ','-','_')).strip()
|
37 |
+
save_name = f"video_{safe_caption}.mp4"
|
38 |
+
save_path = os.path.join('downloads', save_name)
|
39 |
+
|
40 |
+
# Copy the video file
|
41 |
+
shutil.copy2(video_path, save_path)
|
42 |
+
return save_path
|
43 |
+
|
44 |
+
def generate_video_from_image(image, progress_bar=None):
|
45 |
+
"""Generate a video based on image caption using ModelScope's text-to-video model."""
|
46 |
try:
|
47 |
if progress_bar:
|
48 |
progress_bar.progress(0.1, "Generating image caption...")
|
|
|
|
|
|
|
49 |
|
50 |
# Generate caption
|
51 |
+
caption = st.session_state.caption_pipeline(image)[0]['generated_text']
|
52 |
st.write(f"Generated caption: *{caption}*")
|
53 |
|
54 |
if progress_bar:
|
55 |
+
progress_bar.progress(0.3, "Generating video...")
|
56 |
+
|
57 |
+
# Prepare input for video generation
|
58 |
+
input_text = {'text': caption}
|
|
|
|
|
|
|
59 |
|
|
|
|
|
|
|
60 |
# Generate video
|
61 |
+
output = st.session_state.video_pipeline(input_text)
|
62 |
video_path = output[OutputKeys.OUTPUT_VIDEO]
|
63 |
|
64 |
+
# Save video with meaningful name
|
65 |
+
final_path = save_video(video_path, caption)
|
66 |
+
|
67 |
if progress_bar:
|
68 |
progress_bar.progress(1.0, "Video generation complete!")
|
69 |
+
|
70 |
+
return final_path, caption
|
71 |
|
72 |
except Exception as e:
|
73 |
st.error(f"Error generating video: {str(e)}")
|
|
|
76 |
def main():
|
77 |
st.set_page_config(page_title="AI Video Generator", page_icon="🎥")
|
78 |
|
79 |
+
st.title("🎥 Text-to-Video Generator")
|
80 |
st.write("""
|
81 |
+
Upload an image to generate a video based on its content. The app will:
|
82 |
+
1. Generate a caption for your image
|
83 |
+
2. Create a video based on that caption
|
84 |
+
3. Provide options to view and download the video
|
85 |
+
""")
|
86 |
+
|
87 |
+
# Display model limitations
|
88 |
+
st.warning("""
|
89 |
+
Model Limitations:
|
90 |
+
- Only English text is supported
|
91 |
+
- Cannot generate clear text in videos
|
92 |
+
- May have limitations with complex scenes
|
93 |
+
- Generation takes several minutes
|
94 |
""")
|
95 |
|
96 |
+
# Initialize models
|
97 |
+
with st.spinner("Loading models... This may take a minute..."):
|
98 |
+
initialize_models()
|
99 |
|
100 |
# File uploader
|
101 |
uploaded_file = st.file_uploader("Choose an image", type=['png', 'jpg', 'jpeg'])
|
|
|
120 |
with open(video_path, 'rb') as video_file:
|
121 |
video_bytes = video_file.read()
|
122 |
|
123 |
+
# Create a container for the video and download options
|
124 |
+
st.success("Video generated successfully!")
|
125 |
+
|
126 |
+
col1, col2 = st.columns(2)
|
127 |
+
|
128 |
+
with col1:
|
129 |
+
# Primary download button
|
130 |
+
st.download_button(
|
131 |
+
label="💾 Download Video",
|
132 |
+
data=video_bytes,
|
133 |
+
file_name=os.path.basename(video_path),
|
134 |
+
mime="video/mp4",
|
135 |
+
key="download1"
|
136 |
+
)
|
137 |
+
|
138 |
+
with col2:
|
139 |
+
# Additional download button with caption
|
140 |
+
st.download_button(
|
141 |
+
label="📥 Download with Caption",
|
142 |
+
data=video_bytes,
|
143 |
+
file_name=f"{caption[:30]}.mp4",
|
144 |
+
mime="video/mp4",
|
145 |
+
key="download2"
|
146 |
+
)
|
147 |
|
148 |
# Display video
|
149 |
st.video(video_bytes)
|
150 |
+
|
151 |
+
# Display additional information
|
152 |
+
st.info(f"""
|
153 |
+
Video Details:
|
154 |
+
- Caption: {caption}
|
155 |
+
- Filename: {os.path.basename(video_path)}
|
156 |
+
- Size: {len(video_bytes)/1024/1024:.1f} MB
|
157 |
+
""")
|
158 |
+
|
159 |
else:
|
160 |
st.error("Failed to generate video. Please try again.")
|
161 |
|
162 |
except Exception as e:
|
163 |
st.error(f"An error occurred: {str(e)}")
|
|
|
|
|
164 |
|
165 |
if __name__ == "__main__":
|
166 |
main()
|