Spaces:
Running
Running
File size: 4,948 Bytes
72cd7d2 67e79b5 72cd7d2 67e79b5 72cd7d2 67e79b5 72cd7d2 67e79b5 72cd7d2 67e79b5 72cd7d2 67e79b5 72cd7d2 67e79b5 72cd7d2 67e79b5 72cd7d2 67e79b5 72cd7d2 67e79b5 72cd7d2 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
import requests
import base64
from io import BytesIO
from PIL import Image
import json
import matplotlib.pyplot as plt
import numpy as np
# This is an example client that demonstrates how to use the Gradio API
# You would replace this URL with the actual URL of your deployed Huggingface Space
GRADIO_API_URL = "https://your-username-nomic-vision-embedding.hf.space/api/predict"
def embed_image_from_url(image_url):
"""
Generate embeddings for an image using the Gradio API
Args:
image_url: URL of the image to embed
Returns:
The embedding vector and its dimension
"""
try:
# Download the image
response = requests.get(image_url)
image = Image.open(BytesIO(response.content))
# Convert image to bytes
img_byte_arr = BytesIO()
image.save(img_byte_arr, format='PNG')
img_byte_arr = img_byte_arr.getvalue()
# Prepare the request
files = {
'data': ('image.png', img_byte_arr, 'image/png')
}
# Send the request to the Gradio API
response = requests.post(GRADIO_API_URL, files=files)
# Parse the response
if response.status_code == 200:
result = response.json()
embedding_data = result['data'][0]
embedding_dim = result['data'][1]
return embedding_data, embedding_dim
else:
print(f"Error: HTTP {response.status_code}")
print(response.text)
return None, None
except Exception as e:
print(f"Error: {str(e)}")
return None, None
def embed_image_from_file(image_path):
"""
Generate embeddings for an image using the Gradio API
Args:
image_path: Path to the image file
Returns:
The embedding vector and its dimension
"""
try:
# Load the image
image = Image.open(image_path)
# Convert image to bytes
img_byte_arr = BytesIO()
image.save(img_byte_arr, format=image.format if image.format else 'PNG')
img_byte_arr = img_byte_arr.getvalue()
# Prepare the request
files = {
'data': ('image.png', img_byte_arr, 'image/png')
}
# Send the request to the Gradio API
response = requests.post(GRADIO_API_URL, files=files)
# Parse the response
if response.status_code == 200:
result = response.json()
embedding_data = result['data'][0]
embedding_dim = result['data'][1]
return embedding_data, embedding_dim
else:
print(f"Error: HTTP {response.status_code}")
print(response.text)
return None, None
except Exception as e:
print(f"Error: {str(e)}")
return None, None
def visualize_embedding(embedding):
"""
Visualize the embedding vector
Args:
embedding: The embedding vector
"""
# Convert the embedding to a numpy array
embedding_array = np.array(embedding)
# Plot the embedding
plt.figure(figsize=(10, 5))
plt.plot(embedding_array)
plt.title("Embedding Vector")
plt.xlabel("Dimension")
plt.ylabel("Value")
plt.grid(True)
plt.show()
# Plot the histogram of the embedding
plt.figure(figsize=(10, 5))
plt.hist(embedding_array, bins=50)
plt.title("Embedding Histogram")
plt.xlabel("Value")
plt.ylabel("Frequency")
plt.grid(True)
plt.show()
if __name__ == "__main__":
# Example usage with an image URL
image_url = "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/model_doc/bert-architects.png"
print(f"Generating embedding for image: {image_url}")
embedding_data, embedding_dim = embed_image_from_url(image_url)
if embedding_data:
print(f"Embedding dimension: {embedding_dim}")
print(f"First 10 values of embedding: {embedding_data['embedding'][:10]}...")
# Visualize the embedding
visualize_embedding(embedding_data['embedding'])
# Example usage with a local image file
# Uncomment the following lines to use a local image file
# image_path = "path/to/your/image.jpg"
# print(f"Generating embedding for image: {image_path}")
# embedding_data, embedding_dim = embed_image_from_file(image_path)
#
# if embedding_data:
# print(f"Embedding dimension: {embedding_dim}")
# print(f"First 10 values of embedding: {embedding_data['embedding'][:10]}...")
#
# # Visualize the embedding
# visualize_embedding(embedding_data['embedding']) |