Spaces:
No application file
No application file
Update app.py
Browse files
app.py
CHANGED
|
@@ -1121,44 +1121,10 @@ with gr.Blocks() as demo:
|
|
| 1121 |
# Mount Gradio app to FastAPI
|
| 1122 |
app = gr.mount_gradio_app(app, demo, path="/")
|
| 1123 |
|
| 1124 |
-
#
|
| 1125 |
-
|
| 1126 |
-
|
| 1127 |
if __name__ == "__main__":
|
| 1128 |
-
|
| 1129 |
-
|
| 1130 |
-
|
| 1131 |
-
|
| 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
|
|
|
|
| 1121 |
# Mount Gradio app to FastAPI
|
| 1122 |
app = gr.mount_gradio_app(app, demo, path="/")
|
| 1123 |
|
| 1124 |
+
# For Hugging Face Spaces, we don't need to run the server manually
|
| 1125 |
+
# The platform will handle it for us
|
|
|
|
| 1126 |
if __name__ == "__main__":
|
| 1127 |
+
# Only run the server directly when not in a Hugging Face Space
|
| 1128 |
+
if not os.environ.get("SPACE_ID"):
|
| 1129 |
+
import uvicorn
|
| 1130 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|