Spaces:
Sleeping
Sleeping
File size: 1,378 Bytes
69847a0 c5a0bf8 20688a8 c5a0bf8 29e22ca 69847a0 c5a0bf8 20688a8 69847a0 20688a8 69847a0 c5a0bf8 69847a0 c5a0bf8 69847a0 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
from fastapi import FastAPI, Request
from pydantic import BaseModel
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
import torch
# Khởi tạo FastAPI app
app = FastAPI()
# Tải model và tokenizer
model_name = "VietAI/vit5-base"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
# Thiết bị (GPU nếu có, nếu không dùng CPU)
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model.to(device)
# Schema cho input
class SummarizeInput(BaseModel):
text: str
@app.get("/")
async def root():
return {"message": "VietAI vit5-base summarization API is running."}
@app.post("/summarize")
async def summarize(input: SummarizeInput):
prefix = "vietnews: "
text = prefix + input.text.strip() + " </s>"
# Tokenize và chuyển sang device
encoding = tokenizer(text, return_tensors="pt", max_length=512, truncation=True)
input_ids = encoding["input_ids"].to(device)
attention_mask = encoding["attention_mask"].to(device)
# Sinh tóm tắt
summary_ids = model.generate(
input_ids=input_ids,
attention_mask=attention_mask,
max_length=256,
early_stopping=True
)
summary = tokenizer.decode(summary_ids[0], skip_special_tokens=True, clean_up_tokenization_spaces=True)
return {"summary": summary}
|