Spaces:
Running
Running
fix: torch_dtype->dtype, robust app launch for HF Spaces
Browse files- app.py +32 -21
- sonicoder/model/loader.py +1 -1
app.py
CHANGED
|
@@ -17,6 +17,7 @@ from __future__ import annotations
|
|
| 17 |
|
| 18 |
import json
|
| 19 |
import logging
|
|
|
|
| 20 |
from pathlib import Path
|
| 21 |
|
| 22 |
# Ensure workspace exists
|
|
@@ -54,49 +55,59 @@ def main():
|
|
| 54 |
else:
|
| 55 |
logger.info(f"MCP server '{name}' connected")
|
| 56 |
|
| 57 |
-
# Create the Gradio app
|
| 58 |
from sonicoder.server import get_app, api_get_config
|
| 59 |
-
application = get_app()
|
| 60 |
|
| 61 |
-
#
|
| 62 |
config_json = api_get_config()
|
| 63 |
config_data = json.loads(config_json)
|
| 64 |
config_str = json.dumps(config_data, ensure_ascii=False)
|
| 65 |
|
|
|
|
| 66 |
index_path = Path(__file__).parent / "static" / "index.html"
|
|
|
|
| 67 |
if index_path.exists():
|
| 68 |
html = index_path.read_text(encoding="utf-8")
|
| 69 |
html = html.replace(
|
| 70 |
"__RUNTIME_CONFIG__ || {}",
|
| 71 |
config_str + " || {}"
|
| 72 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
|
|
|
|
|
|
| 77 |
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
for attr_name in ("app", "server_app", "root_path"):
|
| 83 |
-
underlying = getattr(application, attr_name, None)
|
| 84 |
if underlying is not None:
|
| 85 |
break
|
| 86 |
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
|
| 95 |
-
logger.info("Launching web interface on http://0.0.0.0:7860")
|
| 96 |
application.launch(
|
| 97 |
show_error=True,
|
| 98 |
server_name="0.0.0.0",
|
| 99 |
-
server_port=
|
| 100 |
share=False,
|
| 101 |
)
|
| 102 |
|
|
|
|
| 17 |
|
| 18 |
import json
|
| 19 |
import logging
|
| 20 |
+
import os
|
| 21 |
from pathlib import Path
|
| 22 |
|
| 23 |
# Ensure workspace exists
|
|
|
|
| 55 |
else:
|
| 56 |
logger.info(f"MCP server '{name}' connected")
|
| 57 |
|
| 58 |
+
# Create the Gradio app with custom HTML
|
| 59 |
from sonicoder.server import get_app, api_get_config
|
|
|
|
| 60 |
|
| 61 |
+
# Build config JSON for frontend injection
|
| 62 |
config_json = api_get_config()
|
| 63 |
config_data = json.loads(config_json)
|
| 64 |
config_str = json.dumps(config_data, ensure_ascii=False)
|
| 65 |
|
| 66 |
+
# Read index.html and inject config
|
| 67 |
index_path = Path(__file__).parent / "static" / "index.html"
|
| 68 |
+
custom_html = None
|
| 69 |
if index_path.exists():
|
| 70 |
html = index_path.read_text(encoding="utf-8")
|
| 71 |
html = html.replace(
|
| 72 |
"__RUNTIME_CONFIG__ || {}",
|
| 73 |
config_str + " || {}"
|
| 74 |
)
|
| 75 |
+
custom_html = html
|
| 76 |
+
logger.info("Custom HTML prepared")
|
| 77 |
+
|
| 78 |
+
application = get_app()
|
| 79 |
|
| 80 |
+
# Register custom root route after app is built
|
| 81 |
+
if custom_html:
|
| 82 |
+
try:
|
| 83 |
+
from fastapi import Response
|
| 84 |
+
from fastapi.responses import HTMLResponse
|
| 85 |
|
| 86 |
+
# Try multiple ways to access the underlying app in Gradio 6.x
|
| 87 |
+
underlying = None
|
| 88 |
+
for attr in ("app", "server_app", "_app", "app_obj"):
|
| 89 |
+
underlying = getattr(application, attr, None)
|
|
|
|
|
|
|
| 90 |
if underlying is not None:
|
| 91 |
break
|
| 92 |
|
| 93 |
+
if underlying is not None and hasattr(underlying, "get"):
|
| 94 |
+
@underlying.get("/", response_class=HTMLResponse, include_in_schema=False)
|
| 95 |
+
async def serve_index():
|
| 96 |
+
return Response(content=custom_html, media_type="text/html")
|
| 97 |
+
logger.info("Custom root route registered successfully")
|
| 98 |
+
else:
|
| 99 |
+
logger.warning("Could not register custom root route — using default Gradio UI")
|
| 100 |
+
except Exception as e:
|
| 101 |
+
logger.warning(f"Custom root route registration failed: {e}")
|
| 102 |
+
|
| 103 |
+
# Launch — HF Spaces sets PORT env var
|
| 104 |
+
port = int(os.environ.get("PORT", 7860))
|
| 105 |
+
logger.info(f"Launching web interface on port {port}")
|
| 106 |
|
|
|
|
| 107 |
application.launch(
|
| 108 |
show_error=True,
|
| 109 |
server_name="0.0.0.0",
|
| 110 |
+
server_port=port,
|
| 111 |
share=False,
|
| 112 |
)
|
| 113 |
|
sonicoder/model/loader.py
CHANGED
|
@@ -89,7 +89,7 @@ def load_model(model_key: str) -> None:
|
|
| 89 |
model_kwargs = {
|
| 90 |
"trust_remote_code": config_entry.trust_remote_code,
|
| 91 |
"low_cpu_mem_usage": True,
|
| 92 |
-
"
|
| 93 |
}
|
| 94 |
|
| 95 |
if device == "cuda":
|
|
|
|
| 89 |
model_kwargs = {
|
| 90 |
"trust_remote_code": config_entry.trust_remote_code,
|
| 91 |
"low_cpu_mem_usage": True,
|
| 92 |
+
"dtype": dtype,
|
| 93 |
}
|
| 94 |
|
| 95 |
if device == "cuda":
|