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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Gemini API - Client Integration Guide
|
| 2 |
+
|
| 3 |
+
## Authentication
|
| 4 |
+
|
| 5 |
+
All endpoints require JWT authentication via Google Sign-In.
|
| 6 |
+
|
| 7 |
+
```
|
| 8 |
+
Authorization: Bearer <your_jwt_token>
|
| 9 |
+
```
|
| 10 |
+
|
| 11 |
+
---
|
| 12 |
+
|
| 13 |
+
## Endpoints
|
| 14 |
+
|
| 15 |
+
### 1. Generate Text
|
| 16 |
+
**POST** `/gemini/generate-text`
|
| 17 |
+
|
| 18 |
+
```json
|
| 19 |
+
{
|
| 20 |
+
"prompt": "Write a poem about the moon",
|
| 21 |
+
"model": "gemini-2.5-flash" // optional
|
| 22 |
+
}
|
| 23 |
+
```
|
| 24 |
+
|
| 25 |
+
**Response:**
|
| 26 |
+
```json
|
| 27 |
+
{
|
| 28 |
+
"success": true,
|
| 29 |
+
"job_id": "job_abc123",
|
| 30 |
+
"status": "queued",
|
| 31 |
+
"position": 1
|
| 32 |
+
}
|
| 33 |
+
```
|
| 34 |
+
|
| 35 |
+
---
|
| 36 |
+
|
| 37 |
+
### 2. Analyze Image
|
| 38 |
+
**POST** `/gemini/analyze-image`
|
| 39 |
+
|
| 40 |
+
```json
|
| 41 |
+
{
|
| 42 |
+
"base64_image": "<base64_encoded_image>",
|
| 43 |
+
"mime_type": "image/jpeg",
|
| 44 |
+
"prompt": "What's in this image?"
|
| 45 |
+
}
|
| 46 |
+
```
|
| 47 |
+
|
| 48 |
+
---
|
| 49 |
+
|
| 50 |
+
### 3. Edit Image
|
| 51 |
+
**POST** `/gemini/edit-image`
|
| 52 |
+
|
| 53 |
+
```json
|
| 54 |
+
{
|
| 55 |
+
"base64_image": "<base64_encoded_image>",
|
| 56 |
+
"mime_type": "image/jpeg",
|
| 57 |
+
"prompt": "Make the sky purple"
|
| 58 |
+
}
|
| 59 |
+
```
|
| 60 |
+
|
| 61 |
+
---
|
| 62 |
+
|
| 63 |
+
### 4. Generate Video
|
| 64 |
+
**POST** `/gemini/generate-video`
|
| 65 |
+
|
| 66 |
+
```json
|
| 67 |
+
{
|
| 68 |
+
"base64_image": "<base64_encoded_image>",
|
| 69 |
+
"mime_type": "image/jpeg",
|
| 70 |
+
"prompt": "Make this scene come alive with gentle motion",
|
| 71 |
+
"aspect_ratio": "16:9", // or "9:16"
|
| 72 |
+
"resolution": "720p", // or "1080p"
|
| 73 |
+
"number_of_videos": 1 // 1-4
|
| 74 |
+
}
|
| 75 |
+
```
|
| 76 |
+
|
| 77 |
+
---
|
| 78 |
+
|
| 79 |
+
### 5. Generate Animation Prompt
|
| 80 |
+
**POST** `/gemini/generate-animation-prompt`
|
| 81 |
+
|
| 82 |
+
```json
|
| 83 |
+
{
|
| 84 |
+
"base64_image": "<base64_encoded_image>",
|
| 85 |
+
"mime_type": "image/png",
|
| 86 |
+
"custom_prompt": null // optional
|
| 87 |
+
}
|
| 88 |
+
```
|
| 89 |
+
|
| 90 |
+
---
|
| 91 |
+
|
| 92 |
+
## Polling for Results
|
| 93 |
+
|
| 94 |
+
All requests return a `job_id`. Poll for status:
|
| 95 |
+
|
| 96 |
+
**GET** `/gemini/job/{job_id}`
|
| 97 |
+
|
| 98 |
+
### Status Flow:
|
| 99 |
+
```
|
| 100 |
+
queued → processing → completed / failed
|
| 101 |
+
```
|
| 102 |
+
|
| 103 |
+
### Response Examples:
|
| 104 |
+
|
| 105 |
+
**Queued:**
|
| 106 |
+
```json
|
| 107 |
+
{
|
| 108 |
+
"status": "queued",
|
| 109 |
+
"position": 3
|
| 110 |
+
}
|
| 111 |
+
```
|
| 112 |
+
|
| 113 |
+
**Processing:**
|
| 114 |
+
```json
|
| 115 |
+
{
|
| 116 |
+
"status": "processing",
|
| 117 |
+
"started_at": "2024-12-12T13:00:00Z"
|
| 118 |
+
}
|
| 119 |
+
```
|
| 120 |
+
|
| 121 |
+
**Completed (Text/Image/Analyze):**
|
| 122 |
+
```json
|
| 123 |
+
{
|
| 124 |
+
"status": "completed",
|
| 125 |
+
"output": {
|
| 126 |
+
"text": "Here is your poem..."
|
| 127 |
+
}
|
| 128 |
+
}
|
| 129 |
+
```
|
| 130 |
+
|
| 131 |
+
**Completed (Video):**
|
| 132 |
+
```json
|
| 133 |
+
{
|
| 134 |
+
"status": "completed",
|
| 135 |
+
"output": {
|
| 136 |
+
"filename": "video_abc123.mp4"
|
| 137 |
+
},
|
| 138 |
+
"download_url": "/gemini/download/job_abc123"
|
| 139 |
+
}
|
| 140 |
+
```
|
| 141 |
+
|
| 142 |
+
**Failed:**
|
| 143 |
+
```json
|
| 144 |
+
{
|
| 145 |
+
"status": "failed",
|
| 146 |
+
"error": "API rate limit exceeded"
|
| 147 |
+
}
|
| 148 |
+
```
|
| 149 |
+
|
| 150 |
+
---
|
| 151 |
+
|
| 152 |
+
## Download Video
|
| 153 |
+
|
| 154 |
+
**GET** `/gemini/download/{job_id}`
|
| 155 |
+
|
| 156 |
+
Returns the video file directly.
|
| 157 |
+
|
| 158 |
+
---
|
| 159 |
+
|
| 160 |
+
## Cancel Job
|
| 161 |
+
|
| 162 |
+
**POST** `/gemini/job/{job_id}/cancel`
|
| 163 |
+
|
| 164 |
+
Only works for `queued` jobs.
|
| 165 |
+
|
| 166 |
+
```json
|
| 167 |
+
{
|
| 168 |
+
"success": true,
|
| 169 |
+
"status": "cancelled"
|
| 170 |
+
}
|
| 171 |
+
```
|
| 172 |
+
|
| 173 |
+
---
|
| 174 |
+
|
| 175 |
+
## Client Polling Example (JavaScript)
|
| 176 |
+
|
| 177 |
+
```javascript
|
| 178 |
+
async function generateText(prompt) {
|
| 179 |
+
// 1. Submit job
|
| 180 |
+
const response = await fetch('/gemini/generate-text', {
|
| 181 |
+
method: 'POST',
|
| 182 |
+
headers: {
|
| 183 |
+
'Authorization': `Bearer ${token}`,
|
| 184 |
+
'Content-Type': 'application/json'
|
| 185 |
+
},
|
| 186 |
+
body: JSON.stringify({ prompt })
|
| 187 |
+
});
|
| 188 |
+
const { job_id } = await response.json();
|
| 189 |
+
|
| 190 |
+
// 2. Poll until done
|
| 191 |
+
while (true) {
|
| 192 |
+
const status = await fetch(`/gemini/job/${job_id}`, {
|
| 193 |
+
headers: { 'Authorization': `Bearer ${token}` }
|
| 194 |
+
}).then(r => r.json());
|
| 195 |
+
|
| 196 |
+
if (status.status === 'completed') {
|
| 197 |
+
return status.output.text;
|
| 198 |
+
}
|
| 199 |
+
if (status.status === 'failed') {
|
| 200 |
+
throw new Error(status.error);
|
| 201 |
+
}
|
| 202 |
+
|
| 203 |
+
// Wait before next poll
|
| 204 |
+
await new Promise(r => setTimeout(r, 2000));
|
| 205 |
+
}
|
| 206 |
+
}
|
| 207 |
+
```
|
| 208 |
+
|
| 209 |
+
---
|
| 210 |
+
|
| 211 |
+
## Priority Tiers
|
| 212 |
+
|
| 213 |
+
Jobs are processed with different priorities:
|
| 214 |
+
|
| 215 |
+
| Job Type | Priority | Poll Interval |
|
| 216 |
+
|----------|----------|---------------|
|
| 217 |
+
| text, analyze, animation_prompt | Fast | ~5 sec |
|
| 218 |
+
| image, edit_image | Medium | ~30 sec |
|
| 219 |
+
| video | Slow | ~60 sec |
|
| 220 |
+
|
| 221 |
+
---
|
| 222 |
+
|
| 223 |
+
## Error Codes
|
| 224 |
+
|
| 225 |
+
| Code | Meaning |
|
| 226 |
+
|------|---------|
|
| 227 |
+
| 401 | Unauthorized - Invalid/expired token |
|
| 228 |
+
| 402 | Insufficient credits |
|
| 229 |
+
| 404 | Job not found |
|
| 230 |
+
| 429 | Rate limit exceeded |
|