So erstellen Sie Anwendungen für maschinelles Lernen mit FastAPI | von Josephine Amponsah | Juni 2023

0
29


Code-Implementierung

Die Anwendung in diesem Tutorial dient der sentimentalen Analyse von Bewertungen, die von Benutzern eines Dienstes abgegeben wurden.

  • Erstellen Sie einen Ordner für Ihre Bewerbung.
  • Erstellen Sie im Anwendungsordner ‚important.py‘
  • Erstellen Sie einen Ordner für Ihre Module. Sie können es benennen Module‘.
  • Erstellen __init__.py um das Modul als Paket im zu initialisieren Module Ordner.
  • Im gleichen Module Ordner, erstellen Sie Dateien mit dem Namen data_model.py für Ihre Datenmodelle und Funktionen.py für alle Funktionen, die Sie für Ihre Anwendung benötigen.

Schauen wir uns den Code für unsere verschiedenen Dateien an:

data_model.py

# import pydantic, typing 
# from pydantic import BaseModels for information mannequin courses and different information validators
# ! pip set up email-validator to allow pydantic's EmailStr work
from pydantic import BaseModel, EmailStr, PastDate
from datetime import datetime
from typing import Listing, Non-compulsory

# for our enter information , we are going to take the e-mail, date and remark from buyer
# as seen for the date area, you may make an enter worth non-compulsory, giving ...
# ... the person the selection to submit that area or not
class InputData(BaseModel):
date: Non-compulsory[datetime] = None
e mail: EmailStr
evaluation: str

# in our output information , we merely need to add a brand new area to the submitted information
# therefore we merely inherit the InputData information mannequin and add a brand new area
class OutputData(InputData):
sentiment: str

Funktionen.py

# import textblob library for sentimental evaluation
from textblob import TextBlob

# import information fashions from data_model.py to validate operate inputs
from modules.data_models import InputData, OutputData

# outline operate for producing polarity of sentiment of a string
def sentimental_analysis(despatched):
blob = TextBlob(despatched)
evaluation = blob.polarity
if evaluation < 0:
ans = 'Unfavorable'
elif evaluation == 0:
ans = 'Impartial'
else:
ans = 'Constructive'
return ans

# outline operate for retriving remark, working sentimental evaluation ..
# ... including output as new area to the output information
def analyze_data(information: InputData):

evaluation = information.evaluation
sentiment = sentimental_analysis(evaluation)
output = information.dict()
output["sentiment"] = sentiment
return output

important.py


# import information fashions and capabilities
from modules.data_models import InputData, OutputData
from modules.capabilities import analyze_data, sentimental_analysis
from fastapi import FastAPI
import uvicorn

# create occasion of fastAPI
app = FastAPI()

# outline endpoint that accepts person title as path parameter,...
# and returns information utilizing supplied parameter
@app.get('/greet/{title}')
def greet_user(title: str):
greeting = f"Hello {title} , share your expertise with us."
return {"greet": greeting}

# outline endpoint that accepts person evaluation within the type of the outlined enter information
# specify information mannequin of reponse information
@app.put up('/evaluation', response_model=OutputData)
def generate_sentiment(response: InputData):
outcome = analyze_data(response)
return outcome

# specify host and port quantity on which fastAPI software runs
# localhost is used right here
if __name__ == "__main__":
uvicorn.run("important:app", host="127.0.0.1", port=8000,
reload=True)



Source link

HINTERLASSEN SIE EINE ANTWORT

Please enter your comment!
Please enter your name here