dog commited on
Commit
1255ea4
1 Parent(s): 66ca2d1

Upload . with huggingface_hub

Browse files
Files changed (3) hide show
  1. Dockerfile +31 -0
  2. main.py +61 -0
  3. requirements.txt +8 -0
Dockerfile ADDED
@@ -0,0 +1,31 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Use the official Python 3.9 image
2
+ FROM python:3.9
3
+
4
+ RUN apt-get update && apt-get install -y \
5
+ tesseract-ocr-all \
6
+ && rm -rf /var/lib/apt/lists/*
7
+
8
+ # Set the working directory to /code
9
+ WORKDIR /code
10
+
11
+ # Copy the current directory contents into the container at /code
12
+ COPY ./requirements.txt /code/requirements.txt
13
+
14
+ # Install requirements.txt
15
+ RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
16
+
17
+ # Set up a new user named "user" with user ID 1000
18
+ RUN useradd -m -u 1000 user
19
+ # Switch to the "user" user
20
+ USER user
21
+ # Set home to the user's home directory
22
+ ENV HOME=/home/user \
23
+ PATH=/home/user/.local/bin:$PATH
24
+
25
+ # Set the working directory to the user's home directory
26
+ WORKDIR $HOME/app
27
+
28
+ # Copy the current directory contents into the container at $HOME/app setting the owner to the user
29
+ COPY --chown=user . $HOME/app
30
+
31
+ CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"]
main.py ADDED
@@ -0,0 +1,61 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from base64 import b64decode, b64encode
2
+ from io import BytesIO
3
+
4
+ from fastapi import FastAPI, File, Form
5
+ from PIL import Image
6
+ from transformers import pipeline
7
+
8
+
9
+ description = """
10
+ ## DocQA with 🤗 transformers, FastAPI, and Docker
11
+
12
+ This app shows how to do Document Question Answering using
13
+ FastAPI in a Docker Space 🚀
14
+ Check out the docs for the `/predict` endpoint below to try it out!
15
+
16
+ ## Usage
17
+
18
+ #### Python
19
+
20
+ ```python
21
+ import requests
22
+
23
+ url = "https://dog-fastapi-document-qa.hf.space/predict"
24
+ r = requests.get(
25
+ url,
26
+ files={"image_file": open("invoice.png", "rb")},
27
+ data={"question": "What is the invoice number?"}
28
+ )
29
+ print(r.json())
30
+ ```
31
+
32
+ #### Curl
33
+
34
+ ```bash
35
+ curl -X 'POST' \
36
+ 'https://dog-fastapi-document-qa.hf.space/predict' \
37
+ -H 'accept: application/json' \
38
+ -H 'Content-Type: multipart/form-data' \
39
+ -F 'image_file=@invoice.png;type=image/png' \
40
+ -F 'question=What is the invoice number?'
41
+ ```
42
+ """
43
+
44
+ # NOTE - we configure docs_url to serve the interactive Docs at the root path
45
+ # of the app. This way, we can use the docs as a landing page for the app on Spaces.
46
+ app = FastAPI(docs_url="/", description=description)
47
+
48
+ pipe = pipeline("document-question-answering", model="impira/layoutlm-document-qa")
49
+
50
+
51
+ @app.post("/predict")
52
+ def predict(image_file: bytes = File(...), question: str = Form(...)):
53
+ """
54
+ Using the document-question-answering pipeline from `transformers`, take
55
+ a given input document (image) and a question about it, and return the
56
+ predicted answer. The model used is available on the hub at:
57
+ [`impira/layoutlm-document-qa`](https://huggingface.co/impira/layoutlm-document-qa).
58
+ """
59
+ image = Image.open(BytesIO(image_file))
60
+ output = pipe(image, question)
61
+ return output
requirements.txt ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ fastapi==0.74.*
2
+ requests==2.27.*
3
+ uvicorn[standard]==0.17.*
4
+ sentencepiece==0.1.*
5
+ torch==1.11.*
6
+ transformers[vision]==4.*
7
+ pytesseract==0.3.10
8
+ python-multipart==0.0.6