Spaces:
Sleeping
Sleeping
First commit
Browse files- Dockerfile +13 -0
- app.py +45 -0
- requirements.txt +3 -0
Dockerfile
ADDED
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
FROM python:3.9
|
2 |
+
|
3 |
+
RUN useradd -m -u 1000 user
|
4 |
+
USER user
|
5 |
+
ENV PATH="/home/user/.local/bin:$PATH"
|
6 |
+
|
7 |
+
WORKDIR /app
|
8 |
+
|
9 |
+
COPY --chown=user ./requirements.txt requirements.txt
|
10 |
+
RUN pip install --no-cache-dir --upgrade -r requirements.txt
|
11 |
+
|
12 |
+
COPY --chown=user . /app
|
13 |
+
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
|
app.py
ADDED
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from typing import List, Literal
|
2 |
+
from pydantic import BaseModel, Field
|
3 |
+
from fastapi import FastAPI, APIRouter, Request
|
4 |
+
from fastapi.middleware.cors import CORSMiddleware
|
5 |
+
from sentence_transformers import SentenceTransformer
|
6 |
+
import uvicorn
|
7 |
+
|
8 |
+
# Initialize FastAPI app
|
9 |
+
app = FastAPI()
|
10 |
+
|
11 |
+
# CORS middleware
|
12 |
+
app.add_middleware(
|
13 |
+
CORSMiddleware,
|
14 |
+
allow_origins=["*"],
|
15 |
+
allow_credentials=True,
|
16 |
+
allow_methods=["*"],
|
17 |
+
allow_headers=["*"],
|
18 |
+
)
|
19 |
+
|
20 |
+
# Load model
|
21 |
+
model = SentenceTransformer('Alibaba-NLP/gte-multilingual-base', trust_remote_code=True)
|
22 |
+
|
23 |
+
# Define data model
|
24 |
+
class PostEmbeddings(BaseModel):
|
25 |
+
type: Literal['default', 'disease', 'gte'] = Field(default='default')
|
26 |
+
sentences: List[str]
|
27 |
+
|
28 |
+
# Router for embeddings
|
29 |
+
router = APIRouter(prefix="/retrieval", tags=["retrieval"])
|
30 |
+
|
31 |
+
@router.post('/embeddings')
|
32 |
+
def post_embeddings(request: Request, data: PostEmbeddings):
|
33 |
+
embeddings = model.encode(data.sentences)
|
34 |
+
return {"data":{"embeddings": embeddings.tolist()}}
|
35 |
+
|
36 |
+
# Include router
|
37 |
+
app.include_router(router)
|
38 |
+
|
39 |
+
# Define main function to run the app
|
40 |
+
def main():
|
41 |
+
uvicorn.run("api:app", host="0.0.0.0", port=8000, reload=True)
|
42 |
+
|
43 |
+
# Run the app if this script is the main module
|
44 |
+
if __name__ == "__main__":
|
45 |
+
main()
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
fastapi
|
2 |
+
uvicorn[standard]
|
3 |
+
sentence-transformers
|