Upload app.py
Browse files
app.py
CHANGED
|
@@ -1,5 +1,6 @@
|
|
| 1 |
"""
|
| 2 |
CLIP Image Embedding API - Lightweight version for HF Spaces free tier
|
|
|
|
| 3 |
"""
|
| 4 |
|
| 5 |
import gradio as gr
|
|
@@ -8,6 +9,8 @@ from PIL import Image
|
|
| 8 |
from transformers import CLIPProcessor, CLIPModel
|
| 9 |
import requests
|
| 10 |
from io import BytesIO
|
|
|
|
|
|
|
| 11 |
|
| 12 |
# Use CPU and smaller memory footprint
|
| 13 |
model = None
|
|
@@ -21,18 +24,34 @@ def load_model():
|
|
| 21 |
model.eval()
|
| 22 |
return model, processor
|
| 23 |
|
| 24 |
-
def
|
| 25 |
-
"""Get CLIP embedding from image URL"""
|
| 26 |
try:
|
| 27 |
-
if not
|
| 28 |
-
return {"success": False, "error": "Please provide
|
| 29 |
|
| 30 |
# Load model on first use
|
| 31 |
model, processor = load_model()
|
| 32 |
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
# Get embedding
|
| 38 |
inputs = processor(images=image, return_tensors="pt")
|
|
@@ -52,11 +71,11 @@ def get_embedding_from_url(image_url: str):
|
|
| 52 |
|
| 53 |
# Gradio interface with API enabled
|
| 54 |
demo = gr.Interface(
|
| 55 |
-
fn=
|
| 56 |
-
inputs=gr.Textbox(label="Image URL", placeholder="https://example.com/image.jpg"),
|
| 57 |
outputs=gr.JSON(label="Result"),
|
| 58 |
title="CLIP Embedding API",
|
| 59 |
-
description="Get 512-dim CLIP embeddings
|
| 60 |
api_name="predict"
|
| 61 |
)
|
| 62 |
|
|
|
|
| 1 |
"""
|
| 2 |
CLIP Image Embedding API - Lightweight version for HF Spaces free tier
|
| 3 |
+
Supports both URL and base64 image input
|
| 4 |
"""
|
| 5 |
|
| 6 |
import gradio as gr
|
|
|
|
| 9 |
from transformers import CLIPProcessor, CLIPModel
|
| 10 |
import requests
|
| 11 |
from io import BytesIO
|
| 12 |
+
import base64
|
| 13 |
+
import re
|
| 14 |
|
| 15 |
# Use CPU and smaller memory footprint
|
| 16 |
model = None
|
|
|
|
| 24 |
model.eval()
|
| 25 |
return model, processor
|
| 26 |
|
| 27 |
+
def get_embedding(image_input: str):
|
| 28 |
+
"""Get CLIP embedding from image URL or base64 string"""
|
| 29 |
try:
|
| 30 |
+
if not image_input:
|
| 31 |
+
return {"success": False, "error": "Please provide an image URL or base64 string"}
|
| 32 |
|
| 33 |
# Load model on first use
|
| 34 |
model, processor = load_model()
|
| 35 |
|
| 36 |
+
image = None
|
| 37 |
+
|
| 38 |
+
# Check if it's base64 (data:image/... or raw base64)
|
| 39 |
+
if image_input.startswith('data:image'):
|
| 40 |
+
# Extract base64 data after the comma
|
| 41 |
+
base64_data = image_input.split(',')[1] if ',' in image_input else image_input
|
| 42 |
+
image_bytes = base64.b64decode(base64_data)
|
| 43 |
+
image = Image.open(BytesIO(image_bytes)).convert('RGB')
|
| 44 |
+
elif not image_input.startswith('http'):
|
| 45 |
+
# Try as raw base64
|
| 46 |
+
try:
|
| 47 |
+
image_bytes = base64.b64decode(image_input)
|
| 48 |
+
image = Image.open(BytesIO(image_bytes)).convert('RGB')
|
| 49 |
+
except:
|
| 50 |
+
return {"success": False, "error": "Invalid input: provide URL or base64"}
|
| 51 |
+
else:
|
| 52 |
+
# It's a URL - download it
|
| 53 |
+
response = requests.get(image_input, timeout=30)
|
| 54 |
+
image = Image.open(BytesIO(response.content)).convert('RGB')
|
| 55 |
|
| 56 |
# Get embedding
|
| 57 |
inputs = processor(images=image, return_tensors="pt")
|
|
|
|
| 71 |
|
| 72 |
# Gradio interface with API enabled
|
| 73 |
demo = gr.Interface(
|
| 74 |
+
fn=get_embedding,
|
| 75 |
+
inputs=gr.Textbox(label="Image (URL or base64)", placeholder="https://example.com/image.jpg or data:image/jpeg;base64,..."),
|
| 76 |
outputs=gr.JSON(label="Result"),
|
| 77 |
title="CLIP Embedding API",
|
| 78 |
+
description="Get 512-dim CLIP embeddings from image URL or base64",
|
| 79 |
api_name="predict"
|
| 80 |
)
|
| 81 |
|