Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,69 +1,61 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
from PIL import Image
|
| 3 |
-
import torch
|
| 4 |
-
from transformers import ViTFeatureExtractor, ViTForImageClassification
|
| 5 |
-
from accessories import recommend_accessories
|
| 6 |
-
|
| 7 |
-
# Load ViT Model for style classification
|
| 8 |
-
def load_model():
|
| 9 |
-
feature_extractor = ViTFeatureExtractor.from_pretrained("google/vit-base-patch16-224")
|
| 10 |
-
model = ViTForImageClassification.from_pretrained("google/vit-base-patch16-224")
|
| 11 |
-
return feature_extractor, model
|
| 12 |
-
|
| 13 |
-
extractor, model = load_model()
|
| 14 |
-
|
| 15 |
-
def analyze_style(image):
|
| 16 |
-
if image is None:
|
| 17 |
-
return "Please upload an image.", None, None
|
| 18 |
-
|
| 19 |
-
inputs = extractor(images=image, return_tensors="pt")
|
| 20 |
-
with torch.no_grad():
|
| 21 |
-
outputs = model(**inputs)
|
| 22 |
-
predicted_class = outputs.logits.argmax(-1).item()
|
| 23 |
-
style_name = model.config.id2label[predicted_class]
|
| 24 |
-
|
| 25 |
-
style_label = style_name.lower()
|
| 26 |
-
rec = recommend_accessories(style_label)
|
| 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 |
-
analyze_button
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
clear_btn.click(
|
| 64 |
-
fn=lambda: ("", "", None),
|
| 65 |
-
inputs=None,
|
| 66 |
-
outputs=[style_output, rec_output, image_input]
|
| 67 |
-
)
|
| 68 |
-
|
| 69 |
-
demo.launch()
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from PIL import Image
|
| 3 |
+
import torch
|
| 4 |
+
from transformers import ViTFeatureExtractor, ViTForImageClassification
|
| 5 |
+
from accessories import recommend_accessories
|
| 6 |
+
|
| 7 |
+
# Load ViT Model for style classification
|
| 8 |
+
def load_model():
|
| 9 |
+
feature_extractor = ViTFeatureExtractor.from_pretrained("google/vit-base-patch16-224")
|
| 10 |
+
model = ViTForImageClassification.from_pretrained("google/vit-base-patch16-224")
|
| 11 |
+
return feature_extractor, model
|
| 12 |
+
|
| 13 |
+
extractor, model = load_model()
|
| 14 |
+
|
| 15 |
+
def analyze_style(image):
|
| 16 |
+
if image is None:
|
| 17 |
+
return "Please upload an image.", None, None
|
| 18 |
+
|
| 19 |
+
inputs = extractor(images=image, return_tensors="pt")
|
| 20 |
+
with torch.no_grad():
|
| 21 |
+
outputs = model(**inputs)
|
| 22 |
+
predicted_class = outputs.logits.argmax(-1).item()
|
| 23 |
+
style_name = model.config.id2label[predicted_class]
|
| 24 |
+
|
| 25 |
+
style_label = style_name.lower()
|
| 26 |
+
rec = recommend_accessories(style_label)
|
| 27 |
+
|
| 28 |
+
# Return predicted style string, recommendation string, and the image
|
| 29 |
+
return f"**Predicted Style Class:** {style_name}", rec, image
|
| 30 |
+
|
| 31 |
+
# Gradio Interface
|
| 32 |
+
title = "π StyleGuru: AI-Enhanced Fashion Designer"
|
| 33 |
+
|
| 34 |
+
description = """
|
| 35 |
+
**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!
|
| 36 |
+
|
| 37 |
+
**How to use:**
|
| 38 |
+
1. Upload a clear image or sketch of a garment.
|
| 39 |
+
2. View the predicted style.
|
| 40 |
+
3. See recommended accessories and fabrics to enhance your design.
|
| 41 |
+
"""
|
| 42 |
+
|
| 43 |
+
with gr.Blocks() as demo:
|
| 44 |
+
gr.Markdown(f"# {title}")
|
| 45 |
+
gr.Markdown(description)
|
| 46 |
+
|
| 47 |
+
with gr.Row():
|
| 48 |
+
image_input = gr.Image(label="Upload a garment image or sketch", type="pil", tool="upload", interactive=True)
|
| 49 |
+
with gr.Column():
|
| 50 |
+
style_output = gr.Markdown(label="Style Analysis")
|
| 51 |
+
rec_output = gr.Markdown(label="π Accessory & Fabric Recommendation")
|
| 52 |
+
|
| 53 |
+
analyze_button = gr.Button("Analyze Style")
|
| 54 |
+
|
| 55 |
+
analyze_button.click(
|
| 56 |
+
fn=analyze_style,
|
| 57 |
+
inputs=image_input,
|
| 58 |
+
outputs=[style_output, rec_output, image_input],
|
| 59 |
+
)
|
| 60 |
+
|
| 61 |
+
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|