charan commited on
Commit
15beb52
1 Parent(s): 5b86b7f

Deployment ready

Browse files
Files changed (5) hide show
  1. Dockerfile +11 -0
  2. app.py +64 -0
  3. keras_model.h5 +3 -0
  4. labels.txt +2 -0
  5. requirements.txt +54 -0
Dockerfile ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM python:3.9
2
+
3
+ WORKDIR /code
4
+
5
+ COPY ./requirements.txt /code/requirements.txt
6
+
7
+ RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
8
+
9
+ COPY . .
10
+
11
+ CMD ["gunicorn","-b","0.0.0.0:7860","app:app"]
app.py ADDED
@@ -0,0 +1,64 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from flask import Flask, request, jsonify
2
+ from keras.models import load_model
3
+ from PIL import Image, ImageOps
4
+ import numpy as np
5
+ from flask_cors import CORS
6
+
7
+ app = Flask(__name__)
8
+ CORS(app,origin=['*','http://localhost:3000'],allow_headers=['Content-Type','Authorization','Access-Control-Allow-Credentials','Access-Control-Allow-Origin','Access-Control-Allow-Headers','x-xsrf-token','Access-Control-Allow-Methods','Access-Control-Allow-Headers','Access-Control-Allow-Headers','Access-Control-Allow-Origin','Access-Control-Allow-Methods','Authorization','X-Requested-With','Access-Control-Request-Headers','Access-Control-Request-Method'])
9
+ port = 4000
10
+
11
+ # Load the model
12
+ model = load_model("keras_model.h5", compile=False)
13
+
14
+ # Load the labels
15
+ class_names = [line.strip() for line in open("labels.txt", "r").readlines()]
16
+
17
+ # Confidence threshold for predictions
18
+ confidence_threshold = 0.7
19
+
20
+ @app.route('/')
21
+ def hello():
22
+ return f"Server is running on port {port}"
23
+
24
+ @app.route('/predict', methods=['POST'])
25
+ def predict():
26
+ if 'file' not in request.files:
27
+ return jsonify({'error': 'No file provided'}), 400
28
+
29
+ file = request.files['file']
30
+ if file.filename == '':
31
+ return jsonify({'error': 'No selected file'}), 400
32
+
33
+ if file:
34
+ # Process the image file
35
+ image = Image.open(file.stream).convert("RGB")
36
+ size = (224, 224)
37
+ image = ImageOps.fit(image, size, Image.Resampling.LANCZOS)
38
+
39
+ # Convert image to numpy array and normalize
40
+ image_array = np.asarray(image)
41
+ normalized_image_array = (image_array.astype(np.float32) / 127.5) - 1
42
+
43
+ # Prepare the image for prediction
44
+ data = np.ndarray(shape=(1, 224, 224, 3), dtype=np.float32)
45
+ data[0] = normalized_image_array
46
+
47
+ # Predict
48
+ prediction = model.predict(data)
49
+ max_confidence = np.max(prediction)
50
+
51
+ if max_confidence >= confidence_threshold:
52
+ index = np.argmax(prediction)
53
+ class_name = class_names[index]
54
+ confidence_score = float(prediction[0][index])
55
+ else:
56
+ # Classify as "other" if confidence is below threshold
57
+ class_name = "Other"
58
+ confidence_score = float(max_confidence)
59
+
60
+ # Return the result
61
+ response = jsonify({'class': class_name, 'confidence_score': confidence_score})
62
+ return response
63
+ if __name__ == '__main__':
64
+ app.run(debug=True, port=port)
keras_model.h5 ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:afe614bfd48d792b6b0f3654406cbada6cd13f0a2ad6a98c6655b5636a50ee15
3
+ size 2453432
labels.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ 0 Cataract
2
+ 1 Normal
requirements.txt ADDED
@@ -0,0 +1,54 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ absl-py==2.0.0
2
+ astunparse==1.6.3
3
+ blinker==1.7.0
4
+ cachetools==5.3.2
5
+ certifi==2023.11.17
6
+ charset-normalizer==3.3.2
7
+ click==8.1.7
8
+ colorama==0.4.6
9
+ dm-tree==0.1.8
10
+ Flask==3.0.0
11
+ Flask-Cors==4.0.0
12
+ flatbuffers==23.5.26
13
+ gast==0.5.4
14
+ google-auth==2.25.2
15
+ google-auth-oauthlib==1.1.0
16
+ google-pasta==0.2.0
17
+ grpcio==1.60.0
18
+ gunicorn==21.2.0
19
+ h5py==3.10.0
20
+ idna==3.6
21
+ itsdangerous==2.1.2
22
+ Jinja2==3.1.2
23
+ keras==2.15.0
24
+ libclang==16.0.6
25
+ Markdown==3.5.1
26
+ markdown-it-py==3.0.0
27
+ MarkupSafe==2.1.3
28
+ mdurl==0.1.2
29
+ ml-dtypes==0.2.0
30
+ namex==0.0.7
31
+ numpy==1.26.2
32
+ oauthlib==3.2.2
33
+ opt-einsum==3.3.0
34
+ packaging==23.2
35
+ Pillow==10.1.0
36
+ protobuf==4.23.4
37
+ pyasn1==0.5.1
38
+ pyasn1-modules==0.3.0
39
+ Pygments==2.17.2
40
+ requests==2.31.0
41
+ requests-oauthlib==1.3.1
42
+ rich==13.7.0
43
+ rsa==4.9
44
+ six==1.16.0
45
+ tensorboard==2.15.1
46
+ tensorboard-data-server==0.7.2
47
+ tensorflow==2.15.0
48
+ tensorflow-estimator==2.15.0
49
+ tensorflow-io-gcs-filesystem==0.31.0
50
+ termcolor==2.4.0
51
+ typing_extensions==4.9.0
52
+ urllib3==2.1.0
53
+ Werkzeug==3.0.1
54
+ wrapt==1.14.1