Harshith Reddy commited on
Commit
963eb0b
Β·
1 Parent(s): 1204e53

CRITICAL FIX: Create app directly at module level - no functions, no conditionals, no delays

Browse files
Files changed (1) hide show
  1. app.py +8 -76
app.py CHANGED
@@ -966,86 +966,18 @@ def create_interface():
966
 
967
  return demo
968
 
969
- is_spaces = bool(os.getenv("SPACE_ID") or os.getenv("SYSTEM") == "spaces" or os.getenv("HF_SPACE"))
 
970
 
971
- root = FastAPI(title="SRMA-Mamba Unified App")
972
  root.mount("/api", api_app)
973
 
 
 
974
  @root.get("/healthz")
975
- def _root_health():
976
  return {"status": "ok"}
977
 
978
- app = root
979
-
980
- print("=" * 60)
981
- print("Initializing SRMA-Mamba Liver Segmentation Space...")
982
- print("=" * 60)
983
-
984
- try:
985
- demo = create_interface()
986
- demo.queue()
987
- print("βœ“ Gradio interface created and queued")
988
-
989
- if not HAS_MOUNT_GRADIO_APP:
990
- print("βœ— CRITICAL ERROR: mount_gradio_app not available!")
991
- print("βœ— Gradio version is too old. Need >= 4.44.1")
992
- print("βœ— Please check requirements.txt has gradio==4.44.1")
993
- raise ImportError("mount_gradio_app not available. Upgrade Gradio to >= 4.44.1")
994
-
995
- app = mount_gradio_app(root, demo, path="/")
996
- print("βœ“ Gradio mounted on FastAPI app via mount_gradio_app()")
997
- print(f"βœ“ App type after mount: {type(app)}")
998
- print(f"βœ“ App is root: {app is root}")
999
- print(f"βœ“ App is callable: {callable(app)}")
1000
-
1001
- print("β„Ή Models will be loaded on first inference (lazy loading)")
1002
- print("=" * 60)
1003
- print("βœ“ Initialization complete")
1004
-
1005
- except Exception as e:
1006
- print(f"βœ— Failed to initialize: {e}")
1007
- import traceback
1008
- traceback.print_exc()
1009
-
1010
- try:
1011
- with gr.Blocks(title="SRMA-Mamba: Error", theme=gr.themes.Soft()) as error_demo:
1012
- gr.Markdown(f"""
1013
- # ❌ Error Initializing Interface
1014
- The application failed to start. Please check the logs for details.
1015
-
1016
- **Error:** {str(e)}
1017
-
1018
- **Common causes:**
1019
- - Missing dependencies
1020
- - Model files not found
1021
- - Import errors
1022
-
1023
- Please check the Space logs for more information.
1024
- """)
1025
- error_demo.queue()
1026
- root = FastAPI(title="SRMA-Mamba Error App")
1027
- root.mount("/api", api_app)
1028
- app = mount_gradio_app(root, error_demo, path="/")
1029
- demo = error_demo
1030
- print("βœ“ Error interface created as fallback")
1031
- except Exception as e2:
1032
- print(f"βœ— Even error interface failed: {e2}")
1033
- import traceback
1034
- traceback.print_exc()
1035
- with gr.Blocks() as final_fallback:
1036
- gr.Markdown("# Critical Error - Check logs")
1037
- app = final_fallback
1038
- demo = final_fallback
1039
-
1040
- if app is None:
1041
- print("βœ— CRITICAL: app is None after initialization!")
1042
- raise RuntimeError("App was not set during initialization")
1043
-
1044
- print(f"βœ“ Final app variable set: {app is not None}")
1045
- print(f"βœ“ App type at module level: {type(app)}")
1046
- print(f"βœ“ App has __call__: {hasattr(app, '__call__')}")
1047
-
1048
  if __name__ == "__main__":
1049
- if not is_spaces:
1050
- import uvicorn
1051
- uvicorn.run(app, host="0.0.0.0", port=7860)
 
966
 
967
  return demo
968
 
969
+ demo = create_interface()
970
+ demo.queue()
971
 
972
+ root = FastAPI(title="SRMA-Mamba")
973
  root.mount("/api", api_app)
974
 
975
+ app = mount_gradio_app(root, demo, path="/")
976
+
977
  @root.get("/healthz")
978
+ def healthz():
979
  return {"status": "ok"}
980
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
981
  if __name__ == "__main__":
982
+ import uvicorn
983
+ uvicorn.run(app, host="0.0.0.0", port=7860)