3v324v23's picture
feat: switch Space app.py to launch NNGen UI
3b7db47
import os
import sys
from pathlib import Path
# This app.py switches the Space to run the NNGen Gradio UI
# located in NNGen/scripts/gradio_app.py (added to the repo).
# It keeps the Space configuration (sdk: gradio, app_file: app.py) intact.
HERE = Path(__file__).parent.resolve()
NNGEN_ROOT = HERE / "NNGen"
# Ensure NNGen is on sys.path so we can import its packages
if str(NNGEN_ROOT) not in sys.path:
sys.path.insert(0, str(NNGEN_ROOT))
try:
from scripts.gradio_app import app as nn_app
except Exception as e:
# Provide a clear error in the UI instead of crashing silently
import gradio as gr
def _err_ui():
with gr.Blocks(title="NNGen — Error") as demo:
gr.Markdown(
f"""
## Failed to import NNGen app
- Error: {type(e).__name__}: {e}
- Expected path: {NNGEN_ROOT}
- Ensure the NNGen folder exists in the Space and contains `scripts/gradio_app.py`.
"""
)
return demo
demo = _err_ui()
else:
demo = nn_app()
if __name__ == "__main__":
port = int(os.getenv("PORT", "7860"))
demo.launch(server_name="0.0.0.0", server_port=port)