Spaces:
No application file
No application file
Upload 8 files
Browse files- .gitignore +55 -0
- DEPLOYMENT.md +105 -0
- DEPLOYMENT_SUMMARY.md +66 -0
- app.py +273 -0
- packages.txt +12 -0
- requirements.txt +9 -0
- test_deployment.py +75 -0
- video_generator.py +112 -0
.gitignore
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Environment files
|
| 2 |
+
.env
|
| 3 |
+
.env.local
|
| 4 |
+
.env.production
|
| 5 |
+
|
| 6 |
+
# Python cache
|
| 7 |
+
__pycache__/
|
| 8 |
+
*.py[cod]
|
| 9 |
+
*$py.class
|
| 10 |
+
*.so
|
| 11 |
+
|
| 12 |
+
# Generated videos
|
| 13 |
+
*.mp4
|
| 14 |
+
*.mov
|
| 15 |
+
*.avi
|
| 16 |
+
|
| 17 |
+
# Manim outputs
|
| 18 |
+
media/
|
| 19 |
+
new*.py
|
| 20 |
+
|
| 21 |
+
# Temporary files
|
| 22 |
+
*.tmp
|
| 23 |
+
*.temp
|
| 24 |
+
|
| 25 |
+
# OS files
|
| 26 |
+
.DS_Store
|
| 27 |
+
Thumbs.db
|
| 28 |
+
|
| 29 |
+
# IDE files
|
| 30 |
+
.vscode/
|
| 31 |
+
.idea/
|
| 32 |
+
*.swp
|
| 33 |
+
*.swo
|
| 34 |
+
|
| 35 |
+
# Logs
|
| 36 |
+
*.log
|
| 37 |
+
|
| 38 |
+
# Distribution / packaging
|
| 39 |
+
.Python
|
| 40 |
+
build/
|
| 41 |
+
develop-eggs/
|
| 42 |
+
dist/
|
| 43 |
+
downloads/
|
| 44 |
+
eggs/
|
| 45 |
+
.eggs/
|
| 46 |
+
lib/
|
| 47 |
+
lib64/
|
| 48 |
+
parts/
|
| 49 |
+
sdist/
|
| 50 |
+
var/
|
| 51 |
+
wheels/
|
| 52 |
+
*.egg-info/
|
| 53 |
+
.installed.cfg
|
| 54 |
+
*.egg
|
| 55 |
+
MANIFEST
|
DEPLOYMENT.md
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# 🚀 Deployment Guide for Hugging Face Spaces
|
| 2 |
+
|
| 3 |
+
## Files Created for Deployment
|
| 4 |
+
|
| 5 |
+
1. **app.py** - Main Gradio application
|
| 6 |
+
2. **README.md** - Space configuration and documentation
|
| 7 |
+
3. **requirements.txt** - Python dependencies
|
| 8 |
+
4. **packages.txt** - System dependencies for Manim
|
| 9 |
+
5. **.gitignore** - Files to exclude from git
|
| 10 |
+
6. **test_deployment.py** - Local testing script
|
| 11 |
+
|
| 12 |
+
## Step-by-Step Deployment Instructions
|
| 13 |
+
|
| 14 |
+
### 1. Create a New Hugging Face Space
|
| 15 |
+
|
| 16 |
+
1. Go to [Hugging Face Spaces](https://huggingface.co/spaces)
|
| 17 |
+
2. Click "Create new Space"
|
| 18 |
+
3. Choose:
|
| 19 |
+
- **Space name**: `ai-video-generator` (or your preferred name)
|
| 20 |
+
- **License**: MIT
|
| 21 |
+
- **SDK**: Gradio
|
| 22 |
+
- **Hardware**: CPU Basic (free tier) or GPU if you have Pro subscription
|
| 23 |
+
|
| 24 |
+
### 2. Upload Files
|
| 25 |
+
|
| 26 |
+
Upload all the files from your video_generator folder:
|
| 27 |
+
- `app.py`
|
| 28 |
+
- `README.md`
|
| 29 |
+
- `requirements.txt`
|
| 30 |
+
- `packages.txt`
|
| 31 |
+
- `.gitignore`
|
| 32 |
+
|
| 33 |
+
**DO NOT upload**:
|
| 34 |
+
- `.env` file (contains your API key)
|
| 35 |
+
- `video_generator.py` (original file, not needed)
|
| 36 |
+
|
| 37 |
+
### 3. Set Environment Variables
|
| 38 |
+
|
| 39 |
+
1. Go to your Space settings
|
| 40 |
+
2. Click on "Variables and secrets"
|
| 41 |
+
3. Add a new secret:
|
| 42 |
+
- **Name**: `GEMINI_API_KEY`
|
| 43 |
+
- **Value**: Your actual Gemini API key (from your .env file)
|
| 44 |
+
|
| 45 |
+
### 4. Wait for Build
|
| 46 |
+
|
| 47 |
+
- The space will automatically build and install dependencies
|
| 48 |
+
- This may take 5-15 minutes due to Manim installation
|
| 49 |
+
- Check the build logs for any errors
|
| 50 |
+
|
| 51 |
+
### 5. Test the Application
|
| 52 |
+
|
| 53 |
+
1. Once built, test with a simple topic like "Introduction to Python"
|
| 54 |
+
2. Check if videos generate correctly
|
| 55 |
+
3. Verify the interface works smoothly
|
| 56 |
+
|
| 57 |
+
## Troubleshooting
|
| 58 |
+
|
| 59 |
+
### Common Issues and Solutions
|
| 60 |
+
|
| 61 |
+
1. **Build Timeout**
|
| 62 |
+
- Manim installation can be slow
|
| 63 |
+
- Try using a smaller requirements.txt with fewer versions specified
|
| 64 |
+
|
| 65 |
+
2. **Memory Issues**
|
| 66 |
+
- Use shorter video durations (1 minute)
|
| 67 |
+
- Consider upgrading to GPU hardware
|
| 68 |
+
|
| 69 |
+
3. **API Key Issues**
|
| 70 |
+
- Double-check the secret name is exactly `GEMINI_API_KEY`
|
| 71 |
+
- Verify the API key is valid and has quota
|
| 72 |
+
|
| 73 |
+
4. **Video Generation Fails**
|
| 74 |
+
- Check the generation logs in the interface
|
| 75 |
+
- Manim syntax might need adjustment for the cloud environment
|
| 76 |
+
|
| 77 |
+
## Alternative Deployment (GitHub → HF Spaces)
|
| 78 |
+
|
| 79 |
+
If you prefer using Git:
|
| 80 |
+
|
| 81 |
+
1. Create a GitHub repository
|
| 82 |
+
2. Push all files (except .env)
|
| 83 |
+
3. Connect the repository to Hugging Face Spaces
|
| 84 |
+
4. Set the API key in Spaces secrets
|
| 85 |
+
|
| 86 |
+
## Performance Tips
|
| 87 |
+
|
| 88 |
+
1. **Use shorter durations** (1-2 minutes max)
|
| 89 |
+
2. **Simple topics** work better than complex ones
|
| 90 |
+
3. **Monitor usage** to stay within API limits
|
| 91 |
+
4. **Consider caching** for frequently requested topics
|
| 92 |
+
|
| 93 |
+
## Security Notes
|
| 94 |
+
|
| 95 |
+
- Never commit API keys to public repositories
|
| 96 |
+
- Use Hugging Face Spaces secrets for sensitive data
|
| 97 |
+
- The .gitignore file excludes sensitive files
|
| 98 |
+
|
| 99 |
+
## Support
|
| 100 |
+
|
| 101 |
+
If you encounter issues:
|
| 102 |
+
1. Check the Space logs
|
| 103 |
+
2. Verify all dependencies in requirements.txt
|
| 104 |
+
3. Test locally first with test_deployment.py
|
| 105 |
+
4. Consider the Hugging Face Spaces documentation
|
DEPLOYMENT_SUMMARY.md
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# 🎬 AI Video Generator - Deployment Summary
|
| 2 |
+
|
| 3 |
+
## ✅ Files Created for Hugging Face Spaces
|
| 4 |
+
|
| 5 |
+
### Core Application Files
|
| 6 |
+
- **`app.py`** - Main Gradio application with AI video generation
|
| 7 |
+
- **`requirements.txt`** - Python dependencies optimized for HF Spaces
|
| 8 |
+
- **`packages.txt`** - System dependencies for Manim
|
| 9 |
+
- **`README.md`** - Space configuration and documentation
|
| 10 |
+
|
| 11 |
+
### Supporting Files
|
| 12 |
+
- **`.gitignore`** - Excludes sensitive and unnecessary files
|
| 13 |
+
- **`DEPLOYMENT.md`** - Complete deployment instructions
|
| 14 |
+
- **`test_deployment.py`** - Local testing script
|
| 15 |
+
|
| 16 |
+
## 🚀 Ready for Deployment!
|
| 17 |
+
|
| 18 |
+
Your video generator is now ready to deploy to Hugging Face Spaces. Here's what it includes:
|
| 19 |
+
|
| 20 |
+
### Features
|
| 21 |
+
- 🤖 AI-powered content generation using Google Gemini
|
| 22 |
+
- 🎨 Professional Manim animations with clean presentation style
|
| 23 |
+
- ⏱️ Customizable video duration (1-3 minutes)
|
| 24 |
+
- 🎯 Educational focus with various topic support
|
| 25 |
+
- 📱 Modern Gradio web interface
|
| 26 |
+
- ⚡ Optimized for cloud deployment
|
| 27 |
+
|
| 28 |
+
### Key Improvements Made
|
| 29 |
+
1. **Stability**: Using stable model versions and error handling
|
| 30 |
+
2. **Performance**: Reduced complexity and added timeouts
|
| 31 |
+
3. **User Experience**: Better interface with status updates
|
| 32 |
+
4. **Cloud Ready**: Proper environment variable handling
|
| 33 |
+
5. **Error Handling**: Comprehensive error messages and fallbacks
|
| 34 |
+
|
| 35 |
+
## 📋 Next Steps
|
| 36 |
+
|
| 37 |
+
1. **Create Hugging Face Space**
|
| 38 |
+
- Go to https://huggingface.co/spaces
|
| 39 |
+
- Create new Gradio space
|
| 40 |
+
- Upload all files (except .env)
|
| 41 |
+
|
| 42 |
+
2. **Set API Key**
|
| 43 |
+
- In Space settings → Variables and secrets
|
| 44 |
+
- Add secret: `GEMINI_API_KEY` = your actual API key
|
| 45 |
+
|
| 46 |
+
3. **Wait for Build**
|
| 47 |
+
- Initial build takes 10-15 minutes
|
| 48 |
+
- Manim installation is the longest part
|
| 49 |
+
|
| 50 |
+
4. **Test and Share**
|
| 51 |
+
- Try generating a simple video
|
| 52 |
+
- Share your space with others!
|
| 53 |
+
|
| 54 |
+
## 🔧 Local Testing
|
| 55 |
+
|
| 56 |
+
Run `python test_deployment.py` to verify dependencies before deployment.
|
| 57 |
+
|
| 58 |
+
## 📞 Support
|
| 59 |
+
|
| 60 |
+
If you encounter issues:
|
| 61 |
+
- Check DEPLOYMENT.md for detailed troubleshooting
|
| 62 |
+
- Verify API key is set correctly
|
| 63 |
+
- Monitor Space build logs for errors
|
| 64 |
+
- Start with simple topics for testing
|
| 65 |
+
|
| 66 |
+
**Your AI Video Generator is ready for the world! 🌟**
|
app.py
ADDED
|
@@ -0,0 +1,273 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import google.generativeai as genai
|
| 3 |
+
import os
|
| 4 |
+
import re
|
| 5 |
+
import subprocess
|
| 6 |
+
import tempfile
|
| 7 |
+
import shutil
|
| 8 |
+
from pathlib import Path
|
| 9 |
+
import sys
|
| 10 |
+
|
| 11 |
+
# Check if API key is available
|
| 12 |
+
api_key = os.getenv("GEMINI_API_KEY")
|
| 13 |
+
if not api_key:
|
| 14 |
+
print("⚠️ Warning: GEMINI_API_KEY not found in environment variables")
|
| 15 |
+
print("Please set your Gemini API key in the Hugging Face Spaces secrets")
|
| 16 |
+
|
| 17 |
+
# Configure Gemini API
|
| 18 |
+
genai.configure(api_key=api_key)
|
| 19 |
+
|
| 20 |
+
model = genai.GenerativeModel(
|
| 21 |
+
model_name="gemini-1.5-flash", # Using more stable model for deployment
|
| 22 |
+
generation_config={
|
| 23 |
+
"temperature": 0.3,
|
| 24 |
+
"top_p": 0.95,
|
| 25 |
+
"top_k": 40,
|
| 26 |
+
"max_output_tokens": 8000, # Reduced for better performance
|
| 27 |
+
}
|
| 28 |
+
)
|
| 29 |
+
|
| 30 |
+
def generate_video(topic, duration_minutes=1):
|
| 31 |
+
if not api_key:
|
| 32 |
+
return None, "❌ Error: GEMINI_API_KEY not configured. Please set it in Hugging Face Spaces secrets."
|
| 33 |
+
|
| 34 |
+
try:
|
| 35 |
+
# Step 1: Generate plan for Manim animation
|
| 36 |
+
planning_prompt = f"""
|
| 37 |
+
You are a professional with deep expertise in Manim CE 0.18.0.
|
| 38 |
+
|
| 39 |
+
Your task is to design a presentation-style video using Manim for the topic: "{topic}"
|
| 40 |
+
|
| 41 |
+
Guidelines:
|
| 42 |
+
- The video should consist of **slides of text** with **smooth transitions** between them.
|
| 43 |
+
- Structure the scene logically with 3-5 main points maximum.
|
| 44 |
+
- Target duration: {duration_minutes} minute(s).
|
| 45 |
+
- Use **simple text objects only**, no equations, graphs, or complex animations.
|
| 46 |
+
- Keep text concise and readable.
|
| 47 |
+
- Include details like:
|
| 48 |
+
- Text to be shown (keep it short and clear)
|
| 49 |
+
- When to fade in/out
|
| 50 |
+
- Slide duration (2-4 seconds per slide)
|
| 51 |
+
- Transitions (FadeIn, FadeOut, Transform)
|
| 52 |
+
- Do not output any code here — only the **presentation plan**, step by step.
|
| 53 |
+
|
| 54 |
+
Output Format:
|
| 55 |
+
Scene 1:
|
| 56 |
+
- Text: "Title: {topic}"
|
| 57 |
+
- Action: FadeIn
|
| 58 |
+
- Duration: 3 seconds
|
| 59 |
+
|
| 60 |
+
Scene 2:
|
| 61 |
+
- Text: "Key Point 1: [brief description]"
|
| 62 |
+
- Action: Transform from previous
|
| 63 |
+
- Duration: 4 seconds
|
| 64 |
+
...
|
| 65 |
+
"""
|
| 66 |
+
|
| 67 |
+
plan_response = model.generate_content(planning_prompt)
|
| 68 |
+
plan = plan_response.text.strip()
|
| 69 |
+
|
| 70 |
+
# Step 2: Generate Manim code based on the plan
|
| 71 |
+
code_prompt = f"""
|
| 72 |
+
Using this animation plan:
|
| 73 |
+
|
| 74 |
+
{plan}
|
| 75 |
+
|
| 76 |
+
Write a complete Manim CE 0.18.0 compatible Python file with a class called `VideoScene` that inherits from Scene.
|
| 77 |
+
|
| 78 |
+
Critical Requirements:
|
| 79 |
+
- Import all necessary modules: from manim import *
|
| 80 |
+
- Use ONLY basic text objects and simple transitions
|
| 81 |
+
- Background should be WHITE: self.camera.background_color = WHITE
|
| 82 |
+
- Text should be BLACK for contrast
|
| 83 |
+
- Keep text size reasonable (font_size=24 to 36)
|
| 84 |
+
- Use FadeIn, FadeOut, and Transform only
|
| 85 |
+
- Add self.clear() between slides to avoid overlapping text
|
| 86 |
+
- Include proper self.wait() durations
|
| 87 |
+
- Make sure the class is properly indented and structured
|
| 88 |
+
- NO complex animations, NO equations, NO images
|
| 89 |
+
- Keep it simple and clean
|
| 90 |
+
|
| 91 |
+
Example structure:
|
| 92 |
+
```python
|
| 93 |
+
from manim import *
|
| 94 |
+
|
| 95 |
+
class VideoScene(Scene):
|
| 96 |
+
def construct(self):
|
| 97 |
+
self.camera.background_color = WHITE
|
| 98 |
+
|
| 99 |
+
# Title slide
|
| 100 |
+
title = Text("Your Title", font_size=36, color=BLACK)
|
| 101 |
+
self.play(FadeIn(title))
|
| 102 |
+
self.wait(3)
|
| 103 |
+
self.play(FadeOut(title))
|
| 104 |
+
self.clear()
|
| 105 |
+
|
| 106 |
+
# Next slide...
|
| 107 |
+
```
|
| 108 |
+
|
| 109 |
+
Output: Just the complete Python code, no explanations.
|
| 110 |
+
"""
|
| 111 |
+
|
| 112 |
+
final_code = model.generate_content(code_prompt)
|
| 113 |
+
raw_code = final_code.text.strip()
|
| 114 |
+
|
| 115 |
+
# Clean the code
|
| 116 |
+
cleaned_code = re.sub(r"^(```|''')python\s*", "", raw_code)
|
| 117 |
+
cleaned_code = re.sub(r"(```|''')\s*$", "", cleaned_code)
|
| 118 |
+
|
| 119 |
+
# Create temporary directory for this generation
|
| 120 |
+
with tempfile.TemporaryDirectory() as temp_dir:
|
| 121 |
+
temp_path = Path(temp_dir)
|
| 122 |
+
|
| 123 |
+
# Save the generated code
|
| 124 |
+
manim_file = temp_path / "generated_video.py"
|
| 125 |
+
with open(manim_file, "w", encoding="utf-8") as f:
|
| 126 |
+
f.write(cleaned_code)
|
| 127 |
+
|
| 128 |
+
# Run manim to generate video with error handling
|
| 129 |
+
try:
|
| 130 |
+
# Use low quality for faster generation in cloud environment
|
| 131 |
+
result = subprocess.run([
|
| 132 |
+
"manim", "-pql", "--fps", "15", str(manim_file), "VideoScene"
|
| 133 |
+
],
|
| 134 |
+
cwd=temp_dir,
|
| 135 |
+
capture_output=True,
|
| 136 |
+
text=True,
|
| 137 |
+
check=True,
|
| 138 |
+
timeout=120 # 2 minute timeout
|
| 139 |
+
)
|
| 140 |
+
|
| 141 |
+
# Find the generated video file
|
| 142 |
+
media_dir = temp_path / "media"
|
| 143 |
+
video_files = list(media_dir.rglob("*.mp4"))
|
| 144 |
+
|
| 145 |
+
if video_files:
|
| 146 |
+
video_file = video_files[0]
|
| 147 |
+
|
| 148 |
+
# Copy to a location accessible by Gradio
|
| 149 |
+
output_file = f"video_{abs(hash(topic)) % 10000}.mp4"
|
| 150 |
+
shutil.copy2(video_file, output_file)
|
| 151 |
+
|
| 152 |
+
success_msg = f"✅ Video generated successfully!\n\n**Plan:**\n{plan}\n\n**Generated Code:**\n```python\n{cleaned_code}\n```"
|
| 153 |
+
return output_file, success_msg
|
| 154 |
+
else:
|
| 155 |
+
error_msg = f"❌ No video file found after generation.\n\n**Plan:**\n{plan}\n\n**Generated Code:**\n```python\n{cleaned_code}\n```\n\n**Manim Output:**\n{result.stdout}"
|
| 156 |
+
return None, error_msg
|
| 157 |
+
|
| 158 |
+
except subprocess.TimeoutExpired:
|
| 159 |
+
return None, f"❌ Video generation timed out. Try a shorter duration or simpler topic.\n\n**Plan:**\n{plan}"
|
| 160 |
+
except subprocess.CalledProcessError as e:
|
| 161 |
+
error_msg = f"❌ Manim generation failed:\n{e.stderr}\n\n**Plan:**\n{plan}\n\n**Generated Code:**\n```python\n{cleaned_code}\n```"
|
| 162 |
+
return None, error_msg
|
| 163 |
+
|
| 164 |
+
except Exception as e:
|
| 165 |
+
return None, f"❌ Unexpected error: {str(e)}"
|
| 166 |
+
|
| 167 |
+
# Create Gradio interface
|
| 168 |
+
with gr.Blocks(title="AI Video Generator", theme=gr.themes.Soft()) as demo:
|
| 169 |
+
gr.Markdown("""
|
| 170 |
+
# 🎬 AI Video Generator
|
| 171 |
+
|
| 172 |
+
Generate educational videos using AI and Manim animations. Simply enter a topic and get a professional presentation-style video!
|
| 173 |
+
|
| 174 |
+
**Features:**
|
| 175 |
+
- 🤖 AI-powered content generation using Gemini
|
| 176 |
+
- 🎨 Professional Manim animations
|
| 177 |
+
- ⏱️ Customizable duration (1-3 minutes)
|
| 178 |
+
- 📱 Clean presentation style
|
| 179 |
+
|
| 180 |
+
**Note:** Video generation may take 1-3 minutes depending on complexity.
|
| 181 |
+
""")
|
| 182 |
+
|
| 183 |
+
with gr.Row():
|
| 184 |
+
with gr.Column(scale=2):
|
| 185 |
+
topic_input = gr.Textbox(
|
| 186 |
+
label="📝 Video Topic",
|
| 187 |
+
placeholder="Enter an educational topic (e.g., 'Introduction to Python', 'Basic Math Concepts')",
|
| 188 |
+
value="Introduction to Python Programming",
|
| 189 |
+
lines=2
|
| 190 |
+
)
|
| 191 |
+
|
| 192 |
+
duration_input = gr.Slider(
|
| 193 |
+
minimum=1,
|
| 194 |
+
maximum=3,
|
| 195 |
+
value=1,
|
| 196 |
+
step=1,
|
| 197 |
+
label="⏱️ Duration (minutes)",
|
| 198 |
+
info="Shorter durations generate faster"
|
| 199 |
+
)
|
| 200 |
+
|
| 201 |
+
generate_btn = gr.Button("🚀 Generate Video", variant="primary", size="lg")
|
| 202 |
+
|
| 203 |
+
with gr.Column(scale=1):
|
| 204 |
+
gr.Markdown("""
|
| 205 |
+
### 💡 Tips for Best Results:
|
| 206 |
+
- Use clear, educational topics
|
| 207 |
+
- Avoid overly complex subjects
|
| 208 |
+
- Shorter durations work better
|
| 209 |
+
- Be patient - generation takes time
|
| 210 |
+
|
| 211 |
+
### 🎯 Great Topic Examples:
|
| 212 |
+
- "Introduction to Python"
|
| 213 |
+
- "Basic Data Structures"
|
| 214 |
+
- "How Photosynthesis Works"
|
| 215 |
+
- "Understanding Fractions"
|
| 216 |
+
- "Solar System Overview"
|
| 217 |
+
""")
|
| 218 |
+
|
| 219 |
+
with gr.Row():
|
| 220 |
+
with gr.Column():
|
| 221 |
+
video_output = gr.Video(
|
| 222 |
+
label="🎥 Generated Video",
|
| 223 |
+
height=400
|
| 224 |
+
)
|
| 225 |
+
|
| 226 |
+
with gr.Column():
|
| 227 |
+
log_output = gr.Textbox(
|
| 228 |
+
label="📋 Generation Details",
|
| 229 |
+
lines=15,
|
| 230 |
+
max_lines=25,
|
| 231 |
+
show_copy_button=True,
|
| 232 |
+
placeholder="Generation logs and AI-created content will appear here..."
|
| 233 |
+
)
|
| 234 |
+
|
| 235 |
+
# Progress indicator
|
| 236 |
+
with gr.Row():
|
| 237 |
+
status_text = gr.Textbox(
|
| 238 |
+
label="📊 Status",
|
| 239 |
+
value="Ready to generate video",
|
| 240 |
+
interactive=False
|
| 241 |
+
)
|
| 242 |
+
|
| 243 |
+
def update_status(message):
|
| 244 |
+
return message
|
| 245 |
+
|
| 246 |
+
def generate_with_status(topic, duration):
|
| 247 |
+
yield None, "🔄 Starting video generation...", "🔄 Initializing AI content creation..."
|
| 248 |
+
|
| 249 |
+
try:
|
| 250 |
+
result_video, result_log = generate_video(topic, duration)
|
| 251 |
+
|
| 252 |
+
if result_video:
|
| 253 |
+
yield result_video, result_log, "✅ Video generated successfully!"
|
| 254 |
+
else:
|
| 255 |
+
yield None, result_log, "❌ Video generation failed"
|
| 256 |
+
|
| 257 |
+
except Exception as e:
|
| 258 |
+
yield None, f"❌ Error: {str(e)}", "❌ Generation failed with error"
|
| 259 |
+
|
| 260 |
+
generate_btn.click(
|
| 261 |
+
fn=generate_with_status,
|
| 262 |
+
inputs=[topic_input, duration_input],
|
| 263 |
+
outputs=[video_output, log_output, status_text],
|
| 264 |
+
show_progress=True
|
| 265 |
+
)
|
| 266 |
+
|
| 267 |
+
if __name__ == "__main__":
|
| 268 |
+
demo.launch(
|
| 269 |
+
share=False,
|
| 270 |
+
server_name="0.0.0.0",
|
| 271 |
+
server_port=7860,
|
| 272 |
+
show_error=True
|
| 273 |
+
)
|
packages.txt
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
ffmpeg
|
| 2 |
+
libcairo2-dev
|
| 3 |
+
libpango1.0-dev
|
| 4 |
+
libglib2.0-dev
|
| 5 |
+
libxml2-dev
|
| 6 |
+
libgdk-pixbuf2.0-dev
|
| 7 |
+
libgirepository1.0-dev
|
| 8 |
+
libjpeg-dev
|
| 9 |
+
libpng-dev
|
| 10 |
+
zlib1g-dev
|
| 11 |
+
pkg-config
|
| 12 |
+
build-essential
|
requirements.txt
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio==4.44.0
|
| 2 |
+
manim==0.18.1
|
| 3 |
+
google-generativeai==0.8.3
|
| 4 |
+
numpy==1.24.3
|
| 5 |
+
pillow==10.0.1
|
| 6 |
+
scipy==1.11.4
|
| 7 |
+
matplotlib==3.7.2
|
| 8 |
+
pycairo==1.25.1
|
| 9 |
+
manimpango==0.5.0
|
test_deployment.py
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""
|
| 3 |
+
Test script to verify the video generator works locally before deployment
|
| 4 |
+
"""
|
| 5 |
+
|
| 6 |
+
import os
|
| 7 |
+
import sys
|
| 8 |
+
from pathlib import Path
|
| 9 |
+
|
| 10 |
+
def test_imports():
|
| 11 |
+
"""Test if all required packages can be imported"""
|
| 12 |
+
try:
|
| 13 |
+
import gradio as gr
|
| 14 |
+
print("✅ Gradio imported successfully")
|
| 15 |
+
|
| 16 |
+
import google.generativeai as genai
|
| 17 |
+
print("✅ Google Generative AI imported successfully")
|
| 18 |
+
|
| 19 |
+
import manim
|
| 20 |
+
print("✅ Manim imported successfully")
|
| 21 |
+
|
| 22 |
+
import numpy
|
| 23 |
+
print("✅ NumPy imported successfully")
|
| 24 |
+
|
| 25 |
+
return True
|
| 26 |
+
except ImportError as e:
|
| 27 |
+
print(f"❌ Import error: {e}")
|
| 28 |
+
return False
|
| 29 |
+
|
| 30 |
+
def test_api_key():
|
| 31 |
+
"""Test if API key is available"""
|
| 32 |
+
api_key = os.getenv("GEMINI_API_KEY")
|
| 33 |
+
if api_key:
|
| 34 |
+
print("✅ GEMINI_API_KEY found")
|
| 35 |
+
return True
|
| 36 |
+
else:
|
| 37 |
+
print("⚠️ GEMINI_API_KEY not found - set it in environment variables")
|
| 38 |
+
return False
|
| 39 |
+
|
| 40 |
+
def test_manim():
|
| 41 |
+
"""Test if Manim can run a simple scene"""
|
| 42 |
+
try:
|
| 43 |
+
from manim import Scene, Text, FadeIn
|
| 44 |
+
print("✅ Manim core classes imported successfully")
|
| 45 |
+
return True
|
| 46 |
+
except Exception as e:
|
| 47 |
+
print(f"❌ Manim test failed: {e}")
|
| 48 |
+
return False
|
| 49 |
+
|
| 50 |
+
def main():
|
| 51 |
+
"""Run all tests"""
|
| 52 |
+
print("🧪 Running pre-deployment tests...\n")
|
| 53 |
+
|
| 54 |
+
all_tests = [
|
| 55 |
+
("Import Test", test_imports),
|
| 56 |
+
("API Key Test", test_api_key),
|
| 57 |
+
("Manim Test", test_manim),
|
| 58 |
+
]
|
| 59 |
+
|
| 60 |
+
results = []
|
| 61 |
+
for test_name, test_func in all_tests:
|
| 62 |
+
print(f"Running {test_name}...")
|
| 63 |
+
result = test_func()
|
| 64 |
+
results.append(result)
|
| 65 |
+
print()
|
| 66 |
+
|
| 67 |
+
if all(results):
|
| 68 |
+
print("🎉 All tests passed! Ready for deployment to Hugging Face Spaces")
|
| 69 |
+
return 0
|
| 70 |
+
else:
|
| 71 |
+
print("❌ Some tests failed. Please fix the issues before deployment")
|
| 72 |
+
return 1
|
| 73 |
+
|
| 74 |
+
if __name__ == "__main__":
|
| 75 |
+
sys.exit(main())
|
video_generator.py
ADDED
|
@@ -0,0 +1,112 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import google.generativeai as genai
|
| 2 |
+
import os
|
| 3 |
+
import re
|
| 4 |
+
import subprocess
|
| 5 |
+
from dotenv import load_dotenv
|
| 6 |
+
|
| 7 |
+
# Load environment variables from .env file
|
| 8 |
+
load_dotenv()
|
| 9 |
+
|
| 10 |
+
# Configure Gemini API with environment variable
|
| 11 |
+
genai.configure(api_key=os.getenv("GEMINI_API_KEY"))
|
| 12 |
+
|
| 13 |
+
model = genai.GenerativeModel(
|
| 14 |
+
model_name="gemini-2.5-pro",
|
| 15 |
+
generation_config={
|
| 16 |
+
"temperature": 0.3,
|
| 17 |
+
"top_p": 0.95,
|
| 18 |
+
"top_k": 40,
|
| 19 |
+
"max_output_tokens": 20000,
|
| 20 |
+
}
|
| 21 |
+
)
|
| 22 |
+
|
| 23 |
+
task="""Advanced DSA Concepts
|
| 24 |
+
"""
|
| 25 |
+
# Step 1: Generate plan for Manim animation
|
| 26 |
+
planning_prompt = f"""
|
| 27 |
+
You are a professional with deep expertise in Manim CE 0.18.0.
|
| 28 |
+
|
| 29 |
+
Your task is to design a presentation-style video using Manim for the topic: "{task}"
|
| 30 |
+
|
| 31 |
+
Guidelines:
|
| 32 |
+
- The video should consist of **slides of text** with **smooth transitions** between them.
|
| 33 |
+
- Structure the scene logically.
|
| 34 |
+
-I just want content for 1 minute only.
|
| 35 |
+
- Use **simple text objects only**, no equations, graphs, or images.
|
| 36 |
+
- Include details like:
|
| 37 |
+
- Text to be shown
|
| 38 |
+
- When to fade in/out
|
| 39 |
+
- Slide duration
|
| 40 |
+
- Transitions (e.g., `FadeIn`, `FadeOut`, `Transform`)
|
| 41 |
+
- Do not output any code here — only the **presentation plan**, step by step.
|
| 42 |
+
|
| 43 |
+
Output Format:
|
| 44 |
+
Scene 1:
|
| 45 |
+
- Text: "Title Slide: Second World War"
|
| 46 |
+
- Action: FadeIn
|
| 47 |
+
- Duration: 3 seconds
|
| 48 |
+
...
|
| 49 |
+
|
| 50 |
+
"""
|
| 51 |
+
|
| 52 |
+
plan_response = model.generate_content(planning_prompt)
|
| 53 |
+
plan = plan_response.text.strip()
|
| 54 |
+
|
| 55 |
+
# Step 2: Generate Manim code based on the plan
|
| 56 |
+
code_prompt = f"""
|
| 57 |
+
Using this animation plan:
|
| 58 |
+
|
| 59 |
+
{plan}
|
| 60 |
+
|
| 61 |
+
Write a full Manim CE 0.18.0 compatible class called `Video`.
|
| 62 |
+
|
| 63 |
+
Important Instructions:
|
| 64 |
+
- Style it like a **PowerPoint presentation**: clean layout, one idea per slide.
|
| 65 |
+
-You can change the background to white&black glassmorphism and text to black also make it look more professional.
|
| 66 |
+
- Only use **text and transitions** (no math or images).
|
| 67 |
+
- Each slide should be a simple text message with appropriate **FadeIn, FadeOut, or Transform** effects.
|
| 68 |
+
- Do NOT use deprecated methods like `.to_edge()` or `.move_to(ORIGIN)` unnecessarily.
|
| 69 |
+
- Use `self.wait()` to control slide durations.
|
| 70 |
+
- Add brief pauses between scenes to simulate human-like pacing.
|
| 71 |
+
|
| 72 |
+
High Priority:
|
| 73 |
+
-Text shouldn't merge with other text.
|
| 74 |
+
- Strictly use Manim CE 0.18.0 syntax.
|
| 75 |
+
- No explanations. Just return the final code block.
|
| 76 |
+
|
| 77 |
+
Output: Just the Python code for the Manim scene.
|
| 78 |
+
|
| 79 |
+
"""
|
| 80 |
+
|
| 81 |
+
final_code = model.generate_content(code_prompt)
|
| 82 |
+
print(final_code.text.strip())
|
| 83 |
+
# Get the LLM output
|
| 84 |
+
raw_code = final_code.text.strip()
|
| 85 |
+
|
| 86 |
+
# Remove leading ```python or ``` and trailing ``` if present
|
| 87 |
+
# Also handles '''python and ''' for safety
|
| 88 |
+
cleaned_code = re.sub(r"^(```|''')python\s*", "", raw_code)
|
| 89 |
+
cleaned_code = re.sub(r"(```|''')\s*$", "", cleaned_code)
|
| 90 |
+
|
| 91 |
+
# Set base filename
|
| 92 |
+
base_filename = "new"
|
| 93 |
+
extension = ".py"
|
| 94 |
+
i = 0
|
| 95 |
+
|
| 96 |
+
# Find next available filename
|
| 97 |
+
while os.path.exists(f"{base_filename}{i}{extension}"):
|
| 98 |
+
i += 1
|
| 99 |
+
|
| 100 |
+
filename = f"{base_filename}{i}{extension}"
|
| 101 |
+
|
| 102 |
+
# Save cleaned code
|
| 103 |
+
with open(filename, "w", encoding="utf-8") as f:
|
| 104 |
+
f.write(cleaned_code)
|
| 105 |
+
|
| 106 |
+
print(f"Code saved to {filename}")
|
| 107 |
+
|
| 108 |
+
try:
|
| 109 |
+
print(f"🚀 Running {filename} ...\n")
|
| 110 |
+
subprocess.run(["manim", "-pql", filename, "Video"], check=True)
|
| 111 |
+
except subprocess.CalledProcessError as e:
|
| 112 |
+
print(f"❌ Error running {filename}:\n{e}")
|