sonuprasad commited on
Commit
13f4a00
1 Parent(s): aefce3d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -22
app.py CHANGED
@@ -1,36 +1,51 @@
1
- import gradio as gr
 
2
  from tensorflow.keras.models import load_model
3
  from PIL import Image
4
  import numpy as np
 
5
 
6
- # Load the model
7
- model = load_model('./model.h5')
 
 
 
8
 
9
  def detect_image(input_image):
10
- img = Image.fromarray(input_image).resize((256, 256))
11
- img_array = np.array(img) / 255.0
12
- img_array = np.expand_dims(img_array, axis=0)
 
 
 
 
 
 
13
 
14
- prediction = model.predict(img_array)[0][0]
15
- probability_real = prediction * 100
16
- probability_ai = (1 - prediction) * 100
 
 
 
 
17
 
18
- if probability_real > probability_ai:
19
- result = 'Input Image is Real'
20
- confidence = probability_real
21
- else:
22
- result = 'Input Image is AI Generated'
23
- confidence = probability_ai
24
 
25
- return result, confidence
 
 
 
26
 
27
- demo = gr.Interface(
 
28
  fn=detect_image,
29
- inputs=gr.Image(type="numpy", shape=(256, 256)),
30
- outputs=[gr.Textbox(label="Result"), gr.Textbox(label="Confidence (%)")],
31
  title="Deepfake Detection",
32
  description="Upload an image to detect if it's real or AI generated."
33
- )
34
 
35
- # Deploy the interface on Gradio Hub
36
- demo.launch(share=True)
 
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