Patryk Ptasiński
Disable queue to allow direct API calls
65ff348
raw
history blame
2.54 kB
from typing import List
import gradio as gr
from sentence_transformers import SentenceTransformer
model = SentenceTransformer("nomic-ai/nomic-embed-text-v1.5", trust_remote_code=True, device='cpu')
def embed(document: str):
return model.encode(document)
with gr.Blocks(title="Nomic Text Embeddings") as app:
gr.Markdown("# Nomic Text Embeddings v1.5")
gr.Markdown("Generate embeddings for your text using the nomic-embed-text-v1.5 model.")
# Create an input text box
text_input = gr.Textbox(label="Enter text to embed", placeholder="Type or paste your text here...")
# Create an output component to display the embedding
output = gr.JSON(label="Text Embedding")
# Add a submit button with API name
submit_btn = gr.Button("Generate Embedding", variant="primary")
# Handle both button click and text submission
submit_btn.click(embed, inputs=text_input, outputs=output, api_name="predict")
text_input.submit(embed, inputs=text_input, outputs=output)
# Add API usage guide
gr.Markdown("## API Usage")
gr.Markdown("""
You can use this API programmatically with direct HTTP requests (queue disabled).
### Using cURL
```bash
curl -X POST https://ipepe-nomic-embeddings.hf.space/api/predict \
-H "Content-Type: application/json" \
-d '{
"fn_index": 0,
"data": ["Your text to embed goes here"]
}'
```
### Python Example (using requests)
```python
import requests
response = requests.post(
"https://ipepe-nomic-embeddings.hf.space/api/predict",
json={"fn_index": 0, "data": ["Your text to embed goes here"]}
)
print(response.json()) # Returns the embedding array
```
### Python Example (using Gradio Client)
```python
from gradio_client import Client
client = Client("ipepe/nomic-embeddings")
result = client.predict(
"Your text to embed goes here",
api_name="/predict"
)
print(result) # Returns the embedding array
```
### JavaScript/Node.js Example
```javascript
import { client } from "@gradio/client";
const app = await client("ipepe/nomic-embeddings");
const result = await app.predict("/predict", ["Your text to embed goes here"]);
console.log(result.data);
```
The response will contain the embedding array as a list of floats.
""")
if __name__ == '__main__':
app.launch(server_name="0.0.0.0", show_error=True, server_port=7860)