chmawia commited on
Commit
ab3e1b1
Β·
verified Β·
1 Parent(s): 8f04ed3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -56
app.py CHANGED
@@ -1,37 +1,30 @@
1
  import os
2
  import gradio as gr
3
  import json
4
- from gradio_client import Client, handle_file
5
-
6
- # βœ… Check if BACKEND is set before using it
7
- BACKEND_URL = os.getenv("BACKEND")
8
- TOKEN = os.getenv("TOKEN")
9
-
10
- if not BACKEND_URL:
11
- raise ValueError("❌ Error: BACKEND environment variable is missing or empty. Please set it before running the script.")
12
 
13
- backend = Client(BACKEND_URL, hf_token=TOKEN)
 
 
 
14
 
15
- # βœ… Safe function for DeepFake detection
16
  def detect(image):
17
- if not image:
18
- return "❌ No image uploaded", "❌ No image uploaded", "❌ No image uploaded"
19
-
20
  try:
21
- file_path = handle_file(image) # Handle uploaded file
22
- result_text = backend.predict(image=file_path, api_name="/detect") # Call backend
23
- result = json.loads(result_text) # Parse response
24
-
25
- # βœ… Check if API response is valid
26
- if result.get("status") == "ok":
27
- return result["overall"], result["aigen"], result["deepfake"]
28
- else:
29
- return "❌ Unexpected API response", "❌ Unexpected API response", "❌ Unexpected API response"
30
-
31
  except Exception as e:
32
- return f"❌ API Error: {str(e)}", "❌ API Error", "❌ API Error"
 
 
 
 
 
 
 
 
 
 
 
 
33
 
34
- # βœ… Custom CSS for buttons
35
  custom_css = """
36
  .button-gradient {
37
  background: linear-gradient(45deg, #ff416c, #ff4b2b, #ff9b00, #ff416c);
@@ -60,51 +53,43 @@ custom_css = """
60
  }
61
  """
62
 
63
- # βœ… UI Components
64
- MARKDOWN0 = """# DeepFake Detector - ❀️ Like if this space helps
65
- #### [Learn more about our Deepfake Detection.](https://faceonlive.com/deepfake-detector)"""
 
66
  MARKDOWN3 = """
67
- <div align="right"><a href="https://faceonlive.com/face-search-online" target='_blank' style='font-size: 16px;'>Reverse Face Search</a></div><br/>
68
- <div align="right"><a href="https://faceonlive.com/reverse-image-search" target='_blank' style='font-size: 16px;'>Reverse Image Search</a></div>
69
  """
 
 
 
70
 
71
- lbl_overall = gr.Label(label="Overall")
72
- lbl_aigen = gr.Label(label="Generative AI Model")
73
- lbl_deepfake = gr.Label(label="Face Manipulation")
74
-
75
- # βœ… Gradio Interface
76
  with gr.Blocks(css=custom_css) as demo:
77
  gr.Markdown(MARKDOWN0)
78
-
79
  with gr.Row():
80
- with gr.Column(scale=1):
81
  image = gr.Image(type='filepath', height=360)
82
  gr.HTML("<div id='limit'></div>")
83
- detect_button = gr.Button("πŸš€ Detect", elem_classes="button-gradient")
84
- gr.Examples(
85
- ['examples/1.jpg', 'examples/2.jpg'],
86
- inputs=image,
87
- cache_examples=True,
88
- fn=detect,
89
- outputs=[lbl_overall, lbl_aigen, lbl_deepfake]
90
- )
91
-
92
- with gr.Column(scale=2):
93
  lbl_overall.render()
94
  with gr.Row():
95
  with gr.Column():
96
  lbl_aigen.render()
97
  with gr.Column():
98
  lbl_deepfake.render()
99
-
100
  gr.HTML(MARKDOWN3)
 
 
 
 
 
101
 
102
- # βœ… Fixes Button Click Action
103
- detect_button.click(
104
- detect,
105
- inputs=[image],
106
- outputs=[lbl_overall, lbl_aigen, lbl_deepfake]
107
- )
108
 
109
- # βœ… Launching App with Safe Configurations
110
- demo.queue(api_open=False, default_concurrency_limit=8).launch(server_name="0.0.0.0", show_api=False)
 
1
  import os
2
  import gradio as gr
3
  import json
 
 
 
 
 
 
 
 
4
 
5
+ from gradio_client import Client, handle_file
6
+ backend = Client(os.getenv("BACKEND"), hf_token=os.getenv("TOKEN"))
7
+ JS_FUNC1 = os.getenv("JS_FUNC1")
8
+ JS_FUNC2 = os.getenv("JS_FUNC2")
9
 
 
10
  def detect(image):
 
 
 
11
  try:
12
+ file_1 = handle_file(image)
 
 
 
 
 
 
 
 
 
13
  except Exception as e:
14
+ gr.Info("Please upload an image file.")
15
+ return "", "", ""
16
+
17
+ result_text = backend.predict(
18
+ image=handle_file(image),
19
+ api_name="/detect"
20
+ )
21
+
22
+ result = json.loads(result_text)
23
+ if result and result["status"] == "ok":
24
+ return result["overall"], result["aigen"], result["deepfake"]
25
+ else:
26
+ raise gr.Error("Error in processing image")
27
 
 
28
  custom_css = """
29
  .button-gradient {
30
  background: linear-gradient(45deg, #ff416c, #ff4b2b, #ff9b00, #ff416c);
 
53
  }
54
  """
55
 
56
+ MARKDOWN0 = """
57
+ # DeepFake Detector - ❀️Like above if this space helps
58
+ #### [Learn more about our Deepfake Detection.](https://faceonlive.com/deepfake-detector)
59
+ """
60
  MARKDOWN3 = """
61
+ <div align="right"><a href="https://faceonlive.com/face-search-online" target='_blank' style='font-size: 16px;'>Reverse Face Search</div><br/>
62
+ <div align="right"><a href="https://faceonlive.com/reverse-image-search" target='_blank' style='font-size: 16px;'>Reverse Image Search</div>
63
  """
64
+ lbl_overall = gr.Label(label = "Overall")
65
+ lbl_aigen = gr.Label(label = "Generative AI Model")
66
+ lbl_deepfake = gr.Label(label = "Face Manipulation")
67
 
 
 
 
 
 
68
  with gr.Blocks(css=custom_css) as demo:
69
  gr.Markdown(MARKDOWN0)
 
70
  with gr.Row():
71
+ with gr.Column(scale=1) as col1:
72
  image = gr.Image(type='filepath', height=360)
73
  gr.HTML("<div id='limit'></div>")
74
+ limit_button = gr.Button("πŸš€ Detect", elem_classes="button-gradient")
75
+ detect_button = gr.Button("Detect", visible=False, elem_id="submit_btn")
76
+ gr.Examples(['examples/1.jpg', 'examples/2.jpg'], inputs=image, cache_examples=True, fn=detect, outputs = [lbl_overall, lbl_aigen, lbl_deepfake])
77
+ with gr.Column(scale=2) as col2:
 
 
 
 
 
 
78
  lbl_overall.render()
79
  with gr.Row():
80
  with gr.Column():
81
  lbl_aigen.render()
82
  with gr.Column():
83
  lbl_deepfake.render()
 
84
  gr.HTML(MARKDOWN3)
85
+ with gr.Row():
86
+ with gr.Column(scale=1):
87
+ gr.HTML('<a href="https://visitorbadge.io/status?path=https%3A%2F%2Fhuggingface.co%2Fspaces%2FFaceOnLive%2FDeep-Fake-Detector"><img src="https://api.visitorbadge.io/api/visitors?path=https%3A%2F%2Fhuggingface.co%2Fspaces%2FFaceOnLive%2FDeep-Fake-Detector&labelColor=%23ff8a65&countColor=%2337d67a&style=flat&labelStyle=upper" /></a>')
88
+ with gr.Column(scale=5):
89
+ html = gr.HTML()
90
 
91
+ demo.load(None, inputs=None, outputs=html, js=JS_FUNC1)
92
+ limit_button.click(None, js=JS_FUNC2)
93
+ detect_button.click(detect, inputs=[image], outputs=[lbl_overall, lbl_aigen, lbl_deepfake], api_name=False)
 
 
 
94
 
95
+ demo.queue(api_open=False, default_concurrency_limit=8).launch(server_name="0.0.0.0", show_api=False)