Skip to content

Instantly share code, notes, and snippets.

@qweliant
Last active September 1, 2020 18:00
Show Gist options
  • Select an option

  • Save qweliant/19735ad142e88daf27b8f86fbb775655 to your computer and use it in GitHub Desktop.

Select an option

Save qweliant/19735ad142e88daf27b8f86fbb775655 to your computer and use it in GitHub Desktop.
FastAPI for serving NLP model
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