Akshay-Vs commited on
Commit
69ac9b0
1 Parent(s): 0b86ed3

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -0
app.py ADDED
@@ -0,0 +1,44 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import numpy as np
3
+ import tensorflow as tf
4
+ import streamlit as st
5
+ from tensorflow.keras.preprocessing.image import load_img, img_to_array
6
+ from flask import Flask, request, jsonify
7
+
8
+ app = Flask(__name__)
9
+ st.title("Nova'23 Classification Model API")
10
+ st.write("Listening...")
11
+
12
+ @app.route('/predict', methods=['POST'])
13
+ def predict_image():
14
+ # Check if an image was uploaded
15
+ if 'file' not in request.files:
16
+ return 'No file uploaded'
17
+ file = request.files['file']
18
+
19
+ # Save the image to a temporary file
20
+ file_path = 'temp_image.jpg'
21
+ file.save(file_path)
22
+
23
+ # Load and preprocess the image
24
+ height = 180
25
+ width = 180
26
+ channels = 3
27
+ img = load_img(file_path, target_size=(height, width))
28
+ img_array = img_to_array(img)
29
+ img_array = img_array / 255.0
30
+ img_array = tf.reshape(img_array, [1, height, width, channels])
31
+
32
+ # Load the model and make a prediction
33
+ model = tf.keras.models.load_model('nova.h5')
34
+ prediction = model.predict(img_array)
35
+ st.write("Prediction: ", prediction)
36
+
37
+ # Delete the temporary file
38
+ os.remove(file_path)
39
+
40
+ # Return the prediction as JSON
41
+ return jsonify({'prediction': 'Pneumonia' if prediction[0][0] > 0.5 else 'Normal'})
42
+
43
+ if __name__ == '__main__':
44
+ app.run()