Skip to content

Commit

Permalink
add api demo
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielSarmiento04 committed Aug 13, 2023
1 parent 3fa9864 commit 085a247
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 4 deletions.
28 changes: 28 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Select the base image
FROM python:3.10

# Set the working directory
WORKDIR /code

# Copy the requirements file
COPY ./requirements.txt /code/requirements.txt

# Copy the source code
COPY ./api /code/api
COPY ./cifar.h5 /code/cifar.h5
COPY ./api/ /code/api/
COPY ./cifar10/ /code/cifar10/


# Install ffmpeg
RUN apt-get update -y
RUN apt-get install ffmpeg libsm6 libxext6 libxext6 -y

# Install the dependencies
RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt


# Expose the port
EXPOSE 80

CMD ["uvicorn", "api:app", "--host", "0.0.0.0", "--port", "80"]
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ Resumen First architecture tested
![](docs/first_result.jpg)
[Complete graphics (Basic)](https://tensorboard.dev/experiment/82Hg4m0YQ1uMWkw69qSv6w/#scalars)


[Complete graphics (Best)](https://tensorboard.dev/experiment/jhqlZ8dBRtOzxMm9o8wQcw/#scalars)

![Transfer learning result](./docs/transfer_learning_result.jpg)

[Architecture using transfer learning](docs/model_plot3.png)
![Transfer learning result](./docs/transfer_learning_result.jpg)


[Complete Graphics (Transfer LEarning)](https://tensorboard.dev/experiment/o7f6pvAMT3KSpX88ge0h1Q/#scalars)

Expand Down
25 changes: 25 additions & 0 deletions api/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from fastapi import FastAPI

app = FastAPI(
title="Cifar 10 Api",
description='''
This is a simple api for cifar 10 dataset.
The classes trainer are the next:
- airplane
- automobile
- bird
- cat
- deer
- dog
- frog
- horse
- ship
- truck
''',
version="1.0",

)

from .views import *
38 changes: 38 additions & 0 deletions api/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
from . import app
from fastapi import UploadFile, File, HTTPException
import cv2
from cifar10 import Model
import numpy as np

model = Model()

ALLOW_EXTENSION = ['jpg', 'jpeg', 'png', ]

@app.get('/')
def index():
return {'message': 'Hello, World!'}

@app.post('/analyze_image')
async def analyze_image(file: UploadFile = File(...)):
'''
Analyze image and return the result
Args:
file (UploadFile): image file
Returns:
dict: result
'''
extension = file.filename.split('.')[-1].lower()

if extension not in ALLOW_EXTENSION:
raise HTTPException(status_code=400, detail='File extension not allowed')

contents = await file.read()
nparr = np.fromstring(contents, np.uint8)
img = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

result = model.predict(img)

return {'predict': result}
7 changes: 6 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,9 @@ opencv-python
ipykernel

#devs
matplotlib
matplotlib

#api
fastapi
uvicorn
python-multipart
7 changes: 6 additions & 1 deletion requirements_macos.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,9 @@ ipykernel


#dev
matplotlib
matplotlib

#api
fastapi
uvicorn
python-multipart

0 comments on commit 085a247

Please sign in to comment.