mozzic commited on
Commit
b22bb2a
·
verified ·
1 Parent(s): 55fa0a8

Upload app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +41 -48
app.py CHANGED
@@ -1,5 +1,5 @@
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,68 +7,61 @@ from pathlib import Path
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}")
 
 
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}")