SadatHossain01 commited on
Commit
a4c7c88
Β·
verified Β·
1 Parent(s): 44f7023

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -35
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
- # Create simple result string instead of complex JSON
79
- result_text = f"""βœ… Embedding Extracted Successfully!
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
- πŸ”’ Preview (first 10 values):
91
- {embedding[:10].tolist()}
 
 
 
 
 
92
 
93
- πŸ’‘ Usage: The full {len(embedding)}-dimensional embedding vector can be used for similarity search, classification, or clustering tasks."""
 
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"❌ Error processing image: {str(e)}"
101
-
102
- # Create simple Blocks interface
103
- demo = gr.Blocks(title="MobileCLIP Embeddings")
104
 
105
- with demo:
106
- gr.Markdown("# πŸš€ MobileCLIP Image Embedding Extractor")
107
- gr.Markdown("Extract 512-dimensional embeddings from images using MobileCLIP-S2")
108
-
109
- with gr.Row():
110
- image_input = gr.Image(type="pil", label="Upload Image")
111
- text_output = gr.Textbox(label="Results", lines=12, interactive=False)
112
-
113
- btn = gr.Button("Extract Embeddings")
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=True
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
+ )