SuriRaja commited on
Commit
a264a9a
Β·
verified Β·
1 Parent(s): 6f1c37a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -75
app.py CHANGED
@@ -1,85 +1,58 @@
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")
 
 
 
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")