Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -90,30 +90,101 @@ def validate_url(url):
|
|
| 90 |
except:
|
| 91 |
return False
|
| 92 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
def download_video_from_url(original_url):
|
| 94 |
-
"""
|
| 95 |
try:
|
| 96 |
if not validate_url(original_url):
|
| 97 |
raise ValueError("Invalid URL format")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 99 |
proxy_base = "https://9cee417c-5874-4e53-939a-52ad3f6f2f30-00-16i6nbwyeqga.picard.replit.dev/"
|
| 100 |
-
proxy_url = f"{proxy_base}?url={
|
| 101 |
|
| 102 |
-
#
|
| 103 |
try:
|
| 104 |
-
parsed_url = urlparse(
|
| 105 |
url_filename = os.path.basename(parsed_url.path)
|
| 106 |
if url_filename and '.' in url_filename:
|
| 107 |
-
|
| 108 |
-
temp_path = f"temp_{url_filename}"
|
| 109 |
else:
|
| 110 |
-
# Fallback to numbered temp file
|
| 111 |
import time
|
| 112 |
-
temp_path = f"
|
| 113 |
except:
|
| 114 |
-
|
|
|
|
| 115 |
|
| 116 |
-
print(f"DEBUG: Downloading
|
| 117 |
|
| 118 |
with requests.get(proxy_url, stream=True, timeout=30) as response:
|
| 119 |
response.raise_for_status()
|
|
@@ -122,8 +193,31 @@ def download_video_from_url(original_url):
|
|
| 122 |
if chunk:
|
| 123 |
f.write(chunk)
|
| 124 |
return temp_path
|
|
|
|
| 125 |
except Exception as e:
|
| 126 |
-
raise RuntimeError(f"Failed to download video: {e}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 127 |
|
| 128 |
# --- Global variables for toggling ---
|
| 129 |
current_video_file = None
|
|
@@ -311,7 +405,7 @@ def on_video_upload_change(video_file, use_blip):
|
|
| 311 |
return "uploaded_video", error_msg
|
| 312 |
|
| 313 |
def on_video_url_change(url, use_blip):
|
| 314 |
-
"""Handle URL input change with
|
| 315 |
global current_video_file, current_video_url, blip_generated_name, original_filename
|
| 316 |
|
| 317 |
if not url or url.strip() == "":
|
|
@@ -319,32 +413,43 @@ def on_video_url_change(url, use_blip):
|
|
| 319 |
current_video_url = None
|
| 320 |
blip_generated_name = ""
|
| 321 |
original_filename = ""
|
| 322 |
-
return None, "", "Enter a
|
| 323 |
|
| 324 |
try:
|
| 325 |
-
|
|
|
|
|
|
|
| 326 |
video_path = download_video_from_url(url)
|
| 327 |
|
| 328 |
# Store the current video info
|
| 329 |
current_video_file = None # Clear file when using URL
|
| 330 |
current_video_url = video_path
|
| 331 |
|
| 332 |
-
# Set original filename
|
| 333 |
try:
|
| 334 |
-
|
| 335 |
-
|
| 336 |
-
|
| 337 |
-
|
| 338 |
-
|
| 339 |
-
|
| 340 |
-
|
| 341 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 342 |
else:
|
| 343 |
original_filename = "downloaded_video"
|
|
|
|
| 344 |
except:
|
| 345 |
-
original_filename = "downloaded_video"
|
| 346 |
|
| 347 |
-
print(f"DEBUG:
|
| 348 |
|
| 349 |
blip_generated_name = ""
|
| 350 |
|
|
@@ -355,15 +460,16 @@ def on_video_url_change(url, use_blip):
|
|
| 355 |
if len(frames) > 0:
|
| 356 |
frame = frames[len(frames) // 2]
|
| 357 |
blip_generated_name = generate_blip_name(frame)
|
| 358 |
-
print(f"DEBUG:
|
| 359 |
except Exception as e:
|
| 360 |
print(f"BLIP naming failed: {e}")
|
| 361 |
blip_generated_name = ""
|
| 362 |
|
| 363 |
# Return appropriate name
|
| 364 |
final_name = blip_generated_name if (use_blip and blip_generated_name) else original_filename
|
| 365 |
-
|
| 366 |
-
|
|
|
|
| 367 |
|
| 368 |
except Exception as e:
|
| 369 |
error_msg = f"Download failed: {str(e)}"
|
|
@@ -456,7 +562,7 @@ with gr.Blocks(analytics_enabled=False, title="Video Depth Anything") as demo:
|
|
| 456 |
with gr.Row():
|
| 457 |
video_url = gr.Textbox(
|
| 458 |
label="🔗 Video URL",
|
| 459 |
-
placeholder="Paste video URL here...",
|
| 460 |
scale=4
|
| 461 |
)
|
| 462 |
use_blip = gr.Checkbox(
|
|
|
|
| 90 |
except:
|
| 91 |
return False
|
| 92 |
|
| 93 |
+
def detect_video_source(url):
|
| 94 |
+
"""Detect video source (MidJourney, Civitai, or unknown)"""
|
| 95 |
+
if "image.civitai.com" in url:
|
| 96 |
+
return "civitai"
|
| 97 |
+
elif "cdn.midjourney.com" in url or "midjourney" in url.lower():
|
| 98 |
+
return "midjourney"
|
| 99 |
+
else:
|
| 100 |
+
return "unknown"
|
| 101 |
+
|
| 102 |
+
def download_civitai_video(civitai_url):
|
| 103 |
+
"""Direct download for Civitai videos (no proxy needed)"""
|
| 104 |
+
try:
|
| 105 |
+
# Civitai videos können oft direkt geladen werden
|
| 106 |
+
headers = {
|
| 107 |
+
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36',
|
| 108 |
+
'Referer': 'https://civitai.com/',
|
| 109 |
+
'Accept': 'video/webm,video/mp4,video/*;q=0.9,*/*;q=0.8',
|
| 110 |
+
}
|
| 111 |
+
|
| 112 |
+
# Try direct download first
|
| 113 |
+
print(f"DEBUG: Trying direct download from Civitai: {civitai_url}")
|
| 114 |
+
|
| 115 |
+
response = requests.get(civitai_url, headers=headers, stream=True, timeout=30)
|
| 116 |
+
response.raise_for_status()
|
| 117 |
+
|
| 118 |
+
# Create filename based on URL
|
| 119 |
+
try:
|
| 120 |
+
parsed_url = urlparse(civitai_url)
|
| 121 |
+
# Extract filename from URL path
|
| 122 |
+
path_parts = parsed_url.path.split('/')
|
| 123 |
+
if len(path_parts) > 1:
|
| 124 |
+
# Get the last part that might be a filename
|
| 125 |
+
filename_part = path_parts[-1]
|
| 126 |
+
if '.' in filename_part:
|
| 127 |
+
temp_path = f"temp_civitai_{filename_part}"
|
| 128 |
+
else:
|
| 129 |
+
temp_path = f"temp_civitai_{int(time.time())}.webm"
|
| 130 |
+
else:
|
| 131 |
+
temp_path = f"temp_civitai_{int(time.time())}.webm"
|
| 132 |
+
except:
|
| 133 |
+
import time
|
| 134 |
+
temp_path = f"temp_civitai_{int(time.time())}.webm"
|
| 135 |
+
|
| 136 |
+
# Download the file
|
| 137 |
+
with open(temp_path, "wb") as f:
|
| 138 |
+
for chunk in response.iter_content(chunk_size=8192):
|
| 139 |
+
if chunk:
|
| 140 |
+
f.write(chunk)
|
| 141 |
+
|
| 142 |
+
print(f"DEBUG: Civitai video downloaded to: {temp_path}")
|
| 143 |
+
return temp_path
|
| 144 |
+
|
| 145 |
+
except Exception as e:
|
| 146 |
+
raise RuntimeError(f"Failed to download Civitai video: {e}")
|
| 147 |
+
|
| 148 |
def download_video_from_url(original_url):
|
| 149 |
+
"""Universal video downloader for MJ, Civitai, and others"""
|
| 150 |
try:
|
| 151 |
if not validate_url(original_url):
|
| 152 |
raise ValueError("Invalid URL format")
|
| 153 |
+
|
| 154 |
+
# Detect source and use appropriate method
|
| 155 |
+
source = detect_video_source(original_url)
|
| 156 |
+
print(f"DEBUG: Detected video source: {source}")
|
| 157 |
+
|
| 158 |
+
if source == "civitai":
|
| 159 |
+
return download_civitai_video(original_url)
|
| 160 |
+
elif source == "midjourney":
|
| 161 |
+
return download_midjourney_video(original_url)
|
| 162 |
+
else:
|
| 163 |
+
return download_generic_video(original_url)
|
| 164 |
|
| 165 |
+
except Exception as e:
|
| 166 |
+
raise RuntimeError(f"Failed to download video: {e}")
|
| 167 |
+
|
| 168 |
+
def download_midjourney_video(mj_url):
|
| 169 |
+
"""Download MidJourney videos via proxy"""
|
| 170 |
+
try:
|
| 171 |
proxy_base = "https://9cee417c-5874-4e53-939a-52ad3f6f2f30-00-16i6nbwyeqga.picard.replit.dev/"
|
| 172 |
+
proxy_url = f"{proxy_base}?url={mj_url}"
|
| 173 |
|
| 174 |
+
# Create filename
|
| 175 |
try:
|
| 176 |
+
parsed_url = urlparse(mj_url)
|
| 177 |
url_filename = os.path.basename(parsed_url.path)
|
| 178 |
if url_filename and '.' in url_filename:
|
| 179 |
+
temp_path = f"temp_mj_{url_filename}"
|
|
|
|
| 180 |
else:
|
|
|
|
| 181 |
import time
|
| 182 |
+
temp_path = f"temp_mj_{int(time.time())}.mp4"
|
| 183 |
except:
|
| 184 |
+
import time
|
| 185 |
+
temp_path = f"temp_mj_{int(time.time())}.mp4"
|
| 186 |
|
| 187 |
+
print(f"DEBUG: Downloading MJ video via proxy: {proxy_url}")
|
| 188 |
|
| 189 |
with requests.get(proxy_url, stream=True, timeout=30) as response:
|
| 190 |
response.raise_for_status()
|
|
|
|
| 193 |
if chunk:
|
| 194 |
f.write(chunk)
|
| 195 |
return temp_path
|
| 196 |
+
|
| 197 |
except Exception as e:
|
| 198 |
+
raise RuntimeError(f"Failed to download MJ video: {e}")
|
| 199 |
+
|
| 200 |
+
def download_generic_video(url):
|
| 201 |
+
"""Fallback for unknown video sources"""
|
| 202 |
+
try:
|
| 203 |
+
headers = {
|
| 204 |
+
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'
|
| 205 |
+
}
|
| 206 |
+
|
| 207 |
+
response = requests.get(url, headers=headers, stream=True, timeout=30)
|
| 208 |
+
response.raise_for_status()
|
| 209 |
+
|
| 210 |
+
import time
|
| 211 |
+
temp_path = f"temp_generic_{int(time.time())}.mp4"
|
| 212 |
+
|
| 213 |
+
with open(temp_path, "wb") as f:
|
| 214 |
+
for chunk in response.iter_content(chunk_size=8192):
|
| 215 |
+
if chunk:
|
| 216 |
+
f.write(chunk)
|
| 217 |
+
return temp_path
|
| 218 |
+
|
| 219 |
+
except Exception as e:
|
| 220 |
+
raise RuntimeError(f"Failed to download generic video: {e}")
|
| 221 |
|
| 222 |
# --- Global variables for toggling ---
|
| 223 |
current_video_file = None
|
|
|
|
| 405 |
return "uploaded_video", error_msg
|
| 406 |
|
| 407 |
def on_video_url_change(url, use_blip):
|
| 408 |
+
"""Handle URL input change with support for MJ and Civitai"""
|
| 409 |
global current_video_file, current_video_url, blip_generated_name, original_filename
|
| 410 |
|
| 411 |
if not url or url.strip() == "":
|
|
|
|
| 413 |
current_video_url = None
|
| 414 |
blip_generated_name = ""
|
| 415 |
original_filename = ""
|
| 416 |
+
return None, "", "Enter a video URL (MidJourney or Civitai supported)"
|
| 417 |
|
| 418 |
try:
|
| 419 |
+
source = detect_video_source(url)
|
| 420 |
+
print(f"Downloading {source} video from URL: {url}")
|
| 421 |
+
|
| 422 |
video_path = download_video_from_url(url)
|
| 423 |
|
| 424 |
# Store the current video info
|
| 425 |
current_video_file = None # Clear file when using URL
|
| 426 |
current_video_url = video_path
|
| 427 |
|
| 428 |
+
# Set original filename based on source
|
| 429 |
try:
|
| 430 |
+
if source == "civitai":
|
| 431 |
+
# Extract filename from Civitai URL
|
| 432 |
+
parsed_url = urlparse(url)
|
| 433 |
+
path_parts = parsed_url.path.split('/')
|
| 434 |
+
# Look for meaningful filename in path
|
| 435 |
+
for part in reversed(path_parts):
|
| 436 |
+
if part and '.' not in part and len(part) > 3:
|
| 437 |
+
cleaned = "".join(c for c in part if c.isalnum() or c in "_-")[:20]
|
| 438 |
+
if cleaned:
|
| 439 |
+
original_filename = f"civitai_{cleaned}"
|
| 440 |
+
break
|
| 441 |
+
else:
|
| 442 |
+
original_filename = "civitai_video"
|
| 443 |
+
|
| 444 |
+
elif source == "midjourney":
|
| 445 |
+
original_filename = "midjourney_video"
|
| 446 |
else:
|
| 447 |
original_filename = "downloaded_video"
|
| 448 |
+
|
| 449 |
except:
|
| 450 |
+
original_filename = f"{source}_video" if source != "unknown" else "downloaded_video"
|
| 451 |
|
| 452 |
+
print(f"DEBUG: {source.title()} original filename set to: '{original_filename}'")
|
| 453 |
|
| 454 |
blip_generated_name = ""
|
| 455 |
|
|
|
|
| 460 |
if len(frames) > 0:
|
| 461 |
frame = frames[len(frames) // 2]
|
| 462 |
blip_generated_name = generate_blip_name(frame)
|
| 463 |
+
print(f"DEBUG: {source.title()} BLIP name generated: '{blip_generated_name}'")
|
| 464 |
except Exception as e:
|
| 465 |
print(f"BLIP naming failed: {e}")
|
| 466 |
blip_generated_name = ""
|
| 467 |
|
| 468 |
# Return appropriate name
|
| 469 |
final_name = blip_generated_name if (use_blip and blip_generated_name) else original_filename
|
| 470 |
+
success_msg = f"✅ {source.title()} video downloaded successfully!"
|
| 471 |
+
print(f"DEBUG: {source.title()} final name returned: '{final_name}' (BLIP: {use_blip})")
|
| 472 |
+
return video_path, final_name, success_msg
|
| 473 |
|
| 474 |
except Exception as e:
|
| 475 |
error_msg = f"Download failed: {str(e)}"
|
|
|
|
| 562 |
with gr.Row():
|
| 563 |
video_url = gr.Textbox(
|
| 564 |
label="🔗 Video URL",
|
| 565 |
+
placeholder="Paste MidJourney or Civitai video URL here...",
|
| 566 |
scale=4
|
| 567 |
)
|
| 568 |
use_blip = gr.Checkbox(
|