xcurvnubaim commited on
Commit
38d4385
1 Parent(s): 43f794d

feat: init api

Browse files
Files changed (4) hide show
  1. Dockerfile +16 -0
  2. README.md +1 -0
  3. app.py +34 -0
  4. requirements.txt +11 -0
Dockerfile ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # read the doc: https://huggingface.co/docs/hub/spaces-sdks-docker
2
+ # you will also find guides on how best to write your Dockerfile
3
+
4
+ FROM python:3.9
5
+
6
+ WORKDIR /code
7
+
8
+ COPY ./requirements.txt /code/requirements.txt
9
+
10
+ COPY ./app /code/app
11
+
12
+ RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
13
+
14
+ COPY . .
15
+
16
+ CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "7860"]
README.md CHANGED
@@ -4,6 +4,7 @@ emoji: 🏢
4
  colorFrom: blue
5
  colorTo: purple
6
  sdk: docker
 
7
  pinned: false
8
  ---
9
 
 
4
  colorFrom: blue
5
  colorTo: purple
6
  sdk: docker
7
+ app_port:
8
  pinned: false
9
  ---
10
 
app.py ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import numpy as np
2
+ from fastapi import FastAPI, File, UploadFile
3
+ import tensorflow as tf
4
+ from io import StringIO
5
+ from PIL import Image
6
+
7
+ app = FastAPI()
8
+
9
+ labels = []
10
+ model = tf.keras.models.load_model('./models.h5')
11
+ with open("labels.txt") as f:
12
+ for line in f:
13
+ labels.append(line.replace('\n', ''))
14
+
15
+ def classify_image(inp):
16
+ # Create a copy of the input array to avoid reference issues
17
+ inp_copy = np.copy(inp)
18
+ # Resize the input image to the expected shape (224, 224)
19
+ inp_copy = Image.fromarray(inp_copy)
20
+ inp_copy = inp_copy.resize((224, 224))
21
+ inp_copy = np.array(inp_copy)
22
+ inp_copy = inp_copy.reshape((-1, 224, 224, 3))
23
+ inp_copy = tf.keras.applications.efficientnet.preprocess_input(inp_copy)
24
+ prediction = model.predict(inp_copy).flatten()
25
+ confidences = {labels[i]: float(prediction[i]) for i in range(90)}
26
+ return confidences
27
+
28
+ @app.post("/predict/")
29
+ async def predict(file: UploadFile = File(...)):
30
+ contents = await file.read()
31
+ img = Image.open(StringIO(contents))
32
+ img = np.array(img)
33
+ confidences = classify_image(img)
34
+ return confidences
requirements.txt ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ tensorflow
2
+ tensorflow-estimator==2.15.0
3
+ tensorflow-gcs-config==2.15.0
4
+ tensorflow-hub==0.16.1
5
+ tensorflow-io-gcs-filesystem==0.36.0
6
+ tensorflow-metadata==1.15.0
7
+ tensorflow-probability==0.23.0
8
+ fastapi
9
+ numpy==1.25.2
10
+ Pillow==9.4.0
11
+ keras==2.15.0