Spaces:
Running
Running
File size: 4,538 Bytes
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 |
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 MCP server
# You would replace this URL with the actual URL of your deployed Huggingface Space
MCP_SERVER_URL = "https://your-username-nomic-vision-mcp.hf.space/mcp"
def embed_image_from_url(image_url):
"""
Generate embeddings for an image using the MCP server's embed_image tool
Args:
image_url: URL of the image to embed
Returns:
The embedding vector and its dimension
"""
# Prepare the MCP request
mcp_request = {
"jsonrpc": "2.0",
"method": "callTool",
"params": {
"name": "embed_image",
"arguments": {
"image_url": image_url
}
},
"id": 1
}
# Send the request to the MCP server
response = requests.post(MCP_SERVER_URL, json=mcp_request)
# Parse the response
result = response.json()
if "error" in result:
print(f"Error: {result['error']['message']}")
return None
# Extract the embedding from the response
content = result["result"]["content"][0]["text"]
embedding_data = json.loads(content)
return embedding_data
def embed_image_from_file(image_path):
"""
Generate embeddings for an image using the MCP server's embed_image tool
Args:
image_path: Path to the image file
Returns:
The embedding vector and its dimension
"""
# Load the image
with open(image_path, "rb") as f:
image_data = f.read()
# Encode the image as base64
image_base64 = base64.b64encode(image_data).decode("utf-8")
# Prepare the MCP request
mcp_request = {
"jsonrpc": "2.0",
"method": "callTool",
"params": {
"name": "embed_image",
"arguments": {
"image_data": image_base64
}
},
"id": 1
}
# Send the request to the MCP server
response = requests.post(MCP_SERVER_URL, json=mcp_request)
# Parse the response
result = response.json()
if "error" in result:
print(f"Error: {result['error']['message']}")
return None
# Extract the embedding from the response
content = result["result"]["content"][0]["text"]
embedding_data = json.loads(content)
return embedding_data
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 = embed_image_from_url(image_url)
if embedding_data:
print(f"Embedding dimension: {embedding_data['dimension']}")
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 = embed_image_from_file(image_path)
#
# if embedding_data:
# print(f"Embedding dimension: {embedding_data['dimension']}")
# print(f"First 10 values of embedding: {embedding_data['embedding'][:10]}...")
#
# # Visualize the embedding
# visualize_embedding(embedding_data['embedding']) |