Spaces:
Sleeping
Sleeping
Create main.py
Browse files
main.py
ADDED
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi import FastAPI
|
2 |
+
from pydantic import BaseModel
|
3 |
+
from transformers import T5ForConditionalGeneration, T5Tokenizer
|
4 |
+
import torch
|
5 |
+
|
6 |
+
# Load your fine-tuned model
|
7 |
+
model_path = "./t5-summarizer" # Path inside Docker container
|
8 |
+
model = T5ForConditionalGeneration.from_pretrained(model_path)
|
9 |
+
tokenizer = T5Tokenizer.from_pretrained(model_path)
|
10 |
+
|
11 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
12 |
+
model = model.to(device)
|
13 |
+
|
14 |
+
app = FastAPI()
|
15 |
+
|
16 |
+
class TextInput(BaseModel):
|
17 |
+
text: str
|
18 |
+
|
19 |
+
@app.post("/summarize/")
|
20 |
+
def summarize_text(input: TextInput):
|
21 |
+
input_text = "summarize: " + input.text.strip().replace("\n", " ")
|
22 |
+
|
23 |
+
inputs = tokenizer.encode(input_text, return_tensors="pt", max_length=512, truncation=True).to(device)
|
24 |
+
summary_ids = model.generate(inputs, max_length=150, min_length=30, length_penalty=2.0, num_beams=4, early_stopping=True)
|
25 |
+
|
26 |
+
summary = tokenizer.decode(summary_ids[0], skip_special_tokens=True)
|
27 |
+
return {"summary": summary}
|