from fastapi import APIRouter, HTTPException from pydantic import BaseModel from transformers import BartTokenizer, BartForConditionalGeneration from langdetect import detect, LangDetectException import torch import os router = APIRouter() # Define request model class TranslationRequest(BaseModel): text: str max_length: int = 100 min_length: int = 20 temperature: float = 1.0 top_k: int = 50 top_p: float = 0.95 # Load tokenizer and model once during startup try: model_name = "facebook/bart-large-cnn" cache_dir = "model_cache" print(f"Loading tokenizer from {model_name}...") tokenizer = BartTokenizer.from_pretrained(model_name, cache_dir=cache_dir) print(f"Loading model from {model_name}...") model = BartForConditionalGeneration.from_pretrained(model_name, cache_dir=cache_dir) print("Model and tokenizer loaded successfully!") except Exception as e: print(f"Error loading model: {str(e)}") raise @router.post("/summarize") async def summarize_text(request: TranslationRequest): try: # Detect language detected_lang = detect(request.text) if detected_lang != "en": raise HTTPException(status_code=400, detail="Only English text is supported.") except LangDetectException: raise HTTPException(status_code=400, detail="Could not detect the language of the input.") # Clean and prepare text clean_text = " ".join(request.text.strip().split()) inputs = tokenizer(clean_text, return_tensors="pt", max_length=1024, truncation=True) # Generate with advanced settings summary_ids = model.generate( inputs["input_ids"], max_length=request.max_length, min_length=request.min_length, do_sample=True, temperature=request.temperature, top_k=request.top_k, top_p=request.top_p, num_beams=4, early_stopping=True ) summary = tokenizer.decode(summary_ids[0], skip_special_tokens=True) return { "summary": summary, "language": detected_lang, "params": { "temperature": request.temperature, "top_k": request.top_k, "top_p": request.top_p } } @router.get("/health") async def health_check(): return {"status": "healthy", "model": "facebook/bart-large-cnn"}