blaxx14 commited on
Commit
20e43d5
·
1 Parent(s): 6f6f4ff

Add application file

Browse files
Dockerfile ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM python:3.9
2
+
3
+ RUN useradd -m -u 1000 user
4
+ USER user
5
+ ENV PATH="/home/user/.local/bin:$PATH"
6
+
7
+ WORKDIR /app
8
+
9
+ COPY --chown=user ./requirements.txt requirements.txt
10
+ RUN pip install --no-cache-dir --upgrade -r requirements.txt
11
+
12
+ COPY --chown=user . /app
13
+ CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
__pycache__/app.cpython-310.pyc ADDED
Binary file (1.5 kB). View file
 
app.py ADDED
@@ -0,0 +1,40 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI, UploadFile, File
2
+ import tensorflow as tf
3
+ import numpy as np
4
+ from PIL import Image
5
+ import io
6
+ import requests
7
+ import os
8
+
9
+ MODEL_URL = "https://huggingface.co/blaxx14/cat-vs-dog-inceptionv3/resolve/main/cat_dog_inception_v3.h5"
10
+ MODEL_PATH = "cat_dog_inception_v3.h5"
11
+
12
+ app = FastAPI()
13
+
14
+ def download_model():
15
+ if not os.path.exists(MODEL_PATH):
16
+ print("Downloading model...")
17
+ response = requests.get(MODEL_URL)
18
+ with open(MODEL_PATH, "wb") as f:
19
+ f.write(response.content)
20
+ print("Model downloaded!")
21
+
22
+ download_model()
23
+
24
+ print("Loading model...")
25
+ model = tf.keras.models.load_model(MODEL_PATH)
26
+ print("Model loaded!")
27
+
28
+ def preprocess_image(image):
29
+ img = Image.open(io.BytesIO(image)).convert("RGB")
30
+ img = img.resize((150, 150))
31
+ img = np.array(img) / 255.0
32
+ img = np.expand_dims(img, axis=0)
33
+ return img
34
+
35
+ @app.post("/predict/")
36
+ async def predict(file: UploadFile = File(...)):
37
+ image = await file.read()
38
+ processed_img = preprocess_image(image)
39
+ prediction = model.predict(processed_img)
40
+ return {"prediction": prediction.tolist()}
cat_dog_inception_v3.h5 ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:5706614a14c05b6be091aecb989b31cb236bbeece2377bac949da33c37f9157b
3
+ size 505377176
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ fastapi
2
+ pillow
3
+ tensorflow==2.18.0
4
+ uvicorn[standard]