Spaces:
Sleeping
Sleeping
Update app.py
Browse files28 unmodified lines2930313233343526 unmodified lines626364656667684 unmodified lines73747576777879808182831 unmodified line858687888990919293949596975 unmodified lines1031041051061071081091101111122 unmodified lines11511611711811912012112212312412512612712812913013113213313413513613728 unmodified lines# Embed a subset of dataset imagesimage_vectors = []image_indices = []N = 500 # You can increase this if performance is okayfor i in range(N):img = dataset[i]['image'].convert("RGB")26 unmodified lines).to(device)pipe.enable_attention_slicing()def generate_outfits(input_image, n=10):prompt = "fashion outfit design inspired by the clothing item"init_image = input_image.resize((512, 512))generated_images = []4 unmodified linesreturn generated_images# Main functiondef recommend_from_upload(uploaded_image):uploaded_image = uploaded_image.convert("RGB")# Check if the uploaded image already exists in the datasetclosest_idx = Nonefor i in range(len(image_indices)):dataset_image = dataset[image_indices[i]]['image'].convert("RGB")1 unmodified lineclosest_idx = ibreak# Find 3 similar dataset items + get uploaded image embeddingsimilar_imgs, query_vec = find_similar(uploaded_image, top_k=3, exclude_index=closest_idx)# Generate 10 new outfit imagesgenerated_imgs = generate_outfits(uploaded_image, n=10)# Find the most relevant generated imagebest_score = -1best_generated_img = Nonefor img in generated_imgs:5 unmodified linesbest_score = simbest_generated_img = img# Final output: input + 3 similar + 1 most relevant generatedreturn [uploaded_image] + similar_imgs + [best_generated_img]# Example images (make sure they are in the root folder)example_paths = [["example1.jpg"],["example2.jpg"],2 unmodified lines["example5.jpg"]]# Gradio Interfacedemo = gr.Interface(fn=recommend_from_upload,inputs=gr.Image(type="pil", label="Upload a clothing item"),outputs=[gr.Image(label="Your Input"),gr.Image(label="Similar Item 1"),gr.Image(label="Similar Item 2"),gr.Image(label="Similar Item 3"),gr.Image(label="Best AI-Generated Outfit")],title="👗 Fashion Outfit Recommender",description="Upload a clothing image to get 3 similar items from the dataset and 1 best AI-generated outfit design.",examples=example_paths)if __name__ == "__main__":demo.launch()28 unmodified lines2930313233343526 unmodified lines62636465666768694 unmodified lines74757677787980818283841 unmodified line868788899091929394959697985 unmodified lines1041051061071081091101111122 unmodified lines11511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614728 unmodified lines# Embed a subset of dataset imagesimage_vectors = []image_indices = []N = 500for i in range(N):img = dataset[i]['image'].convert("RGB")26 unmodified lines).to(device)pipe.enable_attention_slicing()# Generate outfits (2 only)def generate_outfits(input_image, n=2):prompt = "fashion outfit design inspired by the clothing item"init_image = input_image.resize((512, 512))generated_images = []4 unmodified linesreturn generated_images# Main recommendation functiondef recommend_from_upload(uploaded_image):uploaded_image = uploaded_image.convert("RGB")# Check if the uploaded image exists in datasetclosest_idx = Nonefor i in range(len(image_indices)):dataset_image = dataset[image_indices[i]]['image'].convert("RGB")1 unmodified lineclosest_idx = ibreak# Get 3 similar items + embeddingsimilar_imgs, query_vec = find_similar(uploaded_image, top_k=3, exclude_index=closest_idx)# Generate 2 synthetic outfitsgenerated_imgs = generate_outfits(uploaded_image, n=2)# Select most similar generated imagebest_score = -1best_generated_img = Nonefor img in generated_imgs:5 unmodified linesbest_score = simbest_generated_img = imgreturn [uploaded_image] + similar_imgs + [best_generated_img]# Example image paths (must exist in root folder)example_paths = [["example1.jpg"],["example2.jpg"],2 unmodified lines["example5.jpg"]]# Gradio Interface with buttonwith gr.Blocks() as demo:gr.Markdown("## 👗 Fashion Outfit Recommender")gr.Markdown("Upload a clothing image to get 3 similar items from the dataset and 1 AI-generated outfit design.")with gr.Row():image_input = gr.Image(type="pil", label="Upload a clothing item")generate_btn = gr.Button("Generate Recommendations")with gr.Row():output1 = gr.Image(label="Your Input")output2 = gr.Image(label="Similar Item 1")output3 = gr.Image(label="Similar Item 2")output4 = gr.Image(label="Similar Item 3")output5 = gr.Image(label="Best AI-Generated Outfit")examples = gr.Examples(examples=example_paths,inputs=image_input,label="Try an Example")generate_btn.click(fn=recommend_from_upload,inputs=image_input,outputs=[output1, output2, output3, output4, output5])if __name__ == "__main__":demo.launch()
app.py+36-26