Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,116 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import zipfile
|
| 3 |
+
import os
|
| 4 |
+
import requests
|
| 5 |
+
from pathlib import Path
|
| 6 |
+
|
| 7 |
+
# Configuration
|
| 8 |
+
# SECURE: Get the link from the environment variable "link"
|
| 9 |
+
ZIP_URL = os.environ.get("link")
|
| 10 |
+
ZIP_FILE = "consolidated_epstein_media.zip"
|
| 11 |
+
EXTRACT_PATH = "extracted_media"
|
| 12 |
+
|
| 13 |
+
def get_images():
|
| 14 |
+
"""Scans the extraction folder for images."""
|
| 15 |
+
image_extensions = {".jpg", ".jpeg", ".png", ".bmp", ".gif", ".webp"}
|
| 16 |
+
images = []
|
| 17 |
+
if os.path.exists(EXTRACT_PATH):
|
| 18 |
+
for root, dirs, files in os.walk(EXTRACT_PATH):
|
| 19 |
+
for file in files:
|
| 20 |
+
if Path(file).suffix.lower() in image_extensions:
|
| 21 |
+
images.append(os.path.join(root, file))
|
| 22 |
+
return sorted(images)
|
| 23 |
+
|
| 24 |
+
def process_files(progress=gr.Progress()):
|
| 25 |
+
"""Downloads and unzips the file with progress bars."""
|
| 26 |
+
|
| 27 |
+
# Security Check
|
| 28 |
+
if not ZIP_URL:
|
| 29 |
+
return [("Error: Secret 'link' not found. Please add it in Settings > Repository Secrets.", "Error")]
|
| 30 |
+
|
| 31 |
+
# 1. Download Phase
|
| 32 |
+
if not os.path.exists(ZIP_FILE):
|
| 33 |
+
print("Starting download...")
|
| 34 |
+
try:
|
| 35 |
+
response = requests.get(ZIP_URL, stream=True)
|
| 36 |
+
if response.status_code != 200:
|
| 37 |
+
return [f"Error downloading: HTTP {response.status_code}"]
|
| 38 |
+
|
| 39 |
+
total_size = int(response.headers.get('content-length', 0))
|
| 40 |
+
block_size = 1024 * 1024 # 1MB chunks
|
| 41 |
+
|
| 42 |
+
with open(ZIP_FILE, 'wb') as file:
|
| 43 |
+
downloaded = 0
|
| 44 |
+
for data in response.iter_content(block_size):
|
| 45 |
+
file.write(data)
|
| 46 |
+
downloaded += len(data)
|
| 47 |
+
if total_size > 0:
|
| 48 |
+
# Report download progress (0.0 to 0.5 of total bar)
|
| 49 |
+
percentage = (downloaded / total_size) * 0.5
|
| 50 |
+
progress(percentage, desc=f"Downloading zip... {int((downloaded/total_size)*100)}%")
|
| 51 |
+
except Exception as e:
|
| 52 |
+
return [f"Error downloading file: {str(e)}"]
|
| 53 |
+
|
| 54 |
+
# 2. Unzip Phase
|
| 55 |
+
if not os.path.exists(EXTRACT_PATH):
|
| 56 |
+
print("Starting unzip...")
|
| 57 |
+
try:
|
| 58 |
+
with zipfile.ZipFile(ZIP_FILE, 'r') as zip_ref:
|
| 59 |
+
file_list = zip_ref.namelist()
|
| 60 |
+
total_files = len(file_list)
|
| 61 |
+
|
| 62 |
+
os.makedirs(EXTRACT_PATH, exist_ok=True)
|
| 63 |
+
|
| 64 |
+
for index, file in enumerate(file_list):
|
| 65 |
+
if index % 10 == 0:
|
| 66 |
+
# Report unzip progress (0.5 to 1.0 of total bar)
|
| 67 |
+
percentage = 0.5 + ((index / total_files) * 0.5)
|
| 68 |
+
progress(percentage, desc=f"Unzipping files... {int((index/total_files)*100)}%")
|
| 69 |
+
|
| 70 |
+
zip_ref.extract(file, EXTRACT_PATH)
|
| 71 |
+
except zipfile.BadZipFile:
|
| 72 |
+
return ["Error: The downloaded file is not a valid zip archive."]
|
| 73 |
+
|
| 74 |
+
progress(1.0, desc="Done!")
|
| 75 |
+
return get_images()
|
| 76 |
+
|
| 77 |
+
# CSS for clean, sharp UI
|
| 78 |
+
custom_css = """
|
| 79 |
+
.gradio-container {
|
| 80 |
+
background-color: white !important;
|
| 81 |
+
}
|
| 82 |
+
/* Force sharp corners and black borders on images */
|
| 83 |
+
button.gallery-item {
|
| 84 |
+
border-radius: 0px !important;
|
| 85 |
+
border: 2px solid black !important;
|
| 86 |
+
overflow: hidden;
|
| 87 |
+
}
|
| 88 |
+
img {
|
| 89 |
+
border-radius: 0px !important;
|
| 90 |
+
}
|
| 91 |
+
/* Hide default rounded containers */
|
| 92 |
+
.block {
|
| 93 |
+
border: none !important;
|
| 94 |
+
background: transparent !important;
|
| 95 |
+
}
|
| 96 |
+
"""
|
| 97 |
+
|
| 98 |
+
with gr.Blocks(css=custom_css, title="Media Viewer") as demo:
|
| 99 |
+
gr.Markdown("### Consolidated Media Viewer")
|
| 100 |
+
|
| 101 |
+
# Gallery with lazy loading
|
| 102 |
+
gallery = gr.Gallery(
|
| 103 |
+
label="Media Gallery",
|
| 104 |
+
show_label=False,
|
| 105 |
+
columns=[4],
|
| 106 |
+
rows=[2],
|
| 107 |
+
object_fit="contain",
|
| 108 |
+
height="80vh",
|
| 109 |
+
interactive=False
|
| 110 |
+
)
|
| 111 |
+
|
| 112 |
+
# Trigger process on load
|
| 113 |
+
demo.load(process_files, inputs=None, outputs=gallery)
|
| 114 |
+
|
| 115 |
+
if __name__ == "__main__":
|
| 116 |
+
demo.queue().launch(server_name="0.0.0.0", server_port=7860)
|