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 -
|
| 3 |
import os
|
| 4 |
import sys
|
| 5 |
import importlib.util
|
|
@@ -9,45 +9,131 @@ from types import ModuleType
|
|
| 9 |
app_dir = Path("/app")
|
| 10 |
sys.path.insert(0, str(app_dir))
|
| 11 |
|
| 12 |
-
print("
|
| 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 |
-
#
|
| 20 |
-
|
| 21 |
-
|
| 22 |
for item in sorted(app_dir.iterdir()):
|
| 23 |
name = item.name
|
| 24 |
-
if name.startswith("src\\") and name.endswith(".py") and
|
| 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 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
|
|
|
| 38 |
except Exception as e:
|
| 39 |
-
print(f" {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
-
# Step 2: Now execute all modules (imports will work now)
|
| 42 |
print("\nExecuting src modules:")
|
| 43 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
try:
|
| 45 |
-
|
| 46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
except Exception as e:
|
| 48 |
-
print(f" {
|
|
|
|
|
|
|
| 49 |
|
| 50 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
print("\nLoading ui/app.py:")
|
| 52 |
ui_app_path = None
|
| 53 |
for item in app_dir.iterdir():
|
|
@@ -56,21 +142,20 @@ for item in app_dir.iterdir():
|
|
| 56 |
break
|
| 57 |
|
| 58 |
if ui_app_path and ui_app_path.exists():
|
| 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),
|
| 65 |
-
"src": src_package,
|
| 66 |
-
}
|
| 67 |
-
|
| 68 |
-
# Add all src modules to globals
|
| 69 |
-
for key in list(sys.modules.keys()):
|
| 70 |
-
if key.startswith("src."):
|
| 71 |
-
exec_globals[key.split(".")[-1]] = sys.modules[key]
|
| 72 |
-
|
| 73 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
print(" Executing ui/app.py...")
|
| 75 |
exec(compile(code, str(ui_app_path), "exec"), exec_globals)
|
| 76 |
|
|
@@ -85,7 +170,6 @@ if ui_app_path and ui_app_path.exists():
|
|
| 85 |
)
|
| 86 |
else:
|
| 87 |
print(" create_gradio_app not found")
|
| 88 |
-
|
| 89 |
except Exception as e:
|
| 90 |
print(f" Error: {e}\n")
|
| 91 |
import traceback
|
|
|
|
| 1 |
#!/usr/bin/env python3
|
| 2 |
+
"""Hugging Face Spaces - intelligent module loading"""
|
| 3 |
import os
|
| 4 |
import sys
|
| 5 |
import importlib.util
|
|
|
|
| 9 |
app_dir = Path("/app")
|
| 10 |
sys.path.insert(0, str(app_dir))
|
| 11 |
|
| 12 |
+
print("Loading application...\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 |
+
# Read all src files and fix their imports
|
| 20 |
+
src_files_content = {}
|
| 21 |
+
print("Reading src files:")
|
| 22 |
for item in sorted(app_dir.iterdir()):
|
| 23 |
name = item.name
|
| 24 |
+
if name.startswith("src\\") and name.endswith(".py") and name != "src\\__init__.py":
|
| 25 |
module_name = name.split("\\")[-1].replace(".py", "")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
try:
|
| 27 |
+
with open(str(item), 'r', encoding='utf-8', errors='ignore') as f:
|
| 28 |
+
content = f.read()
|
| 29 |
+
# Fix imports: convert backslash paths to forward slash
|
| 30 |
+
content = content.replace("from src\\", "from src.")
|
| 31 |
+
content = content.replace("import src\\", "import src.")
|
| 32 |
+
# Also handle cases where backslash is in string
|
| 33 |
+
src_files_content[module_name] = content
|
| 34 |
+
print(f" Read {module_name}")
|
| 35 |
except Exception as e:
|
| 36 |
+
print(f" {module_name}: {e}")
|
| 37 |
+
|
| 38 |
+
# Execute modules in order (simple modules first)
|
| 39 |
+
load_order = [
|
| 40 |
+
"models",
|
| 41 |
+
"parser",
|
| 42 |
+
"indexing",
|
| 43 |
+
"intent",
|
| 44 |
+
"retrieval",
|
| 45 |
+
"reasoning",
|
| 46 |
+
"dependencies",
|
| 47 |
+
"groq_integration",
|
| 48 |
+
"evaluation",
|
| 49 |
+
"notebook_downloader",
|
| 50 |
+
]
|
| 51 |
|
|
|
|
| 52 |
print("\nExecuting src modules:")
|
| 53 |
+
executed_modules = {}
|
| 54 |
+
|
| 55 |
+
for module_name in load_order:
|
| 56 |
+
if module_name not in src_files_content:
|
| 57 |
+
continue
|
| 58 |
+
|
| 59 |
try:
|
| 60 |
+
# Create module
|
| 61 |
+
full_name = f"src.{module_name}"
|
| 62 |
+
module = ModuleType(full_name)
|
| 63 |
+
module.__file__ = str(app_dir / f"src\\{module_name}.py")
|
| 64 |
+
module.__loader__ = None
|
| 65 |
+
|
| 66 |
+
# Create namespace with all previously loaded modules
|
| 67 |
+
namespace = {
|
| 68 |
+
"__name__": full_name,
|
| 69 |
+
"__file__": module.__file__,
|
| 70 |
+
"__package__": "src",
|
| 71 |
+
"src": src_package,
|
| 72 |
+
}
|
| 73 |
+
|
| 74 |
+
# Add all already-loaded src modules
|
| 75 |
+
for loaded_name, loaded_module in executed_modules.items():
|
| 76 |
+
namespace[loaded_name] = loaded_module
|
| 77 |
+
setattr(src_package, loaded_name, loaded_module)
|
| 78 |
+
|
| 79 |
+
# Execute the code
|
| 80 |
+
code = src_files_content[module_name]
|
| 81 |
+
exec(compile(code, module.__file__, "exec"), namespace)
|
| 82 |
+
|
| 83 |
+
# Update module with namespace
|
| 84 |
+
for key, value in namespace.items():
|
| 85 |
+
if not key.startswith("_"):
|
| 86 |
+
setattr(module, key, value)
|
| 87 |
+
|
| 88 |
+
# Register
|
| 89 |
+
sys.modules[full_name] = module
|
| 90 |
+
sys.modules[module_name] = module
|
| 91 |
+
setattr(src_package, module_name, module)
|
| 92 |
+
executed_modules[module_name] = module
|
| 93 |
+
|
| 94 |
+
print(f" Loaded {full_name}")
|
| 95 |
+
|
| 96 |
except Exception as e:
|
| 97 |
+
print(f" {module_name}: {str(e)[:60]}")
|
| 98 |
+
import traceback
|
| 99 |
+
traceback.print_exc()
|
| 100 |
|
| 101 |
+
# Load remaining modules not in load_order
|
| 102 |
+
print("\nLoading remaining modules:")
|
| 103 |
+
for module_name in src_files_content:
|
| 104 |
+
if module_name not in executed_modules:
|
| 105 |
+
try:
|
| 106 |
+
full_name = f"src.{module_name}"
|
| 107 |
+
module = ModuleType(full_name)
|
| 108 |
+
module.__file__ = str(app_dir / f"src\\{module_name}.py")
|
| 109 |
+
|
| 110 |
+
namespace = {
|
| 111 |
+
"__name__": full_name,
|
| 112 |
+
"__file__": module.__file__,
|
| 113 |
+
"__package__": "src",
|
| 114 |
+
"src": src_package,
|
| 115 |
+
}
|
| 116 |
+
|
| 117 |
+
for loaded_name, loaded_module in executed_modules.items():
|
| 118 |
+
namespace[loaded_name] = loaded_module
|
| 119 |
+
setattr(src_package, loaded_name, loaded_module)
|
| 120 |
+
|
| 121 |
+
code = src_files_content[module_name]
|
| 122 |
+
exec(compile(code, module.__file__, "exec"), namespace)
|
| 123 |
+
|
| 124 |
+
for key, value in namespace.items():
|
| 125 |
+
if not key.startswith("_"):
|
| 126 |
+
setattr(module, key, value)
|
| 127 |
+
|
| 128 |
+
sys.modules[full_name] = module
|
| 129 |
+
sys.modules[module_name] = module
|
| 130 |
+
setattr(src_package, module_name, module)
|
| 131 |
+
executed_modules[module_name] = module
|
| 132 |
+
print(f" Loaded {full_name}")
|
| 133 |
+
except Exception as e:
|
| 134 |
+
print(f" {module_name}: {str(e)[:60]}")
|
| 135 |
+
|
| 136 |
+
# Now load ui/app.py
|
| 137 |
print("\nLoading ui/app.py:")
|
| 138 |
ui_app_path = None
|
| 139 |
for item in app_dir.iterdir():
|
|
|
|
| 142 |
break
|
| 143 |
|
| 144 |
if ui_app_path and ui_app_path.exists():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 145 |
try:
|
| 146 |
+
with open(str(ui_app_path), 'r', encoding='utf-8', errors='ignore') as f:
|
| 147 |
+
code = f.read()
|
| 148 |
+
|
| 149 |
+
exec_globals = {
|
| 150 |
+
"__name__": "__main__",
|
| 151 |
+
"__file__": str(ui_app_path),
|
| 152 |
+
"src": src_package,
|
| 153 |
+
}
|
| 154 |
+
|
| 155 |
+
# Add all src modules
|
| 156 |
+
for module_name, module in executed_modules.items():
|
| 157 |
+
exec_globals[module_name] = module
|
| 158 |
+
|
| 159 |
print(" Executing ui/app.py...")
|
| 160 |
exec(compile(code, str(ui_app_path), "exec"), exec_globals)
|
| 161 |
|
|
|
|
| 170 |
)
|
| 171 |
else:
|
| 172 |
print(" create_gradio_app not found")
|
|
|
|
| 173 |
except Exception as e:
|
| 174 |
print(f" Error: {e}\n")
|
| 175 |
import traceback
|