Created
April 4, 2021 17:26
-
-
Save yashprakash13/998e7322769ce3257b9f8dae786c0e36 to your computer and use it in GitHub Desktop.
FastApi example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import uvicorn | |
from fastapi import FastAPI | |
from model import SentimentModel, SentimentQueryModel | |
app = FastAPI() | |
model = SentimentModel() | |
@app.post('/predict') | |
def predict(data: SentimentQueryModel): | |
data = data.dict() | |
polarity, subjectivity = model.get_sentiment(data['text']) | |
return { | |
'polarity': polarity, | |
'subjectivity': subjectivity | |
} | |
if __name__ == '__main__': | |
uvicorn.run(app, host='127.0.0.1', port=8000) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import spacy | |
from spacytextblob import SpacyTextBlob | |
from pydantic import BaseModel | |
class SentimentQueryModel(BaseModel): | |
text : str | |
class SentimentModel: | |
@Language.spacytextblob | |
def get_sentiment(self, text): | |
nlp = spacy.load('en_core_web_sm') | |
spacy_text_blob = SpacyTextBlob() | |
nlp.add_pipe(spacy_text_blob) | |
doc = nlp(text) | |
polarity = doc._.sentiment.polarity | |
subjectivity = doc._.sentiment.subjectivity | |
return polarity, subjectivity |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment