Eman90 commited on
Commit
e12df45
1 Parent(s): 2f40e7c

Upload 3 files

Browse files
Files changed (3) hide show
  1. Dockerfile +25 -0
  2. app.py +55 -0
  3. requirements.txt +11 -0
Dockerfile ADDED
@@ -0,0 +1,25 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM python:3.9
2
+
3
+ # Create a new user 'user' with user id 1000
4
+ RUN useradd -m -u 1000 user
5
+
6
+ # Set the working directory to /app
7
+ WORKDIR /app
8
+
9
+ # Copy the requirements.txt file into the container at /app/requirements.txt
10
+ COPY --chown=user ./requirements.txt requirements.txt
11
+
12
+ # Install the Python dependencies from requirements.txt
13
+ RUN pip install --no-cache-dir --upgrade -r requirements.txt
14
+
15
+ # Install wget to ensure the next RUN command works
16
+ RUN apt-get update && apt-get install -y wget && rm -rf /var/lib/apt/lists/*
17
+
18
+ # Download the model file using wget
19
+ RUN wget https://huggingface.co/Ahmed007/chest_xray/resolve/main/model.h5 -O model.h5 && chown user:user model.h5
20
+
21
+ # Copy the current directory into the container at /app
22
+ COPY --chown=user . /app
23
+
24
+ # Command to run the application
25
+ CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
app.py ADDED
@@ -0,0 +1,55 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI, File, UploadFile, HTTPException
2
+ from fastapi.responses import JSONResponse
3
+ from tensorflow.keras.models import load_model
4
+ from tensorflow.keras.preprocessing import image
5
+ import numpy as np
6
+ import logging
7
+ from PIL import Image
8
+ import io
9
+
10
+ # Configure logging
11
+ logging.basicConfig(level=logging.DEBUG)
12
+
13
+ # Initialize FastAPI app
14
+ app = FastAPI()
15
+
16
+ # Load your trained model
17
+ model = load_model('model.h5')
18
+ class_names = ['Normal', 'bacteria', 'virus']
19
+
20
+ def preprocess_image(img, target_size):
21
+ """Resize and preprocess the image for the model."""
22
+ if img.mode != "RGB":
23
+ img = img.convert("RGB")
24
+ img = img.resize(target_size)
25
+ img_array = image.img_to_array(img)
26
+ img_array = np.expand_dims(img_array, axis=0) # Add batch dimension
27
+ return img_array
28
+
29
+ @app.post("/predict")
30
+ async def predict(file: UploadFile = File(...)):
31
+ if not file:
32
+ raise HTTPException(status_code=400, detail="No file provided")
33
+ try:
34
+ # Read the file's content into a BytesIO object
35
+ img_bytes = io.BytesIO(await file.read())
36
+
37
+ # Use PIL to open the image
38
+ img = Image.open(img_bytes)
39
+ img_array = preprocess_image(img, (224, 224))
40
+
41
+ # Make prediction
42
+ predictions = model.predict(img_array)
43
+
44
+ predicted_class = np.argmax(predictions, axis=1)
45
+
46
+ # Return the prediction
47
+ predictions = {
48
+ 'class': class_names[predicted_class[0]],
49
+ 'confidence': float(predictions[0][predicted_class[0]])
50
+ }
51
+ return JSONResponse(content=predictions)
52
+ except Exception as e:
53
+ logging.debug(f"Error processing the file: {str(e)}")
54
+ raise HTTPException(status_code=500, detail=f"Error processing the file: {str(e)}")
55
+
requirements.txt ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ keras
2
+ tensorflow
3
+ numpy
4
+ pandas
5
+ matplotlib
6
+ flask
7
+ gensim
8
+ huggingface-hub
9
+ gunicorn
10
+ fastapi
11
+ uvicorn[standard]