🎓 Theorem Explanation Agent
Generate educational videos explaining mathematical theorems and concepts using AI
{'#!/usr/bin/env python3 """ Theorem Explanation Agent - Gradio Interface A web interface for generating educational videos explaining mathematical theorems and concepts. """ import os import sys import json import traceback import tempfile import shutil from typing import Optional, List, Dict, Any import gradio as gr from pathlib import Path import asyncio import threading from datetime import datetime # Add the project root to Python path project_root = Path(__file__).parent sys.path.insert(0, str(project_root)) # Demo mode flag - set to True for deployment environments with limited resources DEMO_MODE = os.getenv("DEMO_MODE", "true").lower() == "true" # Default to demo mode for HF Spaces # Global variables for managing video generation video_generator = None generation_status = {} # Flag to track if we can import all dependencies CAN_IMPORT_DEPENDENCIES = True def initialize_video_generator(): """Initialize the video generator with default settings.""" global video_generator, CAN_IMPORT_DEPENDENCIES try: if DEMO_MODE: return "✅ Demo mode - Video generator simulation enabled" if not CAN_IMPORT_DEPENDENCIES: return "⚠️ Running in fallback mode due to missing dependencies" from generate_video import VideoGenerator video_generator = VideoGenerator( planner_model="gemini/gemini-2.0-flash", helper_model="gemini/gemini-2.0-flash", scene_model="gemini/gemini-2.0-flash", output_dir="output", use_rag=False, use_context_learning=False, use_visual_fix_code=False, print_response=False ) return "✅ Video generator initialized successfully" except Exception as e: CAN_IMPORT_DEPENDENCIES = False return f"❌ Failed to initialize video generator: {str(e)}\n\n🔧 Running in demo mode" def simulate_video_generation(topic: str, context: str, max_scenes: int) -> Dict[str, Any]: """Simulate video generation for demo purposes.""" import time import random # Simulate different stages stages = [ ("Planning video structure", 20), ("Generating scene outlines", 40), ("Creating animations", 60), ("Rendering videos", 80), ("Finalizing output", 100) ] for stage, progress in stages: time.sleep(random.uniform(0.1, 0.3)) # Faster for demo return { "success": True, "message": f"Demo video generated for topic: {topic}", "scenes_created": max_scenes, "total_duration": "2.5 minutes", "demo_note": "This is a simulated result. In production, actual Manim videos would be generated." } def generate_video_demo(topic: str, context: str = "", max_scenes: int = 3) -> str: """Generate a video explanation for the given topic (demo version).""" if not topic.strip(): return "❌ Please enter a topic to explain" try: # Simulate video generation result = simulate_video_generation(topic, context, max_scenes) output = f"""🎓 **Theorem Explanation Agent** **Topic:** {topic} **Context:** {context if context else "None provided"} **Max Scenes:** {max_scenes} **✅ Demo Generation Complete!** 📊 **Results:** - Scenes Created: {result['scenes_created']} - Total Duration: {result['total_duration']} - Status: {result['message']} ⚠️ **Demo Mode Note:** {result['demo_note']} 🚀 **To enable full video generation:** 1. Set up API keys (GEMINI_API_KEY, etc.) 2. Install full dependencies (Manim, FFmpeg, etc.) 3. Set DEMO_MODE=false 📝 **Example topics to try:** - Pythagorean Theorem - Velocity in Physics - Derivatives in Calculus - Newton's Laws of Motion """ return output except Exception as e: return f"❌ Error during generation: {str(e)}\n\nPlease try with a simpler topic." def get_example_topics() -> List[List[str]]: """Get example topics for the interface.""" return [ ["Pythagorean Theorem", "Explain with visual proof and real-world applications"], ["Velocity", "Explain velocity in physics with detailed examples"], ["Derivatives", "Explain derivatives in calculus with geometric interpretation"], ["Newton's Laws", "Explain Newton's three laws of motion with examples"], ["Quadratic Formula", "Derive and explain the quadratic formula step by step"], ["Logarithms", "Explain logarithms and their properties with examples"], ["Probability", "Explain basic probability concepts with practical examples"], ["Trigonometry", "Explain basic trigonometric functions and their uses"] ] # Create the main interface with gr.Blocks( title="🎓 Theorem Explanation Agent", theme=gr.themes.Soft(), css=""" .main-header { text-align: center; margin-bottom: 30px; padding: 20px; background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); border-radius: 10px; color: white; } .demo-warning { background-color: #fff3cd; border: 1px solid #ffeaa7; border-radius: 5px; padding: 15px; margin: 10px 0; color: #856404; } """ ) as demo: # Header gr.HTML(f"""
Generate educational videos explaining mathematical theorems and concepts using AI
{'