FastAPIによる自然言語処理APIの構築
フレームワークの簡単な歴史。
FASTAPIはsebastiのnラムのrezによって構築されたrez.これは、Flatapiを使用して他のツールとライブラリをミックスし、一致するように簡単ですマイクロフレームワークです.FastAPIのアーキテクチャとデザインに関する特定の情報はFastapi ' sdocumentation website
ビジネスに戻る.
自然言語処理機能を備えたAPIを構築します.プロジェクトの構造は次のようになります
各ファイルの内容を詳細に説明する.
開始するには、次のコマンドを端末で実行します
pip install virtualenv
, virtualenv <folder-name>
, cd <folder-name>\scripts
, activate
. コマンドのこのセットはオプションですが、reccommendedされます.Naviagteからメインへ<folder-name>
残りのコマンドを実行し続けるディレクトリ.git clone https://github.com/Bamimore-Tomi/inteligencia.git
pip install -r requirements.txt
. このコマンドはfastapi
, uvicorn
, nltk
, spacy
. python -c "import nltk;nltk.download('wordnet')"
. このコマンドは、我々が実装されるNLP機能のために必要とされる若干の訓練された語を含むWordNetをダウンロードします.他のNLP依存関係はpip install -r requirements.txt
コマンド.🚀🚀🚀🚀🚀
我々は、メインで輸入のカップルから始めます.Pyファイル
#main.py file
from fastapi import FastAPI
from pydantic import BaseModel
from utils import *
FastAPIは、主なアプリケーションオブジェクトです.PydanticモジュールはAPIのためのすべての入力と出力の上でタイプ検証のためにFastAPIによって使われます🐞 一般的にAPIを安全にするのに役立ちます🔐🔐.#main.py file
tags_metadata=[
{
'name':'similarity',
'description':'Finds the similarity between 2 sentences using their word vectors.'
},
{
'name':'tokenize',
'description':'Takes in word, sentences e.t.c and return lexical infromation about each of words. e.g Nouns, Abstract Nouns, Co-ordinating conjunction.'
},
{
'name':'synonyms',
'description':'Takes in a word or a group of words separated by commas and return a list of English language synonyms for the words.'
},
{
'name':'antonyms',
'description':'Takes in a word or a group of words separated by commas and return a list of English language antonyms for the words.'
},
{
'name':'tospeech',
'description':'Takes in a string and returns an audio file of the text.'
}
]
この変数の主な目的は、このFastAPIアプリケーションの異なるURLパスの機能のドキュメントです.それは必要であり、省略することができます.公共の消費のためにAPIを構築するとき、それは良いですが.FastAPIの主要なperkは、どのように箱のドキュメントをサポートしています.#main.py file
app = FastAPI(title='Tageit',
description='This is a hobby project for people interesed in using NLP. Email [email protected] for new functionality you want to be added.',
openapi_tags=tags_metadata)
これは、FastAPIアプリケーションが初期化される場所です.いくつかの余分なキーワード引数がドキュメントに渡されます.再び、これは良いです.アプリケーションは、単にapp= FastAPI()
.次に、ファイルの後で型検証のために使用されるいくつかのpydanticモデルが宣言されます.
#main.py file
class SimilarityIn(BaseModel):
text_1 : str
text_2 : str
class SimilarityOut(BaseModel):
score : float
class TokenizeIn(BaseModel):
text : str
class SynonymIn(BaseModel):
text : str
class AntonymsIn(BaseModel):
text : str
class TextToSpeech(BaseModel):
text : str
language : Optional[str] = 'en'
大規模かつ複雑なプロジェクトで作業する場合は、主アプリケーションファイル内の型検証モデルを宣言するのは悪い習慣です.これは小さなプロジェクトである😄😄 それで、同じファイルであなたのモデルを宣言するのを傷つけません.あなたのモデルを宣言する多くの方法があります.ここで使用する方法はfastapiによって推奨されます.相続の本質BaseModel
PyDanticヘルパークラスは、FastAPIが動作するように型検証クラスを動作させる方法です.それぞれのタイプの背後にある理由は、私たちが、私たちが達成したい機能、必要な入力、および必要な出力(s)を見るとき、彼らがより直観的になるので、我々が続けて説明されるでしょう.それはすべて明らかになる💡💡💡.APIエンドポイント。
ホーム
#main.py file
@app.get('/')
def home():
return 'Welcome here'
これは重要ではありませんが、あなたが成功した最初のフラスコアプリケーションを今すぐに実行することができますuvicorn main:app --reload
. このコマンドは、ローカルホスト上のuvicornサーバを生成します.コマンドの直観はuviorn <file where your main FastAPI application is>:<name of the FastAPI application variable in the file>
.The --reload
アプリケーションファイルに変更を加えるとサーバーがリロードされます.すべてのあなたのDjangoアプリケーションをオフにすることができますので😄😄 新しいシェリフが町にいるから👮🏻👮🏻👮🏻. これがうまくいけば、あなたは何かを見るべきです$ uvicorn main:app --reload
INFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
INFO: Started reloader process [1720]
INFO: Started server process [6822]
INFO: Waiting for application startup.
INFO: Application startup complete.
類似点:この終点は2つのテキストを含んでいるポスト要求を受けて、語の間で余弦類似性を返します.
#main.py file
@app.post('/similarity' , response_model=SimilarityOut,tags=['similarity'])
def similarity(text : SimilarityIn):
score = similarity_(text.text_1, text.text_2)
return {'score':score}
このresponse_model=SimilarityOut
引き数はFastAPIに指示する.クラスSimilarityOut
として#main.py file
class SimilarityOut(BaseModel):
score : float
関数の戻り値が読み込まれますreturn {'score':score}
. じっと見るscore : float
上記のクラスとscore
関数の戻り値のキーワード.それをより簡単にするために.私はFastAPIにこのルートが辞書を返すと言いましたscore
とscore
はfloat
. また、あなたはそれに気づくでしょうsimilarity
関数は位置引数を持つtext : SimilarityIn
. The SimilarityIn
クラスは#main.py file
class SimilarityIn(BaseModel):
text_1 : str
text_2 : str
これはFastAPIに、ポストリクエストがTextChan 1とTextCage 2である2つのキーを持っていることを期待していることを通知しますstr
.フィル😪😪😪, これは全く冗長なものでしたが、現在FastAPIの入力と出力の確認をすべて取得する必要があります.これはストレスと不必要に見えるかもしれませんアミーゴ、私はあなたがより大きなAPIを構築し始めるので、重要性を見始めるでしょう.
tokenize :
このAPI終点は、URL経路に掲示されるテキストに関する語彙情報を返します.
#main.py file
@app.post('/tokenize', response_model=dict,tags=['tokenize'])
def tokenize(text : TokenizeIn):
tokens = tokenize_(text.text)
return tokens
あなたは、そのresponse_model
このエンドポイントはdict
. つまり、endpointがPythonを返すことを意味しますdict
. レスポンスの内容を指定する前のエンドポイントとは異なり.これは非常に柔軟性があり、あなたの応答の正確な内容ではなく、型を知っているときに便利です.これも、あなたの応答モデルが作り付けのPythonデータ型でありえることを示しますint
, float
, str
. この行でtokens = tokenize_(text.text)
POSTリクエストのテキストはtext.text
. これはtext : TokenizeIn
positional argument passed into the tokenize
関数.私は、ファイルで宣言された次の終点をスキップします.
#main.py file
@app.post('/synonyms', response_model=dict, tags=['synonyms'])
def synonyms(text : SynonymIn ):
words = text.text.replace(' ','').split(',')
response = {}
for i in words:
syns = synonyms_(i.strip())
response[i]=syns
return response
@app.post('/antonyms', response_model=dict, tags=['antonyms'])
def antonyms(text : AntonymsIn ):
words = text.text.replace(' ','').split(',')
response = {}
for i in words:
syns = antonyms_(i.strip())
response[i]=syns
return response
彼らはすべて、すでに説明されている前のエンドポイントと同じロジックに従います.最後のエンドポイントは、説明が必要ないくつかの特性があります.トースピーチ
このエンドポイントはPOSTリクエストからテキストを受け取り、Aを返します.テキストのMP 3ファイル.
#main.py file
@app.post('/tospeech' ,tags=['tospeech'])
def text_to_speech(text : TextToSpeech ):
language = text.language
if len(language)>2:
language=language[:2].lower()
elif len(language)<2:
language='en'
audio_object = text_to_speech_(text.text,language=language)
audio_object.save('aud.mp3')
return FileResponse('aud.mp3')
これは私が使用する場所ですFileResponse
ファイルの先頭にインポートします.このクラスは、ファイルオブジェクトを返すのに必要なすべての汚い仕事を要約します.The text_to_speech
関数は位置引数をとるtext : TextToSpeech
. The TextToSpeech
ファイルの初期にも宣言されました.注意すべきクラスの構文language : Optional[str] = 'en'
. Optional
がlanguage
リクエストにおいてキーワードの可能性があります.POSTリクエストに存在しない場合は、文字列en
. また、それに気づくでしょうresponse_model
が指定されていない場合、the FileResponse
私たちの舞台裏のハンドル.結論
FastAPIがドキュメント、タイプ検証、およびファイル応答でどのように機能するかを見ました.配備されたアプリケーションはheroku and source code . 我々はまた、物事を取得し、FastAPIを使用して実行する方法を簡単に見ている.私はあなたが行くには、この素晴らしいFastapiの旅をお楽しみください.
Reference
この問題について(FastAPIによる自然言語処理APIの構築), 我々は、より多くの情報をここで見つけました https://dev.to/bamimoretomi/building-a-natural-language-processing-api-with-fastapi-2e5mテキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol