Spaces:
Running
Running
Update utils.py
Browse files
utils.py
CHANGED
|
@@ -2,6 +2,7 @@ import gradio as gr
|
|
| 2 |
from typing import Any, Dict
|
| 3 |
from urllib.request import urlopen, Request
|
| 4 |
from io import BytesIO
|
|
|
|
| 5 |
from PIL import Image
|
| 6 |
from functools import lru_cache
|
| 7 |
|
|
@@ -14,6 +15,36 @@ EXAMPLE_ITEMS = [
|
|
| 14 |
("https://img.freepik.com/premium-photo/portrait-beautiful-young-woman_1048944-5548042.jpg", "viddexa/nsfw-detection-nano"),
|
| 15 |
]
|
| 16 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
@lru_cache(maxsize=32)
|
| 19 |
def download_image(url: str) -> Image.Image:
|
|
@@ -23,6 +54,22 @@ def download_image(url: str) -> Image.Image:
|
|
| 23 |
return Image.open(BytesIO(resp.read())).convert("RGB")
|
| 24 |
|
| 25 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
def load_model(model_id: str, token: str | None = None) -> Any:
|
| 27 |
"""Load model with caching."""
|
| 28 |
if model_id not in _MODEL_CACHE:
|
|
|
|
| 2 |
from typing import Any, Dict
|
| 3 |
from urllib.request import urlopen, Request
|
| 4 |
from io import BytesIO
|
| 5 |
+
from pathlib import Path
|
| 6 |
from PIL import Image
|
| 7 |
from functools import lru_cache
|
| 8 |
|
|
|
|
| 15 |
("https://img.freepik.com/premium-photo/portrait-beautiful-young-woman_1048944-5548042.jpg", "viddexa/nsfw-detection-nano"),
|
| 16 |
]
|
| 17 |
|
| 18 |
+
CUSTOM_CSS = """
|
| 19 |
+
.header {
|
| 20 |
+
text-align: center;
|
| 21 |
+
padding: 2rem 1rem;
|
| 22 |
+
background: linear-gradient(135deg, #14b8a6 0%, #0d9488 100%);
|
| 23 |
+
color: white;
|
| 24 |
+
border-radius: 8px;
|
| 25 |
+
margin-bottom: 2rem;
|
| 26 |
+
}
|
| 27 |
+
.header h1 { margin: 0; font-size: 2.5em; }
|
| 28 |
+
.header p { margin: 0.5rem 0; opacity: 0.9; }
|
| 29 |
+
.header a { color: white; text-decoration: underline; }
|
| 30 |
+
|
| 31 |
+
/* Hide built-in Gradio footer */
|
| 32 |
+
footer {
|
| 33 |
+
display: none !important;
|
| 34 |
+
}
|
| 35 |
+
|
| 36 |
+
/* Custom footer styling */
|
| 37 |
+
.custom-footer {
|
| 38 |
+
text-align: center;
|
| 39 |
+
padding: 1rem;
|
| 40 |
+
color: #888;
|
| 41 |
+
margin-top: 2rem;
|
| 42 |
+
}
|
| 43 |
+
"""
|
| 44 |
+
|
| 45 |
+
EXAMPLES_DIR = Path("examples")
|
| 46 |
+
EXAMPLES_DIR.mkdir(exist_ok=True)
|
| 47 |
+
|
| 48 |
|
| 49 |
@lru_cache(maxsize=32)
|
| 50 |
def download_image(url: str) -> Image.Image:
|
|
|
|
| 54 |
return Image.open(BytesIO(resp.read())).convert("RGB")
|
| 55 |
|
| 56 |
|
| 57 |
+
def prepare_examples():
|
| 58 |
+
"""Download and prepare example images for display."""
|
| 59 |
+
examples = []
|
| 60 |
+
for i, (url, model) in enumerate(EXAMPLE_ITEMS):
|
| 61 |
+
img_path = EXAMPLES_DIR / f"example_{i}.jpg"
|
| 62 |
+
if not img_path.exists():
|
| 63 |
+
try:
|
| 64 |
+
img = download_image(url)
|
| 65 |
+
img.save(img_path, "JPEG", quality=95)
|
| 66 |
+
except Exception as e:
|
| 67 |
+
print(f"Warning: Could not download example {i}: {e}")
|
| 68 |
+
continue
|
| 69 |
+
examples.append([str(img_path), model])
|
| 70 |
+
return examples
|
| 71 |
+
|
| 72 |
+
|
| 73 |
def load_model(model_id: str, token: str | None = None) -> Any:
|
| 74 |
"""Load model with caching."""
|
| 75 |
if model_id not in _MODEL_CACHE:
|