|
|
|
|
|
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)") |
|
|
|
|
|
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'.") |
|
|
|
|
|
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) |
|
|
|
|
|
def create_mock_voiceover(story): |
|
tts = gTTS(story) |
|
audio_path = "mock_voice.mp3" |
|
tts.save(audio_path) |
|
return audio_path |
|
|
|
|
|
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") |