Upload folder using huggingface_hub
Browse files- README.md +24 -2
- handler.py +12 -9
README.md
CHANGED
|
@@ -16,7 +16,9 @@ model-index:
|
|
| 16 |
- [Reranker model](#reranker-model)
|
| 17 |
- [Brief information](#brief-information)
|
| 18 |
- [Supporting architectures](#supporting-architectures)
|
| 19 |
-
- [
|
|
|
|
|
|
|
| 20 |
|
| 21 |
|
| 22 |
|
|
@@ -38,7 +40,27 @@ This repository contains reranker model ```bge-reranker-v2-m3``` which you can r
|
|
| 38 |
- GPU (Nvidia T4)
|
| 39 |
- Infernia 2 (2 cores, 32 Gb RAM)
|
| 40 |
|
| 41 |
-
##
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
```python
|
| 44 |
from FlagEmbedding import FlagReranker
|
|
|
|
| 16 |
- [Reranker model](#reranker-model)
|
| 17 |
- [Brief information](#brief-information)
|
| 18 |
- [Supporting architectures](#supporting-architectures)
|
| 19 |
+
- [Example usage](#example-usage)
|
| 20 |
+
- [HuggingFace Inference Endpoints](#huggingface-inference-endpoints)
|
| 21 |
+
- [Local inference](#local-inference)
|
| 22 |
|
| 23 |
|
| 24 |
|
|
|
|
| 40 |
- GPU (Nvidia T4)
|
| 41 |
- Infernia 2 (2 cores, 32 Gb RAM)
|
| 42 |
|
| 43 |
+
## Example usage
|
| 44 |
+
### HuggingFace Inference Endpoints
|
| 45 |
+
|
| 46 |
+
```bash
|
| 47 |
+
curl "https://xxxxxx.us-east-1.aws.endpoints.huggingface.cloud" \
|
| 48 |
+
-X POST \
|
| 49 |
+
-H "Accept: application/json" \
|
| 50 |
+
-H "Authorization: Bearer hf_yyyyyyy" \
|
| 51 |
+
-H "Content-Type: application/json" \
|
| 52 |
+
-d '{
|
| 53 |
+
"inputs":"",
|
| 54 |
+
"query": "Hello, world!",
|
| 55 |
+
"texts": [
|
| 56 |
+
"Hello! How are you?",
|
| 57 |
+
"Cats and dogs",
|
| 58 |
+
"The sky is blue"
|
| 59 |
+
]
|
| 60 |
+
}'
|
| 61 |
+
```
|
| 62 |
+
|
| 63 |
+
### Local inference
|
| 64 |
|
| 65 |
```python
|
| 66 |
from FlagEmbedding import FlagReranker
|
handler.py
CHANGED
|
@@ -18,23 +18,26 @@ class EndpointHandler:
|
|
| 18 |
"""
|
| 19 |
Expected input format:
|
| 20 |
{
|
| 21 |
-
"
|
| 22 |
-
|
|
|
|
|
|
|
| 23 |
"normalize": true # Optional; defaults to False
|
| 24 |
}
|
| 25 |
"""
|
| 26 |
-
|
| 27 |
-
|
|
|
|
| 28 |
normalize = data.get("normalize", False)
|
| 29 |
|
| 30 |
-
if not query or not
|
| 31 |
-
return [{"error": "Both 'query' and '
|
| 32 |
|
| 33 |
# Prepare input pairs
|
| 34 |
-
pairs = [[query, text] for text in
|
| 35 |
|
| 36 |
# Tokenize input pairs
|
| 37 |
-
|
| 38 |
pairs,
|
| 39 |
padding=True,
|
| 40 |
truncation=True,
|
|
@@ -44,7 +47,7 @@ class EndpointHandler:
|
|
| 44 |
|
| 45 |
with torch.no_grad():
|
| 46 |
# Get model logits
|
| 47 |
-
outputs = self.model(**
|
| 48 |
scores = outputs.logits.view(-1)
|
| 49 |
|
| 50 |
# Apply sigmoid normalization if requested
|
|
|
|
| 18 |
"""
|
| 19 |
Expected input format:
|
| 20 |
{
|
| 21 |
+
"inputs": {
|
| 22 |
+
"query": "Your query here",
|
| 23 |
+
"documents": ["Document 1", "Document 2", ...]
|
| 24 |
+
},
|
| 25 |
"normalize": true # Optional; defaults to False
|
| 26 |
}
|
| 27 |
"""
|
| 28 |
+
inputs = data.get("inputs", {})
|
| 29 |
+
query = inputs.get("query")
|
| 30 |
+
documents = inputs.get("documents", [])
|
| 31 |
normalize = data.get("normalize", False)
|
| 32 |
|
| 33 |
+
if not query or not documents:
|
| 34 |
+
return [{"error": "Both 'query' and 'documents' fields are required inside 'inputs'."}]
|
| 35 |
|
| 36 |
# Prepare input pairs
|
| 37 |
+
pairs = [[query, text] for text in documents]
|
| 38 |
|
| 39 |
# Tokenize input pairs
|
| 40 |
+
tokenizer_inputs = self.tokenizer(
|
| 41 |
pairs,
|
| 42 |
padding=True,
|
| 43 |
truncation=True,
|
|
|
|
| 47 |
|
| 48 |
with torch.no_grad():
|
| 49 |
# Get model logits
|
| 50 |
+
outputs = self.model(**tokenizer_inputs)
|
| 51 |
scores = outputs.logits.view(-1)
|
| 52 |
|
| 53 |
# Apply sigmoid normalization if requested
|