TKM03 commited on
Commit
23d88ae
·
verified ·
1 Parent(s): 7938b1d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +146 -60
app.py CHANGED
@@ -1,83 +1,169 @@
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
 
8
- def detect(image):
9
- result_text = backend.predict(
10
- image=handle_file(image),
11
- api_name="/detect"
12
- )
13
 
14
- result = json.loads(result_text)
15
- if result and result["status"] == "ok":
16
- return result["overall"], result["aigen"], result["deepfake"]
17
- else:
18
- raise gr.Error("Error in processing image")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
 
 
20
  custom_css = """
 
 
 
 
 
 
 
 
 
 
 
21
  .button-gradient {
22
- background: linear-gradient(45deg, #ff416c, #ff4b2b, #ff9b00, #ff416c);
23
- background-size: 400% 400%;
24
- border: none;
25
- padding: 14px 28px;
26
- font-size: 16px;
27
- font-weight: bold;
28
- color: white;
29
- border-radius: 10px;
30
- cursor: pointer;
31
- transition: 0.3s ease-in-out;
32
- animation: gradientAnimation 2s infinite linear;
33
- box-shadow: 0 4px 10px rgba(255, 65, 108, 0.6);
 
 
 
 
34
  }
35
  @keyframes gradientAnimation {
36
- 0% { background-position: 0% 50%; }
37
- 25% { background-position: 50% 100%; }
38
- 50% { background-position: 100% 50%; }
39
- 75% { background-position: 50% 0%; }
40
- 100% { background-position: 0% 50%; }
41
  }
42
- .button-gradient:hover {
43
- transform: scale(1.05);
44
- box-shadow: 0 6px 15px rgba(255, 75, 43, 0.8);
 
 
 
 
 
 
 
 
 
45
  }
46
  """
47
 
 
48
  MARKDOWN0 = """
49
- # DeepFake Detector - ❤️Like above if this space helps
50
- #### [Learn more about our Deepfake Detection.](https://faceonlive.com/deepfake-detector)
 
 
 
 
 
 
 
51
  """
 
52
  MARKDOWN3 = """
53
- <div align="right"><a href="https://faceonlive.com/face-search-online" target='_blank' style='font-size: 16px;'>Reverse Face Search</div><br/>
54
- <div align="right"><a href="https://faceonlive.com/reverse-image-search" target='_blank' style='font-size: 16px;'>Reverse Image Search</div>
 
 
 
 
 
 
 
 
 
 
55
  """
56
- lbl_overall = gr.Label(label = "Overall")
57
- lbl_aigen = gr.Label(label = "Generative AI Model")
58
- lbl_deepfake = gr.Label(label = "Face Manipulation")
59
 
60
- with gr.Blocks(css=custom_css) as demo:
61
  gr.Markdown(MARKDOWN0)
62
- with gr.Row():
63
- with gr.Column(scale=1) as col1:
64
- image = gr.Image(type='filepath', height=360)
65
- detect_button = gr.Button("Detect", elem_classes="button-gradient")
66
- gr.Examples(['examples 1.jpg', 'examples 2.jpg'], inputs=image, cache_examples=True, fn=detect, outputs = [lbl_overall, lbl_aigen, lbl_deepfake])
67
- with gr.Column(scale=2) as col2:
68
- lbl_overall.render()
69
- with gr.Row():
70
- with gr.Column():
71
- lbl_aigen.render()
72
- with gr.Column():
73
- lbl_deepfake.render()
74
- gr.HTML(MARKDOWN3)
75
- with gr.Row():
76
  with gr.Column(scale=1):
77
- 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>')
78
- with gr.Column(scale=5):
79
- html = gr.HTML()
80
-
81
- detect_button.click(detect, inputs=[image], outputs=[lbl_overall, lbl_aigen, lbl_deepfake], api_name=False)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
82
 
83
- 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
  from gradio_client import Client, handle_file
 
5
 
6
+ # Initialize backend client with error handling
7
+ try:
8
+ backend = Client(os.getenv("BACKEND"), hf_token=os.getenv("TOKEN"))
9
+ except Exception as e:
10
+ raise Exception(f"Failed to initialize backend client: {str(e)}")
11
 
12
+ def detect(image):
13
+ """Detect deepfake content in an image with comprehensive error handling"""
14
+ if image is None:
15
+ raise gr.Error("Please upload an image to analyze")
16
+
17
+ try:
18
+ result_text = backend.predict(
19
+ image=handle_file(image),
20
+ api_name="/detect"
21
+ )
22
+
23
+ result = json.loads(result_text)
24
+ if not result or result.get("status") != "ok":
25
+ raise gr.Error("Analysis failed: Invalid response from backend")
26
+
27
+ # Format results professionally
28
+ overall = f"{result['overall']}% Confidence"
29
+ aigen = f"{result['aigen']}% (AI-Generated Content Likelihood)"
30
+ deepfake = f"{result['deepfake']}% (Face Manipulation Likelihood)"
31
+
32
+ return overall, aigen, deepfake
33
+
34
+ except json.JSONDecodeError:
35
+ raise gr.Error("Error processing analysis results")
36
+ except Exception as e:
37
+ raise gr.Error(f"Analysis error: {str(e)}")
38
 
39
+ # Enhanced professional CSS
40
  custom_css = """
41
+ .container {
42
+ max-width: 1200px;
43
+ margin: 0 auto;
44
+ padding: 20px;
45
+ font-family: 'Arial', sans-serif;
46
+ }
47
+ .header {
48
+ color: #2c3e50;
49
+ border-bottom: 2px solid #3498db;
50
+ padding-bottom: 10px;
51
+ }
52
  .button-gradient {
53
+ background: linear-gradient(45deg, #3498db, #2ecc71, #9b59b6);
54
+ background-size: 400% 400%;
55
+ border: none;
56
+ padding: 12px 24px;
57
+ font-size: 16px;
58
+ font-weight: 600;
59
+ color: white;
60
+ border-radius: 8px;
61
+ cursor: pointer;
62
+ transition: all 0.3s ease;
63
+ animation: gradientAnimation 3s ease infinite;
64
+ box-shadow: 0 2px 8px rgba(52, 152, 219, 0.3);
65
+ }
66
+ .button-gradient:hover {
67
+ transform: translateY(-2px);
68
+ box-shadow: 0 4px 12px rgba(52, 152, 219, 0.5);
69
  }
70
  @keyframes gradientAnimation {
71
+ 0% { background-position: 0% 50%; }
72
+ 50% { background-position: 100% 50%; }
73
+ 100% { background-position: 0% 50%; }
 
 
74
  }
75
+ .label {
76
+ font-weight: 600;
77
+ color: #34495e;
78
+ background: #f8f9fa;
79
+ padding: 10px;
80
+ border-radius: 5px;
81
+ margin: 5px 0;
82
+ }
83
+ .footer {
84
+ color: #7f8c8d;
85
+ font-size: 14px;
86
+ margin-top: 20px;
87
  }
88
  """
89
 
90
+ # Professional content
91
  MARKDOWN0 = """
92
+ <div class="header">
93
+ <h1>DeepFake Detection System</h1>
94
+ <p>Advanced AI-powered analysis for identifying manipulated media</p>
95
+ </div>
96
+ <div style="margin: 15px 0;">
97
+ <a href="https://faceonlive.com/deepfake-detector" target="_blank" style="color: #3498db; text-decoration: none;">
98
+ Learn About Our Technology
99
+ </a>
100
+ </div>
101
  """
102
+
103
  MARKDOWN3 = """
104
+ <div class="footer">
105
+ <p>Additional Tools:</p>
106
+ <div style="margin: 10px 0;">
107
+ <a href="https://faceonlive.com/face-search-online" target="_blank" style="color: #3498db; text-decoration: none; margin-right: 15px;">
108
+ Face Search Technology
109
+ </a>
110
+ <a href="https://faceonlive.com/reverse-image-search" target="_blank" style="color: #3498db; text-decoration: none;">
111
+ Reverse Image Search
112
+ </a>
113
+ </div>
114
+ <p>© 2025 FaceOnLive - All Rights Reserved</p>
115
+ </div>
116
  """
 
 
 
117
 
118
+ with gr.Blocks(css=custom_css, theme=gr.themes.Default()) as demo:
119
  gr.Markdown(MARKDOWN0)
120
+
121
+ with gr.Row(elem_classes="container"):
 
 
 
 
 
 
 
 
 
 
 
 
122
  with gr.Column(scale=1):
123
+ image = gr.Image(
124
+ type='filepath',
125
+ height=400,
126
+ label="Upload Image for Analysis",
127
+ interactive=True
128
+ )
129
+ detect_button = gr.Button(
130
+ "Analyze Image",
131
+ elem_classes="button-gradient"
132
+ )
133
+ gr.Examples(
134
+ examples=['examples 1.jpg', 'examples 2.jpg'],
135
+ inputs=image,
136
+ outputs=['overall', 'aigen', 'deepfake'],
137
+ fn=detect,
138
+ cache_examples=True
139
+ )
140
+
141
+ with gr.Column(scale=2):
142
+ overall = gr.Label(label="Confidence Score", elem_classes="label")
143
+ with gr.Row():
144
+ aigen = gr.Label(label="AI-Generated Content", elem_classes="label")
145
+ deepfake = gr.Label(label="Face Manipulation", elem_classes="label")
146
+
147
+ gr.Markdown(MARKDOWN3)
148
+
149
+ # Visitor badge
150
+ gr.HTML("""
151
+ <div style="margin-top: 20px;">
152
+ <a href="https://visitorbadge.io/status?path=https%3A%2F%2Fhuggingface.co%2Fspaces%2FFaceOnLive%2FDeep-Fake-Detector">
153
+ <img src="https://api.visitorbadge.io/api/visitors?path=https%3A%2F%2Fhuggingface.co%2Fspaces%2FFaceOnLive%2FDeep-Fake-Detector&labelColor=%233495db&countColor=%232ecc71&style=flat" />
154
+ </a>
155
+ </div>
156
+ """)
157
+
158
+ detect_button.click(
159
+ fn=detect,
160
+ inputs=[image],
161
+ outputs=[overall, aigen, deepfake],
162
+ _js="() => {return [document.querySelector('input[type=file]').files[0]]}"
163
+ )
164
 
165
+ demo.queue(api_open=False, concurrency_count=8).launch(
166
+ server_name="0.0.0.0",
167
+ show_api=False,
168
+ debug=True
169
+ )