aikanava commited on
Commit
942ade9
Β·
verified Β·
1 Parent(s): 0e7d289

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +61 -69
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
- return f"**Predicted Style Class:** {style_name}", rec, image
29
-
30
- title = "StyleCraft: AI-Enhanced Fashion Designer"
31
-
32
- description = """
33
- **StyleCraft** helps fashion enthusiasts and designers analyze garment styles and get accessory & fabric recommendations. Upload a photo or sketch, and let AI do the magic!
34
-
35
- **How to use:**
36
- 1. Upload a clear image or sketch of a garment.
37
- 2. View the predicted style.
38
- 3. See recommended accessories and fabrics to enhance your design.
39
- """
40
-
41
- with gr.Blocks() as demo:
42
- gr.Markdown(f"# {title}")
43
- gr.Markdown(description)
44
-
45
- with gr.Row():
46
- # Remove paste and webcam by using image upload only:
47
- image_input = gr.Image(label="Upload a garment image or sketch", type="pil",
48
- interactive=True, source="upload")
49
-
50
- with gr.Column():
51
- style_output = gr.Markdown(label="Style Analysis")
52
- rec_output = gr.Markdown(label="πŸ’ Accessory & Fabric Recommendation")
53
- clear_btn = gr.Button("Clear")
54
-
55
- analyze_button = gr.Button("Analyze Style")
56
-
57
- analyze_button.click(
58
- fn=analyze_style,
59
- inputs=image_input,
60
- outputs=[style_output, rec_output, image_input],
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()