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
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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
---
title: Gemini Task Automation
emoji: πŸ€–
colorFrom: blue
colorTo: purple
sdk: docker
app_port: 8080
pinned: false
---

# πŸ€– Gemini Task Automation System

**An AI-powered task automation service that receives task descriptions, generates complete web applications using Gemini AI, and automatically deploys them to GitHub Pages.**

## 🎯 What Does This Project Do?

This is an **automated code generation and deployment pipeline** that:

1. **Receives Task Requests** via REST API (POST /ready endpoint)
2. **Generates Code** using Google's Gemini AI based on natural language descriptions
3. **Creates GitHub Repositories** automatically for each task
4. **Deploys to GitHub Pages** making the generated apps instantly accessible
5. **Notifies Completion** by sending deployment details to a callback URL

### πŸ”„ Complete Workflow

```
User sends task request β†’ API validates β†’ Gemini generates code β†’ 
Creates GitHub repo β†’ Commits & pushes β†’ Enables GitHub Pages β†’ 
Sends notification with live URL
```

## ✨ Key Features

- **Fully Generic** - No hardcoded templates, pure AI-driven generation
- **Background Processing** - Returns HTTP 200 immediately, processes asynchronously
- **Round-based Updates** - Round 1 creates new repos, Round 2+ updates existing ones
- **Attachment Support** - Can include images (logos, mockups, sample data) for AI context
- **Robust Error Handling** - Detailed logging with specific error types
- **JSON Schema Enforcement** - Ensures structured, parseable AI responses
- **Exponential Backoff** - Retries for GitHub API operations
- **Docker Ready** - Production-ready containerization

## πŸ“‹ How It Works (Technical Deep Dive)

### 1️⃣ Request Reception
```json
POST /ready
{
  "email": "user@example.com",
  "secret": "auth-token",
  "task": "chess-game",
  "round": 1,
  "brief": "Create a chess game with...",
  "checks": ["Has license", "Works in browser"],
  "evaluation_url": "https://callback.example.com",
  "attachments": []
}
```

### 2️⃣ AI Code Generation
- Sends task brief + checks + attachments to **Gemini 2.5 Flash**
- Uses **JSON schema** to enforce structured output
- AI generates all files (HTML, CSS, JS, README, LICENSE)
- Returns: `{"files": [{"path": "index.html", "content": "..."}]}`

### 3️⃣ GitHub Repository Setup
- **Round 1:** Creates new repository via GitHub API
- **Round 2+:** Clones existing repo, updates files
- Configures git with user credentials
- Commits with descriptive messages

### 4️⃣ Deployment
- Pushes to GitHub with retry logic (5 attempts, exponential backoff)
- Enables GitHub Pages via API
- Waits for Pages to become active

### 5️⃣ Notification
- POSTs deployment results to `evaluation_url`:
```json
{
  "email": "user@example.com",
  "task": "chess-game",
  "repo_url": "https://github.com/user/chess-game",
  "pages_url": "https://user.github.io/chess-game",
  "commit_sha": "abc123..."
}
```

## πŸš€ Deployment Options

### Option 1: Docker (Recommended)
```bash
docker build -t gemini-automation .
docker run -p 8080:8080 \
  -e GEMINI_API_KEY=your_key \
  -e GITHUB_TOKEN=your_token \
  -e GITHUB_USERNAME=your_username \
  -e STUDENT_SECRET=your_secret \
  gemini-automation
```

### Option 2: Cloud Platform
Deploy to any platform supporting Docker:
- **Hugging Face Spaces** (includes GPU option)
- **Google Cloud Run** (serverless, auto-scaling)
- **AWS ECS/Fargate** (enterprise-grade)
- **Azure Container Instances** (pay-per-use)
- **DigitalOcean App Platform** (simple, affordable)

### Option 3: Local Development
```bash
# 1. Clone repository
git clone https://github.com/YOUR_USERNAME/GEMINI_TDS_PROJECT1.git
cd GEMINI_TDS_PROJECT1

# 2. Create virtual environment
python -m venv .venv
source .venv/bin/activate  # Linux/Mac
# OR
.venv\Scripts\Activate.ps1  # Windows

# 3. Install dependencies
pip install -r requirements.txt

# 4. Configure environment
cp .env.example .env
# Edit .env with your API keys

# 5. Run server
uvicorn main:app --reload --port 8080
```

Access at: `http://localhost:8080`

## πŸ”‘ Required API Keys

### 1. Google Gemini API Key
- Go to: https://aistudio.google.com/app/apikey
- Click "Create API Key"
- Copy the key (starts with `AIza...`)
- **Free tier:** 15 requests/minute, 1500 requests/day

### 2. GitHub Personal Access Token
- Go to: GitHub Settings β†’ Developer settings β†’ Personal access tokens β†’ Tokens (classic)
- Click "Generate new token (classic)"
- Select scopes: `repo` (full control of private repositories)
- Generate and copy token (starts with `ghp_...`)
- **Never commit this token!**

### 3. Student Secret (Custom Auth)
- Create your own secret string (e.g., `my-secret-key-12345`)
- Used to authenticate incoming requests
- Can be any string you choose

## βš™οΈ Environment Variables

Create a `.env` file in the project root:

```env
GEMINI_API_KEY=AIzaSy...your_key_here
GITHUB_TOKEN=ghp_...your_token_here
GITHUB_USERNAME=your_github_username
STUDENT_SECRET=your_custom_secret_string
```

| Variable | Required | Description |
|----------|----------|-------------|
| `GEMINI_API_KEY` | βœ… Yes | Google Generative AI API key for code generation |
| `GITHUB_TOKEN` | βœ… Yes | GitHub PAT with `repo` scope for repo operations |
| `GITHUB_USERNAME` | βœ… Yes | Your GitHub username for repository creation |
| `STUDENT_SECRET` | βœ… Yes | Shared secret for authenticating incoming requests |

## πŸ“Š Project Architecture

```
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”      β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”      β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚   Client    │─────▢│  FastAPI     │─────▢│  Gemini AI  β”‚
β”‚  (Postman)  │◀─────│  /ready      │◀─────│  (Code Gen) β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜      β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜      β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                            β”‚
                            β–Ό
                     β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                     β”‚  GitPython   β”‚
                     β”‚  (Local Ops) β”‚
                     β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                            β”‚
                            β–Ό
                     β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”      β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                     β”‚  GitHub API  │─────▢│GitHub Pages β”‚
                     β”‚ (Create Repo)β”‚      β”‚  (Deploy)   β”‚
                     β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜      β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                            β”‚
                            β–Ό
                     β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                     β”‚ Callback URL β”‚
                     β”‚ (Notify Done)β”‚
                     β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
```

## πŸ› οΈ Technology Stack

| Component | Technology | Purpose |
|-----------|-----------|---------|
| **API Framework** | FastAPI | High-performance REST API |
| **AI Model** | Gemini 2.5 Flash | Code generation from natural language |
| **Validation** | Pydantic | Request/config validation |
| **Git Operations** | GitPython | Local repo management |
| **GitHub Integration** | GitHub REST API | Repo creation, Pages deployment |
| **Async Tasks** | asyncio | Background task processing |
| **HTTP Client** | httpx | Async HTTP requests |
| **Container** | Docker | Production deployment |

## πŸ“ Project Structure

```
GEMINI_TDS_PROJECT1/
β”œβ”€β”€ main.py              # FastAPI app + orchestration logic
β”œβ”€β”€ config.py            # Environment config with validation
β”œβ”€β”€ models.py            # Pydantic request/response models
β”œβ”€β”€ requirements.txt     # Python dependencies
β”œβ”€β”€ Dockerfile           # Production container definition
β”œβ”€β”€ .dockerignore        # Docker build exclusions
β”œβ”€β”€ .gitignore           # Git exclusions
β”œβ”€β”€ .env.example         # Template for environment variables
β”œβ”€β”€ LICENSE              # MIT license
└── README.md            # This file
```

## πŸ“– API Documentation

### POST /ready

**Description:** Submit a task for AI-powered code generation and deployment

**Request Body:**
```json
{
  "email": "user@example.com",
  "secret": "your_student_secret",
  "task": "unique-task-id",
  "round": 1,
  "nonce": "unique-request-id",
  "brief": "Detailed description of what to build...",
  "checks": ["Requirement 1", "Requirement 2"],
  "evaluation_url": "https://webhook.site/your-id",
  "attachments": [
    {
      "name": "logo.png",
      "url": "data:image/png;base64,iVBORw0KGgo..."
    }
  ]
}
```

**Response:**
```json
{
  "message": "Task received successfully!",
  "task_id": "unique-task-id"
}
```

**Status Codes:**
- `200 OK` - Task accepted, processing in background
- `403 Forbidden` - Invalid secret
- `422 Unprocessable Entity` - Invalid request format

### Callback Notification

When deployment completes, the API POSTs to your `evaluation_url`:

```json
{
  "email": "user@example.com",
  "task": "unique-task-id",
  "round": 1,
  "nonce": "unique-request-id",
  "repo_url": "https://github.com/username/unique-task-id",
  "commit_sha": "abc123def456...",
  "pages_url": "https://username.github.io/unique-task-id"
}
```

## πŸ§ͺ Testing

### Test with Postman / cURL

**1. Get a webhook URL:**
- Go to https://webhook.site
- Copy your unique URL

**2. Send test request:**

```bash
curl -X POST http://localhost:8080/ready \
  -H "Content-Type: application/json" \
  -d '{
    "email": "test@example.com",
    "secret": "your_student_secret",
    "task": "hello-world-test",
    "round": 1,
    "nonce": "test-001",
    "brief": "Create a simple hello world webpage with a gradient background and centered text saying Hello World!",
    "checks": ["Has index.html", "Has MIT license", "Text displays"],
    "evaluation_url": "YOUR_WEBHOOK_URL_HERE",
    "attachments": []
  }'
```

**3. Check results:**
- API returns immediately: `{"message": "Task received successfully!"}`
- Watch webhook.site for completion notification (~30-60 seconds)
- Visit the `pages_url` in notification to see live site

### Example Tasks

<details>
<summary><b>Calculator App</b></summary>

```json
{
  "email": "test@example.com",
  "secret": "your_secret",
  "task": "calculator-app",
  "round": 1,
  "nonce": "calc-001",
  "brief": "Create a calculator with: 1) Basic operations (+, -, Γ—, Γ·), 2) Clear button, 3) Decimal support, 4) Keyboard input, 5) Responsive design with Tailwind CSS",
  "checks": [
    "Has MIT license",
    "README explains usage",
    "Calculator performs addition",
    "Calculator performs subtraction",
    "Has clear button",
    "Responsive design"
  ],
  "evaluation_url": "https://webhook.site/your-id",
  "attachments": []
}
```
</details>

<details>
<summary><b>Todo List</b></summary>

```json
{
  "email": "test@example.com",
  "secret": "your_secret",
  "task": "todo-list-app",
  "round": 1,
  "nonce": "todo-001",
  "brief": "Create a todo list with: 1) Add new tasks, 2) Mark tasks as complete, 3) Delete tasks, 4) LocalStorage persistence, 5) Filter by All/Active/Completed, 6) Task counter, 7) Beautiful UI with animations",
  "checks": [
    "Can add tasks",
    "Can mark complete",
    "Can delete tasks",
    "Tasks persist on refresh",
    "Has filter buttons",
    "Shows task count"
  ],
  "evaluation_url": "https://webhook.site/your-id",
  "attachments": []
}
```
</details>

<details>
<summary><b>Chess Game (With Attachments)</b></summary>

```json
{
  "email": "test@example.com",
  "secret": "your_secret",
  "task": "chess-game-pro",
  "round": 1,
  "nonce": "chess-001",
  "brief": "Create a chess game with: 1) Full chess rules, 2) Drag-and-drop pieces, 3) Move validation, 4) Check/Checkmate detection, 5) Timed modes (Blitz 5min, Rapid 10min), 6) Move history, 7) Captured pieces display",
  "checks": [
    "All pieces move correctly",
    "Check detection works",
    "Checkmate ends game",
    "Timer counts down",
    "Move history displays"
  ],
  "evaluation_url": "https://webhook.site/your-id",
  "attachments": []
}
```
</details>

## πŸ› Troubleshooting

### Common Issues

**Problem:** `403 Forbidden` response
- **Solution:** Check that `secret` in request matches `STUDENT_SECRET` env var

**Problem:** Task accepted but no notification received
- **Solution:** Check Hugging Face Space logs or local console for errors. Common causes:
  - Invalid GitHub token or insufficient permissions
  - Gemini API quota exceeded
  - Invalid evaluation_url

**Problem:** GitHub API errors (403, 404)
- **Solution:** Verify GitHub token has `repo` scope:
  ```bash
  curl -H "Authorization: token YOUR_TOKEN" https://api.github.com/user
  ```

**Problem:** Gemini AI returns invalid JSON
- **Solution:** Check logs for response. The system now has improved error handling with specific error messages.

**Problem:** Pages deployment times out
- **Solution:** GitHub Pages can take 1-2 minutes to activate. The system retries 5 times with exponential backoff.

### Debug Mode

Enable detailed logging:
```python
# In main.py, add at top:
import logging
logging.basicConfig(level=logging.DEBUG)
```

Or set environment variable:
```bash
export LOG_LEVEL=DEBUG  # Linux/Mac
$env:LOG_LEVEL="DEBUG"  # Windows PowerShell
```

### Viewing Logs

**Docker:**
```bash
docker logs -f CONTAINER_ID
```

**Hugging Face Space:**
Go to Space β†’ "Logs" tab

## πŸ”’ Security Best Practices

1. **Never commit `.env` file** - Already in `.gitignore`
2. **Rotate API keys regularly** - Every 90 days recommended
3. **Use environment-specific secrets** - Different keys for dev/prod
4. **Limit GitHub token scope** - Only `repo` or `public_repo` needed
5. **Validate incoming requests** - `secret` field prevents unauthorized access
6. **Monitor API usage** - Check Gemini and GitHub API quotas

## πŸ“ˆ Performance & Limits

| Metric | Value | Notes |
|--------|-------|-------|
| Average task duration | 30-60s | Depends on complexity |
| Gemini API rate limit | 15/min | Free tier |
| GitHub API rate limit | 5000/hour | Authenticated |
| Max attachment size | ~10MB | Base64 encoding adds 33% |
| Concurrent tasks | Unlimited | Background processing |

## 🀝 Contributing

Contributions welcome! Areas for improvement:
- [ ] Add support for GitLab/Bitbucket deployment
- [ ] Implement task queue with Redis
- [ ] Add progress tracking API
- [ ] Support multiple AI models (Claude, GPT-4)
- [ ] Add unit tests
- [ ] Implement rate limiting
- [ ] Add metrics/monitoring

## πŸ“„ License

MIT License - see [LICENSE](LICENSE) file for details

## πŸ™ Acknowledgments

- **Google Gemini AI** - Code generation capabilities
- **FastAPI** - Modern Python web framework
- **GitHub** - Repository hosting and Pages deployment
- **Hugging Face** - Spaces platform for easy deployment

---

**Built for TDS Project 1** - Automated task generation and deployment system