# Similarity API in Python

This page will show you examples of how to work with the Similarity API from Python, but you can also find more explanations and examples in other languages from the Similarity API page.

## Prerequisites

You need to create a deployment from the deployment view. The deployment view will show you the information you need to use the Deployment API:

• The name of the input feature used for similarity

• The URL and deployment token to use for your requests

• The status of the deployment. Make sure it is enabled, or queries will be denied

## Examples

### Find similar texts in Python

Text data can be any kind of text written in natural languages, i.e., the way humans read and write. Check the Find similar Google questions tutorial to see how to deploy a model for this task.

import base64
import requests

url = 'https://api<...>deployment/similarity/987eec28-4f87<...>beb17282c317'
token = '9dba2730-0488<...>9a0d-20e4a8ed9681'

feature_name = 'question'
text = 'Who is the most famous?'

number_of_replies = 2

#This converts the token given on the platform to its base 64 encoding
def token_conv(token):
token = token + ':'
sample_string_bytes = token.encode('ascii')
base64_bytes = base64.b64encode(sample_string_bytes)
return base64_bytes.decode('ascii')

multiform_text = {feature_name: (None, text)}

for sim in response.json()['similarities']:
print(f'Distance: {sim["distance"]}, Question: {sim["features"]["question"]}')

> Distance: 0.146, Question: who is the most famous person on youtube, Answer: PewDiePie
> Distance: 0.156, Question: who is the most famous person from maine, Answer: n/a

### Find similar images in Python

You can also find images that have a visual similarity with the example that you send. You can send images of any size, and the platform will automatically resize them to the expected resolution according to the settings specified on the datasets view.

import requests
from PIL import Image
import json
import base64
import io

url = 'https://api<...>deployment/similarity/987eec28-4f87-44bf-b4d8-beb17282c317'
token = '9dba2730-0488-<...>-9a0d-20e4a8ed9681'

feature_name = 'image'
image_file_to_send = 'picture_001.jpg'

#This converts the token given on the platform to its base 64 encoding
def token_conv(token):
token = token + ':'
sample_string_bytes = token.encode('ascii')
base64_bytes = base64.b64encode(sample_string_bytes)
return base64_bytes.decode('ascii')

multiform_image=[(feature_name,(image_file_to_send,open(image_file_to_send,'rb'),'image/jpeg'))]

# This converts raw image data to a usable PIL image
def decode_image(msg):
msg = base64.b64decode(msg)
buf = io.BytesIO(msg)
img = Image.open(buf)
return img

for sim in response.json()['similarities']:
print(f'Distance: {sim["distance"]}')
img = decode_img(item['features']['data'])
display(img)

> Distance: 0.021
> <Most similar image>
> Distance: 0.113
> <MoSecond most similar image>
> ...