Skip to content

Commit

Permalink
mnist path adjustment
Browse files Browse the repository at this point in the history
  • Loading branch information
Anemosx committed Jun 24, 2024
1 parent 471e64b commit 4f519f3
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 10 deletions.
14 changes: 7 additions & 7 deletions app/routers/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,23 @@

router = APIRouter()

mnist_html_file_path = os.path.join("static", "mnist", "index.html")
with open(mnist_html_file_path, "r") as file:
mnist_content = file.read()
base_html_file_path = os.path.join("static", "base", "index.html")
with open(base_html_file_path, "r") as file:
base_content = file.read()


@router.get("/mnist", response_class=HTMLResponse)
@router.get("/", response_class=HTMLResponse)
async def read_root() -> HTMLResponse:
"""
Serve the MNIST HTML page from the static directory.
Serve the Base HTML page from the static directory.
Returns
-------
response : HTMLResponse
An HTML response containing the MNIST page content.
An HTML response containing the Base page content.
"""

response = HTMLResponse(content=mnist_content)
response = HTMLResponse(content=base_content)
response.headers["Cache-Control"] = "public, max-age=3600"

return response
25 changes: 24 additions & 1 deletion app/routers/mnist.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import base64
import io
import os

from PIL import Image
from fastapi import APIRouter, HTTPException
from fastapi.responses import JSONResponse
from fastapi.responses import JSONResponse, HTMLResponse
from pydantic import BaseModel

from app.vision.mnist import predict
Expand All @@ -24,6 +25,28 @@ class ImageData(BaseModel):
image: str


mnist_html_file_path = os.path.join("static", "mnist", "index.html")
with open(mnist_html_file_path, "r") as file:
mnist_content = file.read()


@router.get("/mnist", response_class=HTMLResponse)
async def read_mnist() -> HTMLResponse:
"""
Serve the MNIST HTML page from the static directory.
Returns
-------
response : HTMLResponse
An HTML response containing the MNIST page content.
"""

response = HTMLResponse(content=mnist_content)
response.headers["Cache-Control"] = "public, max-age=3600"

return response


@router.post("/predict-mnist")
async def predict_digit(data: ImageData) -> JSONResponse:
"""
Expand Down
1 change: 0 additions & 1 deletion app/vision/mnist.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import io
import os

import numpy as np
Expand Down
10 changes: 10 additions & 0 deletions static/base/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Home</title>
</head>
<body>

</body>
</html>
2 changes: 1 addition & 1 deletion tests/app/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class TestBase:

@pytest.mark.asyncio
def test_get_root(self, client: TestClient) -> None:
response = client.get(f"/mnist")
response = client.get(f"/")

assert response.status_code == status.HTTP_200_OK
assert response.headers["Content-Type"] == "text/html; charset=utf-8"
Expand Down
13 changes: 13 additions & 0 deletions tests/app/test_predict.py → tests/app/test_mnist.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,25 @@
from unittest.mock import MagicMock, patch

import numpy as np
import pytest
from fastapi import status
from fastapi.testclient import TestClient


class TestPredictDigit:

@pytest.mark.asyncio
def test_get_mnist(self, client: TestClient) -> None:
response = client.get(f"/mnist")

assert response.status_code == status.HTTP_200_OK
assert response.headers["Content-Type"] == "text/html; charset=utf-8"

html_content = response.text

assert "html" in html_content
assert "<body>" in html_content

@patch("app.routers.mnist.predict_digit")
def test_predict_digit(
self, mock: MagicMock, client: TestClient, create_mnist_image: tuple[str, int]
Expand Down

0 comments on commit 4f519f3

Please sign in to comment.