Update app.py
Browse files
app.py
CHANGED
|
@@ -54,12 +54,15 @@ def get_feature_flags():
|
|
| 54 |
return flags
|
| 55 |
|
| 56 |
# ===========================================
|
| 57 |
-
# FIX FOR ASYNC EVENT LOOP ISSUES
|
| 58 |
# ===========================================
|
|
|
|
|
|
|
| 59 |
try:
|
| 60 |
import nest_asyncio
|
| 61 |
-
|
| 62 |
-
|
|
|
|
| 63 |
except ImportError:
|
| 64 |
logger.warning("β οΈ nest_asyncio not available, async operations may have issues")
|
| 65 |
|
|
@@ -332,10 +335,10 @@ class AsyncRunner:
|
|
| 332 |
return wrapper
|
| 333 |
|
| 334 |
# ===========================================
|
| 335 |
-
# SIMPLE SETTINGS - FIXED: Added missing
|
| 336 |
# ===========================================
|
| 337 |
class Settings:
|
| 338 |
-
"""Simple settings class - FIXED: Added
|
| 339 |
def __init__(self):
|
| 340 |
self.arf_mode = "demo"
|
| 341 |
self.use_true_arf = True
|
|
@@ -355,6 +358,10 @@ class Settings:
|
|
| 355 |
self.telemetry_enabled = True
|
| 356 |
self.mcp_mode = "simulated"
|
| 357 |
self.enterprise_features = ["simulated_execution", "rollback_guarantee"]
|
|
|
|
|
|
|
|
|
|
|
|
|
| 358 |
|
| 359 |
settings = Settings()
|
| 360 |
|
|
@@ -2386,10 +2393,9 @@ def create_demo_interface():
|
|
| 2386 |
# Get feature flags
|
| 2387 |
flags = get_feature_flags()
|
| 2388 |
|
| 2389 |
-
# Create interface
|
| 2390 |
with gr.Blocks(
|
| 2391 |
-
title=f"π ARF Investor Demo v3.3.9 - TRUE ARF OSS Integration"
|
| 2392 |
-
css=load_css_files() # Load CSS directly
|
| 2393 |
) as demo:
|
| 2394 |
|
| 2395 |
# MODERN UI INITIALIZATION
|
|
@@ -2987,7 +2993,7 @@ def create_dark_mode_toggle():
|
|
| 2987 |
"""
|
| 2988 |
|
| 2989 |
# ===========================================
|
| 2990 |
-
# MAIN EXECUTION - CRITICAL: THIS LAUNCHES THE APP
|
| 2991 |
# ===========================================
|
| 2992 |
|
| 2993 |
def main():
|
|
@@ -3020,18 +3026,15 @@ def main():
|
|
| 3020 |
port = int(os.getenv("GRADIO_SERVER_PORT", "7860"))
|
| 3021 |
server_name = os.getenv("GRADIO_SERVER_NAME", "0.0.0.0")
|
| 3022 |
|
| 3023 |
-
#
|
| 3024 |
-
# Use a port range instead of a single port
|
| 3025 |
-
server_ports = [port, port + 1, port + 2] # Try multiple ports
|
| 3026 |
-
|
| 3027 |
-
# Get CSS if available
|
| 3028 |
css_styles = load_css_files()
|
| 3029 |
|
| 3030 |
-
logger.info(f"π Launching on {server_name}
|
| 3031 |
print(f"π Starting on http://{server_name}:{port}")
|
| 3032 |
|
| 3033 |
# SIMPLE LAUNCH FOR SPACES COMPATIBILITY
|
| 3034 |
demo.launch(
|
|
|
|
| 3035 |
server_name=server_name,
|
| 3036 |
server_port=port,
|
| 3037 |
share=False,
|
|
@@ -3049,7 +3052,7 @@ def main():
|
|
| 3049 |
sys.exit(1)
|
| 3050 |
|
| 3051 |
# ===========================================
|
| 3052 |
-
# HUGGING FACE SPACES COMPATIBILITY
|
| 3053 |
# ===========================================
|
| 3054 |
|
| 3055 |
# This is the entry point that Hugging Face Spaces will use
|
|
@@ -3059,35 +3062,14 @@ if __name__ == "__main__":
|
|
| 3059 |
|
| 3060 |
# ============ CRITICAL FIXES FOR HUGGING FACE SPACES ============
|
| 3061 |
|
| 3062 |
-
# 1.
|
| 3063 |
-
# This must happen before ANY asyncio operations
|
| 3064 |
-
try:
|
| 3065 |
-
import nest_asyncio
|
| 3066 |
-
import asyncio
|
| 3067 |
-
|
| 3068 |
-
# Get or create event loop
|
| 3069 |
-
try:
|
| 3070 |
-
loop = asyncio.get_event_loop()
|
| 3071 |
-
except RuntimeError:
|
| 3072 |
-
# No event loop exists yet, create one
|
| 3073 |
-
loop = asyncio.new_event_loop()
|
| 3074 |
-
asyncio.set_event_loop(loop)
|
| 3075 |
-
|
| 3076 |
-
# Apply nest_asyncio to the event loop
|
| 3077 |
-
nest_asyncio.apply(loop)
|
| 3078 |
-
logger.info("β
Applied nest_asyncio to event loop - fixes uvicorn loop_factory error")
|
| 3079 |
-
except Exception as e:
|
| 3080 |
-
logger.warning(f"β οΈ Could not apply nest_asyncio: {e}")
|
| 3081 |
-
# Continue anyway - some versions might work without it
|
| 3082 |
-
|
| 3083 |
-
# 2. Set environment variables for Hugging Face Spaces compatibility
|
| 3084 |
os.environ["GRADIO_ANALYTICS_ENABLED"] = "False"
|
| 3085 |
os.environ["GRADIO_SERVER_PORT"] = "7860" # Spaces will override this if needed
|
| 3086 |
os.environ["GRADIO_SERVER_NAME"] = "0.0.0.0"
|
| 3087 |
os.environ["GRADIO_HOT_RELOAD"] = "False" # Disable hot reload in Spaces
|
| 3088 |
os.environ["GRADIO_QUEUE_ENABLED"] = "True" # Enable queue for stability
|
| 3089 |
|
| 3090 |
-
#
|
| 3091 |
os.environ["UVICORN_LOG_LEVEL"] = "warning" # Reduce uvicorn log noise
|
| 3092 |
os.environ["UVICORN_ACCESS_LOG"] = "False" # Disable access logs
|
| 3093 |
|
|
@@ -3097,13 +3079,13 @@ if __name__ == "__main__":
|
|
| 3097 |
print(f"π Python version: {sys.version}")
|
| 3098 |
print("="*60 + "\n")
|
| 3099 |
|
| 3100 |
-
#
|
| 3101 |
is_huggingface_space = "SPACE_ID" in os.environ or "HF_SPACE" in os.environ
|
| 3102 |
if is_huggingface_space:
|
| 3103 |
print("β
Hugging Face Spaces environment detected")
|
| 3104 |
print("π€ Using Spaces-optimized configuration")
|
| 3105 |
|
| 3106 |
-
#
|
| 3107 |
required_files = ["styles/modern.css", "styles/responsive.css", "ui/components.py"]
|
| 3108 |
missing_files = []
|
| 3109 |
|
|
@@ -3129,7 +3111,7 @@ if __name__ == "__main__":
|
|
| 3129 |
except Exception as e:
|
| 3130 |
print(f"β οΈ Could not create {css_file}: {e}")
|
| 3131 |
|
| 3132 |
-
#
|
| 3133 |
try:
|
| 3134 |
import gradio as gr
|
| 3135 |
logger.info(f"β
Gradio {gr.__version__} loaded successfully")
|
|
@@ -3138,7 +3120,7 @@ if __name__ == "__main__":
|
|
| 3138 |
print("β CRITICAL: Gradio failed to load")
|
| 3139 |
raise
|
| 3140 |
|
| 3141 |
-
#
|
| 3142 |
try:
|
| 3143 |
main()
|
| 3144 |
except Exception as e:
|
|
|
|
| 54 |
return flags
|
| 55 |
|
| 56 |
# ===========================================
|
| 57 |
+
# FIX FOR ASYNC EVENT LOOP ISSUES - UPDATED FOR SPACES COMPATIBILITY
|
| 58 |
# ===========================================
|
| 59 |
+
# CRITICAL FIX: Don't apply nest_asyncio here - let uvicorn handle it
|
| 60 |
+
# This fixes the "loop_factory" TypeError
|
| 61 |
try:
|
| 62 |
import nest_asyncio
|
| 63 |
+
# Only apply if we're NOT in the main thread (detect if uvicorn is running)
|
| 64 |
+
# We'll handle this differently in the main() function
|
| 65 |
+
logger.info("β
nest_asyncio imported but not applied yet")
|
| 66 |
except ImportError:
|
| 67 |
logger.warning("β οΈ nest_asyncio not available, async operations may have issues")
|
| 68 |
|
|
|
|
| 335 |
return wrapper
|
| 336 |
|
| 337 |
# ===========================================
|
| 338 |
+
# SIMPLE SETTINGS - FIXED: Added missing attributes
|
| 339 |
# ===========================================
|
| 340 |
class Settings:
|
| 341 |
+
"""Simple settings class - FIXED: Added all missing attributes"""
|
| 342 |
def __init__(self):
|
| 343 |
self.arf_mode = "demo"
|
| 344 |
self.use_true_arf = True
|
|
|
|
| 358 |
self.telemetry_enabled = True
|
| 359 |
self.mcp_mode = "simulated"
|
| 360 |
self.enterprise_features = ["simulated_execution", "rollback_guarantee"]
|
| 361 |
+
self.default_savings_rate = 0.25 # FIXED: Ensure it's defined
|
| 362 |
+
self.enable_mcp_integration = True # FIXED: Added missing
|
| 363 |
+
self.enable_learning_engine = True # FIXED: Added missing
|
| 364 |
+
self.max_concurrent_incidents = 5 # FIXED: Added missing
|
| 365 |
|
| 366 |
settings = Settings()
|
| 367 |
|
|
|
|
| 2393 |
# Get feature flags
|
| 2394 |
flags = get_feature_flags()
|
| 2395 |
|
| 2396 |
+
# Create interface WITHOUT css parameter (fixes Gradio 6.0 warning)
|
| 2397 |
with gr.Blocks(
|
| 2398 |
+
title=f"π ARF Investor Demo v3.3.9 - TRUE ARF OSS Integration"
|
|
|
|
| 2399 |
) as demo:
|
| 2400 |
|
| 2401 |
# MODERN UI INITIALIZATION
|
|
|
|
| 2993 |
"""
|
| 2994 |
|
| 2995 |
# ===========================================
|
| 2996 |
+
# MAIN EXECUTION - CRITICAL: THIS LAUNCHES THE APP - UPDATED FOR SPACES
|
| 2997 |
# ===========================================
|
| 2998 |
|
| 2999 |
def main():
|
|
|
|
| 3026 |
port = int(os.getenv("GRADIO_SERVER_PORT", "7860"))
|
| 3027 |
server_name = os.getenv("GRADIO_SERVER_NAME", "0.0.0.0")
|
| 3028 |
|
| 3029 |
+
# Get CSS
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3030 |
css_styles = load_css_files()
|
| 3031 |
|
| 3032 |
+
logger.info(f"π Launching on {server_name}:{port}")
|
| 3033 |
print(f"π Starting on http://{server_name}:{port}")
|
| 3034 |
|
| 3035 |
# SIMPLE LAUNCH FOR SPACES COMPATIBILITY
|
| 3036 |
demo.launch(
|
| 3037 |
+
css=css_styles, # CSS moved to launch() - fixes Gradio 6.0 warning
|
| 3038 |
server_name=server_name,
|
| 3039 |
server_port=port,
|
| 3040 |
share=False,
|
|
|
|
| 3052 |
sys.exit(1)
|
| 3053 |
|
| 3054 |
# ===========================================
|
| 3055 |
+
# HUGGING FACE SPACES COMPATIBILITY - UPDATED
|
| 3056 |
# ===========================================
|
| 3057 |
|
| 3058 |
# This is the entry point that Hugging Face Spaces will use
|
|
|
|
| 3062 |
|
| 3063 |
# ============ CRITICAL FIXES FOR HUGGING FACE SPACES ============
|
| 3064 |
|
| 3065 |
+
# 1. Set environment variables for Hugging Face Spaces compatibility
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3066 |
os.environ["GRADIO_ANALYTICS_ENABLED"] = "False"
|
| 3067 |
os.environ["GRADIO_SERVER_PORT"] = "7860" # Spaces will override this if needed
|
| 3068 |
os.environ["GRADIO_SERVER_NAME"] = "0.0.0.0"
|
| 3069 |
os.environ["GRADIO_HOT_RELOAD"] = "False" # Disable hot reload in Spaces
|
| 3070 |
os.environ["GRADIO_QUEUE_ENABLED"] = "True" # Enable queue for stability
|
| 3071 |
|
| 3072 |
+
# 2. Additional fixes for uvicorn warnings
|
| 3073 |
os.environ["UVICORN_LOG_LEVEL"] = "warning" # Reduce uvicorn log noise
|
| 3074 |
os.environ["UVICORN_ACCESS_LOG"] = "False" # Disable access logs
|
| 3075 |
|
|
|
|
| 3079 |
print(f"π Python version: {sys.version}")
|
| 3080 |
print("="*60 + "\n")
|
| 3081 |
|
| 3082 |
+
# 3. Detect if we're running in Hugging Face Spaces
|
| 3083 |
is_huggingface_space = "SPACE_ID" in os.environ or "HF_SPACE" in os.environ
|
| 3084 |
if is_huggingface_space:
|
| 3085 |
print("β
Hugging Face Spaces environment detected")
|
| 3086 |
print("π€ Using Spaces-optimized configuration")
|
| 3087 |
|
| 3088 |
+
# 4. Check for required files with better error handling
|
| 3089 |
required_files = ["styles/modern.css", "styles/responsive.css", "ui/components.py"]
|
| 3090 |
missing_files = []
|
| 3091 |
|
|
|
|
| 3111 |
except Exception as e:
|
| 3112 |
print(f"β οΈ Could not create {css_file}: {e}")
|
| 3113 |
|
| 3114 |
+
# 5. Import gradio early to prevent threading issues
|
| 3115 |
try:
|
| 3116 |
import gradio as gr
|
| 3117 |
logger.info(f"β
Gradio {gr.__version__} loaded successfully")
|
|
|
|
| 3120 |
print("β CRITICAL: Gradio failed to load")
|
| 3121 |
raise
|
| 3122 |
|
| 3123 |
+
# 6. Start the main application with better error handling
|
| 3124 |
try:
|
| 3125 |
main()
|
| 3126 |
except Exception as e:
|