capNimo commited on
Commit
2f97760
·
verified ·
1 Parent(s): 57e3cb8

Upload 8 files

Browse files
Dockerfile ADDED
@@ -0,0 +1,49 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # syntax=docker/dockerfile:1
2
+
3
+ # Comments are provided throughout this file to help you get started.
4
+ # If you need more help, visit the Dockerfile reference guide at
5
+ # https://docs.docker.com/engine/reference/builder/
6
+
7
+ ARG PYTHON_VERSION=3.10.9
8
+ FROM python:${PYTHON_VERSION}-slim as base
9
+
10
+ # Prevents Python from writing pyc files.
11
+ ENV PYTHONDONTWRITEBYTECODE=1
12
+
13
+ # Keeps Python from buffering stdout and stderr to avoid situations where
14
+ # the application crashes without emitting any logs due to buffering.
15
+ ENV PYTHONUNBUFFERED=1
16
+
17
+ WORKDIR /app
18
+
19
+ # Create a non-privileged user that the app will run under.
20
+ # See https://docs.docker.com/go/dockerfile-user-best-practices/
21
+ # ARG UID=10001
22
+ # RUN adduser \
23
+ # --disabled-password \
24
+ # --gecos "" \
25
+ # --home "/nonexistent" \
26
+ # --shell "/sbin/nologin" \
27
+ # --no-create-home \
28
+ # --uid "${UID}" \
29
+ # appuser
30
+
31
+ # Download dependencies as a separate step to take advantage of Docker's caching.
32
+ # Leverage a cache mount to /root/.cache/pip to speed up subsequent builds.
33
+ # Leverage a bind mount to requirements.txt to avoid having to copy them into
34
+ # into this layer.
35
+ RUN --mount=type=cache,target=/root/.cache/pip \
36
+ --mount=type=bind,source=requirements.txt,target=requirements.txt \
37
+ python -m pip install -r requirements.txt
38
+
39
+ # Switch to the non-privileged user to run the application.
40
+ # USER appuser
41
+
42
+ # Copy the source code into the container.
43
+ COPY . .
44
+
45
+ # Expose the port that the application listens on.
46
+ EXPOSE 8000
47
+
48
+ # Run the application.
49
+ CMD uvicorn 'main:app' --host=0.0.0.0 --port=8000
__pycache__/main.cpython-310.pyc ADDED
Binary file (743 Bytes). View file
 
__pycache__/model.cpython-310.pyc ADDED
Binary file (662 Bytes). View file
 
compose.yaml ADDED
@@ -0,0 +1,49 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Comments are provided throughout this file to help you get started.
2
+ # If you need more help, visit the Docker compose reference guide at
3
+ # https://docs.docker.com/compose/compose-file/
4
+
5
+ # Here the instructions define your application as a service called "server".
6
+ # This service is built from the Dockerfile in the current directory.
7
+ # You can add other services your application may depend on here, such as a
8
+ # database or a cache. For examples, see the Awesome Compose repository:
9
+ # https://github.com/docker/awesome-compose
10
+ services:
11
+ server:
12
+ build:
13
+ context: .
14
+ ports:
15
+ - 8000:8000
16
+
17
+ # The commented out section below is an example of how to define a PostgreSQL
18
+ # database that your application can use. `depends_on` tells Docker Compose to
19
+ # start the database before your application. The `db-data` volume persists the
20
+ # database data between container restarts. The `db-password` secret is used
21
+ # to set the database password. You must create `db/password.txt` and add
22
+ # a password of your choosing to it before running `docker compose up`.
23
+ # depends_on:
24
+ # db:
25
+ # condition: service_healthy
26
+ # db:
27
+ # image: postgres
28
+ # restart: always
29
+ # user: postgres
30
+ # secrets:
31
+ # - db-password
32
+ # volumes:
33
+ # - db-data:/var/lib/postgresql/data
34
+ # environment:
35
+ # - POSTGRES_DB=example
36
+ # - POSTGRES_PASSWORD_FILE=/run/secrets/db-password
37
+ # expose:
38
+ # - 5432
39
+ # healthcheck:
40
+ # test: [ "CMD", "pg_isready" ]
41
+ # interval: 10s
42
+ # timeout: 5s
43
+ # retries: 5
44
+ # volumes:
45
+ # db-data:
46
+ # secrets:
47
+ # db-password:
48
+ # file: db/password.txt
49
+
main.py ADDED
@@ -0,0 +1,29 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from model import model_pipeline
2
+
3
+ from typing import Union
4
+
5
+ from fastapi import FastAPI, UploadFile
6
+ import io
7
+ from PIL import Image
8
+
9
+ app = FastAPI()
10
+
11
+
12
+ @app.get("/")
13
+ def read_root():
14
+ return {"Hello": "World"}
15
+
16
+
17
+ @app.post("/ask")
18
+ def ask(text: str, image: UploadFile):
19
+ photos = image.file.read()
20
+
21
+ image = Image.open(io.BytesIO(photos))
22
+
23
+
24
+ result = model_pipeline(text, image)
25
+ return {"answer": result}
26
+
27
+
28
+
29
+
model.py ADDED
@@ -0,0 +1,18 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import ViltProcessor, ViltForQuestionAnswering
2
+ from PIL import Image
3
+
4
+ # 470MB
5
+ processor = ViltProcessor.from_pretrained("dandelin/vilt-b32-finetuned-vqa")
6
+ model = ViltForQuestionAnswering.from_pretrained("dandelin/vilt-b32-finetuned-vqa")
7
+
8
+
9
+ def model_pipeline(text: str, image: Image):
10
+ # prepare inputs
11
+ encoding = processor(image, text, return_tensors="pt")
12
+
13
+ # forward pass
14
+ outputs = model(**encoding)
15
+ logits = outputs.logits
16
+ idx = logits.argmax(-1).item()
17
+
18
+ return model.config.id2label[idx]
requirements.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ fastapi
2
+ Pillow
3
+ python-multipart
4
+ torch
5
+ transformers
6
+ uvicorn