File size: 2,543 Bytes
2efc2e9
 
00a3608
2efc2e9
ad5cfd2
2efc2e9
 
ad5cfd2
2efc2e9
 
ad5cfd2
2efc2e9
 
 
 
 
 
 
ad5cfd2
2efc2e9
 
a264a9a
 
2efc2e9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# app.py - Subject Explainer Demo (Mock Output with Streamlit)

import streamlit as st
import random
import os
from moviepy.editor import concatenate_videoclips, TextClip, CompositeVideoClip, AudioFileClip
from gtts import gTTS

st.set_page_config(page_title="AI Subject Explainer", layout="centered")
st.title("πŸ“˜ Realistic Subject Explainer (Demo)")

# Mock story generation
def generate_detailed_story(topic):
    samples = {
        "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.",
        "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."
    }
    return samples.get(topic, f"Sorry, no story found for '{topic}'. Try 'Newton's Third Law' or 'Photosynthesis'.")

# Create dummy video scene as text clips
def create_mock_video_scenes(story):
    scenes = story.split(". ")
    clips = []
    for i, scene in enumerate(scenes):
        txt_clip = TextClip(scene, fontsize=32, color='black', size=(800, 400), bg_color='white', method='caption')
        txt_clip = txt_clip.set_duration(3)
        clips.append(txt_clip)
    return concatenate_videoclips(clips)

# Generate mock voice
def create_mock_voiceover(story):
    tts = gTTS(story)
    audio_path = "mock_voice.mp3"
    tts.save(audio_path)
    return audio_path

# Main UI
topic = st.text_input("Enter a topic (e.g., Newton's Third Law)")

if st.button("Generate Video") and topic:
    with st.spinner("Generating explanation..."):
        story = generate_detailed_story(topic)
        st.subheader("🧠 Explanation")
        st.write(story)

        video = create_mock_video_scenes(story)
        audio_path = create_mock_voiceover(story)
        audio = AudioFileClip(audio_path).set_duration(video.duration)

        final = CompositeVideoClip([video.set_audio(audio)])
        output_path = f"final_demo_{random.randint(1000,9999)}.mp4"
        final.write_videofile(output_path, fps=24, codec='libx264', audio_codec='aac')

    st.video(output_path)
    with open(audio_path, "rb") as a:
        st.download_button("πŸ”Š Download Audio", a, file_name="voice.mp3")
    with open(output_path, "rb") as v:
        st.download_button("🎬 Download Video", v, file_name="explainer_demo.mp4")