Spaces:
No application file
No application file
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,3 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from typing import List, Dict, Any, Optional, Union
|
| 2 |
from pydantic import BaseModel, Field, field_validator
|
| 3 |
import logging
|
|
@@ -12,7 +18,6 @@ from functools import lru_cache
|
|
| 12 |
import gradio as gr
|
| 13 |
from fastapi import FastAPI, HTTPException
|
| 14 |
from fastapi.middleware.cors import CORSMiddleware
|
| 15 |
-
import os
|
| 16 |
import pandas as pd
|
| 17 |
from openai import OpenAI, AsyncOpenAI
|
| 18 |
import requests
|
|
@@ -1126,14 +1131,34 @@ if __name__ == "__main__":
|
|
| 1126 |
log_config = uvicorn.config.LOGGING_CONFIG
|
| 1127 |
log_config["formatters"]["access"]["fmt"] = "%(asctime)s - %(levelname)s - %(message)s"
|
| 1128 |
|
| 1129 |
-
# Get port from environment variable
|
| 1130 |
-
port = int(os.environ.get("PORT", 7860))
|
|
|
|
|
|
|
|
|
|
| 1131 |
|
| 1132 |
-
# Run the server
|
| 1133 |
-
|
| 1134 |
-
|
| 1135 |
-
|
| 1136 |
-
|
| 1137 |
-
|
| 1138 |
-
|
| 1139 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import sys
|
| 2 |
+
import os
|
| 3 |
+
|
| 4 |
+
# Add the current directory to Python path
|
| 5 |
+
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
|
| 6 |
+
|
| 7 |
from typing import List, Dict, Any, Optional, Union
|
| 8 |
from pydantic import BaseModel, Field, field_validator
|
| 9 |
import logging
|
|
|
|
| 18 |
import gradio as gr
|
| 19 |
from fastapi import FastAPI, HTTPException
|
| 20 |
from fastapi.middleware.cors import CORSMiddleware
|
|
|
|
| 21 |
import pandas as pd
|
| 22 |
from openai import OpenAI, AsyncOpenAI
|
| 23 |
import requests
|
|
|
|
| 1131 |
log_config = uvicorn.config.LOGGING_CONFIG
|
| 1132 |
log_config["formatters"]["access"]["fmt"] = "%(asctime)s - %(levelname)s - %(message)s"
|
| 1133 |
|
| 1134 |
+
# Get port from environment variable with a fallback
|
| 1135 |
+
port = int(os.environ.get("PORT", os.environ.get("SPACE_PORT", 7860)))
|
| 1136 |
+
|
| 1137 |
+
# Get host from environment variable with a fallback
|
| 1138 |
+
host = os.environ.get("HOST", os.environ.get("SPACE_HOST", "0.0.0.0"))
|
| 1139 |
|
| 1140 |
+
# Run the server with error handling
|
| 1141 |
+
try:
|
| 1142 |
+
uvicorn.run(
|
| 1143 |
+
"app:app", # Use string reference to the app
|
| 1144 |
+
host=host,
|
| 1145 |
+
port=port,
|
| 1146 |
+
log_config=log_config,
|
| 1147 |
+
log_level="info",
|
| 1148 |
+
reload=False # Disable reload in production
|
| 1149 |
+
)
|
| 1150 |
+
except OSError as e:
|
| 1151 |
+
if e.errno == 98: # Address already in use
|
| 1152 |
+
# Try alternative port
|
| 1153 |
+
alt_port = port + 1
|
| 1154 |
+
logger.warning(f"Port {port} is in use, trying port {alt_port}")
|
| 1155 |
+
uvicorn.run(
|
| 1156 |
+
"app:app",
|
| 1157 |
+
host=host,
|
| 1158 |
+
port=alt_port,
|
| 1159 |
+
log_config=log_config,
|
| 1160 |
+
log_level="info",
|
| 1161 |
+
reload=False
|
| 1162 |
+
)
|
| 1163 |
+
else:
|
| 1164 |
+
raise
|