Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -18,7 +18,6 @@ try:
|
|
| 18 |
model.eval()
|
| 19 |
model = model.to(device)
|
| 20 |
|
| 21 |
-
# Use half precision on GPU to save memory
|
| 22 |
if device.type == "cuda":
|
| 23 |
model = model.half()
|
| 24 |
print("Using half precision (FP16) for GPU inference")
|
|
@@ -52,7 +51,6 @@ def process_image(input_img):
|
|
| 52 |
# Preprocess image
|
| 53 |
image_tensor = preprocess(input_img).unsqueeze(0).to(device)
|
| 54 |
|
| 55 |
-
# Use appropriate dtype
|
| 56 |
if device.type == "cuda":
|
| 57 |
image_tensor = image_tensor.half()
|
| 58 |
|
|
@@ -64,10 +62,7 @@ def process_image(input_img):
|
|
| 64 |
else:
|
| 65 |
image_features = model.encode_image(image_tensor)
|
| 66 |
|
| 67 |
-
# Normalize features
|
| 68 |
image_features = image_features / image_features.norm(dim=-1, keepdim=True)
|
| 69 |
-
|
| 70 |
-
# Convert to numpy
|
| 71 |
embedding = image_features.cpu().float().numpy().flatten()
|
| 72 |
|
| 73 |
# Clean up GPU memory
|
|
@@ -75,47 +70,42 @@ def process_image(input_img):
|
|
| 75 |
if torch.cuda.is_available():
|
| 76 |
torch.cuda.empty_cache()
|
| 77 |
|
| 78 |
-
#
|
| 79 |
-
result_text = f"""
|
| 80 |
-
|
| 81 |
-
π Results:
|
| 82 |
-
- Model: MobileCLIP-S2
|
| 83 |
-
- Device: {device}
|
| 84 |
-
- Embedding Dimension: {len(embedding)}
|
| 85 |
-
- Mean: {np.mean(embedding):.4f}
|
| 86 |
-
- Std: {np.std(embedding):.4f}
|
| 87 |
-
- Min: {np.min(embedding):.4f}
|
| 88 |
-
- Max: {np.max(embedding):.4f}
|
| 89 |
|
| 90 |
-
|
| 91 |
-
{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
|
| 93 |
-
|
|
|
|
| 94 |
|
| 95 |
return result_text
|
| 96 |
|
| 97 |
except Exception as e:
|
| 98 |
if torch.cuda.is_available():
|
| 99 |
torch.cuda.empty_cache()
|
| 100 |
-
return f"
|
| 101 |
-
|
| 102 |
-
# Create simple Blocks interface
|
| 103 |
-
demo = gr.Blocks(title="MobileCLIP Embeddings")
|
| 104 |
|
| 105 |
-
with
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
btn.click(process_image, inputs=image_input, outputs=text_output)
|
| 115 |
|
| 116 |
if __name__ == "__main__":
|
| 117 |
demo.launch(
|
| 118 |
server_name="0.0.0.0",
|
| 119 |
server_port=7860,
|
| 120 |
-
share=
|
| 121 |
-
|
|
|
|
|
|
|
|
|
| 18 |
model.eval()
|
| 19 |
model = model.to(device)
|
| 20 |
|
|
|
|
| 21 |
if device.type == "cuda":
|
| 22 |
model = model.half()
|
| 23 |
print("Using half precision (FP16) for GPU inference")
|
|
|
|
| 51 |
# Preprocess image
|
| 52 |
image_tensor = preprocess(input_img).unsqueeze(0).to(device)
|
| 53 |
|
|
|
|
| 54 |
if device.type == "cuda":
|
| 55 |
image_tensor = image_tensor.half()
|
| 56 |
|
|
|
|
| 62 |
else:
|
| 63 |
image_features = model.encode_image(image_tensor)
|
| 64 |
|
|
|
|
| 65 |
image_features = image_features / image_features.norm(dim=-1, keepdim=True)
|
|
|
|
|
|
|
| 66 |
embedding = image_features.cpu().float().numpy().flatten()
|
| 67 |
|
| 68 |
# Clean up GPU memory
|
|
|
|
| 70 |
if torch.cuda.is_available():
|
| 71 |
torch.cuda.empty_cache()
|
| 72 |
|
| 73 |
+
# Simple result format that works with older Gradio
|
| 74 |
+
result_text = f"""Embedding Extracted Successfully!
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
|
| 76 |
+
Model: MobileCLIP-S2
|
| 77 |
+
Device: {device}
|
| 78 |
+
Embedding Dimension: {len(embedding)}
|
| 79 |
+
Mean: {np.mean(embedding):.4f}
|
| 80 |
+
Std: {np.std(embedding):.4f}
|
| 81 |
+
Min: {np.min(embedding):.4f}
|
| 82 |
+
Max: {np.max(embedding):.4f}
|
| 83 |
|
| 84 |
+
First 10 values:
|
| 85 |
+
{embedding[:10].tolist()}"""
|
| 86 |
|
| 87 |
return result_text
|
| 88 |
|
| 89 |
except Exception as e:
|
| 90 |
if torch.cuda.is_available():
|
| 91 |
torch.cuda.empty_cache()
|
| 92 |
+
return f"Error processing image: {str(e)}"
|
|
|
|
|
|
|
|
|
|
| 93 |
|
| 94 |
+
# Use gr.Interface instead of gr.Blocks for better compatibility with 4.16.0
|
| 95 |
+
demo = gr.Interface(
|
| 96 |
+
fn=process_image,
|
| 97 |
+
inputs=gr.Image(type="pil", label="Upload Image"),
|
| 98 |
+
outputs=gr.Textbox(label="Results", lines=15),
|
| 99 |
+
title="MobileCLIP Image Embedding Extractor",
|
| 100 |
+
description="Extract 512-dimensional embeddings from images using MobileCLIP-S2",
|
| 101 |
+
allow_flagging="never" # Disable flagging to avoid potential issues
|
| 102 |
+
)
|
|
|
|
| 103 |
|
| 104 |
if __name__ == "__main__":
|
| 105 |
demo.launch(
|
| 106 |
server_name="0.0.0.0",
|
| 107 |
server_port=7860,
|
| 108 |
+
share=False, # Avoid the HF Spaces warning
|
| 109 |
+
inbrowser=False,
|
| 110 |
+
quiet=False
|
| 111 |
+
)
|