![](https://crypto4nerd.com/wp-content/uploads/2023/06/1GTDxDxYawizF2d_RyLUsBw.jpeg)
Code Implementation
The application in this tutorial is for sentimental analysis of reviews submitted by users of a service.
- Create folder for your application.
- In the application folder, create ‘main.py’
- Create a folder for your modules. You can name it ‘modules’.
- Create __init__.py to initialize the module as a package in the modules folder.
- In the same modules folder, create files named data_model.py for your data models and funtions.py for all the functions you will need for your application.
Let’s look at the code for our various files:
data_model.py
# import pydantic, typing
# from pydantic import BaseModels for data model classes and other data validators
# ! pip install email-validator to enable pydantic's EmailStr work
from pydantic import BaseModel, EmailStr, PastDate
from datetime import datetime
from typing import List, Optional# for our input data , we will take the email, date and comment from customer
# as seen for the date field, you can make an input value optional, giving ...
# ... the user the choice to submit that field or not
class InputData(BaseModel):
date: Optional[datetime] = None
email: EmailStr
review: str
# in our output data , we simply want to add a new field to the submitted data
# hence we simply inherit the InputData data model and add a new field
class OutputData(InputData):
sentiment: str
functions.py
# import textblob library for sentimental analysis
from textblob import TextBlob# import data models from data_model.py to validate function inputs
from modules.data_models import InputData, OutputData
# define function for generating polarity of sentiment of a string
def sentimental_analysis(sent):
blob = TextBlob(sent)
analysis = blob.polarity
if analysis < 0:
ans = 'Negative'
elif analysis == 0:
ans = 'Neutral'
else:
ans = 'Positive'
return ans
# define function for retriving comment, running sentimental analysis ..
# ... adding output as new field to the output data
def analyze_data(data: InputData):
review = data.review
sentiment = sentimental_analysis(review)
output = data.dict()
output["sentiment"] = sentiment
return output
main.py
# import data models and functions
from modules.data_models import InputData, OutputData
from modules.functions import analyze_data, sentimental_analysis
from fastapi import FastAPI
import uvicorn# create instance of fastAPI
app = FastAPI()
# define endpoint that accepts user name as path parameter,...
# and returns data using provided parameter
@app.get('/greet/{name}')
def greet_user(name: str):
greeting = f"Hi {name} , share your experience with us."
return {"greet": greeting}
# define endpoint that accepts user review in the form of the defined input data
# specify data model of reponse data
@app.post('/analysis', response_model=OutputData)
def generate_sentiment(response: InputData):
result = analyze_data(response)
return result
# specify host and port number on which fastAPI application runs
# localhost is used here
if __name__ == "__main__":
uvicorn.run("main:app", host="127.0.0.1", port=8000,
reload=True)