Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -202,20 +202,21 @@ def download_file_with_progress(url, dest, progress: Progress = None, progress_o
|
|
| 202 |
downloaded = 0
|
| 203 |
start_time = time.time()
|
| 204 |
last_update_time = start_time
|
|
|
|
| 205 |
|
| 206 |
if progress and total:
|
| 207 |
-
|
|
|
|
| 208 |
|
| 209 |
with open(dest, 'wb') as f:
|
| 210 |
-
for chunk in r.iter_content(chunk_size=
|
| 211 |
if chunk:
|
| 212 |
f.write(chunk)
|
| 213 |
downloaded += len(chunk)
|
| 214 |
current_time = time.time()
|
| 215 |
|
| 216 |
-
# Update progress
|
| 217 |
-
if progress and total and (current_time - last_update_time) >=
|
| 218 |
-
# Calculate progress within the allocated range
|
| 219 |
file_progress = downloaded / total
|
| 220 |
overall_progress = progress_offset + (file_progress * progress_scale)
|
| 221 |
|
|
@@ -225,14 +226,13 @@ def download_file_with_progress(url, dest, progress: Progress = None, progress_o
|
|
| 225 |
speed_bps = downloaded / elapsed_time
|
| 226 |
speed_mbps = speed_bps / (1024 * 1024)
|
| 227 |
|
| 228 |
-
# Format speed appropriately
|
| 229 |
if speed_mbps >= 1:
|
| 230 |
-
speed_str = f"{speed_mbps:.1f}
|
| 231 |
else:
|
| 232 |
speed_kbps = speed_bps / 1024
|
| 233 |
-
speed_str = f"{speed_kbps:.
|
| 234 |
else:
|
| 235 |
-
speed_str = "
|
| 236 |
|
| 237 |
# Calculate ETA
|
| 238 |
if speed_bps > 0:
|
|
@@ -242,15 +242,16 @@ def download_file_with_progress(url, dest, progress: Progress = None, progress_o
|
|
| 242 |
eta_str = f"{eta_seconds:.0f}s"
|
| 243 |
else:
|
| 244 |
eta_minutes = eta_seconds / 60
|
| 245 |
-
eta_str = f"{eta_minutes:.1f}
|
| 246 |
else:
|
| 247 |
-
eta_str = "
|
| 248 |
|
| 249 |
mb_downloaded = downloaded // 1024 // 1024
|
| 250 |
mb_total = total // 1024 // 1024
|
| 251 |
|
|
|
|
| 252 |
progress(overall_progress,
|
| 253 |
-
desc=f"
|
| 254 |
|
| 255 |
last_update_time = current_time
|
| 256 |
|
|
@@ -258,11 +259,14 @@ def download_file_with_progress(url, dest, progress: Progress = None, progress_o
|
|
| 258 |
final_time = time.time()
|
| 259 |
total_time = final_time - start_time
|
| 260 |
avg_speed = (downloaded / total_time) / (1024 * 1024) if total_time > 0 else 0
|
|
|
|
| 261 |
progress(progress_offset + progress_scale,
|
| 262 |
-
desc=f"✅
|
| 263 |
|
| 264 |
return True, ""
|
| 265 |
except Exception as e:
|
|
|
|
|
|
|
| 266 |
return False, str(e)
|
| 267 |
|
| 268 |
def file_similarity(file1, file2, chunk_size=1024*1024):
|
|
@@ -376,7 +380,7 @@ with gr.Blocks(theme="soft") as demo:
|
|
| 376 |
if not m1 or not m2 or not fname:
|
| 377 |
return "Please provide both model IDs and the file name."
|
| 378 |
|
| 379 |
-
progress(0, desc="Initializing...")
|
| 380 |
|
| 381 |
url1 = f"https://huggingface.co/{m1}/resolve/main/{fname}?download=true"
|
| 382 |
url2 = f"https://huggingface.co/{m2}/resolve/main/{fname}?download=true"
|
|
@@ -385,34 +389,48 @@ with gr.Blocks(theme="soft") as demo:
|
|
| 385 |
f1 = os.path.join(tmp, f"model1_{fname}")
|
| 386 |
f2 = os.path.join(tmp, f"model2_{fname}")
|
| 387 |
|
| 388 |
-
# Download first file (
|
| 389 |
-
progress(0.05, desc=f"
|
| 390 |
-
ok1, err1 = download_file_with_progress(url1, f1, progress, progress_offset=0.05, progress_scale=0.
|
| 391 |
|
| 392 |
if not ok1:
|
| 393 |
-
return f"Download
|
| 394 |
|
| 395 |
-
# Download second file (
|
| 396 |
-
progress(0.5, desc=f"
|
| 397 |
-
ok2, err2 = download_file_with_progress(url2, f2, progress, progress_offset=0.5, progress_scale=0.
|
| 398 |
|
| 399 |
if not ok2:
|
| 400 |
-
return f"Download
|
| 401 |
|
| 402 |
-
# Compare files (
|
| 403 |
-
progress(0.
|
| 404 |
percent, err = file_similarity(f1, f2)
|
| 405 |
if err:
|
| 406 |
-
return f"Comparison error: {err}"
|
| 407 |
|
| 408 |
-
progress(1.0, desc="
|
| 409 |
|
| 410 |
-
#
|
| 411 |
size1 = os.path.getsize(f1)
|
| 412 |
size2 = os.path.getsize(f2)
|
| 413 |
size_mb = size1 // 1024 // 1024
|
| 414 |
|
| 415 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 416 |
adv_btn.click(
|
| 417 |
fn=adv_compare,
|
| 418 |
inputs=[adv_model1, adv_model2, adv_filename],
|
|
|
|
| 202 |
downloaded = 0
|
| 203 |
start_time = time.time()
|
| 204 |
last_update_time = start_time
|
| 205 |
+
update_interval = 1.0 # Update every 1 second for HF Spaces compatibility
|
| 206 |
|
| 207 |
if progress and total:
|
| 208 |
+
mb_total = total // 1024 // 1024
|
| 209 |
+
progress(progress_offset, desc=f"🎯 Starting: {os.path.basename(dest)} ({mb_total}MB)")
|
| 210 |
|
| 211 |
with open(dest, 'wb') as f:
|
| 212 |
+
for chunk in r.iter_content(chunk_size=65536): # 64KB chunks for better performance on HF Spaces
|
| 213 |
if chunk:
|
| 214 |
f.write(chunk)
|
| 215 |
downloaded += len(chunk)
|
| 216 |
current_time = time.time()
|
| 217 |
|
| 218 |
+
# Update progress less frequently for HF Spaces
|
| 219 |
+
if progress and total and (current_time - last_update_time) >= update_interval:
|
|
|
|
| 220 |
file_progress = downloaded / total
|
| 221 |
overall_progress = progress_offset + (file_progress * progress_scale)
|
| 222 |
|
|
|
|
| 226 |
speed_bps = downloaded / elapsed_time
|
| 227 |
speed_mbps = speed_bps / (1024 * 1024)
|
| 228 |
|
|
|
|
| 229 |
if speed_mbps >= 1:
|
| 230 |
+
speed_str = f"{speed_mbps:.1f}MB/s"
|
| 231 |
else:
|
| 232 |
speed_kbps = speed_bps / 1024
|
| 233 |
+
speed_str = f"{speed_kbps:.0f}KB/s"
|
| 234 |
else:
|
| 235 |
+
speed_str = "calculating..."
|
| 236 |
|
| 237 |
# Calculate ETA
|
| 238 |
if speed_bps > 0:
|
|
|
|
| 242 |
eta_str = f"{eta_seconds:.0f}s"
|
| 243 |
else:
|
| 244 |
eta_minutes = eta_seconds / 60
|
| 245 |
+
eta_str = f"{eta_minutes:.1f}min"
|
| 246 |
else:
|
| 247 |
+
eta_str = "calculating..."
|
| 248 |
|
| 249 |
mb_downloaded = downloaded // 1024 // 1024
|
| 250 |
mb_total = total // 1024 // 1024
|
| 251 |
|
| 252 |
+
# Simplified progress message for HF Spaces
|
| 253 |
progress(overall_progress,
|
| 254 |
+
desc=f"⬇️ {mb_downloaded}/{mb_total}MB ({file_progress*100:.0f}%) • {speed_str} • ETA: {eta_str}")
|
| 255 |
|
| 256 |
last_update_time = current_time
|
| 257 |
|
|
|
|
| 259 |
final_time = time.time()
|
| 260 |
total_time = final_time - start_time
|
| 261 |
avg_speed = (downloaded / total_time) / (1024 * 1024) if total_time > 0 else 0
|
| 262 |
+
mb_total = total // 1024 // 1024
|
| 263 |
progress(progress_offset + progress_scale,
|
| 264 |
+
desc=f"✅ Complete: {mb_total}MB downloaded (avg {avg_speed:.1f}MB/s)")
|
| 265 |
|
| 266 |
return True, ""
|
| 267 |
except Exception as e:
|
| 268 |
+
if progress:
|
| 269 |
+
progress(progress_offset + progress_scale, desc=f"❌ Download failed: {str(e)[:50]}...")
|
| 270 |
return False, str(e)
|
| 271 |
|
| 272 |
def file_similarity(file1, file2, chunk_size=1024*1024):
|
|
|
|
| 380 |
if not m1 or not m2 or not fname:
|
| 381 |
return "Please provide both model IDs and the file name."
|
| 382 |
|
| 383 |
+
progress(0.0, desc="🚀 Initializing comparison...")
|
| 384 |
|
| 385 |
url1 = f"https://huggingface.co/{m1}/resolve/main/{fname}?download=true"
|
| 386 |
url2 = f"https://huggingface.co/{m2}/resolve/main/{fname}?download=true"
|
|
|
|
| 389 |
f1 = os.path.join(tmp, f"model1_{fname}")
|
| 390 |
f2 = os.path.join(tmp, f"model2_{fname}")
|
| 391 |
|
| 392 |
+
# Download first file (5% to 47.5%)
|
| 393 |
+
progress(0.05, desc=f"📡 Connecting to {m1.split('/')[-1]}...")
|
| 394 |
+
ok1, err1 = download_file_with_progress(url1, f1, progress, progress_offset=0.05, progress_scale=0.425)
|
| 395 |
|
| 396 |
if not ok1:
|
| 397 |
+
return f"❌ Download failed from {m1}: {err1}"
|
| 398 |
|
| 399 |
+
# Download second file (50% to 92.5%)
|
| 400 |
+
progress(0.5, desc=f"📡 Connecting to {m2.split('/')[-1]}...")
|
| 401 |
+
ok2, err2 = download_file_with_progress(url2, f2, progress, progress_offset=0.5, progress_scale=0.425)
|
| 402 |
|
| 403 |
if not ok2:
|
| 404 |
+
return f"❌ Download failed from {m2}: {err2}"
|
| 405 |
|
| 406 |
+
# Compare files (95% to 100%)
|
| 407 |
+
progress(0.95, desc="🔍 Analyzing files byte-by-byte...")
|
| 408 |
percent, err = file_similarity(f1, f2)
|
| 409 |
if err:
|
| 410 |
+
return f"❌ Comparison error: {err}"
|
| 411 |
|
| 412 |
+
progress(1.0, desc="✅ Analysis complete!")
|
| 413 |
|
| 414 |
+
# Get file info
|
| 415 |
size1 = os.path.getsize(f1)
|
| 416 |
size2 = os.path.getsize(f2)
|
| 417 |
size_mb = size1 // 1024 // 1024
|
| 418 |
|
| 419 |
+
# Enhanced result formatting
|
| 420 |
+
if percent == 100:
|
| 421 |
+
result_icon = "🟢"
|
| 422 |
+
result_text = "IDENTICAL"
|
| 423 |
+
elif percent >= 99:
|
| 424 |
+
result_icon = "🟡"
|
| 425 |
+
result_text = "NEARLY IDENTICAL"
|
| 426 |
+
elif percent >= 90:
|
| 427 |
+
result_icon = "🟠"
|
| 428 |
+
result_text = "SIMILAR"
|
| 429 |
+
else:
|
| 430 |
+
result_icon = "🔴"
|
| 431 |
+
result_text = "DIFFERENT"
|
| 432 |
+
|
| 433 |
+
return f"{result_icon} **{result_text}** ({percent:.3f}% similarity)\n📁 File size: {size_mb}MB\n🔗 Models: {m1.split('/')[-1]} vs {m2.split('/')[-1]}"
|
| 434 |
adv_btn.click(
|
| 435 |
fn=adv_compare,
|
| 436 |
inputs=[adv_model1, adv_model2, adv_filename],
|