Spaces:
Sleeping
Sleeping
| from fastapi import FastAPI | |
| from pydantic import BaseModel | |
| from transformers import T5ForConditionalGeneration, T5Tokenizer | |
| import torch | |
| # Load your fine-tuned model | |
| model_path = "./t5-summarizer" # Path inside Docker container | |
| model = T5ForConditionalGeneration.from_pretrained(model_path) | |
| tokenizer = T5Tokenizer.from_pretrained(model_path) | |
| device = torch.device("cuda" if torch.cuda.is_available() else "cpu") | |
| model = model.to(device) | |
| app = FastAPI() | |
| class TextInput(BaseModel): | |
| text: str | |
| def summarize_text(input: TextInput): | |
| input_text = "summarize: " + input.text.strip().replace("\n", " ") | |
| inputs = tokenizer.encode(input_text, return_tensors="pt", max_length=512, truncation=True).to(device) | |
| summary_ids = model.generate(inputs, max_length=150, min_length=30, length_penalty=2.0, num_beams=4, early_stopping=True) | |
| summary = tokenizer.decode(summary_ids[0], skip_special_tokens=True) | |
| return {"summary": summary} | |