Update app.py
Browse files
app.py
CHANGED
@@ -2,24 +2,56 @@ from fastapi import FastAPI
|
|
2 |
from sentence_transformers import SentenceTransformer
|
3 |
|
4 |
|
5 |
-
app = FastAPI(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
|
7 |
all_MiniLM_L6_V2_model = SentenceTransformer('sentence-transformers/all-MiniLM-L6-v2')
|
8 |
|
9 |
@app.get("/")
|
10 |
-
def
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
</body>
|
17 |
-
</html>
|
18 |
"""
|
|
|
|
|
19 |
|
20 |
from typing import List
|
21 |
|
22 |
@app.post("/generate-embeddings/sentence-transformers-all-MiniLM-L6-v2/")
|
23 |
def generate_embeddings_all_MiniLM_L6_V2_model(sentences: List[str]):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
embeddings = all_MiniLM_L6_V2_model.encode(sentences)
|
25 |
return {"embeddings": embeddings.tolist()} # Return embeddings as a JSON-compatible list
|
|
|
2 |
from sentence_transformers import SentenceTransformer
|
3 |
|
4 |
|
5 |
+
app = FastAPI(
|
6 |
+
title="Generate Embeddings API",
|
7 |
+
description="""
|
8 |
+
## Generate Embeddings API
|
9 |
+
A simple API to generate sentence embeddings using **Sentence Transformers**.
|
10 |
+
|
11 |
+
### 🔗 Useful Links
|
12 |
+
- [Hugging Face Code](https://huggingface.co/spaces/TheMihirNaik/generate-embeddings-api/tree/main)
|
13 |
+
- [SEO Workflow Automation Newsletter](https://mihirnaik.substack.com/)
|
14 |
+
- [Mihir Naik](https://www.mihirnaik.com)
|
15 |
+
|
16 |
+
### How It Works
|
17 |
+
1. Send a **POST request** to `/generate-embeddings/` with a list of sentences.
|
18 |
+
2. Get **embeddings** as JSON response.
|
19 |
+
|
20 |
+
Built by **Mihir Naik** 🚀
|
21 |
+
""",
|
22 |
+
version="1.0.0",
|
23 |
+
contact={
|
24 |
+
"name": "Mihir Naik",
|
25 |
+
"url": "https://www.mihirnaik.com",
|
26 |
+
}
|
27 |
+
)
|
28 |
+
|
29 |
|
30 |
all_MiniLM_L6_V2_model = SentenceTransformer('sentence-transformers/all-MiniLM-L6-v2')
|
31 |
|
32 |
@app.get("/")
|
33 |
+
def redirect_to_docs():
|
34 |
+
"""
|
35 |
+
Redirects to the FastAPI documentation.
|
36 |
+
|
37 |
+
Returns:
|
38 |
+
RedirectResponse: A response that redirects to the documentation URL.
|
|
|
|
|
39 |
"""
|
40 |
+
from fastapi.responses import RedirectResponse
|
41 |
+
return RedirectResponse(url="/docs")
|
42 |
|
43 |
from typing import List
|
44 |
|
45 |
@app.post("/generate-embeddings/sentence-transformers-all-MiniLM-L6-v2/")
|
46 |
def generate_embeddings_all_MiniLM_L6_V2_model(sentences: List[str]):
|
47 |
+
"""
|
48 |
+
Generates embeddings for a list of sentences using the MiniLM model.
|
49 |
+
|
50 |
+
Args:
|
51 |
+
sentences (List[str]): A list of sentences to generate embeddings for.
|
52 |
+
|
53 |
+
Returns:
|
54 |
+
dict: A dictionary containing the embeddings as a JSON-compatible list.
|
55 |
+
"""
|
56 |
embeddings = all_MiniLM_L6_V2_model.encode(sentences)
|
57 |
return {"embeddings": embeddings.tolist()} # Return embeddings as a JSON-compatible list
|