Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
@@ -1,5 +1,6 @@
|
|
1 |
import os
|
2 |
os.environ["TRANSFORMERS_CACHE"] = "/tmp"
|
|
|
3 |
from fastapi import FastAPI
|
4 |
from pydantic import BaseModel
|
5 |
from transformers import PegasusTokenizer, PegasusForConditionalGeneration
|
@@ -7,32 +8,44 @@ import torch
|
|
7 |
|
8 |
app = FastAPI()
|
9 |
|
10 |
-
# Load
|
11 |
model_name = "google/pegasus-cnn_dailymail"
|
12 |
tokenizer = PegasusTokenizer.from_pretrained(model_name)
|
13 |
model = PegasusForConditionalGeneration.from_pretrained(model_name)
|
|
|
|
|
14 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
15 |
model = model.to(device)
|
16 |
|
17 |
-
#
|
18 |
class InputText(BaseModel):
|
19 |
text: str
|
20 |
|
21 |
-
# Hàm tóm tắt
|
22 |
def summarize(text: str) -> str:
|
|
|
23 |
inputs = tokenizer(text, return_tensors="pt", truncation=True, max_length=1024)
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
inputs = {k: v.to(device) for k, v in inputs.items()}
|
|
|
|
|
25 |
summary_ids = model.generate(
|
26 |
inputs["input_ids"],
|
27 |
-
max_length=
|
28 |
-
min_length=
|
29 |
num_beams=4,
|
30 |
no_repeat_ngram_size=3,
|
31 |
early_stopping=True
|
32 |
)
|
|
|
33 |
return tokenizer.decode(summary_ids[0], skip_special_tokens=True)
|
34 |
|
35 |
-
#
|
36 |
@app.post("/summarize")
|
37 |
def summarize_api(input: InputText):
|
38 |
return {"summary": summarize(input.text)}
|
|
|
1 |
import os
|
2 |
os.environ["TRANSFORMERS_CACHE"] = "/tmp"
|
3 |
+
|
4 |
from fastapi import FastAPI
|
5 |
from pydantic import BaseModel
|
6 |
from transformers import PegasusTokenizer, PegasusForConditionalGeneration
|
|
|
8 |
|
9 |
app = FastAPI()
|
10 |
|
11 |
+
# Load model và tokenizer
|
12 |
model_name = "google/pegasus-cnn_dailymail"
|
13 |
tokenizer = PegasusTokenizer.from_pretrained(model_name)
|
14 |
model = PegasusForConditionalGeneration.from_pretrained(model_name)
|
15 |
+
|
16 |
+
# Dùng GPU nếu có
|
17 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
18 |
model = model.to(device)
|
19 |
|
20 |
+
# Định nghĩa input schema
|
21 |
class InputText(BaseModel):
|
22 |
text: str
|
23 |
|
24 |
+
# Hàm tóm tắt tự động điều chỉnh độ dài theo số token
|
25 |
def summarize(text: str) -> str:
|
26 |
+
# Tokenize input text
|
27 |
inputs = tokenizer(text, return_tensors="pt", truncation=True, max_length=1024)
|
28 |
+
input_length = inputs["input_ids"].shape[1]
|
29 |
+
|
30 |
+
# Xác định độ dài summary theo tỷ lệ input
|
31 |
+
summary_max_len = max(30, int(input_length * 0.2)) # tối đa khoảng 20% số token
|
32 |
+
summary_min_len = max(15, int(summary_max_len * 0.6)) # tối thiểu khoảng 60% max
|
33 |
+
|
34 |
inputs = {k: v.to(device) for k, v in inputs.items()}
|
35 |
+
|
36 |
+
# Sinh summary
|
37 |
summary_ids = model.generate(
|
38 |
inputs["input_ids"],
|
39 |
+
max_length=summary_max_len,
|
40 |
+
min_length=summary_min_len,
|
41 |
num_beams=4,
|
42 |
no_repeat_ngram_size=3,
|
43 |
early_stopping=True
|
44 |
)
|
45 |
+
|
46 |
return tokenizer.decode(summary_ids[0], skip_special_tokens=True)
|
47 |
|
48 |
+
# API route
|
49 |
@app.post("/summarize")
|
50 |
def summarize_api(input: InputText):
|
51 |
return {"summary": summarize(input.text)}
|