Update app.py
Browse files
app.py
CHANGED
|
@@ -9,10 +9,11 @@ Deploy notes:
|
|
| 9 |
- Upload this file + requirements.txt + final_image_embeddings.npy + catalog_metadata.parquet
|
| 10 |
to your HF Space repo root.
|
| 11 |
- Update HF_DATASET_REPO / HF_WINNING_MODEL below if your repo names differ.
|
| 12 |
-
-
|
| 13 |
-
|
| 14 |
"""
|
| 15 |
|
|
|
|
| 16 |
import urllib.parse
|
| 17 |
|
| 18 |
import numpy as np
|
|
@@ -37,7 +38,6 @@ HF_DATASET_REPO = "lihicarmeli/fashion-stylist-multimodal-v2" # your HF datase
|
|
| 37 |
HF_WINNING_MODEL = "openai/clip-vit-base-patch32" # winning embedding model (Part 3)
|
| 38 |
EMBEDDINGS_FILE = "final_image_embeddings.npy" # uploaded next to this app.py
|
| 39 |
METADATA_FILE = "catalog_metadata.parquet" # uploaded next to this app.py
|
| 40 |
-
SAMPLE_PHOTOS = ["samples/demo_woman.jpg", "samples/demo_man.jpg", "samples/demo_teen.jpg"]
|
| 41 |
|
| 42 |
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
|
| 43 |
|
|
@@ -49,6 +49,30 @@ ds = load_dataset(HF_DATASET_REPO)
|
|
| 49 |
df = ds["train"].to_pandas()
|
| 50 |
images = [ds["train"][i]["image_improved"] for i in range(len(ds["train"]))]
|
| 51 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
print("Loading precomputed embeddings...")
|
| 53 |
image_embeddings = np.load(EMBEDDINGS_FILE).astype("float32")
|
| 54 |
|
|
@@ -432,4 +456,4 @@ with gr.Blocks(title="AI Fashion Stylist") as demo:
|
|
| 432 |
)
|
| 433 |
|
| 434 |
if __name__ == "__main__":
|
| 435 |
-
demo.launch()
|
|
|
|
| 9 |
- Upload this file + requirements.txt + final_image_embeddings.npy + catalog_metadata.parquet
|
| 10 |
to your HF Space repo root.
|
| 11 |
- Update HF_DATASET_REPO / HF_WINNING_MODEL below if your repo names differ.
|
| 12 |
+
- Quick Starter sample photos are generated automatically from the dataset at startup -
|
| 13 |
+
no manual file upload needed.
|
| 14 |
"""
|
| 15 |
|
| 16 |
+
import os
|
| 17 |
import urllib.parse
|
| 18 |
|
| 19 |
import numpy as np
|
|
|
|
| 38 |
HF_WINNING_MODEL = "openai/clip-vit-base-patch32" # winning embedding model (Part 3)
|
| 39 |
EMBEDDINGS_FILE = "final_image_embeddings.npy" # uploaded next to this app.py
|
| 40 |
METADATA_FILE = "catalog_metadata.parquet" # uploaded next to this app.py
|
|
|
|
| 41 |
|
| 42 |
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
|
| 43 |
|
|
|
|
| 49 |
df = ds["train"].to_pandas()
|
| 50 |
images = [ds["train"][i]["image_improved"] for i in range(len(ds["train"]))]
|
| 51 |
|
| 52 |
+
print("Generating Quick Starter sample photos from the dataset...")
|
| 53 |
+
SAMPLES_DIR = "samples_cache"
|
| 54 |
+
os.makedirs(SAMPLES_DIR, exist_ok=True)
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
def _pick_index(filter_fn, fallback_idx=0):
|
| 58 |
+
matches = df[df.apply(filter_fn, axis=1)]
|
| 59 |
+
return int(matches.index[0]) if len(matches) else fallback_idx
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
_sample_specs = [
|
| 63 |
+
("demo_woman.jpg", lambda r: r["gender"] == "woman"),
|
| 64 |
+
("demo_man.jpg", lambda r: r["gender"] == "man"),
|
| 65 |
+
("demo_teen.jpg", lambda r: r["age_group"] == "teen"),
|
| 66 |
+
]
|
| 67 |
+
|
| 68 |
+
SAMPLE_PHOTOS = []
|
| 69 |
+
for _filename, _filt in _sample_specs:
|
| 70 |
+
_idx = _pick_index(_filt, fallback_idx=len(SAMPLE_PHOTOS))
|
| 71 |
+
_path = os.path.join(SAMPLES_DIR, _filename)
|
| 72 |
+
images[_idx].convert("RGB").save(_path)
|
| 73 |
+
SAMPLE_PHOTOS.append(_path)
|
| 74 |
+
print(f" -> {len(SAMPLE_PHOTOS)} sample photos ready: {SAMPLE_PHOTOS}")
|
| 75 |
+
|
| 76 |
print("Loading precomputed embeddings...")
|
| 77 |
image_embeddings = np.load(EMBEDDINGS_FILE).astype("float32")
|
| 78 |
|
|
|
|
| 456 |
)
|
| 457 |
|
| 458 |
if __name__ == "__main__":
|
| 459 |
+
demo.launch()
|