# In this file, we will write the logic of FastAPIs # Here to use FastAPI, we need to import it first # And we also need to import 'uvicorn' to run our API server # SO we will use :- pip install fastapi uvicorn # So here we will do the Deployment of our model using FastAPI. # FastAPI is a modern, fast (high-performance) python based web framework for building APIs with Python 3.6+ based on standard Python type hints. # So here FastAPI will handdle the server and API endpoints, and we will write the logic of our model in the API endpoints. # It means that server side programming will be handled by FastAPI. # Whenever user sends the request using UI to our API or server, then that request is received by a special layer called as 'API endpoint' and then that API endpoint will process the request and send the response back to the user. # API = Application Programming Interface # Endpoint = A point of entry or access to a resource or service # And FastAPi helps us to create API endpoints easily and efficiently. It also provides automatic documentation for our API using Swagger UI and ReDoc. # uvicorn is a lightning-fast ASGI web server implementation, using uvloop and httptools. It is designed to be easy to use and deploy, and it is compatible with a wide range of web frameworks, including FastAPI. from pathlib import Path import re from fastapi import FastAPI, Request from pydantic import BaseModel # pydantic is a data validation and settings management library for Python. It uses Python type annotations to validate and parse data. It is used in FastAPI to define the structure of the request and response data. # pydantic is a python module which is used to validate the requests. # Like here we want the input text which is coming from the user to be in a specific format, so we will use pydantic to define that format and validate the input data. import torch from transformers import T5ForConditionalGeneration, T5TokenizerFast from fastapi.templating import Jinja2Templates # UI # Here we are importing Jinja2Templates from fastapi.templating to render our HTML templates for the UI. from fastapi.responses import HTMLResponse # Here we are importing HTMLResponse from fastapi.responses to send the HTML response back to the user. # initializing our fastapi app app = FastAPI(title="Text Summarizer App", description="Text Summarization using T5 Transformer", version="1.0") BASE_DIR = Path(__file__).resolve().parent MODEL_DIR = BASE_DIR / "saved_summary_model" # Now we will load our model and tokenizer which we created model = T5ForConditionalGeneration.from_pretrained(str(MODEL_DIR)) tokenizer = T5TokenizerFast.from_pretrained(str(MODEL_DIR)) # That will properly reload both our fine‑tuned model and tokenizer from the directory we saved them in. # Now we will also need to mention what will our device going to be if torch.backends.mps.is_available(): device = torch.device("mps") elif torch.cuda.is_available(): device = torch.device("cuda") else: device = torch.device("cpu") model.to(device) # So here we are actually setting the device for our model # templating # here we are defining where our templates (i.e html, css, JS files) exits i.e here in this directory templates = Jinja2Templates(directory=str(BASE_DIR)) # Input schema (format) for dialogue :- i.e string format class DialogueInput(BaseModel): dialogue: str # declares that the model has one attribute called dialogue, and its type must be a string. # here we just define that there exists a DialogueInput format which contains a key dialogue which is of string type # But we haven't define that the input request must be string yet. # So here in request we get the JSON data which look like :- # So this is DialogueInput actually # { # "dialogue": "some random string" # } # Cleaning the dialogue :- def clean_data(text): text = re.sub(r"\r\n", " ", text) # here we are removing next lines characters text = re.sub(r"\s+", " ", text) # it will remove the empty spaces text = re.sub(r"<.*?>", " ", text) # it will remove any html tag like
,