Spaces:
Running
Running
init project
Browse files- .gitignore +2 -0
- Dockerfile +17 -0
- app.py +27 -0
- requirements.txt +4 -0
.gitignore
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# files
|
| 2 |
+
*.DS_Store
|
Dockerfile
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM python:3.10-slim
|
| 2 |
+
|
| 3 |
+
WORKDIR /app
|
| 4 |
+
|
| 5 |
+
# Đặt biến môi trường cho cache (sử dụng HF_HOME thay vì TRANSFORMERS_CACHE)
|
| 6 |
+
ENV HF_HOME=/tmp/.cache
|
| 7 |
+
|
| 8 |
+
COPY requirements.txt .
|
| 9 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
| 10 |
+
|
| 11 |
+
COPY app.py .
|
| 12 |
+
|
| 13 |
+
RUN mkdir -p /tmp/hf_home /tmp/transformers_cache
|
| 14 |
+
|
| 15 |
+
EXPOSE 7860
|
| 16 |
+
|
| 17 |
+
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
|
app.py
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, Request
|
| 2 |
+
from pydantic import BaseModel
|
| 3 |
+
from transformers import AutoTokenizer, AutoModel
|
| 4 |
+
import torch
|
| 5 |
+
|
| 6 |
+
app = FastAPI()
|
| 7 |
+
|
| 8 |
+
# Load model
|
| 9 |
+
model_name = "AITeamVN/Vietnamese_Embedding_v2"
|
| 10 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 11 |
+
model = AutoModel.from_pretrained(model_name)
|
| 12 |
+
|
| 13 |
+
class InputText(BaseModel):
|
| 14 |
+
text: str
|
| 15 |
+
|
| 16 |
+
@app.get("/")
|
| 17 |
+
def root():
|
| 18 |
+
return {"message": "AITeamVN/Vietnamese_Embedding_v2 embedding API is running."}
|
| 19 |
+
|
| 20 |
+
@app.post("/embed")
|
| 21 |
+
def get_embedding(data: InputText):
|
| 22 |
+
inputs = tokenizer(data.text, return_tensors="pt", padding=True, truncation=True)
|
| 23 |
+
with torch.no_grad():
|
| 24 |
+
outputs = model(**inputs)
|
| 25 |
+
# Get CLS token or use pooling method
|
| 26 |
+
embedding = outputs.last_hidden_state[:, 0, :].squeeze().tolist()
|
| 27 |
+
return {"embedding": embedding}
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
transformers
|
| 2 |
+
torch
|
| 3 |
+
fastapi
|
| 4 |
+
uvicorn
|