Spaces:
Sleeping
Sleeping
| #!/usr/bin/env python3 | |
| """Hugging Face Spaces - intelligent module loading""" | |
| import os | |
| import sys | |
| import importlib.util | |
| from pathlib import Path | |
| from types import ModuleType | |
| app_dir = Path("/app") | |
| sys.path.insert(0, str(app_dir)) | |
| print("Loading application...\n") | |
| # Create src package | |
| src_package = ModuleType("src") | |
| src_package.__path__ = [str(app_dir / "src")] | |
| sys.modules["src"] = src_package | |
| # Read all src files and fix their imports | |
| src_files_content = {} | |
| print("Reading src files:") | |
| for item in sorted(app_dir.iterdir()): | |
| name = item.name | |
| if name.startswith("src\\") and name.endswith(".py") and name != "src\\__init__.py": | |
| module_name = name.split("\\")[-1].replace(".py", "") | |
| try: | |
| with open(str(item), 'r', encoding='utf-8', errors='ignore') as f: | |
| content = f.read() | |
| # Fix imports: convert backslash paths to forward slash | |
| content = content.replace("from src\\", "from src.") | |
| content = content.replace("import src\\", "import src.") | |
| # Also handle cases where backslash is in string | |
| src_files_content[module_name] = content | |
| print(f" Read {module_name}") | |
| except Exception as e: | |
| print(f" {module_name}: {e}") | |
| # Execute modules in order (simple modules first) | |
| load_order = [ | |
| "models", | |
| "parser", | |
| "indexing", | |
| "intent", | |
| "retrieval", | |
| "reasoning", | |
| "dependencies", | |
| "groq_integration", | |
| "evaluation", | |
| "notebook_downloader", | |
| ] | |
| print("\nExecuting src modules:") | |
| executed_modules = {} | |
| for module_name in load_order: | |
| if module_name not in src_files_content: | |
| continue | |
| try: | |
| # Create module | |
| full_name = f"src.{module_name}" | |
| module = ModuleType(full_name) | |
| module.__file__ = str(app_dir / f"src\\{module_name}.py") | |
| module.__loader__ = None | |
| # Create namespace with all previously loaded modules | |
| namespace = { | |
| "__name__": full_name, | |
| "__file__": module.__file__, | |
| "__package__": "src", | |
| "src": src_package, | |
| } | |
| # Add all already-loaded src modules | |
| for loaded_name, loaded_module in executed_modules.items(): | |
| namespace[loaded_name] = loaded_module | |
| setattr(src_package, loaded_name, loaded_module) | |
| # Execute the code | |
| code = src_files_content[module_name] | |
| exec(compile(code, module.__file__, "exec"), namespace) | |
| # Update module with namespace | |
| for key, value in namespace.items(): | |
| if not key.startswith("_"): | |
| setattr(module, key, value) | |
| # Register | |
| sys.modules[full_name] = module | |
| sys.modules[module_name] = module | |
| setattr(src_package, module_name, module) | |
| executed_modules[module_name] = module | |
| print(f" Loaded {full_name}") | |
| except Exception as e: | |
| print(f" {module_name}: {str(e)[:60]}") | |
| import traceback | |
| traceback.print_exc() | |
| # Load remaining modules not in load_order | |
| print("\nLoading remaining modules:") | |
| for module_name in src_files_content: | |
| if module_name not in executed_modules: | |
| try: | |
| full_name = f"src.{module_name}" | |
| module = ModuleType(full_name) | |
| module.__file__ = str(app_dir / f"src\\{module_name}.py") | |
| namespace = { | |
| "__name__": full_name, | |
| "__file__": module.__file__, | |
| "__package__": "src", | |
| "src": src_package, | |
| } | |
| for loaded_name, loaded_module in executed_modules.items(): | |
| namespace[loaded_name] = loaded_module | |
| setattr(src_package, loaded_name, loaded_module) | |
| code = src_files_content[module_name] | |
| exec(compile(code, module.__file__, "exec"), namespace) | |
| for key, value in namespace.items(): | |
| if not key.startswith("_"): | |
| setattr(module, key, value) | |
| sys.modules[full_name] = module | |
| sys.modules[module_name] = module | |
| setattr(src_package, module_name, module) | |
| executed_modules[module_name] = module | |
| print(f" Loaded {full_name}") | |
| except Exception as e: | |
| print(f" {module_name}: {str(e)[:60]}") | |
| # Now load ui/app.py | |
| print("\nLoading ui/app.py:") | |
| ui_app_path = None | |
| for item in app_dir.iterdir(): | |
| if item.name == "ui\\app.py": | |
| ui_app_path = item | |
| break | |
| if ui_app_path and ui_app_path.exists(): | |
| try: | |
| with open(str(ui_app_path), 'r', encoding='utf-8', errors='ignore') as f: | |
| code = f.read() | |
| exec_globals = { | |
| "__name__": "__main__", | |
| "__file__": str(ui_app_path), | |
| "src": src_package, | |
| } | |
| # Add all src modules | |
| for module_name, module in executed_modules.items(): | |
| exec_globals[module_name] = module | |
| print(" Executing ui/app.py...") | |
| exec(compile(code, str(ui_app_path), "exec"), exec_globals) | |
| create_gradio_app = exec_globals.get("create_gradio_app") | |
| if create_gradio_app and callable(create_gradio_app): | |
| print(" Launching Gradio app...\n") | |
| demo = create_gradio_app() | |
| demo.launch( | |
| server_name="0.0.0.0", | |
| server_port=int(os.environ.get("PORT", 7860)), | |
| show_error=True | |
| ) | |
| else: | |
| print(" create_gradio_app not found") | |
| except Exception as e: | |
| print(f" Error: {e}\n") | |
| import traceback | |
| traceback.print_exc() | |
| else: | |
| print(" ui\\app.py not found") | |