Spaces:
Sleeping
Sleeping
Upload 3 files
Browse files- README.md +6 -6
- app.py +87 -0
- requirements.txt +4 -0
README.md
CHANGED
@@ -1,13 +1,13 @@
|
|
1 |
---
|
2 |
-
title:
|
3 |
-
emoji:
|
4 |
-
colorFrom:
|
5 |
-
colorTo:
|
6 |
sdk: gradio
|
7 |
-
sdk_version:
|
8 |
app_file: app.py
|
9 |
pinned: false
|
10 |
license: apache-2.0
|
11 |
---
|
12 |
|
13 |
-
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
1 |
---
|
2 |
+
title: Shirts_Occasion
|
3 |
+
emoji: 🌍
|
4 |
+
colorFrom: red
|
5 |
+
colorTo: blue
|
6 |
sdk: gradio
|
7 |
+
sdk_version: 4.42.0
|
8 |
app_file: app.py
|
9 |
pinned: false
|
10 |
license: apache-2.0
|
11 |
---
|
12 |
|
13 |
+
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
app.py
ADDED
@@ -0,0 +1,87 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
from transformers import ViTForImageClassification, ViTFeatureExtractor, AutoConfig
|
3 |
+
import gradio as gr
|
4 |
+
from PIL import Image
|
5 |
+
import os
|
6 |
+
import logging
|
7 |
+
from safetensors.torch import load_file # Import safetensors loading function
|
8 |
+
|
9 |
+
# Set up logging
|
10 |
+
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
11 |
+
|
12 |
+
# Define the directory containing the model files
|
13 |
+
model_dir = "." # Use current directory
|
14 |
+
|
15 |
+
# Define paths to the specific model files
|
16 |
+
model_path = os.path.join(model_dir, "model.safetensors")
|
17 |
+
config_path = os.path.join(model_dir, "config.json")
|
18 |
+
preprocessor_path = os.path.join(model_dir, "preprocessor_config.json")
|
19 |
+
|
20 |
+
# Check if all required files exist
|
21 |
+
for path in [model_path, config_path, preprocessor_path]:
|
22 |
+
if not os.path.exists(path):
|
23 |
+
logging.error(f"File not found: {path}")
|
24 |
+
raise FileNotFoundError(f"Required file not found: {path}")
|
25 |
+
else:
|
26 |
+
logging.info(f"Found file: {path}")
|
27 |
+
|
28 |
+
# Load the configuration
|
29 |
+
config = AutoConfig.from_pretrained(config_path)
|
30 |
+
|
31 |
+
# Ensure the labels are consistent with the model's config
|
32 |
+
labels = list(config.id2label.values())
|
33 |
+
logging.info(f"Labels: {labels}")
|
34 |
+
|
35 |
+
# Load the feature extractor
|
36 |
+
feature_extractor = ViTFeatureExtractor.from_pretrained(preprocessor_path)
|
37 |
+
|
38 |
+
# Load the model using the safetensors file
|
39 |
+
state_dict = load_file(model_path) # Use safetensors to load the model weights
|
40 |
+
model = ViTForImageClassification.from_pretrained(
|
41 |
+
pretrained_model_name_or_path=None,
|
42 |
+
config=config,
|
43 |
+
state_dict=state_dict
|
44 |
+
)
|
45 |
+
|
46 |
+
# Ensure the model is in evaluation mode
|
47 |
+
model.eval()
|
48 |
+
logging.info("Model set to evaluation mode")
|
49 |
+
|
50 |
+
# Define the prediction function
|
51 |
+
def predict(image):
|
52 |
+
logging.info("Starting prediction")
|
53 |
+
logging.info(f"Input image shape: {image.size}")
|
54 |
+
|
55 |
+
# Preprocess the image
|
56 |
+
logging.info("Preprocessing image")
|
57 |
+
inputs = feature_extractor(images=image, return_tensors="pt")
|
58 |
+
logging.info(f"Preprocessed input shape: {inputs['pixel_values'].shape}")
|
59 |
+
|
60 |
+
logging.info("Running inference")
|
61 |
+
with torch.no_grad():
|
62 |
+
outputs = model(**inputs)
|
63 |
+
logits = outputs.logits
|
64 |
+
probabilities = torch.nn.functional.softmax(logits[0], dim=0)
|
65 |
+
|
66 |
+
logging.info(f"Raw logits: {logits}")
|
67 |
+
logging.info(f"Probabilities: {probabilities}")
|
68 |
+
|
69 |
+
# Prepare the output dictionary
|
70 |
+
result = {labels[i]: float(probabilities[i]) for i in range(len(labels))}
|
71 |
+
logging.info(f"Prediction result: {result}")
|
72 |
+
|
73 |
+
return result
|
74 |
+
|
75 |
+
# Set up the Gradio Interface
|
76 |
+
logging.info("Setting up Gradio interface")
|
77 |
+
gradio_app = gr.Interface(
|
78 |
+
fn=predict,
|
79 |
+
inputs=gr.Image(type="pil"),
|
80 |
+
outputs=gr.Label(num_top_classes=6),
|
81 |
+
title="Shirts Occasion Classifier"
|
82 |
+
)
|
83 |
+
|
84 |
+
# Launch the app
|
85 |
+
if __name__ == "__main__":
|
86 |
+
logging.info("Launching the app")
|
87 |
+
gradio_app.launch()
|
requirements.txt
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
torch
|
2 |
+
transformers
|
3 |
+
gradio
|
4 |
+
pillow
|