Spaces:
Running
Running
File size: 3,661 Bytes
5bfbdc4 |
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 |
# Gemini API - Client Integration Guide
## Authentication
All endpoints require JWT authentication via Google Sign-In.
```
Authorization: Bearer <your_jwt_token>
```
---
## Endpoints
### 1. Generate Text
**POST** `/gemini/generate-text`
```json
{
"prompt": "Write a poem about the moon",
"model": "gemini-2.5-flash" // optional
}
```
**Response:**
```json
{
"success": true,
"job_id": "job_abc123",
"status": "queued",
"position": 1
}
```
---
### 2. Analyze Image
**POST** `/gemini/analyze-image`
```json
{
"base64_image": "<base64_encoded_image>",
"mime_type": "image/jpeg",
"prompt": "What's in this image?"
}
```
---
### 3. Edit Image
**POST** `/gemini/edit-image`
```json
{
"base64_image": "<base64_encoded_image>",
"mime_type": "image/jpeg",
"prompt": "Make the sky purple"
}
```
---
### 4. Generate Video
**POST** `/gemini/generate-video`
```json
{
"base64_image": "<base64_encoded_image>",
"mime_type": "image/jpeg",
"prompt": "Make this scene come alive with gentle motion",
"aspect_ratio": "16:9", // or "9:16"
"resolution": "720p", // or "1080p"
"number_of_videos": 1 // 1-4
}
```
---
### 5. Generate Animation Prompt
**POST** `/gemini/generate-animation-prompt`
```json
{
"base64_image": "<base64_encoded_image>",
"mime_type": "image/png",
"custom_prompt": null // optional
}
```
---
## Polling for Results
All requests return a `job_id`. Poll for status:
**GET** `/gemini/job/{job_id}`
### Status Flow:
```
queued → processing → completed / failed
```
### Response Examples:
**Queued:**
```json
{
"status": "queued",
"position": 3
}
```
**Processing:**
```json
{
"status": "processing",
"started_at": "2024-12-12T13:00:00Z"
}
```
**Completed (Text/Image/Analyze):**
```json
{
"status": "completed",
"output": {
"text": "Here is your poem..."
}
}
```
**Completed (Video):**
```json
{
"status": "completed",
"output": {
"filename": "video_abc123.mp4"
},
"download_url": "/gemini/download/job_abc123"
}
```
**Failed:**
```json
{
"status": "failed",
"error": "API rate limit exceeded"
}
```
---
## Download Video
**GET** `/gemini/download/{job_id}`
Returns the video file directly.
---
## Cancel Job
**POST** `/gemini/job/{job_id}/cancel`
Only works for `queued` jobs.
```json
{
"success": true,
"status": "cancelled"
}
```
---
## Client Polling Example (JavaScript)
```javascript
async function generateText(prompt) {
// 1. Submit job
const response = await fetch('/gemini/generate-text', {
method: 'POST',
headers: {
'Authorization': `Bearer ${token}`,
'Content-Type': 'application/json'
},
body: JSON.stringify({ prompt })
});
const { job_id } = await response.json();
// 2. Poll until done
while (true) {
const status = await fetch(`/gemini/job/${job_id}`, {
headers: { 'Authorization': `Bearer ${token}` }
}).then(r => r.json());
if (status.status === 'completed') {
return status.output.text;
}
if (status.status === 'failed') {
throw new Error(status.error);
}
// Wait before next poll
await new Promise(r => setTimeout(r, 2000));
}
}
```
---
## Priority Tiers
Jobs are processed with different priorities:
| Job Type | Priority | Poll Interval |
|----------|----------|---------------|
| text, analyze, animation_prompt | Fast | ~5 sec |
| image, edit_image | Medium | ~30 sec |
| video | Slow | ~60 sec |
---
## Error Codes
| Code | Meaning |
|------|---------|
| 401 | Unauthorized - Invalid/expired token |
| 402 | Insufficient credits |
| 404 | Job not found |
| 429 | Rate limit exceeded |
|