Spaces:
Sleeping
Sleeping
Update app.py
Browse files28 unmodified lines293031323233343526 unmodified lines6263646565666768694 unmodified lines747576767778798080818283841 unmodified line86878888899091919292939494959697985 unmodified lines1041051061061071081091091101111122 unmodified lines11511611711811912012112212312412512612712812913013113211811912012112212312412512612712812913013113213313413513613713813914014114214314414514614713728 unmodified lines# Embed a subset of dataset imagesimage_vectors = []image_indices = []N = 500 # You can increase this if performance is okayN = 500
for 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=10):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 lines
return generated_images
# Main function# Main recommendation functiondef recommend_from_upload(uploaded_image): uploaded_image = uploaded_image.convert("RGB")
# Check if the uploaded image already exists in the dataset # Check if the uploaded image exists in dataset closest_idx = None for i in range(len(image_indices)): dataset_image = dataset[image_indices[i]]['image'].convert("RGB")1 unmodified line closest_idx = i break
# Find 3 similar dataset items + get uploaded image embedding # Get 3 similar items + embedding similar_imgs, query_vec = find_similar(uploaded_image, top_k=3, exclude_index=closest_idx)
# Generate 10 new outfit images generated_imgs = generate_outfits(uploaded_image, n=10) # Generate 2 synthetic outfits generated_imgs = generate_outfits(uploaded_image, n=2)
# Find the most relevant generated image # Select most similar generated image best_score = -1 best_generated_img = None for img in generated_imgs:5 unmodified lines best_score = sim best_generated_img = img
# Final output: input + 3 similar + 1 most relevant generated return [uploaded_image] + similar_imgs + [best_generated_img]
# Example images (make sure they are in the root folder)# Example image paths (must exist in 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)# 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