viveksardey commited on
Commit
3f4431f
·
verified ·
1 Parent(s): 0993db2

Upload folder using huggingface_hub

Browse files
Files changed (4) hide show
  1. Dockerfile +16 -0
  2. app.py +101 -0
  3. pneumonia_resnet_model.h5 +3 -0
  4. requirements.txt +11 -0
Dockerfile ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM python:3.9-slim
2
+
3
+ # Set the working directory inside the container
4
+ WORKDIR /app
5
+
6
+ # Copy all files from the current directory to the container's working directory
7
+ COPY . .
8
+
9
+ # Install dependencies from the requirements file without using cache to reduce image size
10
+ RUN pip install --no-cache-dir --upgrade -r requirements.txt
11
+
12
+ # Define the command to start the application using Gunicorn with 4 worker processes
13
+ # - `-w 4`: Uses 4 worker processes for handling requests
14
+ # - `-b 0.0.0.0:7860`: Binds the server to port 7860 on all network interfaces
15
+ # - `app:app`: Runs the Flask app (assuming `app.py` contains the Flask instance named `app`)
16
+ CMD ["gunicorn", "-w", "4", "-b", "0.0.0.0:7860", "app:pneumonia_detector_api"]
app.py ADDED
@@ -0,0 +1,101 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Import necessary libraries
2
+ import numpy as np
3
+ import tensorflow as tf
4
+ from flask import Flask, request, jsonify
5
+ from tensorflow.keras.models import load_model
6
+ import os
7
+
8
+ # Initialize Flask App
9
+ pneumonia_detector_api = Flask("Pneumonia Detection API")
10
+
11
+ # Initialize Flask App
12
+ pneumonia_detector_api = Flask("Pneumonia Detection API")
13
+
14
+ # Load trained model
15
+ model = load_model("/content/drive/MyDrive/AIML Practice/Python Basics/deployment_files/capstone_backend_files/pneumonia_resnet_model.h5")
16
+
17
+ print("Model loaded successfully.")
18
+
19
+ IMG_SIZE = 224
20
+
21
+
22
+ # -------------------------------
23
+ # Home Endpoint
24
+ # -------------------------------
25
+ @pneumonia_detector_api.get('/')
26
+ def home():
27
+ return "Welcome to the Pneumonia Detection API"
28
+
29
+
30
+ # -------------------------------
31
+ # Image Preprocessing
32
+ # -------------------------------
33
+ def preprocess_image(img_array):
34
+
35
+ # Resize image
36
+ img = tf.image.resize(img_array, (IMG_SIZE, IMG_SIZE))
37
+
38
+ # Normalize
39
+ img = img / 255.0
40
+
41
+ # Ensure 3 channels
42
+ if img.shape[-1] == 1:
43
+ img = np.repeat(img, 3, axis=-1)
44
+
45
+ # Add batch dimension
46
+ img = np.expand_dims(img, axis=0)
47
+
48
+ return img
49
+
50
+
51
+ # -------------------------------
52
+ # Prediction Endpoint
53
+ # -------------------------------
54
+ @pneumonia_detector_api.post('/v1/predict')
55
+ def predict_pneumonia():
56
+
57
+ if 'file' not in request.files:
58
+ return jsonify({"error": "No file uploaded"}), 400
59
+
60
+ file = request.files['file']
61
+
62
+ try:
63
+
64
+ # Load numpy image
65
+ img_array = np.load(file)
66
+
67
+ # Preprocess
68
+ processed_img = preprocess_image(img_array)
69
+
70
+ # Predict
71
+ prediction = model.predict(processed_img)[0][0]
72
+
73
+ probability = float(prediction)
74
+
75
+ if probability > 0.5:
76
+ label = "Pneumonia"
77
+ else:
78
+ label = "Normal"
79
+
80
+ result = {
81
+ "Predicted_Class": label,
82
+ "Probability_Pneumonia": round(probability, 4)
83
+ }
84
+
85
+ return jsonify(result)
86
+
87
+ except Exception as e:
88
+
89
+ return jsonify({"error": str(e)}), 500
90
+
91
+
92
+ # -------------------------------
93
+ # Run Flask Server
94
+ # -------------------------------
95
+ if __name__ == "__main__":
96
+
97
+ pneumonia_detector_api.run(
98
+ host="0.0.0.0",
99
+ port=5000,
100
+ debug=True
101
+ )
pneumonia_resnet_model.h5 ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:249c043f1fa1352615ab6d9e9b3c99fc8ca3acc5f16a2c1a656b9640c7805eee
3
+ size 169100952
requirements.txt ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ pandas==2.2.2
2
+ numpy==2.0.2
3
+ scikit-learn==1.6.1
4
+ xgboost==2.1.4
5
+ joblib==1.4.2
6
+ Werkzeug==2.2.2
7
+ flask==2.2.2
8
+ gunicorn==20.1.0
9
+ requests==2.28.1
10
+ uvicorn[standard]
11
+ streamlit==1.43.2