Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -8,7 +8,8 @@ from fastapi import FastAPI
|
|
| 8 |
from pydantic import BaseModel
|
| 9 |
import uvicorn
|
| 10 |
import openai
|
| 11 |
-
import socket
|
|
|
|
| 12 |
|
| 13 |
# --- Configuration ---
|
| 14 |
BASE_MODEL_ID = "meta-llama/Llama-3.1-8B-Instruct"
|
|
@@ -292,33 +293,52 @@ app = gr.mount_gradio_app(app, demo, path="/")
|
|
| 292 |
if __name__ == "__main__":
|
| 293 |
load_model()
|
| 294 |
|
| 295 |
-
# 1. Determine the primary port
|
| 296 |
primary_port = int(os.environ.get("PORT", 7860))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 297 |
|
| 298 |
-
#
|
| 299 |
-
|
|
|
|
| 300 |
|
| 301 |
-
|
| 302 |
-
|
| 303 |
-
print(f"Attempting to run Uvicorn on primary port {primary_port}")
|
| 304 |
-
uvicorn.run(app, host="0.0.0.0", port=primary_port, loop="asyncio")
|
| 305 |
-
|
| 306 |
-
# Catch specific socket errors (Address already in use is often socket.error/OSError 98)
|
| 307 |
-
except (OSError, socket.error) as e:
|
| 308 |
-
# Check for "Address already in use" (Error code 98)
|
| 309 |
-
if getattr(e, 'errno', None) == 98 or "address already in use" in str(e).lower():
|
| 310 |
-
print(f"ERROR: Primary port {primary_port} is already in use. Trying fallback port {fallback_port}...")
|
| 311 |
-
|
| 312 |
-
# Try running on the fallback port
|
| 313 |
try:
|
| 314 |
-
|
| 315 |
-
|
| 316 |
-
|
| 317 |
-
|
| 318 |
-
|
| 319 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 320 |
else:
|
| 321 |
-
raise
|
| 322 |
-
|
| 323 |
-
|
| 324 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
from pydantic import BaseModel
|
| 9 |
import uvicorn
|
| 10 |
import openai
|
| 11 |
+
import socket
|
| 12 |
+
import time # Added for retry mechanism
|
| 13 |
|
| 14 |
# --- Configuration ---
|
| 15 |
BASE_MODEL_ID = "meta-llama/Llama-3.1-8B-Instruct"
|
|
|
|
| 293 |
if __name__ == "__main__":
|
| 294 |
load_model()
|
| 295 |
|
| 296 |
+
# 1. Determine the primary port and define a sequence of ports to try
|
| 297 |
primary_port = int(os.environ.get("PORT", 7860))
|
| 298 |
+
# Create a list of ports to try, prioritizing the environment variable, then common Gradio ports
|
| 299 |
+
ports_to_try = [primary_port]
|
| 300 |
+
if 7860 not in ports_to_try:
|
| 301 |
+
ports_to_try.append(7860)
|
| 302 |
+
if 7861 not in ports_to_try:
|
| 303 |
+
ports_to_try.append(7861)
|
| 304 |
|
| 305 |
+
# Simple retry mechanism
|
| 306 |
+
max_retries = 3
|
| 307 |
+
server_started = False
|
| 308 |
|
| 309 |
+
for port in ports_to_try:
|
| 310 |
+
for attempt in range(max_retries):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 311 |
try:
|
| 312 |
+
print(f"Attempting to run Uvicorn on port {port} (Attempt {attempt + 1}/{max_retries})")
|
| 313 |
+
uvicorn.run(app, host="0.0.0.0", port=port, loop="asyncio")
|
| 314 |
+
|
| 315 |
+
# If uvicorn.run returns without an exception, the server has started (or shut down gracefully)
|
| 316 |
+
server_started = True
|
| 317 |
+
print(f"Successfully started server on port {port}.")
|
| 318 |
+
# It's better to let uvicorn run indefinitely. If it exits here, it's usually due to a kill signal.
|
| 319 |
+
# However, in this environment, breaking and exiting is the standard procedure.
|
| 320 |
+
break
|
| 321 |
+
|
| 322 |
+
# Catch specific socket errors (Address already in use is often socket.error/OSError 98)
|
| 323 |
+
except (OSError, socket.error) as e:
|
| 324 |
+
# Check for "Address already in use" (Error code 98)
|
| 325 |
+
if getattr(e, 'errno', None) == 98 or "address already in use" in str(e).lower():
|
| 326 |
+
print(f"WARNING: Port {port} is already in use. Retrying in 1 second...")
|
| 327 |
+
time.sleep(1) # Wait a moment before retrying the same port
|
| 328 |
+
|
| 329 |
+
if attempt == max_retries - 1:
|
| 330 |
+
# If this was the last attempt for this port, move to the next port
|
| 331 |
+
print(f"All {max_retries} attempts failed for port {port}. Trying next port in sequence.")
|
| 332 |
+
break # Break inner loop to try next port in ports_to_try
|
| 333 |
else:
|
| 334 |
+
# Re-raise other unexpected errors
|
| 335 |
+
print(f"An unexpected error occurred: {e}")
|
| 336 |
+
raise e
|
| 337 |
+
|
| 338 |
+
if server_started:
|
| 339 |
+
# If the server started successfully, break the outer port loop
|
| 340 |
+
break
|
| 341 |
+
|
| 342 |
+
if not server_started:
|
| 343 |
+
print("FATAL ERROR: Failed to bind to any available port after multiple retries. Process terminating.")
|
| 344 |
+
exit(1) # Ensure process exits with failure code if no port is found.
|