Ut14 commited on
Commit
c8a6c88
·
verified ·
1 Parent(s): a73ced7

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -0
app.py ADDED
@@ -0,0 +1,44 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import torch
3
+ import numpy as np
4
+ from PIL import Image
5
+ import joblib
6
+ from transformers import CLIPProcessor, CLIPModel
7
+ from huggingface_hub import hf_hub_download
8
+
9
+ # --- Load CLIP Model & Processor from Hugging Face Hub ---
10
+ clip_model = CLIPModel.from_pretrained("Ut14/clip-phone-view", subfolder="clip_model")
11
+ clip_processor = CLIPProcessor.from_pretrained("Ut14/clip-phone-view", subfolder="clip_processor")
12
+
13
+ # --- Download SVM model from Hugging Face Hub ---
14
+ svm_model_path = hf_hub_download(repo_id="Ut14/clip-phone-view", filename="svm_phone_view_model.joblib")
15
+ svm_model = joblib.load(svm_model_path)
16
+
17
+ # --- Label Mapping ---
18
+ label_map = {0: "Front", 1: "Back", 2: "Side"}
19
+
20
+ # --- Extract Features ---
21
+ def extract_clip_embedding(image: Image.Image) -> np.ndarray:
22
+ image = image.convert("RGB")
23
+ inputs = clip_processor(images=image, return_tensors="pt")
24
+ with torch.no_grad():
25
+ features = clip_model.get_image_features(**inputs)
26
+ return features.squeeze().numpy()
27
+
28
+ # --- Prediction Function for Gradio ---
29
+ def predict_view(image: Image.Image):
30
+ embedding = extract_clip_embedding(image)
31
+ pred = svm_model.predict([embedding])[0]
32
+ return label_map[pred]
33
+
34
+ # --- Gradio Interface ---
35
+ iface = gr.Interface(
36
+ fn=predict_view,
37
+ inputs=gr.Image(type="pil", label="Upload Phone Image"),
38
+ outputs=gr.Label(num_top_classes=1, label="Predicted View"),
39
+ title="📱 Phone View Classifier",
40
+ description="Upload an image of a phone (front, back, or side) and get the predicted view using CLIP + SVM."
41
+ )
42
+
43
+ if __name__ == "__main__":
44
+ iface.launch()