wu981526092 commited on
Commit
360b135
Β·
1 Parent(s): 8eb16a3

Fix HF Spaces startup - use port 7860 and simplify startup logic

Browse files
Files changed (2) hide show
  1. app.py +16 -22
  2. backend/config.py +2 -2
app.py CHANGED
@@ -148,36 +148,25 @@ if __name__ == "__main__":
148
  signal.signal(signal.SIGINT, cleanup_handler)
149
  signal.signal(signal.SIGTERM, cleanup_handler)
150
 
151
- print("πŸš€ Starting Edge LLM with auto-build frontend...")
152
 
153
- # Find available port
154
- import os
155
- original_port = int(os.getenv("PORT", "0")) # Use env var or auto-assign
156
- if original_port == 0:
157
- # Auto-assign a free port starting from 8000
158
- original_port = find_free_port(8000)
159
- print(f"πŸ” Auto-assigned port: {original_port}")
160
- else:
161
- kill_processes_on_port(original_port)
162
 
163
- try:
164
- port = find_free_port(original_port)
165
- print(f"πŸ“‘ Using port: {port}")
166
-
167
- if port != original_port:
168
- print(f"⚠️ Port {original_port} was busy, switched to {port}")
169
- update_frontend_config(port)
170
 
171
  # Auto-build frontend if needed
172
  if should_rebuild_frontend():
173
  print("πŸ”¨ Building frontend...")
174
  build_frontend()
175
 
176
- # Start the backend server
177
- print(f"🌐 Starting server on http://localhost:{port}")
178
- print("🎯 Frontend and Backend integrated - ready to use!")
179
-
180
- # Auto-open browser after a short delay
181
  def open_browser():
182
  time.sleep(2)
183
  webbrowser.open(f'http://localhost:{port}')
@@ -186,6 +175,11 @@ if __name__ == "__main__":
186
  browser_thread = threading.Thread(target=open_browser)
187
  browser_thread.daemon = True
188
  browser_thread.start()
 
 
 
 
 
189
 
190
  # Start the server
191
  uvicorn.run(app, host="0.0.0.0", port=port)
 
148
  signal.signal(signal.SIGINT, cleanup_handler)
149
  signal.signal(signal.SIGTERM, cleanup_handler)
150
 
151
+ print("πŸš€ Starting Edge LLM...")
152
 
153
+ # Use HF Spaces default port (7860) or environment variable
154
+ port = int(os.getenv("PORT", "7860"))
155
+ print(f"πŸ“‘ Using port: {port}")
 
 
 
 
 
 
156
 
157
+ # For Hugging Face Spaces, skip complex port logic and auto-build
158
+ is_hf_space = os.getenv("SPACE_ID") is not None
159
+
160
+ if not is_hf_space:
161
+ # Local development: kill existing processes and auto-build
162
+ kill_processes_on_port(port)
 
163
 
164
  # Auto-build frontend if needed
165
  if should_rebuild_frontend():
166
  print("πŸ”¨ Building frontend...")
167
  build_frontend()
168
 
169
+ # Auto-open browser for local development
 
 
 
 
170
  def open_browser():
171
  time.sleep(2)
172
  webbrowser.open(f'http://localhost:{port}')
 
175
  browser_thread = threading.Thread(target=open_browser)
176
  browser_thread.daemon = True
177
  browser_thread.start()
178
+
179
+ try:
180
+ # Start the backend server
181
+ print(f"🌐 Starting server on http://{'0.0.0.0' if is_hf_space else 'localhost'}:{port}")
182
+ print("🎯 Frontend and Backend integrated - ready to use!")
183
 
184
  # Start the server
185
  uvicorn.run(app, host="0.0.0.0", port=port)
backend/config.py CHANGED
@@ -59,6 +59,6 @@ CORS_ORIGINS = ["*"] # Allow all origins for HF Space
59
  FRONTEND_DIST_DIR = "static"
60
  ASSETS_DIR = "static/assets"
61
 
62
- # Server settings (port will be dynamically determined)
63
  HOST = "0.0.0.0"
64
- DEFAULT_PORT = int(os.getenv("PORT", "0")) # 0 means auto-assign a free port
 
59
  FRONTEND_DIST_DIR = "static"
60
  ASSETS_DIR = "static/assets"
61
 
62
+ # Server settings
63
  HOST = "0.0.0.0"
64
+ DEFAULT_PORT = int(os.getenv("PORT", "7860")) # Use HF Spaces default port 7860