mozzic commited on
Commit
8e8ad2d
·
verified ·
1 Parent(s): 21b1198

Upload app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +21 -22
app.py CHANGED
@@ -1,5 +1,5 @@
1
  #!/usr/bin/env python3
2
- """Hugging Face Spaces - create proper module namespace"""
3
  import os
4
  import sys
5
  import importlib.util
@@ -11,42 +11,43 @@ sys.path.insert(0, str(app_dir))
11
 
12
  print("Setting up module namespace...\n")
13
 
14
- # Create fake src package
15
  src_package = ModuleType("src")
16
  src_package.__path__ = [str(app_dir / "src")]
17
  sys.modules["src"] = src_package
18
 
19
- print("Loading src modules into src namespace:")
 
 
20
  for item in sorted(app_dir.iterdir()):
21
  name = item.name
22
  if name.startswith("src\\") and name.endswith(".py") and not name.endswith("__pycache__"):
23
- # Extract module name: "src\models.py" -> "models"
24
  module_name = name.split("\\")[-1].replace(".py", "")
25
-
26
  if module_name == "__init__":
27
  continue
28
 
 
29
  try:
30
- # Load as src.MODULE_NAME
31
- full_name = f"src.{module_name}"
32
  spec = importlib.util.spec_from_file_location(full_name, str(item))
33
  module = importlib.util.module_from_spec(spec)
34
  sys.modules[full_name] = module
35
-
36
- # Also make it accessible as just MODULE_NAME for direct imports
37
  sys.modules[module_name] = module
38
-
39
- # Execute the module
40
- spec.loader.exec_module(module)
41
- print(f" {full_name}")
42
-
43
- # Add to src package
44
  setattr(src_package, module_name, module)
45
-
 
46
  except Exception as e:
47
- print(f" {full_name}: {str(e)[:60]}")
 
 
 
 
 
 
 
 
 
48
 
49
- # Now load ui/app.py
50
  print("\nLoading ui/app.py:")
51
  ui_app_path = None
52
  for item in app_dir.iterdir():
@@ -58,7 +59,6 @@ if ui_app_path and ui_app_path.exists():
58
  with open(str(ui_app_path), 'r', encoding='utf-8', errors='ignore') as f:
59
  code = f.read()
60
 
61
- # Create execution environment with all modules
62
  exec_globals = {
63
  "__name__": "__main__",
64
  "__file__": str(ui_app_path),
@@ -74,10 +74,9 @@ if ui_app_path and ui_app_path.exists():
74
  print(" Executing ui/app.py...")
75
  exec(compile(code, str(ui_app_path), "exec"), exec_globals)
76
 
77
- # Launch the app
78
  create_gradio_app = exec_globals.get("create_gradio_app")
79
  if create_gradio_app and callable(create_gradio_app):
80
- print(" Launching Gradio app...")
81
  demo = create_gradio_app()
82
  demo.launch(
83
  server_name="0.0.0.0",
@@ -88,7 +87,7 @@ if ui_app_path and ui_app_path.exists():
88
  print(" create_gradio_app not found")
89
 
90
  except Exception as e:
91
- print(f" Error: {e}")
92
  import traceback
93
  traceback.print_exc()
94
  else:
 
1
  #!/usr/bin/env python3
2
+ """Hugging Face Spaces - load all modules first, then execute"""
3
  import os
4
  import sys
5
  import importlib.util
 
11
 
12
  print("Setting up module namespace...\n")
13
 
14
+ # Create src package
15
  src_package = ModuleType("src")
16
  src_package.__path__ = [str(app_dir / "src")]
17
  sys.modules["src"] = src_package
18
 
19
+ # Step 1: Register ALL modules in sys.modules first (without executing)
20
+ print("Registering src modules:")
21
+ modules_to_load = []
22
  for item in sorted(app_dir.iterdir()):
23
  name = item.name
24
  if name.startswith("src\\") and name.endswith(".py") and not name.endswith("__pycache__"):
 
25
  module_name = name.split("\\")[-1].replace(".py", "")
 
26
  if module_name == "__init__":
27
  continue
28
 
29
+ full_name = f"src.{module_name}"
30
  try:
 
 
31
  spec = importlib.util.spec_from_file_location(full_name, str(item))
32
  module = importlib.util.module_from_spec(spec)
33
  sys.modules[full_name] = module
 
 
34
  sys.modules[module_name] = module
 
 
 
 
 
 
35
  setattr(src_package, module_name, module)
36
+ modules_to_load.append((full_name, spec, module))
37
+ print(f" Registered {full_name}")
38
  except Exception as e:
39
+ print(f" {full_name}: {str(e)[:50]}")
40
+
41
+ # Step 2: Now execute all modules (imports will work now)
42
+ print("\nExecuting src modules:")
43
+ for full_name, spec, module in modules_to_load:
44
+ try:
45
+ spec.loader.exec_module(module)
46
+ print(f" Executed {full_name}")
47
+ except Exception as e:
48
+ print(f" {full_name}: {str(e)[:60]}")
49
 
50
+ # Step 3: Load ui/app.py
51
  print("\nLoading ui/app.py:")
52
  ui_app_path = None
53
  for item in app_dir.iterdir():
 
59
  with open(str(ui_app_path), 'r', encoding='utf-8', errors='ignore') as f:
60
  code = f.read()
61
 
 
62
  exec_globals = {
63
  "__name__": "__main__",
64
  "__file__": str(ui_app_path),
 
74
  print(" Executing ui/app.py...")
75
  exec(compile(code, str(ui_app_path), "exec"), exec_globals)
76
 
 
77
  create_gradio_app = exec_globals.get("create_gradio_app")
78
  if create_gradio_app and callable(create_gradio_app):
79
+ print(" Launching Gradio app...\n")
80
  demo = create_gradio_app()
81
  demo.launch(
82
  server_name="0.0.0.0",
 
87
  print(" create_gradio_app not found")
88
 
89
  except Exception as e:
90
+ print(f" Error: {e}\n")
91
  import traceback
92
  traceback.print_exc()
93
  else: