geqintan commited on
Commit
2362810
·
1 Parent(s): 20934d7
Files changed (3) hide show
  1. Dockerfile +1 -1
  2. Dockerfile copy +40 -0
  3. app copy.py +68 -0
Dockerfile CHANGED
@@ -10,7 +10,7 @@ ENV TRANSFORMERS_CACHE=/app/.cache
10
  # 修改目录的所有者和权限
11
  RUN mkdir -p /app
12
  RUN chown -R appuser:appuser /app
13
- RUN chmod -R 755 /app
14
 
15
  RUN mkdir -p /app/.cache
16
 
 
10
  # 修改目录的所有者和权限
11
  RUN mkdir -p /app
12
  RUN chown -R appuser:appuser /app
13
+ RUN chmod -R 777 /app
14
 
15
  RUN mkdir -p /app/.cache
16
 
Dockerfile copy ADDED
@@ -0,0 +1,40 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ RUN mkdir -p /app
12
+ RUN chown -R appuser:appuser /app
13
+ RUN chmod -R 755 /app
14
+
15
+ RUN mkdir -p /app/.cache
16
+
17
+ # 设置工作目录
18
+ WORKDIR /app
19
+
20
+ # 将 /app 目录的所有权赋予 appuser
21
+
22
+
23
+ # 复制依赖文件到容器中
24
+ COPY requirements.txt .
25
+
26
+ # 安装依赖
27
+ RUN pip install --no-cache-dir -r requirements.txt
28
+
29
+ # 复制项目代码到容器中
30
+ COPY . .
31
+
32
+ # 设置 /app/.cache 及其子目录的权限
33
+ RUN chown -R appuser:appuser /app/.cache
34
+ RUN chmod -R 777 /app/.cache
35
+
36
+ # 切换到 appuser 用户
37
+ USER appuser
38
+
39
+ # 指定容器启动时运行的命令
40
+ CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
app copy.py ADDED
@@ -0,0 +1,68 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI, HTTPException, Depends, Header
2
+ from pydantic import BaseModel, Field
3
+ from sentence_transformers import SentenceTransformer
4
+ import logging, os
5
+
6
+ # Configure logging
7
+ logging.basicConfig(level=logging.INFO)
8
+ logger = logging.getLogger(__name__)
9
+
10
+ # 定义依赖项来校验 Authorization
11
+ async def check_authorization(authorization: str = Header(..., alias="Authorization")):
12
+ # 去掉 Bearer 和后面的空格
13
+ if not authorization.startswith("Bearer "):
14
+ raise HTTPException(status_code=401, detail="Invalid Authorization header format")
15
+
16
+ token = authorization[len("Bearer "):]
17
+ if token != os.environ.get("AUTHORIZATION"):
18
+ raise HTTPException(status_code=401, detail="Unauthorized access")
19
+ return token
20
+
21
+ app = FastAPI()
22
+
23
+ try:
24
+ # Load the BGE Reranker model
25
+ model = SentenceTransformer("BAAI/bge-reranker-large")
26
+ logger.info("Reranker model loaded successfully.")
27
+ except Exception as e:
28
+ logger.error(f"Failed to load model: {e}")
29
+ raise HTTPException(status_code=500, detail="Model loading failed. Check logs for details.")
30
+
31
+ class RerankerRequest(BaseModel):
32
+ query: str = Field(..., min_length=1, max_length=1000, description="The query text.")
33
+ documents: list[str] = Field(..., min_items=2, description="A list of documents to rerank.")
34
+ truncate: bool = Field(False, description="Whether to truncate the documents.")
35
+
36
+ @app.post("/rerank")
37
+ # async def rerank(request: RerankerRequest, authorization: str = Depends(check_authorization)):
38
+ async def rerank(request: RerankerRequest):
39
+ query = request.query
40
+ documents = request.documents
41
+
42
+ try:
43
+ if not query or not documents:
44
+ raise HTTPException(status_code=400, detail="Query and documents must be provided.")
45
+
46
+ from sentence_transformers import util
47
+
48
+ # Calculate embeddings for the query and documents
49
+ query_embedding = model.encode(query, convert_to_tensor=True)
50
+ document_embeddings = model.encode(documents, convert_to_tensor=True)
51
+
52
+ # Calculate cosine similarity between the query and each document
53
+ scores = util.cos_sim(query_embedding, document_embeddings)[0].tolist()
54
+
55
+ # Create a list of dictionaries containing the document and its score
56
+ results = [{"document": doc, "score": score} for doc, score in zip(documents, scores)]
57
+
58
+ # Sort the results by score in descending order
59
+ ranked_results = sorted(results, key=lambda x: x["score"], reverse=True)
60
+
61
+ return {
62
+ "object": "list",
63
+ "data": ranked_results,
64
+ "model": "BAAI/bge-reranker-large"
65
+ }
66
+ except Exception as e:
67
+ logger.error(f"Error processing reranking: {e}")
68
+ raise HTTPException(status_code=500, detail="Internal Server Error. Check logs for details.")