Skip to content

Commit 2ac7f8e

Browse files
committed
Setup for python environment.
1 parent 40ed9cf commit 2ac7f8e

File tree

6 files changed

+75
-5
lines changed

6 files changed

+75
-5
lines changed

py/Dockerfile

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ RUN mkdir -p /app
1414
WORKDIR /app
1515

1616
ARG UNAME=app
17-
ARG UID
18-
ARG GID
17+
ARG UID=1000
18+
ARG GID=1000
1919
ENV APP_HOME /app
2020

2121
#Create the group for the user
@@ -30,6 +30,6 @@ RUN if [ x"${UID}" != x"" ] ; \
3030
else useradd -m -d ${APP_HOME} -g ${UNAME} -s /bin/bash ${UNAME} ; \
3131
fi
3232

33-
COPY requirements.txt .
33+
COPY requirements.txt sentence_embveddings.py setup.py startup.sh setup.sh /app/
3434

35-
CMD tail -f /dev/null
35+
CMD /app/startup.sh

py/requirements.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,14 @@ sentence_transformers==2.2.2
88
Pillow
99
fastapi
1010
PyPDF2
11-
torch==2.0.1
11+
torch
1212
pydantic
1313
uvicorn
1414
sse-starlette
1515
boto3
1616
# missing from the langchain base image?
1717
langchain-openai
1818
langchain-community
19+
20+
# https://github.com/easydiffusion/easydiffusion/issues/1851
21+
huggingface_hub==0.25.2

py/sentence_embeddings.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
from fastapi import FastAPI, Depends, HTTPException, status, Query
2+
from pydantic import BaseModel
3+
from sentence_transformers import SentenceTransformer
4+
from typing import List, Optional
5+
import os
6+
7+
app = FastAPI()
8+
models = {
9+
'all-mpnet-base-v2': SentenceTransformer('all-mpnet-base-v2'),
10+
'all-MiniLM-L6-v2': SentenceTransformer('all-MiniLM-L6-v2'),
11+
'all-distilrobert-av1': SentenceTransformer('all-distilroberta-v1'),
12+
}
13+
14+
class TextData(BaseModel):
15+
texts: List[str]
16+
model: str
17+
18+
@app.post("/embeddings")
19+
async def create_embeddings(text_data: TextData):
20+
try:
21+
embeddings = models[text_data.model].encode(text_data.texts)
22+
return {"embeddings": embeddings.tolist()}
23+
except Exception as e:
24+
raise HTTPException(status_code=500, detail=str(e))
25+
26+
if __name__ == "__main__":
27+
import uvicorn
28+
uvicorn.run(app, host="0.0.0.0", port=3000)

py/setup.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
from fastapi import FastAPI, Depends, HTTPException, status, Query
2+
from pydantic import BaseModel
3+
from sentence_transformers import SentenceTransformer
4+
from typing import List, Optional
5+
import os
6+
7+
app = FastAPI()
8+
models = {
9+
'all-mpnet-base-v2': SentenceTransformer('all-mpnet-base-v2'),
10+
'all-MiniLM-L6-v2': SentenceTransformer('all-MiniLM-L6-v2'),
11+
'all-distilrobert-av1': SentenceTransformer('all-distilroberta-v1'),
12+
}
13+
14+
class TextData(BaseModel):
15+
texts: List[str]
16+
model: str
17+
18+
@app.post("/embeddings")
19+
async def create_embeddings(text_data: TextData):
20+
try:
21+
embeddings = models[text_data.model].encode(text_data.texts)
22+
return {"embeddings": embeddings.tolist()}
23+
except Exception as e:
24+
raise HTTPException(status_code=500, detail=str(e))
25+
26+
if __name__ == "__main__":
27+
import uvicorn

py/setup.sh

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/bin/bash
2+
3+
if [ ! -e /app/venv/bin/activate ] ; then
4+
mkdir -p /app/venv/tmp /app/venv/cache/pip /app/lib/cache/hf
5+
python -m venv /app/venv
6+
source /app/venv/bin/activate
7+
pip install -r requirements.txt
8+
python setup.py
9+
fi

py/startup.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/bin/bash
2+
source /app/venv/bin/activate
3+
python sentence_embeddings.py

0 commit comments

Comments
 (0)