petter2025 commited on
Commit
1f75a1f
·
verified ·
1 Parent(s): 5d9bea2

Update hf_demo.py

Browse files
Files changed (1) hide show
  1. hf_demo.py +67 -42
hf_demo.py CHANGED
@@ -1,5 +1,5 @@
1
- # COMPLETE FIXED hf_demo.py with all syntax errors resolved
2
- # ARF 3.3.9 DEMO WITH FIXED HTML RENDERING AND SYNTAX ERRORS
3
 
4
  import gradio as gr
5
  import time
@@ -9,10 +9,26 @@ import uuid
9
  import subprocess
10
  import sys
11
  import importlib
 
12
  from datetime import datetime, timedelta
13
  from typing import Dict, List, Optional, Tuple, Any, Union
14
  import numpy as np
15
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16
  # Import enhanced engines
17
  try:
18
  from utils.arf_engine_enhanced import EnhancedARFEngine, BayesianRiskAssessment, RiskCategory
@@ -153,7 +169,6 @@ ENHANCED_CSS = """
153
  border: 3px solid rgba(255, 255, 255, 0.4);
154
  animation: pulse-mathematical 2.5s infinite;
155
  }
156
-
157
  .arf-sim-badge {
158
  background: linear-gradient(135deg, #FF9800, #F57C00, #E65100, #BF360C);
159
  color: white;
@@ -168,13 +183,11 @@ ENHANCED_CSS = """
168
  box-shadow: 0 6px 20px rgba(255, 152, 0, 0.4);
169
  border: 3px solid rgba(255, 255, 255, 0.4);
170
  }
171
-
172
  @keyframes pulse-mathematical {
173
  0% { box-shadow: 0 0 0 0 rgba(76, 175, 80, 0.7), 0 6px 20px rgba(76, 175, 80, 0.4); }
174
  70% { box-shadow: 0 0 0 15px rgba(76, 175, 80, 0), 0 6px 20px rgba(76, 175, 80, 0.4); }
175
  100% { box-shadow: 0 0 0 0 rgba(76, 175, 80, 0), 0 6px 20px rgba(76, 175, 80, 0.4); }
176
  }
177
-
178
  .mathematical-gate {
179
  width: 70px;
180
  height: 70px;
@@ -190,11 +203,9 @@ ENHANCED_CSS = """
190
  z-index: 2;
191
  transition: all 0.5s cubic-bezier(0.34, 1.56, 0.64, 1);
192
  }
193
-
194
  .gate-passed { background: linear-gradient(135deg, #4CAF50, #2E7D32); }
195
  .gate-failed { background: linear-gradient(135deg, #F44336, #D32F2F); }
196
  .gate-pending { background: linear-gradient(135deg, #9E9E9E, #616161); }
197
-
198
  .gate-container {
199
  display: flex;
200
  align-items: center;
@@ -203,14 +214,12 @@ ENHANCED_CSS = """
203
  margin: 20px 0;
204
  flex-wrap: wrap;
205
  }
206
-
207
  .gate-line {
208
  width: 40px;
209
  height: 4px;
210
  background: linear-gradient(90deg, #E0E0E0, #BDBDBD);
211
  border-radius: 2px;
212
  }
213
-
214
  .mathematical-card {
215
  border-radius: 15px;
216
  padding: 25px;
@@ -222,22 +231,18 @@ ENHANCED_CSS = """
222
  background: #FFFFFF;
223
  box-shadow: 0 8px 30px rgba(0,0,0,0.08);
224
  }
225
-
226
  .mathematical-card:hover {
227
  transform: translateY(-5px);
228
  box-shadow: 0 15px 40px rgba(0,0,0,0.15);
229
  }
230
-
231
  .license-oss {
232
  border-top-color: #1E88E5;
233
  background: linear-gradient(145deg, #E3F2FD, #FFFFFF);
234
  }
235
-
236
  .license-trial {
237
  border-top-color: #FFB300;
238
  background: linear-gradient(145deg, #FFF8E1, #FFFFFF);
239
  }
240
-
241
  @media (max-width: 768px) {
242
  .gradio-container { padding: 10px !important; }
243
  .arf-real-badge, .arf-sim-badge { padding: 6px 14px; font-size: 12px; }
@@ -245,7 +250,6 @@ ENHANCED_CSS = """
245
  .gate-line { width: 20px; }
246
  .mathematical-card { padding: 15px; margin: 10px 0; }
247
  }
248
-
249
  @media (max-width: 480px) {
250
  .gradio-container { padding: 5px !important; }
251
  .arf-real-badge, .arf-sim-badge { padding: 4px 10px; font-size: 11px; }
@@ -357,16 +361,17 @@ demo_state = EnhancedDemoState(ARF_UNIFIED_STATUS)
357
 
358
  # ============== GRADIO INTERFACE ==============
359
  def create_enhanced_demo():
360
- """Create enhanced demo with fixed HTML rendering bugs"""
361
 
362
  # Get unified status
363
  arf_display = ARF_UNIFIED_STATUS['display_text']
364
  arf_badge_class = ARF_UNIFIED_STATUS['badge_class']
365
 
 
 
366
  with gr.Blocks(
367
- title=f"ARF {ARF_UNIFIED_STATUS['version']} - Mathematical Sophistication",
368
- theme=gr.themes.Soft(primary_hue="blue", secondary_hue="orange"),
369
- css=ENHANCED_CSS
370
  ) as demo:
371
 
372
  # ===== HEADER =====
@@ -555,6 +560,18 @@ def create_enhanced_demo():
555
  </div>
556
  """)
557
 
 
 
 
 
 
 
 
 
 
 
 
 
558
  # ===== EVENT HANDLERS =====
559
  def update_context(scenario_name):
560
  """Update context with mathematical analysis"""
@@ -750,7 +767,7 @@ def create_enhanced_demo():
750
  # Gate visualization
751
  gates_visualization = ""
752
  for i in range(total_gates):
753
- gate_class = "gate-passed" if i < gates_passed else "gate-failed"
754
  gates_visualization += f'<div class="mathematical-gate {gate_class}">{i+1}</div>'
755
  if i < total_gates - 1:
756
  gates_visualization += '<div class="gate-line"></div>'
@@ -924,22 +941,7 @@ def create_enhanced_demo():
924
  def generate_trial():
925
  """Generate mathematical trial license"""
926
  license_key = generate_mathematical_trial_license()
927
- return license_key, f"""
928
- <div style="text-align: center; padding: 25px; background: linear-gradient(135deg, #FFB300, #FF9800); color: white; border-radius: 15px; box-shadow: 0 12px 40px rgba(255, 179, 0, 0.4);">
929
- <h3 style="margin-top: 0; margin-bottom: 15px;">🎉 Mathematical Trial License Generated!</h3>
930
- <div style="background: white; color: #333; padding: 18px; border-radius: 10px; font-family: monospace; margin: 15px 0; font-size: 15px; letter-spacing: 1px; border: 3px dashed #FFB300;">
931
- {license_key}
932
- </div>
933
- <p style="margin-bottom: 20px;">Copy this key and paste it into the License Key field above.</p>
934
- <div style="background: rgba(255,255,255,0.2); padding: 18px; border-radius: 10px; margin-top: 15px;">
935
- <div style="font-size: 0.95em; line-height: 1.6;">
936
- ⏳ <strong>14-day mathematical trial</strong><br>
937
- 🧮 <strong>Bayesian analysis with confidence intervals</strong><br>
938
- 🛡️ <strong>Mechanical gates with mathematical weights</strong>
939
- </div>
940
- </div>
941
- </div>
942
- """
943
 
944
  # Connect handlers
945
  scenario.change(
@@ -951,7 +953,7 @@ def create_enhanced_demo():
951
  test_btn.click(
952
  fn=test_mathematical_assessment,
953
  inputs=[scenario, context, license_key],
954
- outputs=[oss_results, enterprise_results, license_display]
955
  )
956
 
957
  trial_btn.click(
@@ -967,12 +969,35 @@ if __name__ == "__main__":
967
  print("\n" + "="*80)
968
  print("🚀 LAUNCHING FIXED ARF 3.3.9 DEMO")
969
  print("📊 ARF Status:", ARF_UNIFIED_STATUS['display_text'])
 
970
  print("="*80)
971
 
 
972
  demo = create_enhanced_demo()
973
- demo.launch(
974
- server_name="0.0.0.0",
975
- server_port=7860,
976
- share=False,
977
- debug=False
978
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # COMPLETE FIXED hf_demo.py with Hugging Face Spaces compatibility
2
+ # ARF 3.3.9 DEMO WITH PROPER GRADIO 6.0+ CONFIGURATION
3
 
4
  import gradio as gr
5
  import time
 
9
  import subprocess
10
  import sys
11
  import importlib
12
+ import os # Added for environment detection
13
  from datetime import datetime, timedelta
14
  from typing import Dict, List, Optional, Tuple, Any, Union
15
  import numpy as np
16
 
17
+ # ============== HUGGING FACE SPACES DETECTION ==============
18
+ def is_huggingface_spaces():
19
+ """Detect if running in Hugging Face Spaces environment"""
20
+ return os.environ.get('SPACE_ID') is not None or \
21
+ os.environ.get('HF_SPACE') is not None or \
22
+ os.environ.get('SYSTEM') == 'spaces' or \
23
+ os.path.exists('/.dockerenv') and 'space' in os.environ.get('HOSTNAME', '')
24
+
25
+ # Set environment variables for Hugging Face Spaces
26
+ if is_huggingface_spaces():
27
+ os.environ['GRADIO_ANALYTICS_ENABLED'] = 'False'
28
+ os.environ['GRADIO_SERVER_NAME'] = '0.0.0.0'
29
+ os.environ['GRADIO_SERVER_PORT'] = '7860'
30
+ os.environ['GRADIO_SSR_MODE'] = 'False'
31
+
32
  # Import enhanced engines
33
  try:
34
  from utils.arf_engine_enhanced import EnhancedARFEngine, BayesianRiskAssessment, RiskCategory
 
169
  border: 3px solid rgba(255, 255, 255, 0.4);
170
  animation: pulse-mathematical 2.5s infinite;
171
  }
 
172
  .arf-sim-badge {
173
  background: linear-gradient(135deg, #FF9800, #F57C00, #E65100, #BF360C);
174
  color: white;
 
183
  box-shadow: 0 6px 20px rgba(255, 152, 0, 0.4);
184
  border: 3px solid rgba(255, 255, 255, 0.4);
185
  }
 
186
  @keyframes pulse-mathematical {
187
  0% { box-shadow: 0 0 0 0 rgba(76, 175, 80, 0.7), 0 6px 20px rgba(76, 175, 80, 0.4); }
188
  70% { box-shadow: 0 0 0 15px rgba(76, 175, 80, 0), 0 6px 20px rgba(76, 175, 80, 0.4); }
189
  100% { box-shadow: 0 0 0 0 rgba(76, 175, 80, 0), 0 6px 20px rgba(76, 175, 80, 0.4); }
190
  }
 
191
  .mathematical-gate {
192
  width: 70px;
193
  height: 70px;
 
203
  z-index: 2;
204
  transition: all 0.5s cubic-bezier(0.34, 1.56, 0.64, 1);
205
  }
 
206
  .gate-passed { background: linear-gradient(135deg, #4CAF50, #2E7D32); }
207
  .gate-failed { background: linear-gradient(135deg, #F44336, #D32F2F); }
208
  .gate-pending { background: linear-gradient(135deg, #9E9E9E, #616161); }
 
209
  .gate-container {
210
  display: flex;
211
  align-items: center;
 
214
  margin: 20px 0;
215
  flex-wrap: wrap;
216
  }
 
217
  .gate-line {
218
  width: 40px;
219
  height: 4px;
220
  background: linear-gradient(90deg, #E0E0E0, #BDBDBD);
221
  border-radius: 2px;
222
  }
 
223
  .mathematical-card {
224
  border-radius: 15px;
225
  padding: 25px;
 
231
  background: #FFFFFF;
232
  box-shadow: 0 8px 30px rgba(0,0,0,0.08);
233
  }
 
234
  .mathematical-card:hover {
235
  transform: translateY(-5px);
236
  box-shadow: 0 15px 40px rgba(0,0,0,0.15);
237
  }
 
238
  .license-oss {
239
  border-top-color: #1E88E5;
240
  background: linear-gradient(145deg, #E3F2FD, #FFFFFF);
241
  }
 
242
  .license-trial {
243
  border-top-color: #FFB300;
244
  background: linear-gradient(145deg, #FFF8E1, #FFFFFF);
245
  }
 
246
  @media (max-width: 768px) {
247
  .gradio-container { padding: 10px !important; }
248
  .arf-real-badge, .arf-sim-badge { padding: 6px 14px; font-size: 12px; }
 
250
  .gate-line { width: 20px; }
251
  .mathematical-card { padding: 15px; margin: 10px 0; }
252
  }
 
253
  @media (max-width: 480px) {
254
  .gradio-container { padding: 5px !important; }
255
  .arf-real-badge, .arf-sim-badge { padding: 4px 10px; font-size: 11px; }
 
361
 
362
  # ============== GRADIO INTERFACE ==============
363
  def create_enhanced_demo():
364
+ """Create enhanced demo with fixed HTML rendering bugs and Hugging Face Spaces compatibility"""
365
 
366
  # Get unified status
367
  arf_display = ARF_UNIFIED_STATUS['display_text']
368
  arf_badge_class = ARF_UNIFIED_STATUS['badge_class']
369
 
370
+ # CRITICAL FIX: Remove theme and css from Blocks constructor for Gradio 6.0+
371
+ # These will be passed to launch() instead
372
  with gr.Blocks(
373
+ title=f"ARF {ARF_UNIFIED_STATUS['version']} - Mathematical Sophistication"
374
+ # REMOVED: theme and css parameters - they now go in launch()
 
375
  ) as demo:
376
 
377
  # ===== HEADER =====
 
560
  </div>
561
  """)
562
 
563
+ # ===== ACTION HISTORY =====
564
+ history_display = gr.HTML("""
565
+ <div style="border: 1px solid #E0E0E0; border-radius: 15px; padding: 20px; background: #fafafa; box-shadow: 0 8px 30px rgba(0,0,0,0.08); margin-top: 20px;">
566
+ <h4 style="margin-top: 0; margin-bottom: 15px; color: #333;">📊 Mathematical Action History</h4>
567
+ <div style="overflow-x: auto;">
568
+ <div style="text-align: center; padding: 40px; color: #999; font-style: italic;">
569
+ No mathematical assessments yet. Test an action to see Bayesian analysis in action.
570
+ </div>
571
+ </div>
572
+ </div>
573
+ """)
574
+
575
  # ===== EVENT HANDLERS =====
576
  def update_context(scenario_name):
577
  """Update context with mathematical analysis"""
 
767
  # Gate visualization
768
  gates_visualization = ""
769
  for i in range(total_gates):
770
+ gate_class = "gate-passed" if i < gates_passed else "gate-failed" if i < 3 else "gate-pending"
771
  gates_visualization += f'<div class="mathematical-gate {gate_class}">{i+1}</div>'
772
  if i < total_gates - 1:
773
  gates_visualization += '<div class="gate-line"></div>'
 
941
  def generate_trial():
942
  """Generate mathematical trial license"""
943
  license_key = generate_mathematical_trial_license()
944
+ return license_key
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
945
 
946
  # Connect handlers
947
  scenario.change(
 
953
  test_btn.click(
954
  fn=test_mathematical_assessment,
955
  inputs=[scenario, context, license_key],
956
+ outputs=[oss_results, enterprise_results, license_display, history_display]
957
  )
958
 
959
  trial_btn.click(
 
969
  print("\n" + "="*80)
970
  print("🚀 LAUNCHING FIXED ARF 3.3.9 DEMO")
971
  print("📊 ARF Status:", ARF_UNIFIED_STATUS['display_text'])
972
+ print("🌐 Environment:", "Hugging Face Spaces" if is_huggingface_spaces() else "Local Development")
973
  print("="*80)
974
 
975
+ # Create the demo
976
  demo = create_enhanced_demo()
977
+
978
+ # CRITICAL FIX: Move theme and css to launch() for Gradio 6.0+ compatibility
979
+ # This ensures both Hugging Face Spaces and local development work correctly
980
+
981
+ # Base launch configuration
982
+ launch_kwargs = {
983
+ 'server_name': "0.0.0.0",
984
+ 'server_port': 7860,
985
+ 'share': False, # Don't create share links in Spaces
986
+ 'debug': False, # Disable debug mode in production
987
+ 'ssr_mode': False, # Disable experimental SSR mode
988
+ 'show_error': True, # Show errors for debugging
989
+ 'prevent_thread_lock': is_huggingface_spaces(), # Prevent thread blocking in Spaces
990
+ 'quiet': False, # Show full logs
991
+ # Move theme and css here for Gradio 6.0+ compatibility
992
+ 'theme': gr.themes.Soft(primary_hue="blue", secondary_hue="orange"),
993
+ 'css': ENHANCED_CSS
994
+ }
995
+
996
+ # Add auth for local development if needed
997
+ if not is_huggingface_spaces():
998
+ # For local development, we can enable share links
999
+ launch_kwargs['share'] = True
1000
+ launch_kwargs['debug'] = True
1001
+
1002
+ # Launch the demo
1003
+ demo.launch(**launch_kwargs)