Spaces:
Runtime error
Runtime error
Download app.py from AltrWeddings/ThemeClassifier: direct link, hf CLI and curl.
- Browser
- Download file 2.25 kB
-
https://huggingface.co/spaces/AltrWeddings/ThemeClassifier/resolve/main/app.py
- Command line
-
hf download hf://spaces/AltrWeddings/ThemeClassifier/app.py
-
curl -L -o app.py https://huggingface.co/spaces/AltrWeddings/ThemeClassifier/resolve/main/app.py
2.25 kB
| import streamlit as st | |
| import streamlit.components.v1 | |
| from transformers import CLIPProcessor, CLIPModel | |
| from PIL import Image | |
| import numpy as np | |
| import pandas as pd | |
| USE_DESCRIPTION_AS_LABEL = False | |
| NUM_OTHER_TYPES = 4 | |
| MAX_FILES = 3 | |
| def _get_probs(image, text): | |
| inputs = processor(text=text, images=image, return_tensors="pt", padding=True) | |
| outputs = model(**inputs) | |
| logits_per_image = outputs.logits_per_image | |
| probs = logits_per_image.softmax(dim=1).detach().numpy().flatten() | |
| return probs | |
| smaller_model = "openai/clip-vit-base-patch32" | |
| model = CLIPModel.from_pretrained(smaller_model) | |
| processor = CLIPProcessor.from_pretrained(smaller_model) | |
| themes = pd.read_csv("wedding_themes.csv") | |
| themes = themes.query("use=='Y'") | |
| st.title("What's your wedding persona?") | |
| st.subheader("") | |
| filenames = st.file_uploader(f"Modern chic? Fall foliage? Upload up to {MAX_FILES} photos that have aesthetics that you love (photos don't have to be taken at a wedding).", | |
| accept_multiple_files=True) | |
| if len(filenames) > MAX_FILES: | |
| st.warning(f"Only {MAX_FILES} images are allowed!") | |
| if 0 < len(filenames) <= MAX_FILES and st.button("Process"): | |
| labels = themes.theme.tolist() | |
| descriptions = themes.description.tolist() | |
| all_probs = [] | |
| images = [] | |
| for filename in filenames: | |
| image = Image.open(filename) | |
| if USE_DESCRIPTION_AS_LABEL: | |
| probs = _get_probs(image, descriptions) | |
| else: | |
| labels_with_wedding = [(x + " wedding").lower() for x in labels] | |
| probs = _get_probs(image, labels_with_wedding) | |
| all_probs.append(probs) | |
| images.append(image) | |
| # Calculate the total probability and write results. | |
| combined_probs = np.sum(all_probs, axis=0) | |
| sorted_args = np.argsort(combined_probs)[::-1][:(1+NUM_OTHER_TYPES)].tolist() | |
| top_labels = np.array(labels)[sorted_args].tolist() | |
| top_description = descriptions[sorted_args[0]] | |
| st.subheader(f"Your result: {top_labels[0]}") | |
| st.write(f"{top_description} \n\n Other personas you might like: {', '.join(top_labels[1:])}") | |
| # Display the uploaded images. | |
| cols = st.columns(len(filenames)) | |
| for col, image in zip(cols, images): | |
| col.image(image) | |