# 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

,

tag etc text = text.strip().lower() # Strip leading/trailing spaces and convert to lowercase return text # Here it indicates that this dialogue is of type string & it also retuns the string output only def summarize_dialogue(dialogue : str) -> str: # Now we firstly need to clean the input dialogue before summarizing it dialogue = clean_data(dialogue) # clean # tokenize this dialogue so that our transformer can easily understand it & read it inputs = tokenizer( dialogue, padding="max_length", max_length=512, truncation=True, return_tensors="pt" # now here it will return Pytorch tensors # if we are using TensorFlow, then we will write "tf" here # But here we are using "pt" for PyTorch because by-default Hugging Face models are PyTorch models ).to(device) # generate the summary as the output # here this targets will contains the tokens ids of the summary model.to(device) targets = model.generate( input_ids=inputs["input_ids"], attention_mask=inputs["attention_mask"], max_length=150, # It means that our model will generate 4 different sequences of outputs here # And finally compare all those outputs & then give us the best output out of those num_beams=4, # it means that as soon as we get the best output out of possible 4 outputs, we stops early_stopping=True #early_stopping=True tells it to stop once all beams reach an end‑of‑sequence token, instead of padding them out unnecessarily. ) # But our model will only generate this summary as token ids # SO we have to convert these token isd to text # token ids convert to summary => which is called decoding summary = tokenizer.decode(targets[0], skip_special_tokens=True) # skip_special_tokens=True → removes special tokens like , , , EOS, SEP wtc that are used internally by the model but aren’t meaningful in the final output. return summary # Defining API Endpoints using FastAPI :- @app.post("/summarize") async def summarize(dialogue_input: DialogueInput): summary = summarize_dialogue(dialogue_input.dialogue) return {"summary" : summary} @app.get("/", response_class=HTMLResponse) async def home(request: Request): return templates.TemplateResponse(request, "index.html") # Now ro run this app, we will use this :- # run this command in git bash :- # conda activate pytorch_env : to use the pytorch_env as trasnformers and pytorch are installed in that env only # And we also need to install fastapi & uvicorn in that env only # And then use this :- # uvicorn app:app --reload