Upload 2 files
Browse files- app.py +90 -0
- requirements.txt +0 -0
app.py
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Hàm kiểm tra trạng thái task
|
| 2 |
+
def check_task_status(task_id):
|
| 3 |
+
try:
|
| 4 |
+
status_url = f"https://api.vidu.com/ent/v2/tasks/{task_id}/creations"
|
| 5 |
+
headers = {
|
| 6 |
+
"Authorization": f"Token {VIDU_API_KEY}",
|
| 7 |
+
"Content-Type": "application/json"
|
| 8 |
+
}
|
| 9 |
+
response = requests.get(status_url, headers=headers)
|
| 10 |
+
if not response.ok:
|
| 11 |
+
error_data = response.json()
|
| 12 |
+
raise Exception(error_data.get("message", "Lỗi khi kiểm tra trạng thái"))
|
| 13 |
+
|
| 14 |
+
response_data = response.json()
|
| 15 |
+
state = response_data.get("state")
|
| 16 |
+
video_url = None
|
| 17 |
+
if state == "success" and response_data.get("creations"):
|
| 18 |
+
video_url = response_data["creations"][0].get("url")
|
| 19 |
+
elif state == "failed":
|
| 20 |
+
err_code = response_data.get("err_code", "Không có mã lỗi")
|
| 21 |
+
logging.error(f"Task {task_id} thất bại - Error Code: {err_code}")
|
| 22 |
+
raise Exception(f"Task thất bại - Error Code: {err_code}")
|
| 23 |
+
|
| 24 |
+
logging.info(f"Task {task_id} - State: {state}")
|
| 25 |
+
return state, video_url
|
| 26 |
+
except Exception as e:
|
| 27 |
+
logging.error(f"Lỗi khi kiểm tra trạng thái task {task_id}: {str(e)}")
|
| 28 |
+
raise
|
| 29 |
+
|
| 30 |
+
# Hàm xử lý tạo video và theo dõi trạng thái
|
| 31 |
+
def generate_and_monitor_video(images, prompt, duration, seed, aspect_ratio, resolution, movement_amplitude):
|
| 32 |
+
try:
|
| 33 |
+
# Validate inputs (giữ nguyên)
|
| 34 |
+
if not images or len(images) < 1 or len(images) > 3:
|
| 35 |
+
raise ValueError("Vui lòng chọn từ 1 đến 3 file ảnh")
|
| 36 |
+
if not prompt or len(prompt) > 1500:
|
| 37 |
+
raise ValueError("Mô tả nội dung phải có tối đa 1500 ký tự")
|
| 38 |
+
if duration != 4:
|
| 39 |
+
raise ValueError("Thời lượng phải là 4 giây")
|
| 40 |
+
if resolution not in ["360p", "720p"]:
|
| 41 |
+
raise ValueError("Độ phân giải phải là 360p hoặc 720p")
|
| 42 |
+
|
| 43 |
+
# Upload các ảnh (giữ nguyên)
|
| 44 |
+
image_urls = []
|
| 45 |
+
for image in images:
|
| 46 |
+
validate_image(image)
|
| 47 |
+
image_url = upload_image(image)
|
| 48 |
+
image_urls.append(image_url)
|
| 49 |
+
|
| 50 |
+
# Gửi yêu cầu tạo video
|
| 51 |
+
payload = {
|
| 52 |
+
"model": "vidu2.0",
|
| 53 |
+
"images": image_urls,
|
| 54 |
+
"prompt": prompt,
|
| 55 |
+
"duration": str(duration),
|
| 56 |
+
"seed": str(seed),
|
| 57 |
+
"aspect_ratio": aspect_ratio,
|
| 58 |
+
"resolution": resolution,
|
| 59 |
+
"movement_amplitude": movement_amplitude
|
| 60 |
+
}
|
| 61 |
+
api_url = "https://api.vidu.com/ent/v2/reference2video"
|
| 62 |
+
headers = {
|
| 63 |
+
"Authorization": f"Token {VIDU_API_KEY}",
|
| 64 |
+
"Content-Type": "application/json"
|
| 65 |
+
}
|
| 66 |
+
response = requests.post(api_url, headers=headers, json=payload)
|
| 67 |
+
if not response.ok:
|
| 68 |
+
error_data = response.json()
|
| 69 |
+
raise Exception(error_data.get("message", "Lỗi khi tạo video"))
|
| 70 |
+
|
| 71 |
+
response_data = response.json()
|
| 72 |
+
task_id = response_data.get("task_id")
|
| 73 |
+
logging.info(f"Task created - Task ID: {task_id}")
|
| 74 |
+
|
| 75 |
+
# Theo dõi trạng thái
|
| 76 |
+
for _ in range(30): # 5 phút
|
| 77 |
+
state, video_url = check_task_status(task_id)
|
| 78 |
+
yield f"Task ID: {task_id}\nTrạng thái: {state}", None
|
| 79 |
+
if state == "success" and video_url:
|
| 80 |
+
return f"Video hoàn thành!\nTask ID: {task_id}\nURL: {video_url}", video_url
|
| 81 |
+
elif state == "failed":
|
| 82 |
+
raise Exception(f"Task thất bại - Task ID: {task_id}")
|
| 83 |
+
time.sleep(10)
|
| 84 |
+
|
| 85 |
+
raise Exception(f"Timeout: Task {task_id} chưa hoàn thành sau 5 phút")
|
| 86 |
+
|
| 87 |
+
except Exception as e:
|
| 88 |
+
logging.error(f"Lỗi trong quá trình tạo video: {str(e)}")
|
| 89 |
+
yield f"Lỗi: {str(e)}", None
|
| 90 |
+
return None, None
|
requirements.txt
ADDED
|
Binary file (214 Bytes). View file
|
|
|