Upload FastAPI embedding app
Browse files- Dockerfile +17 -0
- app.py +25 -0
- requirements.txt +4 -0
Dockerfile
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM python:3.10-slim
|
| 2 |
+
|
| 3 |
+
# Tạo thư mục làm việc
|
| 4 |
+
WORKDIR /app
|
| 5 |
+
|
| 6 |
+
# Copy tất cả file vào container
|
| 7 |
+
COPY . /app
|
| 8 |
+
RUN mkdir -p /app/cache
|
| 9 |
+
|
| 10 |
+
# Cài thư viện Python
|
| 11 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
| 12 |
+
|
| 13 |
+
# Mở port cho API
|
| 14 |
+
EXPOSE 7860
|
| 15 |
+
|
| 16 |
+
# Chạy server FastAPI
|
| 17 |
+
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
|
app.py
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI
|
| 2 |
+
from pydantic import BaseModel
|
| 3 |
+
from sentence_transformers import SentenceTransformer
|
| 4 |
+
import os
|
| 5 |
+
os.environ['TRANSFORMERS_CACHE'] = '/app/cache'
|
| 6 |
+
|
| 7 |
+
app = FastAPI(
|
| 8 |
+
title="Text Embedding API",
|
| 9 |
+
description="Dùng mô hình Vietnamese_Embedding từ AITeamVN",
|
| 10 |
+
version="1.0"
|
| 11 |
+
)
|
| 12 |
+
|
| 13 |
+
# Load mô hình
|
| 14 |
+
model = SentenceTransformer("AITeamVN/Vietnamese_Embedding")
|
| 15 |
+
model.max_seq_length = 2048
|
| 16 |
+
|
| 17 |
+
# Định nghĩa schema input
|
| 18 |
+
class TextInput(BaseModel):
|
| 19 |
+
texts: list[str]
|
| 20 |
+
|
| 21 |
+
@app.post("/embed")
|
| 22 |
+
def embed_text(data: TextInput):
|
| 23 |
+
inputs = [t.strip() for t in data.texts]
|
| 24 |
+
embs = model.encode(inputs, convert_to_numpy=True)
|
| 25 |
+
return {"embeddings": [e.tolist() for e in embs]}
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
fastapi
|
| 2 |
+
uvicorn
|
| 3 |
+
sentence-transformers
|
| 4 |
+
pydantic
|