Skip to content

Commit

Permalink
Las changes (#30)
Browse files Browse the repository at this point in the history
* backend folder structure

* packages versions in requirements.txt

* pydantic models defined

* refactoring main.py file (in proggress)

* validate form file input

* Implement notification component

* gitignore backend folder

* validators in pydantic models

* refactoring main.py file (complete)

* cleaner backend folder

* FIX: load varenv since .env file

* allow front url in cors & added it env var

* CI/CD added new env variable & manage repo paths

* Fix layout (#28)

* add dependencies to render markdown and typography

* change layout from row to column, change rendering logic

* change all display text to spanish

* change theme from forest to dracula

* use enviroment variable for the backend URL

* implement loading state

* Update HealthRecommendations.jsx

* Last changes (#29)

* Split inputs for better UX

* remove *multiple* attribute from inputs

* url vite backend pipeline CICD

* models without name validation

* temporal name in web

* unique list to send to backend

* refactoring checking files types-based

---------

Co-authored-by: JVPH <[email protected]>

---------

Co-authored-by: JVPH <[email protected]>
Co-authored-by: JVPH <[email protected]>
  • Loading branch information
3 people authored May 7, 2024
1 parent 02b9751 commit e29e158
Show file tree
Hide file tree
Showing 16 changed files with 801 additions and 161 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/frontend_push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ env:
ECS_SERVICE: medical_assistant_frontend
ECR_REPOSITORY: 891377268337.dkr.ecr.us-east-1.amazonaws.com/medical_assistant_frontend
API_KEY: ${{secrets.API_KEY}}
VITE_BACKEND_URL: http://lb-medical-assistant-503996398.us-east-1.elb.amazonaws.com:8000/

jobs:
build_image_frontend:
Expand All @@ -39,6 +40,8 @@ jobs:
IMAGE_TAG: lastest
run: |
echo VITE_BACKEND_URL=${{env.VITE_BACKEND_URL}} >> ./frontend/.env
docker build -t $ECR_REPOSITORY:$IMAGE_TAG ./frontend
docker push $ECR_REPOSITORY:$IMAGE_TAG
Expand Down
28 changes: 15 additions & 13 deletions backend/api/v1/post_suggestions.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@

from controllers.v1.history_clinic import extract_text_from_pdf
from controllers.v1.MRI_image import predict_tumor_by_img
from controllers.v1.validate_files import processing_file_type_based
from controllers.v1.validate_files import (
check_files,
type_based_checking,
)


from dotenv import load_dotenv
from model import GenerativeModel
Expand Down Expand Up @@ -34,24 +38,22 @@


@router.post(
"/files",
"/uploadfiles",
status_code=200,
dependencies=[
Depends(processing_file_type_based),
],
response_model=dict[str, str],
dependencies=[Depends(check_files)],
)
async def predict_tumor(
files: list[UploadFile],
):
img = None
pdf = None

for file in files:
if file.filename.split(".")[0] == "brain_mri":
img = file
elif file.filename.split(".")[0] == "clinical_history":
pdf = file
# Check the type of the files pydantic model based
dict_files = {
file.filename.split(".")[1]: type_based_checking(file) for file in files
}

# Get the image and the pdf
img = dict_files.get("jpeg") or dict_files.get("png") or dict_files.get("jpg")
pdf = dict_files.get("pdf")

# Call the function to predict the tumor
classification = await predict_tumor_by_img(img=img)
Expand Down
67 changes: 31 additions & 36 deletions backend/controllers/v1/validate_files.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# Check if the number of files is correct
from fastapi import HTTPException, UploadFile
from pydantic import ValidationError
import os

from schemas.ClinicHistoryFileUploaded import ClinicHistoryFileUploaded
from schemas.MRIFileUploaded import MRIFileUploaded
Expand All @@ -11,6 +10,17 @@ def validate_quantity_files(files: list):
return len(files) != 2


def check_files(files: list[UploadFile]):
# Check if each file has an extension
if not all(["." in file.filename for file in files]):
raise HTTPException(status_code=400, detail="File without extension.")

if validate_quantity_files(files):
raise HTTPException(
status_code=400, detail="The number of files is not correct."
)


def is_type_PDF(type_file):
return type_file == "application/pdf"

Expand All @@ -19,40 +29,25 @@ def is_type_image(type_file):
return type_file in ["image/jpeg", "image/png", "image/jpg"]


def processing_file_type_based(files: list[UploadFile]):
if validate_quantity_files(files):
def type_based_checking(file: MRIFileUploaded | ClinicHistoryFileUploaded):
# Check the model type based on defined schema
if is_type_image(file.content_type):
try:
MRIFileUploaded(file=file, type=file.content_type, size=file.size)

except ValidationError as e:
raise HTTPException(status_code=400, detail=str(e).splitlines()[2])

elif is_type_PDF(file.content_type):
try:
ClinicHistoryFileUploaded(file=file, type=file.content_type, size=file.size)

except ValidationError as e:
raise HTTPException(status_code=400, detail=str(e).splitlines()[2])

else:
raise HTTPException(
status_code=400, detail="The number of files is not correct."
status_code=400,
detail=f"The type of the file {file.filename} is not supported.",
)

img = None
pdf = None

for file in files:
# Assign the file to the corresponding variable
if is_type_image(file.content_type):
try:
name = os.path.splitext(file.filename)[0]
img = MRIFileUploaded(
file=file, name=name, type=file.content_type, size=file.size
)

except ValidationError as e:
raise HTTPException(status_code=400, detail=str(e).splitlines()[2])

elif is_type_PDF(file.content_type):
try:
name = os.path.splitext(file.filename)[0]
pdf = ClinicHistoryFileUploaded(
file=file, name=name, type=file.content_type, size=file.size
)

except ValidationError as e:
raise HTTPException(status_code=400, detail=str(e).splitlines()[2])

else:
raise HTTPException(
status_code=400,
detail=f"The type of the file {file.filename} is not supported.",
)
return [img, pdf]
return file
1 change: 0 additions & 1 deletion backend/schemas/ClinicHistoryFileUploaded.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@


class ClinicHistoryFileUploaded(BaseModel):
name: Literal["clinical_history"]
file: UploadFile
type: Literal["application/pdf"]
size: Annotated[int, field_validator("size")]
Expand Down
1 change: 0 additions & 1 deletion backend/schemas/MRIFileUploaded.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@


class MRIFileUploaded(BaseModel):
name: Literal["brain_mri"]
file: UploadFile
type: Literal["image/jpeg", "image/png", "image/jpg"]
size: int
Expand Down
1 change: 1 addition & 0 deletions frontend/.example.env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
VITE_BACKEND_URL="insert backend url"
2 changes: 2 additions & 0 deletions frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"react-dom": "^18.2.0"
},
"devDependencies": {
"@tailwindcss/typography": "^0.5.13",
"@types/react": "^18.2.66",
"@types/react-dom": "^18.2.22",
"@vitejs/plugin-react": "^4.2.1",
Expand All @@ -25,6 +26,7 @@
"eslint-plugin-react-refresh": "^0.4.6",
"postcss": "^8.4.38",
"prettier": "^3.2.5",
"react-markdown": "^9.0.1",
"tailwindcss": "^3.4.3",
"vite": "^5.2.0"
}
Expand Down
Loading

0 comments on commit e29e158

Please sign in to comment.