netandreus commited on
Commit
ea829c4
·
verified ·
1 Parent(s): b906736

Upload folder using huggingface_hub

Browse files
Files changed (2) hide show
  1. README.md +24 -2
  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
- - [Local inference example](#local-inference-example)
 
 
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
- ## Local inference example
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
- "query": "Your query here",
22
- "texts": ["Document 1", "Document 2", ...],
 
 
23
  "normalize": true # Optional; defaults to False
24
  }
25
  """
26
- query = data.get("query")
27
- texts = data.get("texts", [])
 
28
  normalize = data.get("normalize", False)
29
 
30
- if not query or not texts:
31
- return [{"error": "Both 'query' and 'texts' fields are required."}]
32
 
33
  # Prepare input pairs
34
- pairs = [[query, text] for text in texts]
35
 
36
  # Tokenize input pairs
37
- inputs = self.tokenizer(
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(**inputs)
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