Spaces:
Runtime error
Runtime error
動画生成タスクの進捗をポーリングするメソッドをKlingAPIクラスに追加し、動画ダウンロード時のエラーハンドリングを強化しました。また、generate_loop_video関数内で使用するモデルを指定するように変更しました。
Browse files- app.py +6 -1
- utils/kling_api.py +32 -1
app.py
CHANGED
|
@@ -198,6 +198,7 @@ async def generate_loop_video(
|
|
| 198 |
task_id = await kling_api.create_video_task(
|
| 199 |
image_path=current_image_path,
|
| 200 |
prompt=clip_prompt,
|
|
|
|
| 201 |
duration=clip_duration,
|
| 202 |
tail_image_path=tail_image_path
|
| 203 |
)
|
|
@@ -206,7 +207,11 @@ async def generate_loop_video(
|
|
| 206 |
video_url = await kling_api.poll_video_result(task_id, progress, progress_val, 0.7 / num_clips)
|
| 207 |
|
| 208 |
# 動画をダウンロード
|
| 209 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 210 |
video_paths.append(clip_path)
|
| 211 |
temp_manager.add_temp_file(clip_path)
|
| 212 |
|
|
|
|
| 198 |
task_id = await kling_api.create_video_task(
|
| 199 |
image_path=current_image_path,
|
| 200 |
prompt=clip_prompt,
|
| 201 |
+
model="kling-v1-6",
|
| 202 |
duration=clip_duration,
|
| 203 |
tail_image_path=tail_image_path
|
| 204 |
)
|
|
|
|
| 207 |
video_url = await kling_api.poll_video_result(task_id, progress, progress_val, 0.7 / num_clips)
|
| 208 |
|
| 209 |
# 動画をダウンロード
|
| 210 |
+
clip_output_path = temp_manager.get_temp_path(f"_clip_{i+1}.mp4")
|
| 211 |
+
success = await kling_api.download_video(video_url, clip_output_path)
|
| 212 |
+
if not success:
|
| 213 |
+
raise Exception("動画ダウンロードに失敗しました")
|
| 214 |
+
clip_path = clip_output_path
|
| 215 |
video_paths.append(clip_path)
|
| 216 |
temp_manager.add_temp_file(clip_path)
|
| 217 |
|
utils/kling_api.py
CHANGED
|
@@ -702,4 +702,35 @@ class KlingAPI:
|
|
| 702 |
|
| 703 |
async def close(self):
|
| 704 |
"""クライアントを閉じる"""
|
| 705 |
-
await self.client.aclose()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 702 |
|
| 703 |
async def close(self):
|
| 704 |
"""クライアントを閉じる"""
|
| 705 |
+
await self.client.aclose()
|
| 706 |
+
|
| 707 |
+
# === 追加: 動画タスク結果をポーリング(進捗バー連携) ===
|
| 708 |
+
async def poll_video_result(
|
| 709 |
+
self,
|
| 710 |
+
task_id: str,
|
| 711 |
+
progress_callback=None,
|
| 712 |
+
base_progress: float = 0.0,
|
| 713 |
+
progress_delta: float = 0.2,
|
| 714 |
+
max_wait_minutes: int = 60
|
| 715 |
+
) -> Optional[str]:
|
| 716 |
+
"""動画生成タスクの完了を待機してURLを返す
|
| 717 |
+
progress_callback には gr.Progress インスタンスを渡すことを想定
|
| 718 |
+
base_progress はポーリング開始時点の進捗値、
|
| 719 |
+
progress_delta は完了時に上乗せする進捗幅"""
|
| 720 |
+
try:
|
| 721 |
+
# ポーリング開始を通知
|
| 722 |
+
if progress_callback:
|
| 723 |
+
progress_callback(base_progress, "動画生成を待機中...")
|
| 724 |
+
|
| 725 |
+
video_url = await self.poll_task_result(task_id, max_wait_minutes=max_wait_minutes)
|
| 726 |
+
|
| 727 |
+
# 完了を通知(進捗を一気に上げる)
|
| 728 |
+
if progress_callback:
|
| 729 |
+
progress_callback(min(base_progress + progress_delta, 1.0), "動画生成完了")
|
| 730 |
+
|
| 731 |
+
return video_url
|
| 732 |
+
except Exception as e:
|
| 733 |
+
# エラー時にも進捗を更新
|
| 734 |
+
if progress_callback:
|
| 735 |
+
progress_callback(base_progress, "動画生成エラー")
|
| 736 |
+
raise
|