Spaces:
Sleeping
Sleeping
wjm55
commited on
Commit
·
df0e186
1
Parent(s):
12288ff
Add response model to /vectorize endpoint and update README with API documentation
Browse files
README.md
CHANGED
|
@@ -7,4 +7,102 @@ sdk: docker
|
|
| 7 |
pinned: false
|
| 8 |
---
|
| 9 |
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
pinned: false
|
| 8 |
---
|
| 9 |
|
| 10 |
+
# Vector Endpoint
|
| 11 |
+
|
| 12 |
+
A simple API that converts text into vector embeddings using the [LaBSE](https://huggingface.co/sentence-transformers/LaBSE) sentence transformer model.
|
| 13 |
+
|
| 14 |
+
## API Reference
|
| 15 |
+
|
| 16 |
+
### Endpoint
|
| 17 |
+
|
| 18 |
+
```
|
| 19 |
+
POST /vectorize
|
| 20 |
+
```
|
| 21 |
+
|
| 22 |
+
### Request Format
|
| 23 |
+
|
| 24 |
+
```json
|
| 25 |
+
{
|
| 26 |
+
"text": "Your text to be vectorized"
|
| 27 |
+
}
|
| 28 |
+
```
|
| 29 |
+
|
| 30 |
+
### Response Format
|
| 31 |
+
|
| 32 |
+
```json
|
| 33 |
+
{
|
| 34 |
+
"embedding": [0.123, 0.456, ...] // Vector representation of your text
|
| 35 |
+
}
|
| 36 |
+
```
|
| 37 |
+
|
| 38 |
+
## Usage Examples
|
| 39 |
+
|
| 40 |
+
### cURL
|
| 41 |
+
|
| 42 |
+
```bash
|
| 43 |
+
curl -X 'POST' \
|
| 44 |
+
'https://placingholocaust-vector-endpoint.hf.space/vectorize' \
|
| 45 |
+
-H 'accept: application/json' \
|
| 46 |
+
-H 'Content-Type: application/json' \
|
| 47 |
+
-d '{
|
| 48 |
+
"text": "This is a text"
|
| 49 |
+
}'
|
| 50 |
+
```
|
| 51 |
+
|
| 52 |
+
### Python
|
| 53 |
+
|
| 54 |
+
```python
|
| 55 |
+
import requests
|
| 56 |
+
import json
|
| 57 |
+
|
| 58 |
+
url = "https://placingholocaust-vector-endpoint.hf.space/vectorize"
|
| 59 |
+
headers = {
|
| 60 |
+
"accept": "application/json",
|
| 61 |
+
"Content-Type": "application/json"
|
| 62 |
+
}
|
| 63 |
+
data = {
|
| 64 |
+
"text": "This is a text"
|
| 65 |
+
}
|
| 66 |
+
|
| 67 |
+
response = requests.post(url, headers=headers, json=data)
|
| 68 |
+
result = response.json()
|
| 69 |
+
embedding = result["embedding"]
|
| 70 |
+
|
| 71 |
+
print(f"Embedding length: {len(embedding)}")
|
| 72 |
+
print(f"First few values: {embedding[:5]}")
|
| 73 |
+
```
|
| 74 |
+
|
| 75 |
+
### JavaScript
|
| 76 |
+
|
| 77 |
+
```javascript
|
| 78 |
+
// Using fetch
|
| 79 |
+
async function getEmbedding(text) {
|
| 80 |
+
const response = await fetch(
|
| 81 |
+
"https://placingholocaust-vector-endpoint.hf.space/vectorize",
|
| 82 |
+
{
|
| 83 |
+
method: "POST",
|
| 84 |
+
headers: {
|
| 85 |
+
"accept": "application/json",
|
| 86 |
+
"Content-Type": "application/json"
|
| 87 |
+
},
|
| 88 |
+
body: JSON.stringify({ text })
|
| 89 |
+
}
|
| 90 |
+
);
|
| 91 |
+
|
| 92 |
+
const data = await response.json();
|
| 93 |
+
return data.embedding;
|
| 94 |
+
}
|
| 95 |
+
|
| 96 |
+
// Example usage
|
| 97 |
+
getEmbedding("This is a text")
|
| 98 |
+
.then(embedding => {
|
| 99 |
+
console.log(`Embedding length: ${embedding.length}`);
|
| 100 |
+
console.log(`First few values: ${embedding.slice(0, 5)}`);
|
| 101 |
+
})
|
| 102 |
+
.catch(error => console.error("Error:", error));
|
| 103 |
+
```
|
| 104 |
+
|
| 105 |
+
## Model Information
|
| 106 |
+
|
| 107 |
+
This endpoint uses the [sentence-transformers/LaBSE](https://huggingface.co/sentence-transformers/LaBSE) model, which produces 768-dimensional embeddings that capture semantic meaning of text across multiple languages.
|
| 108 |
+
|
app.py
CHANGED
|
@@ -9,7 +9,11 @@ model = SentenceTransformer('sentence-transformers/LaBSE')
|
|
| 9 |
class EmbedRequest(BaseModel):
|
| 10 |
text: str
|
| 11 |
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
| 13 |
async def embed_text(request: EmbedRequest):
|
| 14 |
-
|
|
|
|
| 15 |
|
|
|
|
| 9 |
class EmbedRequest(BaseModel):
|
| 10 |
text: str
|
| 11 |
|
| 12 |
+
class EmbedResponse(BaseModel):
|
| 13 |
+
embedding: list
|
| 14 |
+
|
| 15 |
+
@app.post("/vectorize", response_model=EmbedResponse)
|
| 16 |
async def embed_text(request: EmbedRequest):
|
| 17 |
+
embedding = model.encode(request.text).tolist()
|
| 18 |
+
return {"embedding": embedding}
|
| 19 |
|