File size: 5,739 Bytes
99aaace
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
71641b0
 
 
 
 
 
 
 
99aaace
 
71641b0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
99aaace
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
---
language: en
license: mit
tags:
  - medical
  - sentence-transformers
  - text-embedding
  - sentence-similarity
  - onnx
  - semantic-search
  - opensearch
  - healthcare
  - medical-embeddings
datasets:
  - abhinand/MedEmbed-corpus
metrics:
  - cosine-similarity
library_name: sentence-transformers
pipeline_tag: sentence-similarity
model-index:
  - name: MedEmbed-large-v0.1-onnx
    results:
      - task:
          type: Sentence Similarity
          name: Semantic Retrieval
        dataset:
          type: abhinand/MedEmbed-corpus
          name: MedEmbed corpus
        metrics:
          - type: cosine-similarity
            value: N/A  # Replace with actual value if available
base_model: abhinand/MedEmbed-Large-v0.1
inference: true
---

# MedEmbed-large-v0.1 ONNX Model

This repository contains an ONNX version of the MedEmbed-large-v0.1 model, which was originally a SentenceTransformer model.

## Model Description

The original MedEmbed-large-v0.1 model is a sentence embedding model specialized for medical text. This ONNX version maintains the same functionality but is optimized for deployment in production environments.

This model is a derivative of [abhinand/MedEmbed-Large-v0.1](https://huggingface.co/abhinand/MedEmbed-Large-v0.1), which itself is a fine-tune of [abhinand/MedEmbed-base-v0.1](https://huggingface.co/abhinand/MedEmbed-base-v0.1).

## ONNX Conversion

The model was converted to ONNX format using PyTorch's `torch.onnx.export` functionality with ONNX opset version 14.

## Model Inputs and Outputs

- **Inputs**:
  - `input_ids`: Tensor of shape `[batch_size, sequence_length]`
  - `attention_mask`: Tensor of shape `[batch_size, sequence_length]`

- **Output**:
  - `sentence_embedding`: Tensor of shape `[batch_size, embedding_dimension]`

## Usage with Hugging Face

```python
import onnxruntime as ort
from transformers import AutoTokenizer

# Load tokenizer
tokenizer = AutoTokenizer.from_pretrained("YOUR_MODEL_PATH")

# Load ONNX model
onnx_path = "YOUR_MODEL_PATH/MedEmbed-large-v0.1.onnx"
session = ort.InferenceSession(onnx_path)

# Tokenize input text
text = "Your medical text here"
inputs = tokenizer(text, return_tensors="np", padding=True, truncation=True)

# Run inference with ONNX model
onnx_inputs = {
    "input_ids": inputs["input_ids"],
    "attention_mask": inputs["attention_mask"]
}
embeddings = session.run(None, onnx_inputs)[0]
```

## Usage with OpenSearch

This model can be integrated with OpenSearch for neural search capabilities. Here's how to set it up:

### 1. Upload the model to OpenSearch

```bash
# Create a zip file containing your model files
zip -r medembedlarge.zip MedEmbed-large-v0.1.onnx config.json tokenizer_config.json tokenizer.json vocab.txt special_tokens_map.json

# Upload the model using the OpenSearch REST API
curl -XPUT "https://your-opensearch-endpoint/_plugins/_ml/models/medembedlarge" \
     -H "Content-Type: application/json" \
     -d '{
       "name": "medembedlarge",
       "version": "1.0.0",
       "model_format": "ONNX",
       "model_config": {
         "model_type": "bert",
         "embedding_dimension": 768,
         "framework_type": "sentence_transformers"
       }
     }' -u "admin:admin"

# Upload the model file
curl -XPOST "https://your-opensearch-endpoint/_plugins/_ml/models/medembedlarge/_upload" \
     -H "Content-Type: multipart/form-data" \
     -F "[email protected]" -u "admin:admin"
```

### 2. Deploy the model

```bash
curl -XPOST "https://your-opensearch-endpoint/_plugins/_ml/models/medembedlarge/_deploy" \
     -H "Content-Type: application/json" -u "admin:admin"
```

### 3. Create a neural search pipeline

```bash
curl -XPUT "https://your-opensearch-endpoint/_plugins/_ml/pipelines/medembedlarge-pipeline" \
     -H "Content-Type: application/json" \
     -d '{
       "description": "Neural search pipeline for medical text",
       "processors": [
         {
           "text_embedding": {
             "model_id": "medembedlarge",
             "field_map": {
               "text_field": "text_embedding"
             }
           }
         }
       ]
     }' -u "admin:admin"
```

### 4. Create an index with embedding field

```bash
curl -XPUT "https://your-opensearch-endpoint/medical-documents" \
     -H "Content-Type: application/json" \
     -d '{
       "settings": {
         "index.plugins.search_pipeline.default": "medembedlarge-pipeline"
       },
       "mappings": {
         "properties": {
           "text_field": {
             "type": "text"
           },
           "text_embedding": {
             "type": "knn_vector",
             "dimension": 768,
             "method": {
               "name": "hnsw",
               "space_type": "cosinesimil",
               "engine": "nmslib"
             }
           }
         }
       }
     }' -u "admin:admin"
```

### 5. Index documents with the neural search pipeline

```bash
curl -XPOST "https://your-opensearch-endpoint/medical-documents/_doc" \
     -H "Content-Type: application/json" \
     -d '{
       "text_field": "Patient presented with symptoms of hypertension and diabetes."
     }' -u "admin:admin"
```

### 6. Perform a neural search query

```bash
curl -XPOST "https://your-opensearch-endpoint/medical-documents/_search" \
     -H "Content-Type: application/json" \
     -d '{
       "query": {
         "neural": {
           "text_embedding": {
             "query_text": "hypertension treatment options",
             "model_id": "medembedlarge",
             "k": 10
           }
         }
       }
     }' -u "admin:admin"
```

Note: Replace "https://your-opensearch-endpoint" with your actual OpenSearch endpoint, and adjust authentication credentials as needed for your environment.