CoolFace
Apppublic

dog/fastapi-document-qa

sourceHugging Faceupdated 4y agoView on Hugging Face
1likes
main.py35 linesDownload Raw Back to root
1from base64 import b64decode, b64encode2from io import BytesIO3 4from fastapi import FastAPI, File, Form5from PIL import Image6from transformers import pipeline7 8 9description = """10## DocQA with ๐Ÿค— transformers, FastAPI, and Docker11 12This app shows how to do Document Question Answering using13FastAPI in a Docker Space ๐Ÿš€14Check out the docs for the `/predict` endpoint below to try it out!15"""16 17# NOTE - we configure docs_url to serve the interactive Docs at the root path18# of the app. This way, we can use the docs as a landing page for the app on Spaces.19app = FastAPI(docs_url="/", description=description)20 21pipe = pipeline("document-question-answering", model="impira/layoutlm-document-qa")22 23 24@app.post("/predict")25def predict(image_file: bytes = File(...), question: str = Form(...)):26    """27    Using the document-question-answering pipeline from `transformers`, take28    a given input document (image) and a question about it, and return the29    predicted answer. The model used is available on the hub at:30    [`impira/layoutlm-document-qa`](https://huggingface.co/impira/layoutlm-document-qa).31    """32    image = Image.open(BytesIO(image_file))33    output = pipe(image, question)34    return output35