Spaces:
Sleeping
Sleeping
File size: 2,234 Bytes
942ade9 ec75b3b 942ade9 ec75b3b 942ade9 ec75b3b |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
import gradio as gr
from PIL import Image
import torch
from transformers import ViTFeatureExtractor, ViTForImageClassification
from accessories import recommend_accessories
# Load ViT Model for style classification
def load_model():
feature_extractor = ViTFeatureExtractor.from_pretrained("google/vit-base-patch16-224")
model = ViTForImageClassification.from_pretrained("google/vit-base-patch16-224")
return feature_extractor, model
extractor, model = load_model()
def analyze_style(image):
if image is None:
return "Please upload an image.", None, None
inputs = extractor(images=image, return_tensors="pt")
with torch.no_grad():
outputs = model(**inputs)
predicted_class = outputs.logits.argmax(-1).item()
style_name = model.config.id2label[predicted_class]
style_label = style_name.lower()
rec = recommend_accessories(style_label)
# Return predicted style string, recommendation string, and the image
return f"**Predicted Style Class:** {style_name}", rec, image
title = "π StyleGuru: AI-Enhanced Fashion Designer"
description = """
**StyleGuru** helps fashion enthusiasts and designers analyze garment styles and get accessory & fabric recommendations. Upload a photo or sketch, and let AI do the magic!
**How to use:**
1. Upload a clear image or sketch of a garment.
2. View the predicted style.
3. See recommended accessories and fabrics to enhance your design.
"""
with gr.Blocks() as demo:
# CSS to hide webcam and paste buttons
gr.HTML("""
<style>
button[data-testid="paste-button"],
button[data-testid="webcam-button"] {
display: none !important;
}
</style>
""")
gr.Markdown(f"# {title}")
gr.Markdown(description)
with gr.Row():
image_input = gr.Image(label="Upload a garment image or sketch", type="pil")
with gr.Column():
style_output = gr.Markdown(label="Style Analysis")
rec_output = gr.Markdown(label="π Accessory & Fabric Recommendation")
analyze_button = gr.Button("Analyze Style")
analyze_button.click(
fn=analyze_style,
inputs=image_input,
outputs=[style_output, rec_output, image_input],
)
demo.launch() |