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 - handle
|
| 3 |
import os
|
| 4 |
import sys
|
| 5 |
import importlib.util
|
|
@@ -7,68 +7,61 @@ from pathlib import Path
|
|
| 7 |
|
| 8 |
app_dir = Path("/app")
|
| 9 |
|
| 10 |
-
# Add app_dir to sys.path
|
| 11 |
sys.path.insert(0, str(app_dir))
|
| 12 |
|
| 13 |
-
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
| 15 |
for item in app_dir.iterdir():
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
|
|
|
| 19 |
|
| 20 |
-
if
|
| 21 |
-
print(f"Found
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
-
# Preload
|
| 25 |
-
for
|
| 26 |
-
|
| 27 |
-
|
|
|
|
| 28 |
try:
|
| 29 |
-
spec = importlib.util.spec_from_file_location(module_name, str(
|
| 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 |
-
|
| 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
|
| 68 |
import traceback
|
| 69 |
traceback.print_exc()
|
| 70 |
else:
|
| 71 |
-
print(f"ERROR: Could not find ui
|
| 72 |
-
print(f"
|
| 73 |
-
for item in
|
| 74 |
-
|
|
|
|
|
|
| 1 |
#!/usr/bin/env python3
|
| 2 |
+
"""Hugging Face Spaces entry point - handle backslashes as literal filenames"""
|
| 3 |
import os
|
| 4 |
import sys
|
| 5 |
import importlib.util
|
|
|
|
| 7 |
|
| 8 |
app_dir = Path("/app")
|
| 9 |
|
| 10 |
+
# Add app_dir to sys.path
|
| 11 |
sys.path.insert(0, str(app_dir))
|
| 12 |
|
| 13 |
+
print(f"App dir: {app_dir}")
|
| 14 |
+
print(f"Contents:")
|
| 15 |
+
|
| 16 |
+
# List all items - backslashes are literal in filenames!
|
| 17 |
+
ui_app_path = None
|
| 18 |
for item in app_dir.iterdir():
|
| 19 |
+
print(f" {item.name}")
|
| 20 |
+
# Look for the literal filename "ui\app.py"
|
| 21 |
+
if item.name == "ui\\app.py":
|
| 22 |
+
ui_app_path = item
|
| 23 |
|
| 24 |
+
if ui_app_path and ui_app_path.exists():
|
| 25 |
+
print(f"\n Found ui\\app.py at: {ui_app_path}")
|
| 26 |
+
|
| 27 |
+
# Read the file content and fix imports
|
| 28 |
+
with open(str(ui_app_path), 'r') as f:
|
| 29 |
+
code = f.read()
|
| 30 |
+
|
| 31 |
+
# Replace problematic imports with direct module loading
|
| 32 |
+
code = code.replace(
|
| 33 |
+
"from src.models import",
|
| 34 |
+
"from models import"
|
| 35 |
+
).replace(
|
| 36 |
+
"from src.",
|
| 37 |
+
"from "
|
| 38 |
+
)
|
| 39 |
|
| 40 |
+
# Preload src modules into sys.modules with correct names
|
| 41 |
+
src_files = [f for f in app_dir.iterdir() if str(f).startswith(str(app_dir) + "/src")]
|
| 42 |
+
for src_file in src_files:
|
| 43 |
+
if src_file.name.endswith(".py") and not src_file.name.startswith("_"):
|
| 44 |
+
module_name = src_file.stem
|
| 45 |
try:
|
| 46 |
+
spec = importlib.util.spec_from_file_location(module_name, str(src_file))
|
| 47 |
module = importlib.util.module_from_spec(spec)
|
| 48 |
sys.modules[module_name] = module
|
| 49 |
spec.loader.exec_module(module)
|
| 50 |
print(f" Loaded {module_name}")
|
| 51 |
except Exception as e:
|
| 52 |
print(f" Warning loading {module_name}: {e}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
|
| 54 |
+
# Execute the modified code
|
| 55 |
+
exec_globals = {"__name__": "__main__"}
|
| 56 |
try:
|
| 57 |
+
exec(compile(code, str(ui_app_path), "exec"), exec_globals)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
except Exception as e:
|
| 59 |
+
print(f"Error executing ui/app.py: {e}")
|
| 60 |
import traceback
|
| 61 |
traceback.print_exc()
|
| 62 |
else:
|
| 63 |
+
print(f"\n ERROR: Could not find ui\\app.py")
|
| 64 |
+
print(f"Looking for files with backslash in name...")
|
| 65 |
+
for item in app_dir.iterdir():
|
| 66 |
+
if "\\" in item.name:
|
| 67 |
+
print(f" Found: {item.name}")
|