Spaces:
Sleeping
Sleeping
# app.py | |
import sys | |
import os | |
import importlib.util # Import the utility | |
import traceback # Import traceback for detailed error printing | |
print("--- DETAILED DEBUG INFO ---") | |
print(f"Python Version: {sys.version}") | |
print(f"Current Working Directory: {os.getcwd()}") | |
print("sys.path:") | |
for p in sys.path: | |
print(f" - {p}") | |
print("\n--- Checking Directory Structure ---") | |
root_contents = [] | |
try: | |
root_contents = os.listdir('.') | |
except Exception as e: | |
print(f"[ERROR] Could not list root directory contents: {e}") | |
print(f"Root Directory Contents: {root_contents}") | |
src_path = os.path.join(os.getcwd(), 'src') | |
if os.path.exists(src_path) and os.path.isdir(src_path): | |
print(f"\n[OK] src Directory ('{src_path}') Exists.") | |
try: | |
src_contents = os.listdir(src_path) | |
print(f" src Contents: {src_contents}") | |
chimera_path = os.path.join(src_path, 'chimera') | |
if os.path.exists(chimera_path) and os.path.isdir(chimera_path): | |
print(f" [OK] chimera Directory ('{chimera_path}') Exists.") | |
try: | |
chimera_contents = os.listdir(chimera_path) | |
print(f" chimera Contents: {chimera_contents}") | |
# Explicitly check for __init__.py in chimera | |
chimera_init_path = os.path.join(chimera_path, '__init__.py') | |
if os.path.exists(chimera_init_path): | |
print(" [OK] src/chimera/__init__.py Exists.") | |
else: | |
print(" [ERROR] src/chimera/__init__.py MISSING!") | |
core_path = os.path.join(chimera_path, 'core') | |
if os.path.exists(core_path) and os.path.isdir(core_path): | |
print(f" [OK] core Directory ('{core_path}') Exists.") | |
try: | |
core_contents = os.listdir(core_path) | |
print(f" core Contents: {core_contents}") | |
# Explicitly check for __init__.py in core | |
core_init_path = os.path.join(core_path, '__init__.py') | |
if os.path.exists(core_init_path): | |
print(" [OK] src/chimera/core/__init__.py Exists.") | |
else: | |
print(" [ERROR] src/chimera/core/__init__.py MISSING!") | |
orchestrator_path = os.path.join(core_path, 'orchestrator.py') | |
if os.path.exists(orchestrator_path): | |
print(f" [OK] orchestrator.py ('{orchestrator_path}') Exists.") | |
else: | |
print(f" [ERROR] orchestrator.py ('{orchestrator_path}') MISSING!") | |
except Exception as e: | |
print(f" [ERROR] Could not list core directory contents: {e}") | |
else: | |
print(f" [ERROR] core Directory ('{core_path}') MISSING or is not a directory.") | |
except Exception as e: | |
print(f" [ERROR] Could not list chimera directory contents: {e}") | |
else: | |
print(f" [ERROR] chimera Directory ('{chimera_path}') MISSING or is not a directory.") | |
except Exception as e: | |
print(f"\n[ERROR] Could not list src directory contents: {e}") | |
else: | |
print(f"\n[ERROR] src Directory ('{src_path}') MISSING or is not a directory.") | |
# --- Test importability using importlib --- | |
module_name_to_test = "src.chimera.core.orchestrator" | |
print(f"\n--- Testing importability of '{module_name_to_test}' using importlib ---") | |
spec = None | |
try: | |
# Clear any previous import attempts from sys.modules that might interfere | |
if module_name_to_test in sys.modules: | |
del sys.modules[module_name_to_test] | |
# Also potentially clear parent modules if issues persist | |
# if "src.chimera.core" in sys.modules: del sys.modules["src.chimera.core"] | |
# if "src.chimera" in sys.modules: del sys.modules["src.chimera"] | |
# if "src" in sys.modules: del sys.modules["src"] | |
spec = importlib.util.find_spec(module_name_to_test) | |
except Exception as e: | |
print(f"[ERROR] Exception during importlib.util.find_spec: {type(e).__name__}: {e}") | |
traceback.print_exc() | |
if spec is None: | |
print(f"[FAIL] importlib.util.find_spec could NOT find module '{module_name_to_test}'.") | |
print(" This means Python's import machinery cannot locate this module.") | |
print(" Double-check directory names, __init__.py files, and potential typos.") | |
else: | |
print(f"[OK] importlib.util.find_spec found module '{module_name_to_test}'.") | |
print(f" Module Location Hint: {spec.origin}") | |
# If found, try to actually import it to see if loading fails | |
print(f"\n--- Attempting direct import of '{module_name_to_test}' using importlib.import_module ---") | |
try: | |
imported_module = importlib.import_module(module_name_to_test) | |
print(f"[OK] Successfully imported '{module_name_to_test}'. Module type: {type(imported_module)}") | |
# Try accessing the function | |
if hasattr(imported_module, 'run_analysis'): | |
print("[OK] Function 'run_analysis' found in the imported module.") | |
else: | |
print("[ERROR] Function 'run_analysis' NOT FOUND in the imported module!") | |
except Exception as e: | |
print(f"[FAIL] Error occurred DURING import of '{module_name_to_test}':") | |
traceback.print_exc() # Print the full traceback for the import error | |
print("\n--- Attempting original failing import ---") | |
try: | |
# Ensure sys.modules cache is clean before this attempt too | |
if module_name_to_test in sys.modules: del sys.modules[module_name_to_test] | |
from src.chimera.core.orchestrator import run_analysis | |
print("[OK] Original 'from ... import ...' statement SUCCEEDED.") | |
# If this succeeds now, the issue might have been temporary or fixed by restart | |
except ModuleNotFoundError: | |
print("[FAIL] Original 'from ... import ...' statement FAILED with ModuleNotFoundError.") | |
print(" Refer to the importlib test above for clues.") | |
traceback.print_exc() # Also print traceback for ModuleNotFoundError | |
except Exception as e: | |
print(f"[FAIL] Original 'from ... import ...' statement FAILED with a different error:") | |
traceback.print_exc() | |
print("\n--- End of Debug Script ---") | |
# Prevent Gradio from starting since imports might have failed | |
# =============================================================== | |
# == COMMENT OUT OR REMOVE GRADIO CODE BELOW FOR THIS TEST RUN == | |
# =============================================================== | |
# import gradio as gr | |
# from src.chimera.utils.logging_config import setup_logging, logger # This import would also fail if orchestrator fails | |
# from src.chimera.config import GEMINI_API_KEY | |
# # Setup logging as soon as the app starts | |
# # setup_logging() # Comment out if logging_config import fails | |
# # Check essential configurations on startup | |
# # if not GEMINI_API_KEY: | |
# # # logger.error("CRITICAL: GEMINI_API_KEY is not set. The application might not function correctly.") | |
# # print("CRITICAL: GEMINI_API_KEY is not set.") # Use print during early debug | |
# async def chimera_interface(query: str): | |
# # Placeholder implementation for debugging | |
# print(f"Gradio interface received query (debug): {query}") | |
# if not query: | |
# return "Please enter a query." | |
# # logger.info(f"Gradio interface received query: {query}") | |
# try: | |
# # Need to import run_analysis successfully first | |
# from src.chimera.core.orchestrator import run_analysis | |
# result = await run_analysis(query) | |
# # logger.info("Gradio interface processing complete.") | |
# print("Gradio interface processing complete (debug).") | |
# return result | |
# except NameError: | |
# print("Error: run_analysis could not be imported for Gradio.") | |
# return "Error: Backend function 'run_analysis' is not available due to import issues." | |
# except Exception as e: | |
# # logger.exception("Error in Gradio interface call to run_analysis.") | |
# print(f"Error in Gradio interface call (debug): {e}") | |
# traceback.print_exc() | |
# return f"An unexpected error occurred in the backend: {e}" | |
# # --- Gradio UI Definition --- | |
# with gr.Blocks(theme=gr.themes.Soft(), title="Project Chimera (Debug Mode)") as demo: | |
# gr.Markdown( | |
# """ | |
# # Project Chimera : Real-Time Global Analysis Engine (DEBUG MODE) | |
# Enter your query to analyze real-time data from SERP and other sources using Gemini. | |
# (Example: "Analyze recent news about renewable energy investments in the US") | |
# """ | |
# ) | |
# # ... rest of your Gradio UI definition ... | |
# # --- Launching the App --- | |
# if __name__ == "__main__": | |
# # logger.info("Starting Gradio application...") | |
# print("Starting Gradio application (debug)...") | |
# # demo.launch() # Don't launch Gradio if imports fail |