sonuprasad commited on
Commit
e944060
1 Parent(s): 3088d7a

Upload app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +76 -51
app.py CHANGED
@@ -1,51 +1,76 @@
1
-
2
-
3
- from tensorflow.keras.models import load_model
4
- from PIL import Image
5
- import numpy as np
6
- import gradio as gr
7
-
8
- # Loading the trained model
9
- try:
10
- model = load_model('/model.h5') # Replacing with the path to your saved model
11
- except Exception as e:
12
- print("Error loading the model:", e)
13
-
14
- def detect_image(input_image):
15
- try:
16
- # Function to detect image
17
- img = Image.fromarray(input_image).resize((256, 256)) # Resize image
18
- img_array = np.array(img) / 255.0 # Normalize pixel values
19
- img_array = np.expand_dims(img_array, axis=0) # Add batch dimension
20
-
21
- prediction = model.predict(img_array)[0][0]
22
- probability_real = prediction * 100 # Convert prediction to percentage
23
- probability_ai = (1 - prediction) * 100
24
-
25
- # Determine the final output
26
- if probability_real > probability_ai:
27
- result = 'Input Image is Real'
28
- confidence = probability_real
29
- else:
30
- result = 'Input Image is AI Generated'
31
- confidence = probability_ai
32
-
33
- return result, confidence
34
- except Exception as e:
35
- print("Error detecting image:", e)
36
- return "Error detecting image", 0
37
-
38
- # Define input and output components for Gradio
39
- input_image = gr.Image()
40
- output_text = gr.Textbox(label="Result")
41
- output_confidence = gr.Textbox(label="Confidence (%)")
42
-
43
- # Create Gradio interface
44
- gr.Interface(
45
- fn=detect_image,
46
- inputs=input_image,
47
- outputs=[output_text, output_confidence],
48
- title="Deepfake Detection",
49
- description="Upload an image to detect if it's real or AI generated."
50
- ).launch(share=True)
51
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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)