VietCat commited on
Commit
a0024c9
·
1 Parent(s): 6a61342

fix runtime error

Browse files
Files changed (2) hide show
  1. Dockerfile +14 -3
  2. 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 app.py .
 
 
 
 
9
 
10
- # Expose port 7860 (Spaces yêu cầu)
11
- CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
 
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
- # Fix lỗi ghi cache
7
- os.environ["TRANSFORMERS_CACHE"] = "/tmp/hf_cache"
 
8
 
9
- # Load model
10
- model_name = "bmd1905/BARTpho2-ViT5-question-answering" # model này public
11
  tokenizer = AutoTokenizer.from_pretrained(model_name)
12
  model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
13
 
14
- # FastAPI app
15
  app = FastAPI()
16
 
17
- class InputText(BaseModel):
18
- text: str
19
 
20
- @app.post("/generate")
21
- def generate_text(item: InputText):
22
- inputs = tokenizer(item.text, return_tensors="pt", truncation=True, max_length=512)
23
- outputs = model.generate(**inputs, max_new_tokens=128)
24
- response = tokenizer.decode(outputs[0], skip_special_tokens=True)
25
- return {"answer": response}
 
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}