Update a.py
Browse files
a.py
CHANGED
|
@@ -100,18 +100,52 @@ def generate_video(image_path, prompt_text, progress=gr.Progress()):
|
|
| 100 |
print(f"Task created: {task_id}")
|
| 101 |
yield f"✅ Task created: {task_id}", None
|
| 102 |
|
| 103 |
-
progress(0.3, desc="Polling
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
except Exception as e:
|
| 108 |
-
|
| 109 |
-
if "Connection error" in str(e):
|
| 110 |
-
yield "❌ Connection Error: Cannot reach BytePlus API. This is likely a Hugging Face network restriction. Try running locally instead.", None
|
| 111 |
-
else:
|
| 112 |
-
yield f"❌ Error: {str(e)}", None
|
| 113 |
|
| 114 |
-
# Add this new function for JSON polling fallback
|
| 115 |
def poll_via_json(task_id):
|
| 116 |
"""Fallback method: poll io.json for results"""
|
| 117 |
json_url = "https://1hit.no/proxy/io.json"
|
|
@@ -128,9 +162,65 @@ def poll_via_json(task_id):
|
|
| 128 |
pass
|
| 129 |
return None
|
| 130 |
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 134 |
# Poll for results - TRY SDK FIRST, then fallback to JSON
|
| 135 |
attempts = 0
|
| 136 |
max_attempts = 120
|
|
@@ -176,6 +266,15 @@ def poll_via_json(task_id):
|
|
| 176 |
yield f"⏳ Status: processing... (JSON fallback)", None
|
| 177 |
time.sleep(5) # Poll JSON less frequently
|
| 178 |
attempts += 1
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 179 |
|
| 180 |
def update_prompt(choice):
|
| 181 |
"""Update prompt when shot type changes"""
|
|
|
|
| 100 |
print(f"Task created: {task_id}")
|
| 101 |
yield f"✅ Task created: {task_id}", None
|
| 102 |
|
| 103 |
+
progress(0.3, desc="Polling fimport os
|
| 104 |
+
import time
|
| 105 |
+
import gradio as gr
|
| 106 |
+
from byteplussdkarkruntime import Ark
|
| 107 |
+
import httpx
|
| 108 |
+
import socket
|
| 109 |
+
import requests # Added for JSON polling
|
| 110 |
+
|
| 111 |
+
# Get API key from Hugging Face secret "Key"
|
| 112 |
+
API_KEY = os.environ.get("Key", "")
|
| 113 |
+
#base_url="https://ark.ap-southeast.bytepluses.com/api/v3"
|
| 114 |
+
# Initialize client with better timeout and retry settings
|
| 115 |
+
client = Ark(
|
| 116 |
+
base_url="https://1hit.no/proxy/proxy.php",
|
| 117 |
+
api_key=API_KEY,
|
| 118 |
+
timeout=30.0, # Shorter timeout to fail faster
|
| 119 |
+
max_retries=3,
|
| 120 |
+
)
|
| 121 |
+
|
| 122 |
+
# Fresh new prompts
|
| 123 |
+
DEFAULT_PROMPTS = {
|
| 124 |
+
"Cinematic Nature": "Majestic drone shot soaring above misty mountains at golden hour, sunlight breaking through clouds, cinematic 4k quality, smooth motion --duration 5 --camerafixed false",
|
| 125 |
+
|
| 126 |
+
"Urban Exploration": "Dynamic drone flight through narrow alleyways of a neon-lit Tokyo street at night, rain-slicked surfaces reflecting lights, cyberpunk aesthetic --duration 5 --camerafixed false",
|
| 127 |
+
|
| 128 |
+
"Action Sequence": "High-speed drone chasing a sports car through a winding coastal road, dramatic cliffside views, action movie style --duration 5 --camerafixed false",
|
| 129 |
+
|
| 130 |
+
"Abstract Art": "Surreal drone flight through floating geometric shapes in a dreamlike void, pastel colors, ethereal atmosphere --duration 5 --camerafixed false",
|
| 131 |
+
|
| 132 |
+
"Wildlife Documentary": "Drone following a herd of elephants across the African savanna at sunset, National Geographic style, majestic wildlife cinematography --duration 5 --camerafixed false",
|
| 133 |
+
|
| 134 |
+
"Sports Highlight": "Drone racing alongside professional skiers carving through fresh powder in the Alps, high-energy sports broadcast style --duration 5 --camerafixed false",
|
| 135 |
+
|
| 136 |
+
"Beach Paradise": "Drone gliding over turquoise waters and white sand beaches, palm trees swaying, tropical paradise aerial view --duration 5 --camerafixed false",
|
| 137 |
+
|
| 138 |
+
"Ancient Temple": "Drone circling around ancient Angkor Wat temple at dawn, mystical atmosphere, historical documentary style --duration 5 --camerafixed false"
|
| 139 |
+
}
|
| 140 |
+
|
| 141 |
+
def test_connection():
|
| 142 |
+
"""Test if we can reach the API before trying"""
|
| 143 |
+
try:
|
| 144 |
+
socket.gethostbyname('ark.ap-southeast.bytepluses.com')
|
| 145 |
+
return True, "DNS resolved"
|
| 146 |
except Exception as e:
|
| 147 |
+
return False, f"DNS failed: {e}"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 148 |
|
|
|
|
| 149 |
def poll_via_json(task_id):
|
| 150 |
"""Fallback method: poll io.json for results"""
|
| 151 |
json_url = "https://1hit.no/proxy/io.json"
|
|
|
|
| 162 |
pass
|
| 163 |
return None
|
| 164 |
|
| 165 |
+
def generate_video(image_path, prompt_text, progress=gr.Progress()):
|
| 166 |
+
"""Generate video with robust error handling"""
|
| 167 |
+
|
| 168 |
+
# Test connection first
|
| 169 |
+
conn_ok, conn_msg = test_connection()
|
| 170 |
+
if not conn_ok:
|
| 171 |
+
yield f"❌ Connection Error: {conn_msg}. This might be a network restriction from Hugging Face.", None
|
| 172 |
+
return
|
| 173 |
+
|
| 174 |
+
if not API_KEY:
|
| 175 |
+
yield "❌ API Key not configured. Please add 'Key' secret.", None
|
| 176 |
+
return
|
| 177 |
+
|
| 178 |
+
if image_path is None:
|
| 179 |
+
yield "⚠️ Please upload an image first", None
|
| 180 |
+
return
|
| 181 |
+
|
| 182 |
+
try:
|
| 183 |
+
progress(0, desc="Preparing image...")
|
| 184 |
+
|
| 185 |
+
# Get HF temp URL
|
| 186 |
+
image_url = f"/file={image_path}"
|
| 187 |
+
print(f"Using image URL: {image_url}")
|
| 188 |
+
|
| 189 |
+
yield "✅ Image ready! Creating video request...", None
|
| 190 |
+
|
| 191 |
+
progress(0.2, desc="Creating request...")
|
| 192 |
+
|
| 193 |
+
# Create task with timeout
|
| 194 |
+
try:
|
| 195 |
+
create_result = client.content_generation.tasks.create(
|
| 196 |
+
model="seedance-1-5-pro-251215",
|
| 197 |
+
content=[
|
| 198 |
+
{
|
| 199 |
+
"type": "text",
|
| 200 |
+
"text": prompt_text
|
| 201 |
+
},
|
| 202 |
+
{
|
| 203 |
+
"type": "image_url",
|
| 204 |
+
"image_url": {
|
| 205 |
+
"url": image_url
|
| 206 |
+
}
|
| 207 |
+
}
|
| 208 |
+
]
|
| 209 |
+
)
|
| 210 |
+
except Exception as e:
|
| 211 |
+
error_str = str(e)
|
| 212 |
+
if "Connection error" in error_str or "202602" in error_str:
|
| 213 |
+
yield f"❌ Network Error: Cannot reach BytePlus API. This is likely a Hugging Face network restriction.", None
|
| 214 |
+
else:
|
| 215 |
+
yield f"❌ API Error: {error_str}", None
|
| 216 |
+
return
|
| 217 |
+
|
| 218 |
+
task_id = create_result.id
|
| 219 |
+
print(f"Task created: {task_id}")
|
| 220 |
+
yield f"✅ Task created: {task_id}", None
|
| 221 |
+
|
| 222 |
+
progress(0.3, desc="Polling for results...")
|
| 223 |
+
|
| 224 |
# Poll for results - TRY SDK FIRST, then fallback to JSON
|
| 225 |
attempts = 0
|
| 226 |
max_attempts = 120
|
|
|
|
| 266 |
yield f"⏳ Status: processing... (JSON fallback)", None
|
| 267 |
time.sleep(5) # Poll JSON less frequently
|
| 268 |
attempts += 1
|
| 269 |
+
|
| 270 |
+
yield "⏰ Timeout after 2 minutes", None
|
| 271 |
+
|
| 272 |
+
except Exception as e:
|
| 273 |
+
print(f"Error: {e}")
|
| 274 |
+
if "Connection error" in str(e):
|
| 275 |
+
yield "❌ Connection Error: Cannot reach BytePlus API. This is likely a Hugging Face network restriction. Try running locally instead.", None
|
| 276 |
+
else:
|
| 277 |
+
yield f"❌ Error: {str(e)}", None
|
| 278 |
|
| 279 |
def update_prompt(choice):
|
| 280 |
"""Update prompt when shot type changes"""
|