-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrequest.py
More file actions
228 lines (186 loc) · 9.1 KB
/
request.py
File metadata and controls
228 lines (186 loc) · 9.1 KB
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
"""
request.py — клиент для видеогенерации через proxyapi.ru.
Поддерживает два провайдера:
openai → Sora 2 → POST /openai/v1/videos
google → Veo 3.x → POST /google/v1beta/models/{model}:predictLongRunning
"""
import os
import time
import logging
import requests
from dotenv import load_dotenv
from config import VIDEO_MODELS, OPENAI_BASE_URL, GOOGLE_BASE_URL, TASK_STATUS
load_dotenv()
logger = logging.getLogger(__name__)
API_KEY = os.getenv("API_KEY", "")
def _openai_headers():
return {"Authorization": f"Bearer {API_KEY}", "Content-Type": "application/json"}
def _google_headers():
return {"x-goog-api-key": API_KEY, "Content-Type": "application/json"}
def get_model_config(model_key):
if model_key not in VIDEO_MODELS:
raise KeyError(f"Неизвестная модель: {model_key}. Доступны: {list(VIDEO_MODELS)}")
return VIDEO_MODELS[model_key]
def _nearest(wanted, available):
return min(available, key=lambda x: abs(x - wanted))
# ────────────────────────────────────────────────────────────
# Создание задачи
# ────────────────────────────────────────────────────────────
def create_video_task(prompt, model_key, duration=5, resolution="720p",
image_url=None, negative_prompt="", aspect_ratio="16:9"):
cfg = get_model_config(model_key)
provider = cfg["provider"]
if provider == "openai":
return _create_sora_task(cfg, prompt, duration, aspect_ratio)
else:
return _create_veo_task(cfg, prompt, duration, resolution,
image_url, negative_prompt, aspect_ratio)
def _create_sora_task(cfg, prompt, duration, aspect_ratio):
"""OpenAI Sora 2 — POST /v1/videos"""
duration = _nearest(duration, cfg["durations"])
size = cfg["sizes"].get(aspect_ratio, "1280x720")
payload = {
"model": cfg["api_model_id"],
"prompt": prompt,
"size": size,
"seconds": str(duration),
}
endpoint = f"{OPENAI_BASE_URL}/videos"
logger.info(f"POST {endpoint} | sora-2 | {duration}s | {size}")
try:
resp = requests.post(endpoint, json=payload, headers=_openai_headers(), timeout=30)
if not resp.ok:
logger.error(f"Sora API {resp.status_code}: {resp.text}")
resp.raise_for_status()
data = resp.json()
except requests.HTTPError as e:
raise RuntimeError(f"HTTP {e.response.status_code}: {e.response.text}") from e
# Ответ: {"id": "video_xxx", "status": "queued", ...}
task_id = data.get("id")
if not task_id:
raise ValueError(f"API не вернул id. Ответ: {data}")
cost = cfg["price_per_sec"] * duration
logger.info(f"Задача: {task_id} | ~{cost:.0f} ₽")
return {"task_id": task_id, "model": cfg["name"],
"duration": duration, "estimated_cost": cost}
def _create_veo_task(cfg, prompt, duration, resolution, image_url,
negative_prompt, aspect_ratio):
"""Google Veo — POST /google/v1beta/models/{model}:predictLongRunning"""
duration = _nearest(duration, cfg["durations"])
if resolution == "1080p" and (duration != 8 or aspect_ratio != "16:9"):
resolution = "720p"
instance = {
"prompt": prompt,
"durationSeconds": str(duration),
"aspectRatio": aspect_ratio,
"resolution": resolution,
"personGeneration": "allow_all",
}
if negative_prompt:
instance["negativePrompt"] = negative_prompt
if image_url:
import base64
if image_url.startswith("data:"):
header, data = image_url.split(",", 1)
mime = header.split(":")[1].split(";")[0]
else:
r = requests.get(image_url, timeout=15); r.raise_for_status()
data = base64.b64encode(r.content).decode()
mime = r.headers.get("Content-Type", "image/jpeg").split(";")[0]
instance["image"] = {"bytesBase64Encoded": data, "mimeType": mime}
instance["personGeneration"] = "allow_adult"
endpoint = f"{GOOGLE_BASE_URL}/models/{cfg['api_model_id']}:predictLongRunning"
logger.info(f"POST {endpoint} | {duration}s | {resolution}")
try:
resp = requests.post(endpoint, json={"instances": [instance]},
headers=_google_headers(), timeout=30)
if not resp.ok:
logger.error(f"Veo API {resp.status_code}: {resp.text}")
resp.raise_for_status()
data = resp.json()
except requests.HTTPError as e:
raise RuntimeError(f"HTTP {e.response.status_code}: {e.response.text}") from e
operation_name = data.get("name")
if not operation_name:
raise ValueError(f"API не вернул operation name. Ответ: {data}")
cost = cfg["price_per_sec"] * duration
return {"task_id": operation_name, "model": cfg["name"],
"duration": duration, "estimated_cost": cost}
# ────────────────────────────────────────────────────────────
# Статус
# ────────────────────────────────────────────────────────────
def get_task_status(task_id):
# Определяем провайдера по формату task_id
if task_id.startswith("video_"):
return _get_sora_status(task_id)
else:
return _get_veo_status(task_id)
def _get_sora_status(video_id):
"""GET /v1/videos/{id}"""
endpoint = f"{OPENAI_BASE_URL}/videos/{video_id}"
try:
resp = requests.get(endpoint, headers=_openai_headers(), timeout=15)
resp.raise_for_status()
data = resp.json()
except requests.RequestException as e:
return {"status": "error", "video_url": None, "error": str(e)}
status_raw = data.get("status", "")
if status_raw == "completed":
# Скачиваем видео через /v1/videos/{id}/content
download_url = f"{OPENAI_BASE_URL}/videos/{video_id}/content"
return {
"status": TASK_STATUS["COMPLETED"],
"video_url": download_url,
"video_download_url": download_url,
"video_id": video_id,
"error": None,
}
elif status_raw in ("failed", "cancelled"):
return {"status": TASK_STATUS["FAILED"], "video_url": None,
"error": data.get("error", "Неизвестная ошибка")}
else:
# queued / in_progress
progress = data.get("progress", 0)
return {"status": TASK_STATUS["PROCESSING"], "video_url": None,
"error": None, "progress": progress}
def _get_veo_status(task_id):
"""GET /google/v1beta/{operation_name}"""
endpoint = f"{GOOGLE_BASE_URL}/{task_id}" if "/" in task_id \
else f"{GOOGLE_BASE_URL}/operations/{task_id}"
try:
resp = requests.get(endpoint, headers=_google_headers(), timeout=15)
resp.raise_for_status()
data = resp.json()
except requests.RequestException as e:
return {"status": "error", "video_url": None, "error": str(e)}
if data.get("done"):
if "error" in data:
return {"status": TASK_STATUS["FAILED"], "video_url": None,
"error": str(data["error"])}
try:
uri = data["response"]["generateVideoResponse"]["generatedSamples"][0]["video"]["uri"]
except (KeyError, IndexError):
uri = None
dl = (f"{uri}?key={API_KEY}" if uri and "?" not in uri
else f"{uri}&key={API_KEY}" if uri else None)
return {"status": TASK_STATUS["COMPLETED"], "video_url": uri,
"video_download_url": dl, "error": None}
return {"status": TASK_STATUS["PROCESSING"], "video_url": None, "error": None}
# ────────────────────────────────────────────────────────────
# Polling
# ────────────────────────────────────────────────────────────
def wait_for_completion(task_id, timeout=300, poll_interval=5, progress_callback=None):
start = time.time()
i = 0
while time.time() - start < timeout:
i += 1
result = get_task_status(task_id)
if progress_callback:
progress_callback(result["status"])
if result["status"] == TASK_STATUS["COMPLETED"]:
return result
if result["status"] == TASK_STATUS["FAILED"]:
raise RuntimeError(f"Ошибка генерации: {result.get('error')}")
logger.debug(f"Попытка {i}: {result['status']}")
time.sleep(poll_interval)
raise TimeoutError(f"Не завершилась за {timeout} сек.")