Spaces:
Sleeping
Sleeping
Create Min.py
Browse files
Min.py
ADDED
@@ -0,0 +1,81 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from gtts import gTTS
|
3 |
+
from moviepy.editor import ImageSequenceClip, AudioFileClip
|
4 |
+
from diffusers import StableDiffusionPipeline
|
5 |
+
from PIL import Image, ImageFilter, ImageEnhance
|
6 |
+
import os
|
7 |
+
|
8 |
+
# Function to generate audio using gTTS
|
9 |
+
def generate_audio(text, output_path="audio.mp3"):
|
10 |
+
tts = gTTS(text=text, lang='en')
|
11 |
+
tts.save(output_path)
|
12 |
+
return output_path
|
13 |
+
|
14 |
+
# Function to generate images using Stable Diffusion
|
15 |
+
def generate_images(prompt, num_images=5):
|
16 |
+
# Load Stable Diffusion pipeline (ensure you have the model locally)
|
17 |
+
pipeline = StableDiffusionPipeline.from_pretrained("CompVis/stable-diffusion-v1-4")
|
18 |
+
pipeline = pipeline.to("cpu") # Use "cuda" for GPU if available
|
19 |
+
image_paths = []
|
20 |
+
|
21 |
+
for i in range(num_images):
|
22 |
+
image = pipeline(prompt).images[0] # Generate an image
|
23 |
+
image = apply_cartoon_filter(image) # Apply cartoon effect
|
24 |
+
image_path = f"image_{i}.png"
|
25 |
+
image.save(image_path)
|
26 |
+
image_paths.append(image_path)
|
27 |
+
return image_paths
|
28 |
+
|
29 |
+
# Function to apply a cartoon effect to an image
|
30 |
+
def apply_cartoon_filter(image):
|
31 |
+
# Convert to Pillow format if needed
|
32 |
+
if not isinstance(image, Image.Image):
|
33 |
+
image = Image.fromarray(image)
|
34 |
+
# Enhance colors and edges
|
35 |
+
image = ImageEnhance.Color(image).enhance(1.5) # Increase saturation
|
36 |
+
image = image.filter(ImageFilter.CONTOUR) # Add cartoon-style edges
|
37 |
+
return image
|
38 |
+
|
39 |
+
# Function to create a video using MoviePy
|
40 |
+
def create_video(image_paths, audio_path, output_path="output_video.mp4"):
|
41 |
+
clip = ImageSequenceClip(image_paths, fps=1) # 1 FPS for a slow animation
|
42 |
+
audio = AudioFileClip(audio_path)
|
43 |
+
final_clip = clip.set_audio(audio)
|
44 |
+
final_clip.write_videofile(output_path, fps=24)
|
45 |
+
return output_path
|
46 |
+
|
47 |
+
# Streamlit App
|
48 |
+
st.title("Cartoon Video Generator")
|
49 |
+
st.write("Enter a prompt, and we'll create a cartoon video with scenes and voices!")
|
50 |
+
|
51 |
+
# Input prompt
|
52 |
+
prompt = st.text_area("Enter your prompt:", "A cartoon story about a cat and a dog playing in the park.")
|
53 |
+
|
54 |
+
# Generate Button
|
55 |
+
if st.button("Generate Video"):
|
56 |
+
with st.spinner("Generating cartoon video..."):
|
57 |
+
# Generate Audio
|
58 |
+
audio_file = generate_audio(prompt)
|
59 |
+
|
60 |
+
# Generate Images
|
61 |
+
st.info("Generating cartoon images...")
|
62 |
+
images = generate_images(prompt)
|
63 |
+
|
64 |
+
# Create Video
|
65 |
+
st.info("Creating video...")
|
66 |
+
video_file = create_video(images, audio_file)
|
67 |
+
|
68 |
+
st.success("Video generated successfully!")
|
69 |
+
|
70 |
+
# Show video preview
|
71 |
+
st.video(video_file)
|
72 |
+
|
73 |
+
# Provide download link
|
74 |
+
with open(video_file, "rb") as file:
|
75 |
+
st.download_button(
|
76 |
+
label="Download Video",
|
77 |
+
data=file,
|
78 |
+
file_name="cartoon_video.mp4",
|
79 |
+
mime="video/mp4"
|
80 |
+
)
|
81 |
+
|