lvwerra HF Staff Claude Opus 4.5 commited on
Commit
0801a15
·
1 Parent(s): 17dc243

Add import fallbacks for running server directly

Browse files

Relative imports fail when running `python backend/main.py` directly
instead of `python -m backend.main`. Added try/except fallbacks for:
- code, research, command module imports
- defaultnames import
- jupyter_handler import in utils.py

TODO: Consider a cleaner solution like adding backend/ to sys.path
at startup, or always requiring module-style execution.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>

Files changed (2) hide show
  1. backend/main.py +16 -4
  2. backend/utils.py +4 -1
backend/main.py CHANGED
@@ -38,7 +38,10 @@ PROJECT_ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
38
  # For code execution
39
  try:
40
  from e2b_code_interpreter import Sandbox
41
- from .code import stream_code_execution
 
 
 
42
  from openai import OpenAI
43
  E2B_AVAILABLE = True
44
  except ImportError:
@@ -47,7 +50,10 @@ except ImportError:
47
 
48
  # For research
49
  try:
50
- from .research import stream_research
 
 
 
51
  RESEARCH_AVAILABLE = True
52
  except ImportError as e:
53
  RESEARCH_AVAILABLE = False
@@ -55,7 +61,10 @@ except ImportError as e:
55
 
56
  # For command center with tool-based launching
57
  try:
58
- from .command import stream_command_center
 
 
 
59
  COMMAND_AVAILABLE = True
60
  except ImportError:
61
  COMMAND_AVAILABLE = False
@@ -1042,7 +1051,10 @@ async def api_random_session_name():
1042
  This gives equal weight to all elements regardless of isotope count.
1043
  """
1044
  import random
1045
- from .defaultnames import ISOTOPES
 
 
 
1046
  # Two-stage sampling: first pick element, then pick isotope
1047
  element = random.choice(list(ISOTOPES.keys()))
1048
  mass_number = random.choice(ISOTOPES[element])
 
38
  # For code execution
39
  try:
40
  from e2b_code_interpreter import Sandbox
41
+ try:
42
+ from .code import stream_code_execution
43
+ except ImportError:
44
+ from code import stream_code_execution
45
  from openai import OpenAI
46
  E2B_AVAILABLE = True
47
  except ImportError:
 
50
 
51
  # For research
52
  try:
53
+ try:
54
+ from .research import stream_research
55
+ except ImportError:
56
+ from research import stream_research
57
  RESEARCH_AVAILABLE = True
58
  except ImportError as e:
59
  RESEARCH_AVAILABLE = False
 
61
 
62
  # For command center with tool-based launching
63
  try:
64
+ try:
65
+ from .command import stream_command_center
66
+ except ImportError:
67
+ from command import stream_command_center
68
  COMMAND_AVAILABLE = True
69
  except ImportError:
70
  COMMAND_AVAILABLE = False
 
1051
  This gives equal weight to all elements regardless of isotope count.
1052
  """
1053
  import random
1054
+ try:
1055
+ from .defaultnames import ISOTOPES
1056
+ except ImportError:
1057
+ from defaultnames import ISOTOPES
1058
  # Two-stage sampling: first pick element, then pick isotope
1059
  element = random.choice(list(ISOTOPES.keys()))
1060
  mass_number = random.choice(ISOTOPES[element])
backend/utils.py CHANGED
@@ -8,7 +8,10 @@ from huggingface_hub import InferenceClient
8
  from e2b_code_interpreter import Sandbox
9
  from transformers import AutoTokenizer
10
  from traitlets.config import Config
11
- from jupyter_handler import JupyterNotebook
 
 
 
12
 
13
  logger = logging.getLogger(__name__)
14
 
 
8
  from e2b_code_interpreter import Sandbox
9
  from transformers import AutoTokenizer
10
  from traitlets.config import Config
11
+ try:
12
+ from .jupyter_handler import JupyterNotebook
13
+ except ImportError:
14
+ from jupyter_handler import JupyterNotebook
15
 
16
  logger = logging.getLogger(__name__)
17