Spaces:
Runtime error
Runtime error
Switch to an easier to implement model
Browse files- app.py +14 -19
- requirements.txt +1 -3
app.py
CHANGED
|
@@ -1,18 +1,14 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from PIL import Image
|
| 3 |
import requests
|
| 4 |
-
from transformers import CLIPProcessor, CLIPModel
|
| 5 |
import torch
|
| 6 |
-
from ultralytics import YOLO
|
| 7 |
-
from huggingface_hub import hf_hub_download
|
| 8 |
|
| 9 |
-
#
|
| 10 |
-
|
|
|
|
| 11 |
|
| 12 |
-
# Load the
|
| 13 |
-
face_detector = YOLO(model_path)
|
| 14 |
-
|
| 15 |
-
# Load the CLIP model and processor for image similarity
|
| 16 |
clip_model = CLIPModel.from_pretrained("openai/clip-vit-base-patch32")
|
| 17 |
clip_processor = CLIPProcessor.from_pretrained("openai/clip-vit-base-patch32")
|
| 18 |
|
|
@@ -41,16 +37,16 @@ with torch.no_grad():
|
|
| 41 |
animal_embeddings[name] = image_features
|
| 42 |
|
| 43 |
def find_animal_lookalike(user_image):
|
| 44 |
-
# 1. Detect
|
| 45 |
-
|
| 46 |
|
| 47 |
-
#
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
if
|
| 51 |
-
return "No
|
| 52 |
-
if
|
| 53 |
-
return "Multiple
|
| 54 |
|
| 55 |
# 2. Get the embedding of the user's face using CLIP
|
| 56 |
with torch.no_grad():
|
|
@@ -60,7 +56,6 @@ def find_animal_lookalike(user_image):
|
|
| 60 |
# 3. Calculate similarity with each animal
|
| 61 |
similarities = {}
|
| 62 |
for name, animal_embedding in animal_embeddings.items():
|
| 63 |
-
# Cosine similarity
|
| 64 |
cos = torch.nn.CosineSimilarity(dim=1)
|
| 65 |
similarity = cos(user_face_embedding, animal_embedding)
|
| 66 |
similarities[name] = similarity.item()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from PIL import Image
|
| 3 |
import requests
|
| 4 |
+
from transformers import pipeline, CLIPProcessor, CLIPModel
|
| 5 |
import torch
|
|
|
|
|
|
|
| 6 |
|
| 7 |
+
# 1. Use a standard transformers pipeline for object detection.
|
| 8 |
+
# This model is reliable and loads directly from the Hub.
|
| 9 |
+
face_detector = pipeline("object-detection", model="facebook/detr-resnet-50")
|
| 10 |
|
| 11 |
+
# 2. Load the CLIP model for image similarity (this part was already working correctly).
|
|
|
|
|
|
|
|
|
|
| 12 |
clip_model = CLIPModel.from_pretrained("openai/clip-vit-base-patch32")
|
| 13 |
clip_processor = CLIPProcessor.from_pretrained("openai/clip-vit-base-patch32")
|
| 14 |
|
|
|
|
| 37 |
animal_embeddings[name] = image_features
|
| 38 |
|
| 39 |
def find_animal_lookalike(user_image):
|
| 40 |
+
# 1. Detect people in the image. We'll use this to validate the input.
|
| 41 |
+
detections = face_detector(user_image)
|
| 42 |
|
| 43 |
+
# Filter for 'person' detections with a confidence score > 0.9
|
| 44 |
+
person_detections = [d for d in detections if d['label'] == 'person' and d['score'] > 0.9]
|
| 45 |
+
|
| 46 |
+
if len(person_detections) == 0:
|
| 47 |
+
return "No person detected. Please upload a clear photo of a single person.", None
|
| 48 |
+
if len(person_detections) > 1:
|
| 49 |
+
return "Multiple people detected. Please upload a photo of only one person.", None
|
| 50 |
|
| 51 |
# 2. Get the embedding of the user's face using CLIP
|
| 52 |
with torch.no_grad():
|
|
|
|
| 56 |
# 3. Calculate similarity with each animal
|
| 57 |
similarities = {}
|
| 58 |
for name, animal_embedding in animal_embeddings.items():
|
|
|
|
| 59 |
cos = torch.nn.CosineSimilarity(dim=1)
|
| 60 |
similarity = cos(user_face_embedding, animal_embedding)
|
| 61 |
similarities[name] = similarity.item()
|
requirements.txt
CHANGED
|
@@ -1,8 +1,6 @@
|
|
| 1 |
gradio
|
| 2 |
transformers
|
| 3 |
torch
|
| 4 |
-
torchvision
|
| 5 |
Pillow
|
| 6 |
requests
|
| 7 |
-
|
| 8 |
-
huggingface_hub
|
|
|
|
| 1 |
gradio
|
| 2 |
transformers
|
| 3 |
torch
|
|
|
|
| 4 |
Pillow
|
| 5 |
requests
|
| 6 |
+
timm
|
|
|