sonuprasad commited on
Commit
8085865
1 Parent(s): e2b9b7e

Upload app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +76 -76
app.py CHANGED
@@ -1,76 +1,76 @@
1
- from flask import Flask, request, jsonify, render_template, send_from_directory
2
- from tensorflow.keras.models import load_model
3
- from PIL import Image
4
- import numpy as np
5
- import os
6
-
7
- app = Flask(__name__)
8
-
9
- # Loading the trained model
10
- try:
11
- model = load_model('modelOther.h5') # Replacing with the path to your saved model
12
- except Exception as e:
13
- print("Error loading the model:", e)
14
-
15
- def detect_image(image_path):
16
- try:
17
- # Function to detect image
18
- img = Image.open(image_path).resize((256, 256)) # Resizing image
19
- img_array = np.array(img) / 255.0 # Normalizing pixel values
20
- img_array = np.expand_dims(img_array, axis=0) # Adding batch dimension
21
-
22
- prediction = model.predict(img_array)[0][0]
23
- probability_real = prediction * 100 # Converting prediction to percentage
24
- probability_ai = (1 - prediction) * 100
25
-
26
- # Determine the final output
27
- if probability_real > probability_ai:
28
- result = 'Input Image is Real'
29
- confidence = probability_real
30
- else:
31
- result = 'Input Image is AI Generated'
32
- confidence = probability_ai
33
-
34
- return result, confidence
35
- except Exception as e:
36
- print("Error detecting image:", e)
37
- return "Error detecting image", 0
38
-
39
- @app.route('/')
40
- def index():
41
- return render_template('home.html')
42
-
43
- @app.route('/detect', methods=['POST'])
44
- def detect():
45
- try:
46
- if 'file' not in request.files:
47
- return jsonify({'error': 'No file provided'})
48
-
49
- file = request.files['file']
50
- if file.filename.split('.')[-1].lower() not in ['jpg', 'jpeg', 'png']:
51
- return jsonify({'error': 'Unsupported file type. Please provide an image in JPG, JPEG, or PNG format.'})
52
-
53
- file_path = 'DetectionImage/' + file.filename # Specifying the directory where images will be saved
54
- file.save(file_path)
55
-
56
- result, confidence = detect_image(file_path)
57
-
58
- response = {
59
- 'result': result,
60
- 'confidence': confidence,
61
- 'image_path': file_path
62
- }
63
-
64
- return jsonify(response)
65
- except Exception as e:
66
- print("Error processing request:", e)
67
- return jsonify({'error': 'Error processing request'})
68
-
69
- @app.route('/DetectionImage/<path:filename>')
70
- def serve_image(filename):
71
- return send_from_directory('DetectionImage', filename)
72
-
73
- if __name__ == '__main__':
74
- if not os.path.exists('DetectionImage'):
75
- os.makedirs('DetectionImage')
76
- app.run(debug=True)
 
1
+ from flask import Flask, request, jsonify, render_template, send_from_directory
2
+ from tensorflow.keras.models import load_model
3
+ from PIL import Image
4
+ import numpy as np
5
+ import os
6
+
7
+ app = Flask(__name__)
8
+
9
+ # Loading the trained model
10
+ try:
11
+ model = load_model('model.h5') # Replacing with the path to your saved model
12
+ except Exception as e:
13
+ print("Error loading the model:", e)
14
+
15
+ def detect_image(image_path):
16
+ try:
17
+ # Function to detect image
18
+ img = Image.open(image_path).resize((256, 256)) # Resizing image
19
+ img_array = np.array(img) / 255.0 # Normalizing pixel values
20
+ img_array = np.expand_dims(img_array, axis=0) # Adding batch dimension
21
+
22
+ prediction = model.predict(img_array)[0][0]
23
+ probability_real = prediction * 100 # Converting prediction to percentage
24
+ probability_ai = (1 - prediction) * 100
25
+
26
+ # Determine the final output
27
+ if probability_real > probability_ai:
28
+ result = 'Input Image is Real'
29
+ confidence = probability_real
30
+ else:
31
+ result = 'Input Image is AI Generated'
32
+ confidence = probability_ai
33
+
34
+ return result, confidence
35
+ except Exception as e:
36
+ print("Error detecting image:", e)
37
+ return "Error detecting image", 0
38
+
39
+ @app.route('/')
40
+ def index():
41
+ return render_template('home.html')
42
+
43
+ @app.route('/detect', methods=['POST'])
44
+ def detect():
45
+ try:
46
+ if 'file' not in request.files:
47
+ return jsonify({'error': 'No file provided'})
48
+
49
+ file = request.files['file']
50
+ if file.filename.split('.')[-1].lower() not in ['jpg', 'jpeg', 'png']:
51
+ return jsonify({'error': 'Unsupported file type. Please provide an image in JPG, JPEG, or PNG format.'})
52
+
53
+ file_path = 'DetectionImage/' + file.filename # Specifying the directory where images will be saved
54
+ file.save(file_path)
55
+
56
+ result, confidence = detect_image(file_path)
57
+
58
+ response = {
59
+ 'result': result,
60
+ 'confidence': confidence,
61
+ 'image_path': file_path
62
+ }
63
+
64
+ return jsonify(response)
65
+ except Exception as e:
66
+ print("Error processing request:", e)
67
+ return jsonify({'error': 'Error processing request'})
68
+
69
+ @app.route('/DetectionImage/<path:filename>')
70
+ def serve_image(filename):
71
+ return send_from_directory('DetectionImage', filename)
72
+
73
+ if __name__ == '__main__':
74
+ if not os.path.exists('DetectionImage'):
75
+ os.makedirs('DetectionImage')
76
+ app.run(debug=True)