> For the complete documentation index, see [llms.txt](https://docs.roboflow.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.roboflow.com/roboflow/roboflow-hi/deploy/serverless/foundation-models/clip.md).

# CLIP

CLIP एक machine learning model है जो images और text के लिए embeddings generate करने में सक्षम है. इन embeddings का उपयोग zero shot classification, semantic image search, और कई अन्य use cases में किया जा सकता है. Roboflow Inference Server पर CLIP का उपयोग करने के लिए तीन routes उपलब्ध हैं:

* embed\_image: image embeddings calculate करने के लिए उपयोग किया जाता है
* embed\_text: text embeddings calculate करने के लिए उपयोग किया जाता है
* compare: text और images के embeddings की गणना करके फिर उनकी तुलना करने के लिए उपयोग किया जाता है

## Embed Image

किसी image को embed करना उस image की जानकारी को अधिक manageable size में compress करने जैसा है. जब हम किसी image को embed करते हैं, तो हम image को input के रूप में लेते हैं, जिसमें दसियों हज़ार pixels होते हैं, और उसे घटाकर सिर्फ कुछ सौ संख्याओं तक refine करते हैं जिन्हें embedding कहा जाता है. ये embeddings मानव आंख के लिए विशेष रूप से meaningful होती हैं, लेकिन अन्य embeddings से तुलना करने पर ये बहुत उपयोगी साबित हो सकती हैं.

CLIP और Roboflow Inference Server का उपयोग करके image embedding generate करने के लिए:

{% code overflow="wrap" %}

```python
# Request Payload परिभाषित करें
infer_clip_payload = {
    #Images को urls या base64 encoded strings के रूप में दिया जा सकता है
    "image": {
        # "type" भी "base64" हो सकता है
        "type": "url",
        # "value" भी image data की base64 encoded string हो सकती है
        "value": "https://images.freeimages.com/images/large-previews/36c/raccoons-in-the-wild-4-1624830.jpg",
    },
}

# inference server url परिभाषित करें (localhost:9001, infer.roboflow.com, आदि.)
base_url = "https://infer.roboflow.com"

# अपना Roboflow API Key परिभाषित करें
api_key = <YOUR API KEY HERE>

res = requests.post(
    f"{base_url}/clip/embed_image?api_key={api_key}",
    json=infer_clip_payload,
)

embeddings = res.json()['embeddings']

print(embeddings)
```

{% endcode %}

```bash
[[-0.4853120744228363, ... ]]
```

एक request के साथ multiple images embed की जा सकती हैं:

```python
# Request Payload परिभाषित करें
infer_clip_payload = {
    #Images को urls या base64 encoded strings के रूप में दिया जा सकता है
    "image": [
        {
            "type": "url",
            "value": "https://images.freeimages.com/images/large-previews/36c/raccoons-in-the-wild-4-1624830.jpg",
        },
        {
            "type": "url",
            "value": "https://images.freeimages.com/images/large-previews/36c/raccoons-in-the-wild-4-1624830.jpg",
        }
    ],
}

res = requests.post(
    f"{base_url}/clip/embed_image?api_key={api_key}",
    json=infer_clip_payload,
)
```

## Embed Text

CLIP images की तरह ही text के लिए भी embeddings generate कर सकता है.

```python
# Request Payload परिभाषित करें
infer_clip_payload = {
    "text": "the quick brown fox jumped over the lazy dog",
}

res = requests.post(
    f"{base_url}/clip/embed_text?api_key={api_key}",
    json=infer_clip_payload,
)

embeddings = res.json()['embeddings']

print(embeddings)
```

```
[[0.56842650744228363, ... ]]
```

Multiple text blocks को एक single request में batch किया जा सकता है:

```python
# Request Payload परिभाषित करें
infer_clip_payload = {
    "text": [
        "the quick brown fox jumped over the lazy dog",
        "how vexingly quick daft zebras jump"
    ]
}

res = requests.post(
    f"{base_url}/clip/embed_text?api_key={api_key}",
    json=infer_clip_payload,
)
```

## Compare

CLIP का असली मूल्य embeddings की तुलना करने पर सामने आता है. तुलना cosine similarity का उपयोग करके गणना की गई दो embeddings के बीच की mathematical distance होती है. इस distance को similarity score के रूप में समझा जा सकता है. यदि दो embeddings की cosine similarity 1 के करीब हो, तो वे समान होती हैं.

जब compare किया जाता है, तो आप एक prompt और एक या अधिक subjects define करते हैं. चूंकि आप text या images के किसी भी संयोजन की तुलना कर सकते हैं, इसलिए आपको prompt type और subject type भी define करने होंगे.

```python
# Request Payload परिभाषित करें
infer_clip_payload = {
    "prompt": {
        "type": "url",
        "value": "https://images.freeimages.com/images/large-previews/36c/raccoons-in-the-wild-4-1624830.jpg",
    },
    "prompt_type": "image",
    "subject": "एक बहुत प्यारा raccoon",
    "subject_type": "text",
}

res = requests.post(
    f"{base_url}/clip/compare?api_key={api_key}",
    json=infer_clip_payload,
)

similarity = res.json()['similarity']

print(similarity)
```

```
[0.30969720949239016]
```

Multiple prompts (एक single request में up to eight) एक list के रूप में pass किए जा सकते हैं:

```
infer_clip_payload = {
    "subject": {
        "type": "url",
        "value": "https://i.imgur.com/Q6lDy8B.jpg",
    },
    "subject_type": "image",
    "prompt": [
        "एक बहुत प्यारा raccoon",
        "एक बड़ा dog",
        "एक काली cate",
    ],
    "prompt_type": "text",
}
```

```
[0.80559720949239016, 0.20329720949239016, 0.505559720949239016]
```
