Recording the progress of deploying the NLP project by fastAPI, docker and streamlit.
FastAPI
Create an api for others to use to generate scripts by sending a prompt.
- Create a new project of
app. - Write codes needed to handle the inputs by users.
- Start the api by
uvicorn <filename>:app --reload. (Run this under the dir ofapp.py)
from fastapi import FastAPI
app = FastAPI()
# define the input type classclass InputClass { prompt: str}
@app.post('/generate')fun generate_script(input: InputClass): # deal with the inputDocker
Use docker to pack the fastAPI and project.
- Derive the
requirement.txtfile. - Create the
dockerfile. - Set up the
docker mirror. - Run the docker container (local).
# 1.derive requirement.txtpip freeze > requirements.txt
# 2.create dockerfile# 使用官方 Python 镜像FROM python:3.9-slim
# 设置工作目录WORKDIR /app
# 拷贝依赖和代码COPY requirements.txt .RUN pip install --no-cache-dir -r requirements.txt
COPY . .
# 启动 FastAPI 服务CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "8000"]
# 3.setup docker mirrordocker build -t fastapi-llama-app .
# 4.run container locallydocker run -p 8000:8000 fastapi-llama-app