Spaces:
Running
Running
Patryk Ptasiński
commited on
Commit
·
cc86f1b
1
Parent(s):
06197a9
Add FastAPI endpoint for direct HTTP access without queue
Browse files- CLAUDE.md +15 -7
- app.py +77 -24
- requirements.txt +2 -0
CLAUDE.md
CHANGED
|
@@ -29,10 +29,11 @@ huggingface-cli login
|
|
| 29 |
## Architecture
|
| 30 |
|
| 31 |
The application consists of a single `app.py` file with:
|
| 32 |
-
- **Model Initialization**: SentenceTransformer with `device='cpu'` (line
|
| 33 |
-
- **
|
| 34 |
-
- **
|
| 35 |
-
- **
|
|
|
|
| 36 |
|
| 37 |
## Important Configuration Details
|
| 38 |
|
|
@@ -43,15 +44,22 @@ The application consists of a single `app.py` file with:
|
|
| 43 |
|
| 44 |
## API Usage
|
| 45 |
|
| 46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
```python
|
| 48 |
from gradio_client import Client
|
| 49 |
client = Client("ipepe/nomic-embeddings")
|
| 50 |
result = client.predict("text to embed", api_name="/predict")
|
| 51 |
```
|
| 52 |
|
| 53 |
-
Direct HTTP requires implementing Gradio's queue protocol (join queue, SSE listening, session management).
|
| 54 |
-
|
| 55 |
## Deployment Notes
|
| 56 |
|
| 57 |
- Deployed on Hugging Face Spaces at https://huggingface.co/spaces/ipepe/nomic-embeddings
|
|
|
|
| 29 |
## Architecture
|
| 30 |
|
| 31 |
The application consists of a single `app.py` file with:
|
| 32 |
+
- **Model Initialization**: SentenceTransformer with `device='cpu'` (line 10)
|
| 33 |
+
- **FastAPI App**: Direct HTTP endpoint at `/embed` (lines 13, 21-46)
|
| 34 |
+
- **Embedding Function**: Simple wrapper that calls model.encode() (lines 16-17)
|
| 35 |
+
- **Gradio Interface**: UI components and API endpoint configuration (lines 49-122)
|
| 36 |
+
- **Dual Server**: FastAPI mounted with Gradio using uvicorn (lines 126-129)
|
| 37 |
|
| 38 |
## Important Configuration Details
|
| 39 |
|
|
|
|
| 44 |
|
| 45 |
## API Usage
|
| 46 |
|
| 47 |
+
Two options for API access:
|
| 48 |
+
|
| 49 |
+
1. **Direct FastAPI endpoint** (no queue):
|
| 50 |
+
```bash
|
| 51 |
+
curl -X POST https://ipepe-nomic-embeddings.hf.space/embed \
|
| 52 |
+
-H "Content-Type: application/json" \
|
| 53 |
+
-d '{"text": "your text"}'
|
| 54 |
+
```
|
| 55 |
+
|
| 56 |
+
2. **Gradio client** (handles queue automatically):
|
| 57 |
```python
|
| 58 |
from gradio_client import Client
|
| 59 |
client = Client("ipepe/nomic-embeddings")
|
| 60 |
result = client.predict("text to embed", api_name="/predict")
|
| 61 |
```
|
| 62 |
|
|
|
|
|
|
|
| 63 |
## Deployment Notes
|
| 64 |
|
| 65 |
- Deployed on Hugging Face Spaces at https://huggingface.co/spaces/ipepe/nomic-embeddings
|
app.py
CHANGED
|
@@ -1,15 +1,51 @@
|
|
| 1 |
-
from typing import List
|
|
|
|
| 2 |
|
| 3 |
import gradio as gr
|
|
|
|
|
|
|
| 4 |
from sentence_transformers import SentenceTransformer
|
| 5 |
|
|
|
|
| 6 |
model = SentenceTransformer("nomic-ai/nomic-embed-text-v1.5", trust_remote_code=True, device='cpu')
|
| 7 |
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
def embed(document: str):
|
| 10 |
return model.encode(document)
|
| 11 |
|
| 12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
with gr.Blocks(title="Nomic Text Embeddings") as app:
|
| 14 |
gr.Markdown("# Nomic Text Embeddings v1.5")
|
| 15 |
gr.Markdown("Generate embeddings for your text using the nomic-embed-text-v1.5 model.")
|
|
@@ -30,18 +66,37 @@ with gr.Blocks(title="Nomic Text Embeddings") as app:
|
|
| 30 |
# Add API usage guide
|
| 31 |
gr.Markdown("## API Usage")
|
| 32 |
gr.Markdown("""
|
| 33 |
-
You can use this API
|
| 34 |
|
| 35 |
-
###
|
| 36 |
```bash
|
| 37 |
-
|
| 38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
-
|
| 41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
```
|
| 43 |
|
| 44 |
-
### Python Example (
|
| 45 |
```python
|
| 46 |
from gradio_client import Client
|
| 47 |
|
|
@@ -55,23 +110,21 @@ with gr.Blocks(title="Nomic Text Embeddings") as app:
|
|
| 55 |
|
| 56 |
### JavaScript/Node.js Example
|
| 57 |
```javascript
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
|
|
|
|
|
|
|
|
|
| 63 |
```
|
| 64 |
-
|
| 65 |
-
### Direct HTTP (Advanced)
|
| 66 |
-
Direct HTTP requests require implementing the Gradio queue protocol:
|
| 67 |
-
1. POST to `/queue/join` to join queue
|
| 68 |
-
2. Listen to `/queue/data` via SSE for results
|
| 69 |
-
3. Handle session management
|
| 70 |
-
|
| 71 |
-
For direct HTTP, we recommend using the official Gradio clients above which handle this automatically.
|
| 72 |
-
|
| 73 |
-
The response will contain the embedding array as a list of floats.
|
| 74 |
""")
|
| 75 |
|
| 76 |
if __name__ == '__main__':
|
| 77 |
-
app
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from typing import List, Dict, Any
|
| 2 |
+
import json
|
| 3 |
|
| 4 |
import gradio as gr
|
| 5 |
+
from fastapi import FastAPI
|
| 6 |
+
from fastapi.responses import JSONResponse
|
| 7 |
from sentence_transformers import SentenceTransformer
|
| 8 |
|
| 9 |
+
# Initialize model
|
| 10 |
model = SentenceTransformer("nomic-ai/nomic-embed-text-v1.5", trust_remote_code=True, device='cpu')
|
| 11 |
|
| 12 |
+
# Create FastAPI app
|
| 13 |
+
fastapi_app = FastAPI()
|
| 14 |
+
|
| 15 |
|
| 16 |
def embed(document: str):
|
| 17 |
return model.encode(document)
|
| 18 |
|
| 19 |
|
| 20 |
+
# FastAPI endpoints
|
| 21 |
+
@fastapi_app.post("/embed")
|
| 22 |
+
async def embed_text(data: Dict[str, Any]):
|
| 23 |
+
"""Direct API endpoint for text embedding without queue"""
|
| 24 |
+
try:
|
| 25 |
+
text = data.get("text", "")
|
| 26 |
+
if not text:
|
| 27 |
+
return JSONResponse(
|
| 28 |
+
status_code=400,
|
| 29 |
+
content={"error": "No text provided"}
|
| 30 |
+
)
|
| 31 |
+
|
| 32 |
+
# Generate embedding
|
| 33 |
+
embedding = model.encode(text)
|
| 34 |
+
|
| 35 |
+
return JSONResponse(
|
| 36 |
+
content={
|
| 37 |
+
"embedding": embedding.tolist(),
|
| 38 |
+
"dim": len(embedding),
|
| 39 |
+
"model": "nomic-embed-text-v1.5"
|
| 40 |
+
}
|
| 41 |
+
)
|
| 42 |
+
except Exception as e:
|
| 43 |
+
return JSONResponse(
|
| 44 |
+
status_code=500,
|
| 45 |
+
content={"error": str(e)}
|
| 46 |
+
)
|
| 47 |
+
|
| 48 |
+
|
| 49 |
with gr.Blocks(title="Nomic Text Embeddings") as app:
|
| 50 |
gr.Markdown("# Nomic Text Embeddings v1.5")
|
| 51 |
gr.Markdown("Generate embeddings for your text using the nomic-embed-text-v1.5 model.")
|
|
|
|
| 66 |
# Add API usage guide
|
| 67 |
gr.Markdown("## API Usage")
|
| 68 |
gr.Markdown("""
|
| 69 |
+
You can use this API in two ways: via the direct FastAPI endpoint or through Gradio clients.
|
| 70 |
|
| 71 |
+
### Direct API Endpoint (No Queue!)
|
| 72 |
```bash
|
| 73 |
+
curl -X POST https://ipepe-nomic-embeddings.hf.space/embed \
|
| 74 |
+
-H "Content-Type: application/json" \
|
| 75 |
+
-d '{"text": "Your text to embed goes here"}'
|
| 76 |
+
```
|
| 77 |
+
|
| 78 |
+
Response format:
|
| 79 |
+
```json
|
| 80 |
+
{
|
| 81 |
+
"embedding": [0.123, -0.456, ...],
|
| 82 |
+
"dim": 768,
|
| 83 |
+
"model": "nomic-embed-text-v1.5"
|
| 84 |
+
}
|
| 85 |
+
```
|
| 86 |
+
|
| 87 |
+
### Python Example (Direct API)
|
| 88 |
+
```python
|
| 89 |
+
import requests
|
| 90 |
|
| 91 |
+
response = requests.post(
|
| 92 |
+
"https://ipepe-nomic-embeddings.hf.space/embed",
|
| 93 |
+
json={"text": "Your text to embed goes here"}
|
| 94 |
+
)
|
| 95 |
+
result = response.json()
|
| 96 |
+
embedding = result["embedding"]
|
| 97 |
```
|
| 98 |
|
| 99 |
+
### Python Example (Gradio Client)
|
| 100 |
```python
|
| 101 |
from gradio_client import Client
|
| 102 |
|
|
|
|
| 110 |
|
| 111 |
### JavaScript/Node.js Example
|
| 112 |
```javascript
|
| 113 |
+
// Direct API
|
| 114 |
+
const response = await fetch('https://ipepe-nomic-embeddings.hf.space/embed', {
|
| 115 |
+
method: 'POST',
|
| 116 |
+
headers: { 'Content-Type': 'application/json' },
|
| 117 |
+
body: JSON.stringify({ text: 'Your text to embed goes here' })
|
| 118 |
+
});
|
| 119 |
+
const result = await response.json();
|
| 120 |
+
console.log(result.embedding);
|
| 121 |
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 122 |
""")
|
| 123 |
|
| 124 |
if __name__ == '__main__':
|
| 125 |
+
# Mount FastAPI app to Gradio
|
| 126 |
+
app = gr.mount_gradio_app(fastapi_app, app, path="/")
|
| 127 |
+
|
| 128 |
+
# Run with Uvicorn (Gradio uses this internally)
|
| 129 |
+
import uvicorn
|
| 130 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|
requirements.txt
CHANGED
|
@@ -1,4 +1,6 @@
|
|
| 1 |
sentence_transformers==3.0.1
|
| 2 |
einops==0.7.0
|
| 3 |
torch>=2.0.0
|
|
|
|
|
|
|
| 4 |
--extra-index-url https://download.pytorch.org/whl/cpu
|
|
|
|
| 1 |
sentence_transformers==3.0.1
|
| 2 |
einops==0.7.0
|
| 3 |
torch>=2.0.0
|
| 4 |
+
fastapi
|
| 5 |
+
uvicorn
|
| 6 |
--extra-index-url https://download.pytorch.org/whl/cpu
|