Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# app.py β Cartoon Explainer Generator (Multilingual)
|
| 2 |
+
|
| 3 |
+
import os
|
| 4 |
+
import uuid
|
| 5 |
+
import streamlit as st
|
| 6 |
+
from gtts import gTTS
|
| 7 |
+
from transformers import pipeline
|
| 8 |
+
from diffusers import StableDiffusionPipeline
|
| 9 |
+
from PIL import Image
|
| 10 |
+
from moviepy.editor import ImageClip, AudioFileClip, concatenate_videoclips, CompositeAudioClip
|
| 11 |
+
|
| 12 |
+
# Setup
|
| 13 |
+
st.set_page_config(page_title="π¬ Cartoon Explainer Video Generator", layout="centered")
|
| 14 |
+
st.title("π¨ Cartoon Explainer Video Generator")
|
| 15 |
+
|
| 16 |
+
# Caching Model Load
|
| 17 |
+
@st.cache_resource(show_spinner=False)
|
| 18 |
+
def load_text_gen():
|
| 19 |
+
return pipeline("text-generation", model="tiiuae/falcon-rw-1b")
|
| 20 |
+
|
| 21 |
+
@st.cache_resource(show_spinner=False)
|
| 22 |
+
def load_image_gen():
|
| 23 |
+
return StableDiffusionPipeline.from_pretrained("CompVis/stable-diffusion-v1-4").to("cpu")
|
| 24 |
+
|
| 25 |
+
text_gen = load_text_gen()
|
| 26 |
+
image_gen = load_image_gen()
|
| 27 |
+
|
| 28 |
+
# UI Inputs
|
| 29 |
+
topic = st.text_input("Enter a topic (e.g., Newton's Third Law)")
|
| 30 |
+
lang = st.selectbox("Select Narration Language", ["English", "Telugu", "Hindi"])
|
| 31 |
+
generate = st.button("π₯ Generate Cartoon Explainer")
|
| 32 |
+
|
| 33 |
+
# Language Code Mapping
|
| 34 |
+
lang_map = {"English": "en", "Telugu": "te", "Hindi": "hi"}
|
| 35 |
+
|
| 36 |
+
if generate and topic:
|
| 37 |
+
with st.spinner("π§ Generating story..."):
|
| 38 |
+
prompt = f"Explain the concept '{topic}' using a creative cartoon-style story for school kids."
|
| 39 |
+
story_text = text_gen(prompt, max_new_tokens=200, temperature=0.7)[0]["generated_text"]
|
| 40 |
+
sentences = story_text.strip().split(".")
|
| 41 |
+
scenes = [s.strip() for s in sentences if len(s.strip()) > 20]
|
| 42 |
+
|
| 43 |
+
st.subheader("π Story Summary")
|
| 44 |
+
st.write(story_text)
|
| 45 |
+
|
| 46 |
+
audio_path = f"audio_{uuid.uuid4().hex}.mp3"
|
| 47 |
+
gTTS(text=story_text, lang=lang_map[lang]).save(audio_path)
|
| 48 |
+
|
| 49 |
+
with st.spinner("π¨ Generating cartoon scenes..."):
|
| 50 |
+
image_paths = []
|
| 51 |
+
for i, scene in enumerate(scenes):
|
| 52 |
+
img_prompt = f"Cartoon-style illustration of: {scene}"
|
| 53 |
+
image = image_gen(img_prompt).images[0]
|
| 54 |
+
img_path = f"scene_{i}_{uuid.uuid4().hex}.png"
|
| 55 |
+
image.save(img_path)
|
| 56 |
+
image_paths.append(img_path)
|
| 57 |
+
|
| 58 |
+
with st.spinner("π¬ Creating video..."):
|
| 59 |
+
clips = [
|
| 60 |
+
ImageClip(path).set_duration(3).fadein(0.3).fadeout(0.3)
|
| 61 |
+
for path in image_paths
|
| 62 |
+
]
|
| 63 |
+
final_video = concatenate_videoclips(clips, method="compose")
|
| 64 |
+
|
| 65 |
+
# Narration
|
| 66 |
+
narration = AudioFileClip(audio_path)
|
| 67 |
+
|
| 68 |
+
# Optional background music
|
| 69 |
+
music_path = "bg_music.mp3"
|
| 70 |
+
if os.path.exists(music_path):
|
| 71 |
+
music = AudioFileClip(music_path).volumex(0.1)
|
| 72 |
+
background = music.subclip(0, final_video.duration)
|
| 73 |
+
final_audio = CompositeAudioClip([narration, background])
|
| 74 |
+
else:
|
| 75 |
+
final_audio = narration
|
| 76 |
+
|
| 77 |
+
final_video = final_video.set_audio(final_audio)
|
| 78 |
+
|
| 79 |
+
output_path = f"explainer_{uuid.uuid4().hex}.mp4"
|
| 80 |
+
final_video.write_videofile(output_path, fps=24, codec="libx264", threads=2, preset="ultrafast")
|
| 81 |
+
|
| 82 |
+
st.subheader("β
Cartoon Explainer Video")
|
| 83 |
+
st.video(output_path)
|
| 84 |
+
st.download_button("β¬οΈ Download Video", open(output_path, "rb").read(), file_name="cartoon_explainer.mp4")
|
| 85 |
+
st.download_button("π Download Narration", open(audio_path, "rb").read(), file_name="narration.mp3")
|