noumanjavaid commited on
Commit
bc18442
Β·
verified Β·
1 Parent(s): 1731e74

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +96 -201
app.py CHANGED
@@ -1,24 +1,15 @@
1
  import gradio as gr
2
  import logging
3
- import traceback
4
- import time
5
- import re
6
  import os
7
- from functools import wraps, lru_cache
8
  from dotenv import load_dotenv
9
- from urllib.parse import urlparse
10
 
11
  # Load environment variables
12
  load_dotenv()
13
 
14
- # Enhanced logging configuration
15
  logging.basicConfig(
16
- level=logging.INFO,
17
- format='%(asctime)s - %(levelname)s - %(message)s',
18
- handlers=[
19
- logging.FileHandler('centurion_platform.log', encoding='utf-8', mode='a'),
20
- logging.StreamHandler()
21
- ]
22
  )
23
  logger = logging.getLogger(__name__)
24
 
@@ -32,17 +23,17 @@ css = """
32
 
33
  body {
34
  background-color: #ffffff;
35
- color: #000000;
36
  font-family: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;
37
  }
38
 
39
  .header {
40
  background: #ffffff;
41
  border-bottom: 1px solid #e5e5e5;
42
- padding: 1rem 2rem;
43
  display: flex;
44
  align-items: center;
45
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
 
46
  }
47
 
48
  .logo {
@@ -52,6 +43,10 @@ body {
52
  transition: transform 0.3s ease;
53
  }
54
 
 
 
 
 
55
  .header-title {
56
  color: #000000;
57
  font-size: 24px;
@@ -61,15 +56,14 @@ body {
61
  .container {
62
  max-width: 1400px;
63
  margin: 0 auto;
64
- padding: 2rem;
65
- background: #ffffff;
66
  }
67
 
68
  .main-content {
69
  background: #ffffff;
70
  border-radius: 12px;
71
  box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
72
- margin: 2rem 0;
73
  overflow: hidden;
74
  border: 1px solid #e5e5e5;
75
  }
@@ -79,7 +73,6 @@ body {
79
  width: 100%;
80
  height: 0;
81
  padding-bottom: 65%;
82
- background: #ffffff;
83
  }
84
 
85
  .iframe-container iframe {
@@ -95,7 +88,7 @@ body {
95
  display: grid;
96
  grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
97
  gap: 1.5rem;
98
- margin-top: 2rem;
99
  }
100
 
101
  .nav-card {
@@ -106,178 +99,94 @@ body {
106
  transition: all 0.3s ease;
107
  }
108
 
109
- @media (max-width: 768px) {
110
- .nav-grid {
111
- grid-template-columns: 1fr;
112
- }
113
  }
114
- """
115
 
116
- # Performance and security decorators
117
- def performance_monitor(func):
118
- @wraps(func)
119
- def wrapper(*args, **kwargs):
120
- start = time.time()
121
- try:
122
- result = func(*args, **kwargs)
123
- execution_time = time.time() - start
124
- logger.info(f"{func.__name__} execution time: {execution_time:.2f} seconds")
125
- return result
126
- except Exception as e:
127
- logger.error(f"Error in {func.__name__}: {e}")
128
- logger.error(traceback.format_exc())
129
- raise
130
- return wrapper
131
 
132
- # URL Validation and Sanitization
133
- @lru_cache(maxsize=100)
134
- def validate_and_sanitize_url(url):
135
- """
136
- Comprehensive URL validation and sanitization
137
- """
138
- try:
139
- # Validate URL pattern
140
- url_pattern = re.compile(
141
- r'^https?://' # http:// or https://
142
- r'(?:(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+[A-Z]{2,6}\.?|' # domain...
143
- r'localhost|' # localhost...
144
- r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})' # ...or ip
145
- r'(?::\d+)?' # optional port
146
- r'(?:/?|[/?]\S+)$', re.IGNORECASE)
147
-
148
- if not url_pattern.match(url):
149
- logger.warning(f"Invalid URL format: {url}")
150
- return None
151
-
152
- # Parse and validate URL
153
- parsed_url = urlparse(url)
154
-
155
- # Whitelist allowed domains
156
- allowed_domains = [
157
- 'noumanjavaid-new-space.hf.space',
158
- 'noumanjavaid-centurionv2.hf.space',
159
- 'noumanjavaid-watermark-demo-video.hf.space',
160
- 'noumanjavaid-centii.hf.space',
161
- 'localhost'
162
- ]
163
-
164
- if parsed_url.netloc not in allowed_domains:
165
- logger.warning(f"Unauthorized domain: {parsed_url.netloc}")
166
- return None
167
-
168
- return url
169
- except Exception as e:
170
- logger.error(f"URL validation error: {e}")
171
- return None
172
 
173
- # Platform Configuration
174
- PLATFORM_CONFIG = {
175
- 'main_platform_url': os.getenv('MAIN_PLATFORM_URL', 'https://noumanjavaid-centii.hf.space'),
176
- 'launch_config': {
177
- 'show_error': False,
178
- 'show_api': False,
179
- 'height': 1024,
180
- }
181
  }
182
 
183
- # Error Notification (Optional)
184
- def send_error_notification(error):
185
- """
186
- Send error notifications via external service
187
- """
188
- try:
189
- import requests
190
-
191
- webhook_url = os.getenv('ERROR_WEBHOOK_URL')
192
- if webhook_url:
193
- payload = {
194
- "text": f"🚨 Centurion Platform Error: {str(error)}",
195
- "blocks": [
196
- {
197
- "type": "section",
198
- "text": {
199
- "type": "mrkdwn",
200
- "text": f"```{traceback.format_exc()}```"
201
- }
202
- }
203
- ]
204
- }
205
- requests.post(webhook_url, json=payload)
206
- except Exception as log_error:
207
- logger.error(f"Error sending notification: {log_error}")
208
 
209
- # Configuration Validation
210
- def validate_configuration(demo):
211
- """
212
- Validate Gradio demo configuration
213
- """
214
- required_attributes = [
215
- 'title',
216
- 'css',
217
- 'show_error',
218
- 'show_api',
219
- 'show_tips',
220
- 'height',
221
- 'analytics_enabled',
222
- 'enable_queue'
223
- ]
224
-
225
- validation_results = {}
226
- for attr in required_attributes:
227
- try:
228
- value = getattr(demo, attr, None)
229
- validation_results[attr] = value is not None
230
- except Exception as e:
231
- logger.error(f"Error validating {attr}: {e}")
232
- validation_results[attr] = False
233
-
234
- return validation_results
235
 
236
- @performance_monitor
237
- def create_demo():
238
- """
239
- Create Gradio demo with comprehensive validation
240
- """
241
- # Navigation Cards with URL Validation
242
- cards = [
243
- {
244
- "icon": "πŸ”",
245
- "title": "DeepFake Detection",
246
- "url": validate_and_sanitize_url(os.getenv('DEEPFAKE_URL', 'https://noumanjavaid-new-space.hf.space'))
247
- },
248
- {
249
- "icon": "πŸ“„",
250
- "title": "Document Analysis", "url": validate_and_sanitize_url(os.getenv('DOCUMENT_URL', 'https://noumanjavaid-centurionv2.hf.space'))
251
- },
252
- {
253
- "icon": "πŸŽ₯",
254
- "title": "Video Watermarking",
255
- "url": validate_and_sanitize_url(os.getenv('WATERMARK_URL', 'https://noumanjavaid-watermark-demo-video.hf.space'))
256
- },
257
- {
258
- "icon": "πŸ”",
259
- "title": "Image Authentication",
260
- "url": validate_and_sanitize_url(os.getenv('AUTH_URL', 'https://noumanjavaid-centii.hf.space'))
261
- },
262
- ]
263
 
264
- # Validate navigation cards
265
- invalid_cards = [card for card in cards if card['url'] is None]
266
- if invalid_cards:
267
- logger.warning(f"Invalid navigation card URLs: {invalid_cards}")
268
 
269
- # Generate HTML for navigation cards
270
- html_cards = "".join(
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
271
  f'''
272
- <div class="nav-card" role="navigation" aria-label="{card['title']}" tabindex="0">
273
  <a href="{card['url']}" target="_blank" rel="noopener noreferrer">
274
  {card['icon']} {card['title']}
275
  </a>
276
  </div>
277
- ''' for card in cards if card['url'] is not None
278
- )
279
-
280
- # Create Gradio block
281
  with gr.Blocks(css=css, title="Centurion Analysis Platform") as demo:
282
  # Header
283
  with gr.Row(elem_classes=["header"]):
@@ -292,11 +201,10 @@ def create_demo():
292
  # Main content
293
  with gr.Row(elem_classes=["container"]):
294
  with gr.Column(elem_classes=["main-content"]):
295
- gr.HTML(f'''
296
  <div class="iframe-container">
297
  <iframe
298
- src="{PLATFORM_CONFIG['main_platform_url']}"
299
- loading="lazy"
300
  title="Centurion Main Platform"
301
  ></iframe>
302
  </div>
@@ -304,28 +212,15 @@ def create_demo():
304
 
305
  # Navigation grid
306
  with gr.Row(elem_classes=["nav-grid"]):
307
- gr.HTML(html_cards)
308
-
309
  return demo
310
 
311
- def main():
312
- try:
313
- demo = create_demo()
314
-
315
- # Validate configuration before launching
316
- config_validation = validate_configuration(demo)
317
- logger.info(f"Configuration Validation: {config_validation}")
318
-
319
- validation_errors = [k for k, v in config_validation.items() if not v]
320
- if validation_errors:
321
- logger.error(f"Configuration validation failed for: {validation_errors}")
322
- raise ValueError(f"Invalid configuration: {validation_errors}")
323
-
324
- demo.launch(**PLATFORM_CONFIG['launch_config'])
325
- except Exception as e:
326
- logger.critical(f"Application launch failed: {e}")
327
- logger.critical(traceback.format_exc())
328
- send_error_notification(e)
329
-
330
  if __name__ == "__main__":
331
- main()
 
 
 
 
 
 
 
1
  import gradio as gr
2
  import logging
 
 
 
3
  import os
 
4
  from dotenv import load_dotenv
 
5
 
6
  # Load environment variables
7
  load_dotenv()
8
 
9
+ # Configure logging
10
  logging.basicConfig(
11
+ level=logging.INFO,
12
+ format='%(asctime)s - %(levelname)s - %(message)s'
 
 
 
 
13
  )
14
  logger = logging.getLogger(__name__)
15
 
 
23
 
24
  body {
25
  background-color: #ffffff;
 
26
  font-family: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;
27
  }
28
 
29
  .header {
30
  background: #ffffff;
31
  border-bottom: 1px solid #e5e5e5;
32
+ padding: 1.5rem 2rem;
33
  display: flex;
34
  align-items: center;
35
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
36
+ margin-bottom: 2rem;
37
  }
38
 
39
  .logo {
 
43
  transition: transform 0.3s ease;
44
  }
45
 
46
+ .logo:hover {
47
+ transform: scale(1.05);
48
+ }
49
+
50
  .header-title {
51
  color: #000000;
52
  font-size: 24px;
 
56
  .container {
57
  max-width: 1400px;
58
  margin: 0 auto;
59
+ padding: 0 2rem;
 
60
  }
61
 
62
  .main-content {
63
  background: #ffffff;
64
  border-radius: 12px;
65
  box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
66
+ margin-bottom: 2rem;
67
  overflow: hidden;
68
  border: 1px solid #e5e5e5;
69
  }
 
73
  width: 100%;
74
  height: 0;
75
  padding-bottom: 65%;
 
76
  }
77
 
78
  .iframe-container iframe {
 
88
  display: grid;
89
  grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
90
  gap: 1.5rem;
91
+ margin: 2rem 0;
92
  }
93
 
94
  .nav-card {
 
99
  transition: all 0.3s ease;
100
  }
101
 
102
+ .nav-card:hover {
103
+ transform: translateY(-5px);
104
+ box-shadow: 0 8px 16px rgba(0, 0, 0, 0.1);
105
+ border-color: #000000;
106
  }
 
107
 
108
+ .nav-card a {
109
+ color: #000000;
110
+ text-decoration: none;
111
+ font-weight: 500;
112
+ font-size: 1.1rem;
113
+ display: flex;
114
+ align-items: center;
115
+ gap: 0.8rem;
116
+ }
 
 
 
 
 
 
117
 
118
+ .nav-card a::after {
119
+ content: 'β†’';
120
+ margin-left: auto;
121
+ opacity: 0;
122
+ transition: opacity 0.3s ease;
123
+ }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
124
 
125
+ .nav-card:hover a::after {
126
+ opacity: 1;
 
 
 
 
 
 
127
  }
128
 
129
+ /* Hide Gradio elements */
130
+ footer {
131
+ display: none !important;
132
+ }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
133
 
134
+ .gradio-container {
135
+ margin: 0 !important;
136
+ padding: 0 !important;
137
+ }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
138
 
139
+ @media (max-width: 768px) {
140
+ .nav-grid {
141
+ grid-template-columns: 1fr;
142
+ }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
143
 
144
+ .header {
145
+ padding: 1rem;
146
+ }
 
147
 
148
+ .container {
149
+ padding: 0 1rem;
150
+ }
151
+ }
152
+ """
153
+
154
+ # Navigation cards configuration
155
+ NAVIGATION_CARDS = [
156
+ {
157
+ "icon": "πŸ”",
158
+ "title": "DeepFake Detection",
159
+ "url": "https://noumanjavaid-new-space.hf.space"
160
+ },
161
+ {
162
+ "icon": "πŸ“„",
163
+ "title": "Document Analysis",
164
+ "url": "https://noumanjavaid-centurionv2.hf.space"
165
+ },
166
+ {
167
+ "icon": "πŸŽ₯",
168
+ "title": "Video Watermarking",
169
+ "url": "https://noumanjavaid-watermark-demo-video.hf.space"
170
+ },
171
+ {
172
+ "icon": "πŸ”",
173
+ "title": "Image Authentication",
174
+ "url": "https://noumanjavaid-centii.hf.space"
175
+ }
176
+ ]
177
+
178
+ def create_navigation_cards():
179
+ return "".join([
180
  f'''
181
+ <div class="nav-card">
182
  <a href="{card['url']}" target="_blank" rel="noopener noreferrer">
183
  {card['icon']} {card['title']}
184
  </a>
185
  </div>
186
+ ''' for card in NAVIGATION_CARDS
187
+ ])
188
+
189
+ def create_interface():
190
  with gr.Blocks(css=css, title="Centurion Analysis Platform") as demo:
191
  # Header
192
  with gr.Row(elem_classes=["header"]):
 
201
  # Main content
202
  with gr.Row(elem_classes=["container"]):
203
  with gr.Column(elem_classes=["main-content"]):
204
+ gr.HTML('''
205
  <div class="iframe-container">
206
  <iframe
207
+ src="https://noumanjavaid-centii.hf.space"
 
208
  title="Centurion Main Platform"
209
  ></iframe>
210
  </div>
 
212
 
213
  # Navigation grid
214
  with gr.Row(elem_classes=["nav-grid"]):
215
+ gr.HTML(create_navigation_cards())
216
+
217
  return demo
218
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
219
  if __name__ == "__main__":
220
+ demo = create_interface()
221
+ demo.launch(
222
+ show_error=False,
223
+ show_api=False,
224
+ analytics_enabled=False,
225
+ enable_queue=False
226
+ )