Last active
September 1, 2020 18:00
-
-
Save qweliant/19735ad142e88daf27b8f86fbb775655 to your computer and use it in GitHub Desktop.
FastAPI for serving NLP model
This file contains hidden or 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
| from fastapi import FastAPI | |
| from fastapi.middleware.cors import CORSMiddleware | |
| from pydantic import BaseModel | |
| from app.nlp import NLP | |
| class Message(BaseModel): | |
| input: str | |
| output: str = None | |
| app = FastAPI() | |
| nlp = NLP() | |
| origins = [ | |
| "http://localhost", | |
| "http://localhost:3000", | |
| "http://127.0.0.1:3000" | |
| ] | |
| app.add_middleware( | |
| CORSMiddleware, | |
| allow_origins=origins, | |
| allow_credentials=True, | |
| allow_methods=["POST"], | |
| allow_headers=["*"], | |
| ) | |
| @app.post("/generative/") | |
| async def generate(message: Message): | |
| message.output = nlp.generate(prompt=message.input) | |
| return {"output" : message.output} | |
| @app.post("/sentiment/") | |
| async def sentiment_analysis(message: Message): | |
| message.output = str(nlp.sentiments(message.input)) | |
| return {"output" : message.output} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment