Spaces:
Sleeping
Sleeping
Upload app.py with huggingface_hub
Browse files123451 unmodified line7891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374#!/usr/bin/env python3"""Hugging Face Spaces entry point - handle backslash paths in src imports"""import osimport sysimport importlib.util1 unmodified lineapp_dir = Path("/app")# Add app_dir to sys.path FIRSTsys.path.insert(0, str(app_dir))# Find and preload critical src modulessrc_dir = Nonefor item in app_dir.iterdir():if item.is_dir() and "src" in item.name:src_dir = itembreakif src_dir:print(f"Found src directory: {src_dir}")sys.path.insert(0, str(src_dir))# Preload critical modulesfor py_file in src_dir.glob("*.py"):if py_file.name != "__init__.py" and not py_file.name.startswith("_"):module_name = py_file.stemtry:spec = importlib.util.spec_from_file_location(module_name, str(py_file))module = importlib.util.module_from_spec(spec)sys.modules[module_name] = modulespec.loader.exec_module(module)print(f" Loaded {module_name}")except Exception as e:print(f" Warning loading {module_name}: {e}")# Find ui/app.pyui_app_path = Nonefor item in app_dir.iterdir():if item.is_dir() and "ui" in item.name:ui_app = item / "app.py"if ui_app.exists():ui_app_path = ui_appbreakif ui_app_path and ui_app_path.exists():print(f"Found ui/app.py at: {ui_app_path}")print(f"sys.path: {sys.path[:3]}")try:# Load the modulespec = importlib.util.spec_from_file_location("ui_app", str(ui_app_path))ui_app_module = importlib.util.module_from_spec(spec)sys.modules["ui_app"] = ui_app_module# Execute the module - now imports should workspec.loader.exec_module(ui_app_module)if __name__ == "__main__":demo = ui_app_module.create_gradio_app()demo.launch(server_name="0.0.0.0",server_port=int(os.environ.get("PORT", 7860)),show_error=True)except Exception as e:print(f"Error loading ui/app.py: {e}")import tracebacktraceback.print_exc()else:print(f"ERROR: Could not find ui/app.py")print(f"Contents of {app_dir}:")for item in sorted(app_dir.iterdir()):print(f" {item}")123451 unmodified line78910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667#!/usr/bin/env python3"""Hugging Face Spaces entry point - handle backslashes as literal filenames"""import osimport sysimport importlib.util1 unmodified lineapp_dir = Path("/app")# Add app_dir to sys.pathsys.path.insert(0, str(app_dir))print(f"App dir: {app_dir}")print(f"Contents:")# List all items - backslashes are literal in filenames!ui_app_path = Nonefor item in app_dir.iterdir():print(f" {item.name}")# Look for the literal filename "ui\app.py"if item.name == "ui\\app.py":ui_app_path = itemif ui_app_path and ui_app_path.exists():print(f"\n Found ui\\app.py at: {ui_app_path}")# Read the file content and fix importswith open(str(ui_app_path), 'r') as f:code = f.read()# Replace problematic imports with direct module loadingcode = code.replace("from src.models import","from models import").replace("from src.","from ")# Preload src modules into sys.modules with correct namessrc_files = [f for f in app_dir.iterdir() if str(f).startswith(str(app_dir) + "/src")]for src_file in src_files:if src_file.name.endswith(".py") and not src_file.name.startswith("_"):module_name = src_file.stemtry:spec = importlib.util.spec_from_file_location(module_name, str(src_file))module = importlib.util.module_from_spec(spec)sys.modules[module_name] = modulespec.loader.exec_module(module)print(f" Loaded {module_name}")except Exception as e:print(f" Warning loading {module_name}: {e}")# Execute the modified codeexec_globals = {"__name__": "__main__"}try:exec(compile(code, str(ui_app_path), "exec"), exec_globals)except Exception as e:print(f"Error executing ui/app.py: {e}")import tracebacktraceback.print_exc()else:print(f"\n ERROR: Could not find ui\\app.py")print(f"Looking for files with backslash in name...")for item in app_dir.iterdir():if "\\" in item.name:print(f" Found: {item.name}")
app.py+41-48