Update app.py
Browse files
app.py
CHANGED
|
@@ -1,85 +1,58 @@
|
|
| 1 |
-
# app.py
|
| 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 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
final_audio = CompositeAudioClip([narration, background])
|
| 74 |
-
else:
|
| 75 |
-
final_audio = narration
|
| 76 |
|
| 77 |
-
|
|
|
|
|
|
|
| 78 |
|
| 79 |
-
|
| 80 |
-
|
|
|
|
| 81 |
|
| 82 |
-
st.subheader("β
Cartoon Explainer Video")
|
| 83 |
st.video(output_path)
|
| 84 |
-
|
| 85 |
-
|
|
|
|
|
|
|
|
|
| 1 |
+
# app.py - Subject Explainer Demo (Mock Output with Streamlit)
|
| 2 |
|
|
|
|
|
|
|
| 3 |
import streamlit as st
|
| 4 |
+
import random
|
| 5 |
+
import os
|
| 6 |
+
from moviepy.editor import concatenate_videoclips, TextClip, CompositeVideoClip, AudioFileClip
|
| 7 |
from gtts import gTTS
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
+
st.set_page_config(page_title="AI Subject Explainer", layout="centered")
|
| 10 |
+
st.title("π Realistic Subject Explainer (Demo)")
|
| 11 |
+
|
| 12 |
+
# Mock story generation
|
| 13 |
+
def generate_detailed_story(topic):
|
| 14 |
+
samples = {
|
| 15 |
+
"Newton's Third Law": "In a school cafeteria, two friends are playing air hockey. Every time one hits the puck, the puck pushes back with equal force, gliding smoothly. This shows Newton's Third Law β for every action, there's an equal and opposite reaction.",
|
| 16 |
+
"Photosynthesis": "Imagine a tiny chef inside a plant leaf, cooking food using sunlight, water, and air. That's photosynthesis! The plant takes sunlight and carbon dioxide to make its food and gives us oxygen in return."
|
| 17 |
+
}
|
| 18 |
+
return samples.get(topic, f"Sorry, no story found for '{topic}'. Try 'Newton's Third Law' or 'Photosynthesis'.")
|
| 19 |
+
|
| 20 |
+
# Create dummy video scene as text clips
|
| 21 |
+
def create_mock_video_scenes(story):
|
| 22 |
+
scenes = story.split(". ")
|
| 23 |
+
clips = []
|
| 24 |
+
for i, scene in enumerate(scenes):
|
| 25 |
+
txt_clip = TextClip(scene, fontsize=32, color='black', size=(800, 400), bg_color='white', method='caption')
|
| 26 |
+
txt_clip = txt_clip.set_duration(3)
|
| 27 |
+
clips.append(txt_clip)
|
| 28 |
+
return concatenate_videoclips(clips)
|
| 29 |
+
|
| 30 |
+
# Generate mock voice
|
| 31 |
+
def create_mock_voiceover(story):
|
| 32 |
+
tts = gTTS(story)
|
| 33 |
+
audio_path = "mock_voice.mp3"
|
| 34 |
+
tts.save(audio_path)
|
| 35 |
+
return audio_path
|
| 36 |
+
|
| 37 |
+
# Main UI
|
| 38 |
topic = st.text_input("Enter a topic (e.g., Newton's Third Law)")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
+
if st.button("Generate Video") and topic:
|
| 41 |
+
with st.spinner("Generating explanation..."):
|
| 42 |
+
story = generate_detailed_story(topic)
|
| 43 |
+
st.subheader("π§ Explanation")
|
| 44 |
+
st.write(story)
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
+
video = create_mock_video_scenes(story)
|
| 47 |
+
audio_path = create_mock_voiceover(story)
|
| 48 |
+
audio = AudioFileClip(audio_path).set_duration(video.duration)
|
| 49 |
|
| 50 |
+
final = CompositeVideoClip([video.set_audio(audio)])
|
| 51 |
+
output_path = f"final_demo_{random.randint(1000,9999)}.mp4"
|
| 52 |
+
final.write_videofile(output_path, fps=24, codec='libx264', audio_codec='aac')
|
| 53 |
|
|
|
|
| 54 |
st.video(output_path)
|
| 55 |
+
with open(audio_path, "rb") as a:
|
| 56 |
+
st.download_button("π Download Audio", a, file_name="voice.mp3")
|
| 57 |
+
with open(output_path, "rb") as v:
|
| 58 |
+
st.download_button("π¬ Download Video", v, file_name="explainer_demo.mp4")
|