update
Browse files- Dockerfile +29 -0
- app.py +38 -0
- push.sh +3 -0
- requirements.txt +5 -0
Dockerfile
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# 使用官方 Python 基础镜像
|
| 2 |
+
FROM python:3.9-slim
|
| 3 |
+
|
| 4 |
+
# 创建 appuser 用户和组
|
| 5 |
+
RUN groupadd -r appuser && useradd -r -g appuser appuser
|
| 6 |
+
|
| 7 |
+
# 设置环境变量
|
| 8 |
+
ENV TRANSFORMERS_CACHE=/app/.cache
|
| 9 |
+
|
| 10 |
+
# 设置工作目录
|
| 11 |
+
WORKDIR /app
|
| 12 |
+
|
| 13 |
+
# 将 /app 目录的所有权赋予 appuser
|
| 14 |
+
RUN chown -R appuser:appuser /app
|
| 15 |
+
|
| 16 |
+
# 复制依赖文件到容器中
|
| 17 |
+
COPY requirements.txt .
|
| 18 |
+
|
| 19 |
+
# 安装依赖
|
| 20 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
| 21 |
+
|
| 22 |
+
# 复制项目代码到容器中
|
| 23 |
+
COPY . .
|
| 24 |
+
|
| 25 |
+
# 切换到 appuser 用户
|
| 26 |
+
USER appuser
|
| 27 |
+
|
| 28 |
+
# 指定容器启动时运行的命令
|
| 29 |
+
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
|
app.py
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, Request
|
| 2 |
+
from pydantic import BaseModel
|
| 3 |
+
from sentence_transformers import SentenceTransformer
|
| 4 |
+
import numpy as np
|
| 5 |
+
|
| 6 |
+
app = FastAPI()
|
| 7 |
+
|
| 8 |
+
# Load the Sentence Transformer model
|
| 9 |
+
model = SentenceTransformer("BAAI/bge-large-zh-v1.5")
|
| 10 |
+
|
| 11 |
+
class EmbeddingRequest(BaseModel):
|
| 12 |
+
input: str
|
| 13 |
+
|
| 14 |
+
@app.post("/embeddings")
|
| 15 |
+
async def embeddings(request: EmbeddingRequest):
|
| 16 |
+
input_text = request.input
|
| 17 |
+
|
| 18 |
+
# Calculate embeddings
|
| 19 |
+
embeddings = model.encode(input_text)
|
| 20 |
+
|
| 21 |
+
# Format the embeddings in OpenAI compatible format
|
| 22 |
+
data = {
|
| 23 |
+
"object": "list",
|
| 24 |
+
"data": [
|
| 25 |
+
{
|
| 26 |
+
"object": "embedding",
|
| 27 |
+
"embedding": embeddings.tolist(),
|
| 28 |
+
"index": 0
|
| 29 |
+
}
|
| 30 |
+
],
|
| 31 |
+
"model": "BAAI/bge-large-zh-v1.5",
|
| 32 |
+
"usage": {
|
| 33 |
+
"prompt_tokens": len(input_text),
|
| 34 |
+
"total_tokens": len(input_text)
|
| 35 |
+
}
|
| 36 |
+
}
|
| 37 |
+
|
| 38 |
+
return data
|
push.sh
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
git add .
|
| 2 |
+
git commit -m "update"
|
| 3 |
+
git push
|
requirements.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
fastapi
|
| 2 |
+
uvicorn
|
| 3 |
+
transformers
|
| 4 |
+
torch
|
| 5 |
+
numpy
|