Ubuntu Qwen-Coder commited on
Commit ·
18a9c44
1
Parent(s): 55b4830
Mount Gradio under /ui for HF Spaces, API now publicly accessible
Browse files
main.py
CHANGED
|
@@ -433,3 +433,26 @@ if __name__ == "__main__":
|
|
| 433 |
import uvicorn
|
| 434 |
|
| 435 |
uvicorn.run(app, host="0.0.0.0", port=8000)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 433 |
import uvicorn
|
| 434 |
|
| 435 |
uvicorn.run(app, host="0.0.0.0", port=8000)
|
| 436 |
+
|
| 437 |
+
|
| 438 |
+
# ============== MOUNT GRADIO FOR HF SPACES ==============
|
| 439 |
+
def mount_gradio_ui():
|
| 440 |
+
"""Mount Gradio UI under /ui path for HF Spaces deployment"""
|
| 441 |
+
try:
|
| 442 |
+
import gradio as gr
|
| 443 |
+
# Lazy import to avoid circular dependency
|
| 444 |
+
import importlib.util
|
| 445 |
+
spec = importlib.util.spec_from_file_location("app_gradio", "app_gradio.py")
|
| 446 |
+
app_gradio = importlib.util.module_from_spec(spec)
|
| 447 |
+
spec.loader.exec_module(app_gradio)
|
| 448 |
+
|
| 449 |
+
# Mount Gradio under /ui
|
| 450 |
+
app.mount("/ui", gr.mount_gradio_app(app, app_gradio.blocks, path="/"))
|
| 451 |
+
print("✅ Gradio UI mounted at /ui")
|
| 452 |
+
except Exception as e:
|
| 453 |
+
print(f"⚠️ Could not mount Gradio: {e}")
|
| 454 |
+
|
| 455 |
+
|
| 456 |
+
# Auto-mount when running in HF Spaces (PORT env var set)
|
| 457 |
+
if os.getenv("PORT"):
|
| 458 |
+
mount_gradio_ui()
|
start.sh
CHANGED
|
@@ -3,23 +3,17 @@ set -euo pipefail
|
|
| 3 |
|
| 4 |
cd "$(dirname "$0")"
|
| 5 |
|
| 6 |
-
API_HOST="${API_HOST:-
|
| 7 |
-
API_PORT="${
|
| 8 |
-
UI_HOST="${UI_HOST:-0.0.0.0}"
|
| 9 |
-
UI_PORT="${PORT:-7860}"
|
| 10 |
|
| 11 |
: "${DATABASE_URL:?DATABASE_URL is not set. Add it as a Hugging Face Space Secret (Settings → Repository secrets).}"
|
| 12 |
|
| 13 |
-
echo "Starting FastAPI on http://${API_HOST}:${API_PORT} ..."
|
| 14 |
-
|
| 15 |
-
|
| 16 |
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
kill "${API_PID}" 2>/dev/null || true
|
| 20 |
-
}
|
| 21 |
-
trap cleanup EXIT
|
| 22 |
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
uv run python app_gradio.py
|
|
|
|
| 3 |
|
| 4 |
cd "$(dirname "$0")"
|
| 5 |
|
| 6 |
+
API_HOST="${API_HOST:-0.0.0.0}"
|
| 7 |
+
API_PORT="${PORT:-7860}"
|
|
|
|
|
|
|
| 8 |
|
| 9 |
: "${DATABASE_URL:?DATABASE_URL is not set. Add it as a Hugging Face Space Secret (Settings → Repository secrets).}"
|
| 10 |
|
| 11 |
+
echo "Starting FastAPI + Gradio (mounted at /ui) on http://${API_HOST}:${API_PORT} ..."
|
| 12 |
+
echo "UI: http://localhost:${API_PORT}/ui"
|
| 13 |
+
echo "API Docs: http://localhost:${API_PORT}/docs"
|
| 14 |
|
| 15 |
+
# Internal API URL for Gradio to use
|
| 16 |
+
export KEYVAULT_BASE_URL="http://127.0.0.1:8000"
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
+
# Run on PORT (7860 for HF Spaces) - FastAPI will mount Gradio at /ui
|
| 19 |
+
uv run uvicorn main:app --host "${API_HOST}" --port "${API_PORT}"
|
|
|