petter2025 commited on
Commit
6dc71ce
Β·
verified Β·
1 Parent(s): c4f5850

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -5
app.py CHANGED
@@ -3001,13 +3001,27 @@ def main():
3001
  print("πŸ“Š Architecture: OSS advises β†’ Enterprise executes")
3002
  print("🎭 DOCTRINAL: Historical Evidence + Observation Gate + Sequencing")
3003
  print("🎨 MODERN UI: Design system with responsive components")
3004
- print("🌐 Starting on http://localhost:7860")
3005
  print("="*60 + "\n")
3006
 
3007
- # Launch with configuration for Hugging Face Spaces
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3008
  launch_config = {
3009
  "server_name": "0.0.0.0",
3010
- "server_port": 7860,
3011
  "share": False,
3012
  "favicon_path": None,
3013
  "quiet": False,
@@ -3021,7 +3035,8 @@ def main():
3021
  if css_styles:
3022
  launch_config["css"] = css_styles
3023
 
3024
- logger.info(f"βœ… Launching demo with config: {launch_config}")
 
3025
 
3026
  # LAUNCH THE DEMO - THIS IS THE CRITICAL PART
3027
  demo.launch(**launch_config)
@@ -3046,7 +3061,7 @@ if __name__ == "__main__":
3046
 
3047
  # Set environment variables for better compatibility
3048
  os.environ["GRADIO_ANALYTICS_ENABLED"] = "False"
3049
- os.environ["GRADIO_SERVER_PORT"] = "7860"
3050
  os.environ["GRADIO_SERVER_NAME"] = "0.0.0.0"
3051
 
3052
  print("\n" + "="*60)
@@ -3068,5 +3083,14 @@ if __name__ == "__main__":
3068
  print(f"⚠️ Missing {len(missing_files)} required files")
3069
  print("⚠️ Some features may not work correctly")
3070
 
 
 
 
 
 
 
 
 
 
3071
  # Start the main application
3072
  main()
 
3001
  print("πŸ“Š Architecture: OSS advises β†’ Enterprise executes")
3002
  print("🎭 DOCTRINAL: Historical Evidence + Observation Gate + Sequencing")
3003
  print("🎨 MODERN UI: Design system with responsive components")
 
3004
  print("="*60 + "\n")
3005
 
3006
+ # ============ FIXED: PORT HANDLING FOR HUGGING FACE SPACES ============
3007
+ # Get port from environment variable or use a dynamic port finder
3008
+ port = 7860 # Default
3009
+
3010
+ # Try to find a free port if default is in use
3011
+ try:
3012
+ import socket
3013
+ sock = socket.socket()
3014
+ sock.bind(('', 0))
3015
+ port = sock.getsockname()[1]
3016
+ sock.close()
3017
+ logger.info(f"βœ… Found free port: {port}")
3018
+ except Exception as e:
3019
+ logger.warning(f"⚠️ Could not find free port, using default: {e}")
3020
+
3021
+ # Launch configuration - FIXED: Use dynamic port
3022
  launch_config = {
3023
  "server_name": "0.0.0.0",
3024
+ "server_port": port,
3025
  "share": False,
3026
  "favicon_path": None,
3027
  "quiet": False,
 
3035
  if css_styles:
3036
  launch_config["css"] = css_styles
3037
 
3038
+ logger.info(f"βœ… Launching demo on port {port} with config: {launch_config}")
3039
+ print(f"🌐 Starting on http://localhost:{port}")
3040
 
3041
  # LAUNCH THE DEMO - THIS IS THE CRITICAL PART
3042
  demo.launch(**launch_config)
 
3061
 
3062
  # Set environment variables for better compatibility
3063
  os.environ["GRADIO_ANALYTICS_ENABLED"] = "False"
3064
+ os.environ["GRADIO_SERVER_PORT"] = "7860" # Set default, but main() will find free port
3065
  os.environ["GRADIO_SERVER_NAME"] = "0.0.0.0"
3066
 
3067
  print("\n" + "="*60)
 
3083
  print(f"⚠️ Missing {len(missing_files)} required files")
3084
  print("⚠️ Some features may not work correctly")
3085
 
3086
+ # Fix for uvicorn compatibility issue
3087
+ try:
3088
+ # Apply nest_asyncio before any async operations
3089
+ import nest_asyncio
3090
+ nest_asyncio.apply()
3091
+ logger.info("βœ… Applied nest_asyncio for Hugging Face Spaces compatibility")
3092
+ except Exception as e:
3093
+ logger.warning(f"⚠️ Could not apply nest_asyncio: {e}")
3094
+
3095
  # Start the main application
3096
  main()