Spaces:
Sleeping
Sleeping
Upload app.py with huggingface_hub
Browse files
app.py
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
#!/usr/bin/env python3
|
| 2 |
-
"""Hugging Face Spaces entry point -
|
| 3 |
import os
|
| 4 |
import sys
|
| 5 |
import importlib.util
|
|
@@ -7,53 +7,68 @@ from pathlib import Path
|
|
| 7 |
|
| 8 |
app_dir = Path("/app")
|
| 9 |
|
| 10 |
-
#
|
| 11 |
-
|
| 12 |
-
ui_app_path = None
|
| 13 |
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
if p.exists():
|
| 30 |
-
ui_app_path = p
|
| 31 |
break
|
| 32 |
|
| 33 |
if ui_app_path and ui_app_path.exists():
|
| 34 |
print(f"Found ui/app.py at: {ui_app_path}")
|
|
|
|
| 35 |
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
)
|
|
|
|
|
|
|
| 54 |
else:
|
| 55 |
print(f"ERROR: Could not find ui/app.py")
|
| 56 |
-
print(f"
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
print(f" {f}")
|
|
|
|
| 1 |
#!/usr/bin/env python3
|
| 2 |
+
"""Hugging Face Spaces entry point - handle backslash paths in src imports"""
|
| 3 |
import os
|
| 4 |
import sys
|
| 5 |
import importlib.util
|
|
|
|
| 7 |
|
| 8 |
app_dir = Path("/app")
|
| 9 |
|
| 10 |
+
# Add app_dir to sys.path FIRST
|
| 11 |
+
sys.path.insert(0, str(app_dir))
|
|
|
|
| 12 |
|
| 13 |
+
# Find and preload critical src modules
|
| 14 |
+
src_dir = None
|
| 15 |
+
for item in app_dir.iterdir():
|
| 16 |
+
if item.is_dir() and "src" in item.name:
|
| 17 |
+
src_dir = item
|
| 18 |
+
break
|
| 19 |
+
|
| 20 |
+
if src_dir:
|
| 21 |
+
print(f"Found src directory: {src_dir}")
|
| 22 |
+
sys.path.insert(0, str(src_dir))
|
| 23 |
+
|
| 24 |
+
# Preload critical modules
|
| 25 |
+
for py_file in src_dir.glob("*.py"):
|
| 26 |
+
if py_file.name != "__init__.py" and not py_file.name.startswith("_"):
|
| 27 |
+
module_name = py_file.stem
|
| 28 |
+
try:
|
| 29 |
+
spec = importlib.util.spec_from_file_location(module_name, str(py_file))
|
| 30 |
+
module = importlib.util.module_from_spec(spec)
|
| 31 |
+
sys.modules[module_name] = module
|
| 32 |
+
spec.loader.exec_module(module)
|
| 33 |
+
print(f" Loaded {module_name}")
|
| 34 |
+
except Exception as e:
|
| 35 |
+
print(f" Warning loading {module_name}: {e}")
|
| 36 |
|
| 37 |
+
# Find ui/app.py
|
| 38 |
+
ui_app_path = None
|
| 39 |
+
for item in app_dir.iterdir():
|
| 40 |
+
if item.is_dir() and "ui" in item.name:
|
| 41 |
+
ui_app = item / "app.py"
|
| 42 |
+
if ui_app.exists():
|
| 43 |
+
ui_app_path = ui_app
|
|
|
|
|
|
|
| 44 |
break
|
| 45 |
|
| 46 |
if ui_app_path and ui_app_path.exists():
|
| 47 |
print(f"Found ui/app.py at: {ui_app_path}")
|
| 48 |
+
print(f"sys.path: {sys.path[:3]}")
|
| 49 |
|
| 50 |
+
try:
|
| 51 |
+
# Load the module
|
| 52 |
+
spec = importlib.util.spec_from_file_location("ui_app", str(ui_app_path))
|
| 53 |
+
ui_app_module = importlib.util.module_from_spec(spec)
|
| 54 |
+
sys.modules["ui_app"] = ui_app_module
|
| 55 |
+
|
| 56 |
+
# Execute the module - now imports should work
|
| 57 |
+
spec.loader.exec_module(ui_app_module)
|
| 58 |
+
|
| 59 |
+
if __name__ == "__main__":
|
| 60 |
+
demo = ui_app_module.create_gradio_app()
|
| 61 |
+
demo.launch(
|
| 62 |
+
server_name="0.0.0.0",
|
| 63 |
+
server_port=int(os.environ.get("PORT", 7860)),
|
| 64 |
+
show_error=True
|
| 65 |
+
)
|
| 66 |
+
except Exception as e:
|
| 67 |
+
print(f"Error loading ui/app.py: {e}")
|
| 68 |
+
import traceback
|
| 69 |
+
traceback.print_exc()
|
| 70 |
else:
|
| 71 |
print(f"ERROR: Could not find ui/app.py")
|
| 72 |
+
print(f"Contents of {app_dir}:")
|
| 73 |
+
for item in sorted(app_dir.iterdir()):
|
| 74 |
+
print(f" {item}")
|
|
|