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

Upload app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +57 -42
app.py CHANGED
@@ -1,5 +1,5 @@
1
  #!/usr/bin/env python3
2
- """Hugging Face Spaces entry point - handles backslash filenames"""
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
- # Find ui\app.py (with backslash in the filename on Linux)
11
- ui_app_files = list(app_dir.glob("**/app.py"))
12
- ui_app_path = None
13
 
14
- for f in ui_app_files:
15
- # Look for ui/app.py or ui\app.py
16
- if "ui" in str(f) and f.name == "app.py":
17
- # Skip /app/app.py (the entry point itself)
18
- if f.parent.name == "ui" or "ui" in f.parent.name:
19
- ui_app_path = f
20
- break
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
 
22
- if not ui_app_path:
23
- # Last resort - manually check common locations
24
- possible_paths = [
25
- app_dir / "ui" / "app.py",
26
- app_dir / "ui\\app.py", # With backslash
27
- ]
28
- for p in possible_paths:
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
- # Add app_dir to sys.path so imports work
37
- sys.path.insert(0, str(app_dir))
38
-
39
- # Load the module
40
- spec = importlib.util.spec_from_file_location("ui_app", str(ui_app_path))
41
- ui_app_module = importlib.util.module_from_spec(spec)
42
- sys.modules["ui_app"] = ui_app_module
43
-
44
- # Execute the module
45
- spec.loader.exec_module(ui_app_module)
46
-
47
- if __name__ == "__main__":
48
- demo = ui_app_module.create_gradio_app()
49
- demo.launch(
50
- server_name="0.0.0.0",
51
- server_port=int(os.environ.get("PORT", 7860)),
52
- show_error=True
53
- )
 
 
54
  else:
55
  print(f"ERROR: Could not find ui/app.py")
56
- print(f"App directory: {app_dir}")
57
- print(f"Files found:")
58
- for f in sorted(app_dir.glob("**/*.py")):
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}")