File size: 9,530 Bytes
a5e880f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b6e0003
 
a5e880f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b6e0003
a5e880f
b6e0003
 
a5e880f
 
 
 
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
# οΏ½ Animetrix AI - AI Educational Animation Generator

> Create professional educational animations from natural language using AI and Manim

---

## πŸ“‹ Overview

**Animetrix AI** is an intelligent animation generation platform that transforms simple text descriptions into high-quality educational videos. Powered by Google Gemini AI and Manim Community Edition, it automatically interprets concepts, generates animation code, and renders professional videos with synchronized voiceovers.

### Key Features

- πŸ€– **AI-Powered Generation** β€” Natural language to animated video pipeline
- 🎬 **Synchronized Narration** β€” Step-by-step voiceover perfectly timed with animations
- 🎨 **Professional Quality** β€” Clean Manim animations with smart post-processing
- πŸ”§ **Self-Correcting** β€” Automatic error detection and code sanitization
- πŸš€ **Production Ready** β€” FastAPI backend with modern web interface

---

## πŸ— Architecture

```mermaid
graph TD
    A[User Prompt] --> B[Teacher Module]
    B --> C[Gemini AI - Outline Generation]
    C --> D[Structured JSON with Steps]
    D --> E[Narrator - Per-Step Audio]
    E --> F[Compiler - Code Generation]
    F --> G[Gemini AI - Manim Code]
    G --> H[Code Sanitization]
    H --> I[Runner - Manim Rendering]
    I --> J[Video + Audio Merge]
    J --> K[Final MP4 Output]
```

### Pipeline Stages

1. **Teacher** (`teacher.py`) - Converts prompts into structured educational outlines
2. **Narrator** (`narrator.py`) - Generates per-step narration audio with gTTS
3. **Compiler** (`compiler.py`) - Creates clean Manim code with audio synchronization
4. **Runner** (`runner.py`) - Executes rendering and merges audio/video

---

## πŸ›  Tech Stack

### Backend

- **FastAPI** - High-performance async API framework
- **Google Gemini 2.5 Flash** - AI model for outline and code generation
- **Manim Community Edition** - Professional animation engine
- **gTTS** - Text-to-speech for narration
- **MoviePy** - Audio/video processing

### Frontend

- **Tailwind CSS** - Modern, responsive design
- **Three.js** - Animated particle background
- **Vanilla JavaScript** - Lightweight, no framework overhead

### Infrastructure

- Python 3.8+
- FFmpeg for video encoding
- Environment-based configuration

---

## πŸ“ Project Structure

```
animetrix-ai/
β”œβ”€β”€ backend/
β”‚   β”œβ”€β”€ main.py              # FastAPI application & pipeline orchestration
β”‚   β”œβ”€β”€ teacher.py           # Outline generation (Gemini)
β”‚   β”œβ”€β”€ compiler.py          # Code generation & sanitization (Gemini)
β”‚   β”œβ”€β”€ runner.py            # Manim rendering
β”‚   β”œβ”€β”€ narrator.py          # Audio generation & merging
β”‚   β”œβ”€β”€ requirements.txt     # Python dependencies
β”‚   β”œβ”€β”€ static/
β”‚   β”‚   └── index.html       # Web interface
β”‚   └── media/               # Generated videos & audio
β”œβ”€β”€ .env                     # Environment variables
β”œβ”€β”€ .gitignore
└── README.md
```

---

## πŸš€ Quick Start

### Prerequisites

- Python 3.8 or higher
- FFmpeg installed and in PATH
- Google Gemini API key

### Installation

1. **Clone the repository**

   ```bash
   git clone https://github.com/SayedZahur786/Animetrix_AI.git
   cd Animetrix_AI
   ```

2. **Create virtual environment**

   ```bash
   python -m venv venv
   venv\Scripts\activate  # Windows
   # source venv/bin/activate  # Linux/Mac
   ```

3. **Install dependencies**

   ```bash
   cd backend
   pip install -r requirements.txt
   ```

4. **Configure environment**

   Create `.env` file in the root directory:

   ```env
   GEMINI_API_KEY=your_gemini_api_key_here
   ```

5. **Update FFmpeg path** (Windows)

   Edit [main.py](backend/main.py#L13) and set your FFmpeg path:

   ```python
   ffmpeg_path = r"C:\path\to\ffmpeg\bin"
   ```

### Running the Application

```bash
cd backend
uvicorn main:app --reload
```

Open your browser and navigate to:

```
http://localhost:8000
```

---

## 🎯 Usage

1. **Enter a prompt** in the text area (e.g., "Explain the Pythagorean theorem with a visual proof")
2. **Click Generate** to start the pipeline
3. **Monitor progress** through real-time status updates:
   - Planning (outline generation)
   - Coding (Manim script creation)
   - Executing (animation rendering)
4. **Watch the result** - video automatically plays when ready

### Example Prompts

- "Show how compound interest grows over time"
- "Visualize the concept of derivatives with a tangent line"
- "Demonstrate bubble sort algorithm step by step"
- "Explain photosynthesis with animated diagrams"

---

## πŸ”§ Configuration

### Environment Variables

| Variable         | Description           | Required |
| ---------------- | --------------------- | -------- |
| `GEMINI_API_KEY` | Google Gemini API key | Yes      |

### FFmpeg Configuration

Update the FFmpeg path in [main.py](backend/main.py) line 13:

```python
ffmpeg_path = r"M:\Ap\ffmpeg-7.1.1-essentials_build\ffmpeg-7.1.1-essentials_build\bin"
```

---

## πŸ“‘ API Reference

### POST `/generate`

Generate an educational animation from a text prompt.

**Request Body:**

```json
{
  "prompt": "Explain Newton's first law of motion"
}
```

**Response:**

```json
{
  "status": "started"
}
```

### GET `/status`

Check the current generation status.

**Response:**

```json
{
  "stage": "executing",
  "message": "Rendering Animation Frames...",
  "video_path": null,
  "error": null
}
```

### GET `/video/{path}`

Retrieve the generated video file.

---

## πŸ› Troubleshooting

| Issue                 | Solution                                                          |
| --------------------- | ----------------------------------------------------------------- |
| FFmpeg not found      | Ensure FFmpeg is installed and path is correctly set in `main.py` |
| Gemini API errors     | Verify your API key is valid and has sufficient quota             |
| Import errors         | Run `pip install -r requirements.txt` in the backend directory    |
| Manim rendering fails | Check that Manim CE is properly installed: `pip install manim`    |

---

## 🌐 Free Deployment Options

### Option 1: Render.com (Recommended)

**Best for: Simple, automated deployments**

- βœ… Free tier with 750 hours/month
- βœ… Auto-deploy from GitHub
- βœ… Built-in SSL
- ⚠️ Spins down after inactivity (cold starts ~30s)

**Steps:**

1. Push code to GitHub
2. Connect to [Render.com](https://render.com)
3. Create new Web Service
4. Add environment variable: `GEMINI_API_KEY`
5. Build command: `cd backend && pip install -r requirements.txt`
6. Start command: `cd backend && uvicorn main:app --host 0.0.0.0 --port $PORT`

### Option 2: Railway.app

**Best for: Quick deployments with generous free tier**

- βœ… $5 free credit/month
- βœ… No sleep/cold starts
- βœ… GitHub integration

**Steps:**

1. Connect GitHub repo to [Railway.app](https://railway.app)
2. Add `GEMINI_API_KEY` in Variables
3. Set start command: `cd backend && uvicorn main:app --host 0.0.0.0 --port $PORT`

### Option 3: Fly.io

**Best for: Global edge deployment**

- βœ… Free tier: 3 shared VMs
- βœ… Global CDN
- βœ… Fast performance

**Steps:**

1. Install Fly CLI: `curl -L https://fly.io/install.sh | sh`
2. Login: `fly auth login`
3. Launch: `fly launch`
4. Set secrets: `fly secrets set GEMINI_API_KEY=your_key`

### Option 4: Google Cloud Run

**Best for: Pay-per-use, scales to zero**

- βœ… 2 million requests/month free
- βœ… No cold start issues
- βœ… Integrated with Google services

**Steps:**

1. Create `Dockerfile` in project root:
   ```dockerfile
   FROM python:3.10-slim
   RUN apt-get update && apt-get install -y ffmpeg && rm -rf /var/lib/apt/lists/*
   WORKDIR /app
   COPY backend/requirements.txt .
   RUN pip install --no-cache-dir -r requirements.txt
   COPY backend/ .
   EXPOSE 8080
   CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8080"]
   ```
2. Deploy: `gcloud run deploy animetrix-ai --source .`

### Option 5: Hugging Face Spaces

**Best for: ML/AI community exposure**

- βœ… Free GPU access (limited)
- βœ… Great for demos
- βœ… Built-in community

**Steps:**

1. Create new Space at [huggingface.co/spaces](https://huggingface.co/spaces)
2. Select "Gradio" or "Streamlit" SDK
3. Push code to Space repository
4. Add `GEMINI_API_KEY` in Space secrets

### Comparison Table

| Platform             | Free Tier      | Cold Starts | Deployment | Best For        |
| -------------------- | -------------- | ----------- | ---------- | --------------- |
| **Render.com**       | βœ… 750h/month  | Yes (~30s)  | Easiest    | Beginners       |
| **Railway.app**      | βœ… $5 credit   | No          | Easy       | Active projects |
| **Fly.io**           | βœ… 3 VMs       | Minimal     | Moderate   | Performance     |
| **Google Cloud Run** | βœ… 2M requests | Minimal     | Advanced   | Scale           |
| **Hugging Face**     | βœ… Free GPU    | No          | Easy       | ML Community    |

**πŸ’‘ Recommendation:** Start with **Render.com** for easiest setup, then migrate to **Railway.app** or **Fly.io** for production use.

---

## 🀝 Contributing

Contributions are welcome! Please feel free to submit issues or pull requests.

---

## πŸ“„ License

This project is open source and available under the MIT License.

---

## πŸ‘¨β€πŸ’» Author

**Sayed Zahur**

- GitHub: [@SayedZahur786](https://github.com/SayedZahur786)
- Repository: [Animetrix_AI](https://github.com/SayedZahur786/Animetrix_AI)

---

**Made with ❀️ using AI and Manim**