Create final.py
Browse files
final.py
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
def generate_video(image_path, prompt_text, progress=gr.Progress()):
|
| 2 |
+
"""Generate video with proper polling"""
|
| 3 |
+
|
| 4 |
+
if not API_KEY:
|
| 5 |
+
yield "❌ API Key not configured. Please add 'Key' secret.", None
|
| 6 |
+
return
|
| 7 |
+
|
| 8 |
+
if image_path is None:
|
| 9 |
+
yield "⚠️ Please upload an image first", None
|
| 10 |
+
return
|
| 11 |
+
|
| 12 |
+
try:
|
| 13 |
+
progress(0, desc="Preparing image...")
|
| 14 |
+
|
| 15 |
+
# Get the Hugging Face temp URL for uploaded image
|
| 16 |
+
image_url = f"/file={image_path}"
|
| 17 |
+
print(f"Using uploaded image URL: {image_url}")
|
| 18 |
+
|
| 19 |
+
yield "✅ Image uploaded! Creating video request...", None
|
| 20 |
+
|
| 21 |
+
progress(0.2, desc="Creating request...")
|
| 22 |
+
|
| 23 |
+
# Create task
|
| 24 |
+
try:
|
| 25 |
+
create_result = client.content_generation.tasks.create(
|
| 26 |
+
model="seedance-1-5-pro-251215",
|
| 27 |
+
content=[
|
| 28 |
+
{"type": "text", "text": prompt_text},
|
| 29 |
+
{"type": "image_url", "image_url": {"url": image_url}}
|
| 30 |
+
]
|
| 31 |
+
)
|
| 32 |
+
except Exception as e:
|
| 33 |
+
yield f"❌ API Error: {str(e)}", None
|
| 34 |
+
return
|
| 35 |
+
|
| 36 |
+
task_id = create_result.id
|
| 37 |
+
print(f"Task created: {task_id}")
|
| 38 |
+
yield f"✅ Task created: {task_id}", None
|
| 39 |
+
|
| 40 |
+
progress(0.3, desc="Polling for results...")
|
| 41 |
+
|
| 42 |
+
# Poll for results - TRY SDK FIRST, then fallback to JSON
|
| 43 |
+
attempts = 0
|
| 44 |
+
max_attempts = 120
|
| 45 |
+
used_fallback = False
|
| 46 |
+
|
| 47 |
+
while attempts < max_attempts:
|
| 48 |
+
try:
|
| 49 |
+
# Try SDK method first
|
| 50 |
+
get_result = client.content_generation.tasks.get(task_id=task_id)
|
| 51 |
+
status = get_result.status
|
| 52 |
+
|
| 53 |
+
if status == "succeeded":
|
| 54 |
+
progress(1.0, desc="Complete!")
|
| 55 |
+
video_url = get_result.content.video_url if hasattr(get_result, 'content') else None
|
| 56 |
+
yield "✅ Video generated successfully!", video_url
|
| 57 |
+
return
|
| 58 |
+
|
| 59 |
+
elif status == "failed":
|
| 60 |
+
error_msg = get_result.error if hasattr(get_result, 'error') else "Unknown error"
|
| 61 |
+
yield f"❌ Failed: {error_msg}", None
|
| 62 |
+
return
|
| 63 |
+
else:
|
| 64 |
+
progress(0.3 + (attempts/max_attempts)*0.7, desc=f"Status: {status}")
|
| 65 |
+
yield f"⏳ Status: {status}... (attempt {attempts + 1})", None
|
| 66 |
+
time.sleep(2)
|
| 67 |
+
attempts += 1
|
| 68 |
+
|
| 69 |
+
except Exception as e:
|
| 70 |
+
# If SDK fails, use JSON fallback
|
| 71 |
+
if not used_fallback:
|
| 72 |
+
print("SDK polling failed, using JSON fallback...")
|
| 73 |
+
used_fallback = True
|
| 74 |
+
|
| 75 |
+
# Poll JSON every 5 seconds
|
| 76 |
+
result = poll_via_json(task_id)
|
| 77 |
+
if result == "FAILED":
|
| 78 |
+
yield "❌ Task failed (via JSON)", None
|
| 79 |
+
return
|
| 80 |
+
elif result and result != "PROCESSING":
|
| 81 |
+
yield "✅ Video generated successfully! (via JSON)", result
|
| 82 |
+
return
|
| 83 |
+
else:
|
| 84 |
+
progress(0.3 + (attempts/max_attempts)*0.7, desc="Status: processing (JSON)")
|
| 85 |
+
yield f"⏳ Status: processing... (JSON fallback, attempt {attempts + 1})", None
|
| 86 |
+
time.sleep(5)
|
| 87 |
+
attempts += 1
|
| 88 |
+
|
| 89 |
+
yield "⏰ Timeout after 2 minutes", None
|
| 90 |
+
|
| 91 |
+
except Exception as e:
|
| 92 |
+
yield f"❌ Error: {str(e)}", None
|