Spaces:
Sleeping
Sleeping
Commit ·
ece0cc5
1
Parent(s): f3e14bb
Add application file
Browse files- Dockerfile +13 -0
- app.py +43 -0
- requirements.txt +4 -0
Dockerfile
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM python:3.9
|
| 2 |
+
|
| 3 |
+
RUN useradd -m -u 1000 user
|
| 4 |
+
USER user
|
| 5 |
+
ENV PATH="/home/user/.local/bin:$PATH"
|
| 6 |
+
|
| 7 |
+
WORKDIR /app
|
| 8 |
+
|
| 9 |
+
COPY --chown=user ./requirements.txt requirements.txt
|
| 10 |
+
RUN pip install --no-cache-dir --upgrade -r requirements.txt
|
| 11 |
+
|
| 12 |
+
COPY --chown=user . /app
|
| 13 |
+
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
|
app.py
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import uvicorn
|
| 2 |
+
from fastapi import FastAPI, Body
|
| 3 |
+
from sentence_transformers import SentenceTransformer
|
| 4 |
+
|
| 5 |
+
# ==========================================
|
| 6 |
+
# PHẦN 1: KHỞI TẠO AI ENGINE
|
| 7 |
+
# ==========================================
|
| 8 |
+
print("⏳ Đang tải Model AI (All-MiniLM-L6-v2)...")
|
| 9 |
+
# Tải model 1 lần duy nhất khi Server khởi động
|
| 10 |
+
model = SentenceTransformer('all-MiniLM-L6-v2')
|
| 11 |
+
print("✅ Model đã sẵn sàng!")
|
| 12 |
+
|
| 13 |
+
app = FastAPI()
|
| 14 |
+
|
| 15 |
+
@app.get("/")
|
| 16 |
+
def home():
|
| 17 |
+
return {"status": "AI Server is RUNNING on Hugging Face", "model": "all-MiniLM-L6-v2"}
|
| 18 |
+
|
| 19 |
+
@app.post("/vectorize")
|
| 20 |
+
def vectorize_products(payload: dict = Body(...)):
|
| 21 |
+
"""
|
| 22 |
+
API nhận vào JSON: { "products": ["Tên SP 1", "Tên SP 2"] }
|
| 23 |
+
Trả về: { "vectors": [[0.1, ...], [0.2, ...]] }
|
| 24 |
+
"""
|
| 25 |
+
products_text = payload.get("products", [])
|
| 26 |
+
|
| 27 |
+
# Kiểm tra đầu vào
|
| 28 |
+
if not products_text:
|
| 29 |
+
return {"vectors": []}
|
| 30 |
+
|
| 31 |
+
print(f"⚡ Đang xử lý {len(products_text)} sản phẩm...")
|
| 32 |
+
|
| 33 |
+
# Tính toán Vector
|
| 34 |
+
vectors = model.encode(products_text).tolist()
|
| 35 |
+
|
| 36 |
+
return {"vectors": vectors}
|
| 37 |
+
|
| 38 |
+
# ==========================================
|
| 39 |
+
# PHẦN 2: KHỞI ĐỘNG SERVER
|
| 40 |
+
# ==========================================
|
| 41 |
+
if __name__ == "__main__":
|
| 42 |
+
# Hugging Face yêu cầu bắt buộc chạy ở port 7860
|
| 43 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
fastapi
|
| 2 |
+
uvicorn[standard]
|
| 3 |
+
sentence-transformers
|
| 4 |
+
torch --index-url https://download.pytorch.org/whl/cpu
|