Spaces:
Sleeping
Sleeping
File size: 1,257 Bytes
4f8b78f 3cb93b2 4f8b78f | 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 | from fastapi import FastAPI
from pydantic import BaseModel
from transformers import T5ForConditionalGeneration, T5Tokenizer
import torch
# Initialize FastAPI
app = FastAPI()
# Your specific model path
model_id = "Moncey10/grammar-t5-small-finetuned"
tokenizer = T5Tokenizer.from_pretrained(model_id)
model = T5ForConditionalGeneration.from_pretrained(model_id)
class GrammarRequest(BaseModel):
text: str
@app.get("/")
def home():
return {"status": "Online", "message": "Grammar API is running"}
@app.post("/predict")
def predict(request: GrammarRequest):
# Use the 'gec:' prefix used during your training
input_text = "gec: " + request.text.strip()
# Determine device (CPU/GPU) as done in your training script
device = "cuda" if torch.cuda.is_available() else "cpu"
model.to(device)
inputs = tokenizer(input_text, return_tensors="pt").input_ids.to(device)
# Generation settings from your successful Colab test
outputs = model.generate(
inputs,
max_length=128,
num_beams=10,
early_stopping=True,
no_repeat_ngram_size=2
)
corrected = tokenizer.decode(outputs[0], skip_special_tokens=True)
return {"original": request.text, "corrected": corrected} |