Spaces:
Running
Running
fix runtime error
Browse files- Dockerfile +14 -3
- app.py +15 -14
Dockerfile
CHANGED
@@ -1,11 +1,22 @@
|
|
1 |
FROM python:3.10-slim
|
2 |
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
WORKDIR /app
|
4 |
|
|
|
5 |
COPY requirements.txt .
|
6 |
RUN pip install --no-cache-dir -r requirements.txt
|
7 |
|
8 |
-
COPY
|
|
|
|
|
|
|
|
|
9 |
|
10 |
-
#
|
11 |
-
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "
|
|
|
1 |
FROM python:3.10-slim
|
2 |
|
3 |
+
# Cài đặt các thư viện hệ thống cần thiết
|
4 |
+
RUN apt-get update && apt-get install -y \
|
5 |
+
git \
|
6 |
+
&& rm -rf /var/lib/apt/lists/*
|
7 |
+
|
8 |
+
# Tạo thư mục làm việc
|
9 |
WORKDIR /app
|
10 |
|
11 |
+
# Copy mã nguồn và cài đặt requirements
|
12 |
COPY requirements.txt .
|
13 |
RUN pip install --no-cache-dir -r requirements.txt
|
14 |
|
15 |
+
COPY . .
|
16 |
+
|
17 |
+
# Tạo thư mục cache có quyền ghi
|
18 |
+
RUN mkdir -p /tmp/hf-cache && chmod -R 777 /tmp/hf-cache
|
19 |
+
ENV TRANSFORMERS_CACHE=/tmp/hf-cache
|
20 |
|
21 |
+
# Chạy server FastAPI
|
22 |
+
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "8000"]
|
app.py
CHANGED
@@ -1,25 +1,26 @@
|
|
1 |
import os
|
2 |
-
from fastapi import FastAPI
|
3 |
from pydantic import BaseModel
|
4 |
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
|
|
5 |
|
6 |
-
#
|
7 |
-
os.
|
|
|
8 |
|
9 |
-
#
|
10 |
-
model_name = "
|
11 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
12 |
model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
|
13 |
|
14 |
-
# FastAPI app
|
15 |
app = FastAPI()
|
16 |
|
17 |
-
class
|
18 |
-
|
19 |
|
20 |
-
@app.post("/
|
21 |
-
def
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
return {"
|
|
|
1 |
import os
|
2 |
+
from fastapi import FastAPI, Request
|
3 |
from pydantic import BaseModel
|
4 |
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
5 |
+
import torch
|
6 |
|
7 |
+
# Đặt thư mục cache có quyền ghi
|
8 |
+
os.makedirs("/tmp/hf-cache", exist_ok=True)
|
9 |
+
os.environ["TRANSFORMERS_CACHE"] = "/tmp/hf-cache"
|
10 |
|
11 |
+
# Sử dụng model công khai
|
12 |
+
model_name = "VietAI/vit5-base"
|
13 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
14 |
model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
|
15 |
|
|
|
16 |
app = FastAPI()
|
17 |
|
18 |
+
class InputData(BaseModel):
|
19 |
+
input: str
|
20 |
|
21 |
+
@app.post("/predict")
|
22 |
+
async def predict(request: Request, data: InputData):
|
23 |
+
input_ids = tokenizer.encode(data.input, return_tensors="pt", max_length=512, truncation=True)
|
24 |
+
output_ids = model.generate(input_ids, max_length=128, num_beams=4, early_stopping=True)
|
25 |
+
output = tokenizer.decode(output_ids[0], skip_special_tokens=True)
|
26 |
+
return {"output": output}
|