|
import streamlit as st |
|
from transformers import pipeline |
|
from PIL import Image |
|
import numpy as np |
|
import tempfile |
|
import os |
|
from modelscope.pipelines import pipeline as modelscope_pipeline |
|
from modelscope.outputs import OutputKeys |
|
|
|
def generate_video_from_image(image, duration_seconds=10, progress_bar=None): |
|
""" |
|
Generate a video from an image using ModelScope's video generation. |
|
""" |
|
try: |
|
if progress_bar: |
|
progress_bar.progress(0.1, "Generating image caption...") |
|
|
|
|
|
caption_pipe = pipeline("image-to-text", model="Salesforce/blip-image-captioning-base") |
|
|
|
|
|
caption = caption_pipe(image)[0]['generated_text'] |
|
st.write(f"Generated caption: *{caption}*") |
|
|
|
if progress_bar: |
|
progress_bar.progress(0.3, "Loading Video Generation model...") |
|
|
|
|
|
video_pipe = modelscope_pipeline( |
|
'text-to-video-synthesis', |
|
model='damo/text-to-video-synthesis' |
|
) |
|
|
|
if progress_bar: |
|
progress_bar.progress(0.5, "Generating video...") |
|
|
|
|
|
output = video_pipe(caption) |
|
video_path = output[OutputKeys.OUTPUT_VIDEO] |
|
|
|
if progress_bar: |
|
progress_bar.progress(1.0, "Video generation complete!") |
|
|
|
return video_path, caption |
|
|
|
except Exception as e: |
|
st.error(f"Error generating video: {str(e)}") |
|
raise |
|
|
|
def main(): |
|
st.set_page_config(page_title="AI Video Generator", page_icon="🎥") |
|
|
|
st.title("🎥 AI Video Generator") |
|
st.write(""" |
|
Upload an image to generate a video with AI-powered motion and transitions. |
|
The app will automatically generate a caption for your image and use it as inspiration for the video. |
|
""") |
|
|
|
st.info("Note: Video generation may take several minutes.") |
|
|
|
|
|
uploaded_file = st.file_uploader("Choose an image", type=['png', 'jpg', 'jpeg']) |
|
|
|
if uploaded_file is not None: |
|
|
|
image = Image.open(uploaded_file) |
|
st.image(image, caption="Uploaded Image", use_column_width=True) |
|
|
|
|
|
if st.button("Generate Video"): |
|
try: |
|
|
|
progress_text = "Operation in progress. Please wait..." |
|
my_bar = st.progress(0, text=progress_text) |
|
|
|
|
|
video_path, caption = generate_video_from_image(image, my_bar) |
|
|
|
if video_path and os.path.exists(video_path): |
|
|
|
with open(video_path, 'rb') as video_file: |
|
video_bytes = video_file.read() |
|
|
|
|
|
st.download_button( |
|
label="Download Video", |
|
data=video_bytes, |
|
file_name="generated_video.mp4", |
|
mime="video/mp4" |
|
) |
|
|
|
|
|
st.video(video_bytes) |
|
else: |
|
st.error("Failed to generate video. Please try again.") |
|
|
|
except Exception as e: |
|
st.error(f"An error occurred: {str(e)}") |
|
st.error("Full error message for debugging:") |
|
st.error(e) |
|
|
|
if __name__ == "__main__": |
|
main() |