Gigishot commited on
Commit
ce9ec08
·
verified ·
1 Parent(s): 5c9ce3f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +72 -71
app.py CHANGED
@@ -1,71 +1,72 @@
1
- from flask import Flask, render_template, request
2
- from tensorflow.keras.models import load_model
3
- from tensorflow.keras.preprocessing import image
4
- import numpy as np
5
- import os
6
- import uuid
7
- import tensorflow as tf
8
- import random
9
-
10
- # Fix randomness for reproducibility
11
- os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
12
- tf.random.set_seed(42)
13
- np.random.seed(42)
14
- random.seed(42)
15
-
16
- app = Flask(__name__)
17
-
18
- # Load the model (only one model now)
19
- model = load_model("model/cat_dog_neither_classifier_new.h5", compile=False)
20
- # <-- your model file
21
-
22
- class_names = ['cat', 'dog', 'neither']
23
- UPLOAD_FOLDER = 'static/uploads'
24
- os.makedirs(UPLOAD_FOLDER, exist_ok=True)
25
-
26
- def preprocess_image(img_path):
27
- img = image.load_img(img_path, target_size=(224, 224)) # Ensure matches model input
28
- img_array = image.img_to_array(img) / 255.0
29
- img_array = np.expand_dims(img_array, axis=0)
30
- return img_array
31
-
32
- @app.route('/', methods=['GET'])
33
- def index():
34
- return render_template('upload.html')
35
-
36
- @app.route('/predict', methods=['POST'])
37
- def predict():
38
- if 'file' not in request.files:
39
- return "No file part", 400
40
-
41
- file = request.files['file']
42
- if file.filename == '':
43
- return "No selected file", 400
44
-
45
- filename = str(uuid.uuid4()) + os.path.splitext(file.filename)[1]
46
- img_path = os.path.join(UPLOAD_FOLDER, filename)
47
- file.save(img_path)
48
-
49
- # Preprocess image
50
- processed = preprocess_image(img_path)
51
-
52
- # Predict
53
- prediction = model.predict(processed)[0]
54
- prediction /= np.sum(prediction) # normalize
55
-
56
- class_index = int(np.argmax(prediction))
57
- confidence = round(float(np.max(prediction)) * 100, 2)
58
- final_class = class_names[class_index]
59
-
60
- return render_template(
61
- 'result.html',
62
- prediction=final_class,
63
- confidence=confidence,
64
- img_path='/' + img_path
65
- )
66
-
67
- if __name__ == '__main__':
68
- import os
69
- port = int(os.environ.get("PORT", 5000)) # Render sets PORT
70
- app.run(host='0.0.0.0', port=port, debug=False)
71
-
 
 
1
+ from flask import Flask, render_template, request
2
+ from tensorflow.keras.models import load_model
3
+ from tensorflow.keras.preprocessing import image
4
+ import numpy as np
5
+ import os
6
+ import uuid
7
+ import tensorflow as tf
8
+ import random
9
+
10
+ # Fix randomness for reproducibility
11
+ os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
12
+ tf.random.set_seed(42)
13
+ np.random.seed(42)
14
+ random.seed(42)
15
+
16
+ app = Flask(__name__)
17
+
18
+ # Load the model (only one model now)
19
+ model = load_model("model/cat_dog_neither_classifier_new.h5", compile=False)
20
+ # <-- your model file
21
+
22
+ class_names = ['cat', 'dog', 'neither']
23
+ UPLOAD_FOLDER = 'static/uploads'
24
+ os.makedirs(UPLOAD_FOLDER, exist_ok=True)
25
+
26
+ def preprocess_image(img_path):
27
+ img = image.load_img(img_path, target_size=(224, 224)) # Ensure matches model input
28
+ img_array = image.img_to_array(img) / 255.0
29
+ img_array = np.expand_dims(img_array, axis=0)
30
+ return img_array
31
+
32
+ @app.route('/', methods=['GET'])
33
+ def index():
34
+ return render_template('upload.html')
35
+
36
+ @app.route('/predict', methods=['POST'])
37
+ def predict():
38
+ if 'file' not in request.files:
39
+ return "No file part", 400
40
+
41
+ file = request.files['file']
42
+ if file.filename == '':
43
+ return "No selected file", 400
44
+
45
+ filename = str(uuid.uuid4()) + os.path.splitext(file.filename)[1]
46
+ img_path = os.path.join(UPLOAD_FOLDER, filename)
47
+ file.save(img_path)
48
+
49
+ # Preprocess image
50
+ processed = preprocess_image(img_path)
51
+
52
+ # Predict
53
+ prediction = model.predict(processed)[0]
54
+ prediction /= np.sum(prediction) # normalize
55
+
56
+ class_index = int(np.argmax(prediction))
57
+ confidence = round(float(np.max(prediction)) * 100, 2)
58
+ final_class = class_names[class_index]
59
+
60
+ return render_template(
61
+ 'result.html',
62
+ prediction=final_class,
63
+ confidence=confidence,
64
+ img_path='/' + img_path
65
+ )
66
+
67
+ if __name__ == '__main__':
68
+ import os
69
+ # Hugging Face uses 7860 by default.
70
+ # This line checks for a PORT variable but falls back to 7860.
71
+ port = int(os.environ.get("PORT", 7860))
72
+ app.run(host='0.0.0.0', port=port, debug=False)