File size: 3,846 Bytes
f7e9441 274a73f |
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 |
# Hàm kiểm tra trạng thái task
def check_task_status(task_id):
try:
status_url = f"https://api.vidu.com/ent/v2/tasks/{task_id}/creations"
headers = {
"Authorization": f"Token {VIDU_API_KEY}",
"Content-Type": "application/json"
}
response = requests.get(status_url, headers=headers)
if not response.ok:
error_data = response.json()
raise Exception(error_data.get("message", "Lỗi khi kiểm tra trạng thái"))
response_data = response.json()
state = response_data.get("state")
video_url = None
if state == "success" and response_data.get("creations"):
video_url = response_data["creations"][0].get("url")
elif state == "failed":
err_code = response_data.get("err_code", "Không có mã lỗi")
logging.error(f"Task {task_id} thất bại - Error Code: {err_code}")
raise Exception(f"Task thất bại - Error Code: {err_code}")
logging.info(f"Task {task_id} - State: {state}")
return state, video_url
except Exception as e:
logging.error(f"Lỗi khi kiểm tra trạng thái task {task_id}: {str(e)}")
raise
# Hàm xử lý tạo video và theo dõi trạng thái
def generate_and_monitor_video(images, prompt, duration, seed, aspect_ratio, resolution, movement_amplitude):
try:
# Validate inputs (giữ nguyên)
if not images or len(images) < 1 or len(images) > 3:
raise ValueError("Vui lòng chọn từ 1 đến 3 file ảnh")
if not prompt or len(prompt) > 1500:
raise ValueError("Mô tả nội dung phải có tối đa 1500 ký tự")
if duration != 4:
raise ValueError("Thời lượng phải là 4 giây")
if resolution not in ["360p", "720p"]:
raise ValueError("Độ phân giải phải là 360p hoặc 720p")
# Upload các ảnh (giữ nguyên)
image_urls = []
for image in images:
validate_image(image)
image_url = upload_image(image)
image_urls.append(image_url)
# Gửi yêu cầu tạo video
payload = {
"model": "vidu2.0",
"images": image_urls,
"prompt": prompt,
"duration": str(duration),
"seed": str(seed),
"aspect_ratio": aspect_ratio,
"resolution": resolution,
"movement_amplitude": movement_amplitude
}
api_url = "https://api.vidu.com/ent/v2/reference2video"
headers = {
"Authorization": f"Token {VIDU_API_KEY}",
"Content-Type": "application/json"
}
response = requests.post(api_url, headers=headers, json=payload)
if not response.ok:
error_data = response.json()
raise Exception(error_data.get("message", "Lỗi khi tạo video"))
response_data = response.json()
task_id = response_data.get("task_id")
logging.info(f"Task created - Task ID: {task_id}")
# Theo dõi trạng thái
for _ in range(30): # 5 phút
state, video_url = check_task_status(task_id)
yield f"Task ID: {task_id}\nTrạng thái: {state}", None
if state == "success" and video_url:
return f"Video hoàn thành!\nTask ID: {task_id}\nURL: {video_url}", video_url
elif state == "failed":
raise Exception(f"Task thất bại - Task ID: {task_id}")
time.sleep(10)
raise Exception(f"Timeout: Task {task_id} chưa hoàn thành sau 5 phút")
except Exception as e:
logging.error(f"Lỗi trong quá trình tạo video: {str(e)}")
yield f"Lỗi: {str(e)}", None
return None, None |