euler314 commited on
Commit
390e055
·
verified ·
1 Parent(s): 368bfff

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -3
app.py CHANGED
@@ -38,14 +38,54 @@ for loc in module_locations:
38
  try:
39
  import cubic_cpp
40
  cpp_available = True
41
- # Print the location of the imported module to verify
42
  print(f"Loaded C++ module from: {cubic_cpp.__file__}")
43
  except ImportError as e:
44
- import sys
45
  print(f"C++ acceleration unavailable: {e}")
46
  print(f"Python path: {sys.path}")
47
  print(f"Current directory: {os.getcwd()}")
48
- cpp_available = False
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
49
 
50
  def add_sqrt_support(expr_str):
51
  """Replace 'sqrt(' with 'sp.sqrt(' for sympy compatibility"""
 
38
  try:
39
  import cubic_cpp
40
  cpp_available = True
 
41
  print(f"Loaded C++ module from: {cubic_cpp.__file__}")
42
  except ImportError as e:
 
43
  print(f"C++ acceleration unavailable: {e}")
44
  print(f"Python path: {sys.path}")
45
  print(f"Current directory: {os.getcwd()}")
46
+
47
+ # Try to compile the module on the fly
48
+ try:
49
+ import subprocess
50
+ import sys
51
+ import os
52
+
53
+ print("Attempting to compile C++ module at runtime...")
54
+
55
+ # Get the extension suffix for this Python version
56
+ ext_suffix = subprocess.check_output("python3-config --extension-suffix", shell=True).decode().strip()
57
+ print(f"Extension suffix: {ext_suffix}")
58
+
59
+ # Compile command
60
+ compile_cmd = f"""g++ -O3 -shared -std=c++11 -fPIC \
61
+ $(python3-config --includes) \
62
+ -I$(python3 -c "import pybind11; print(pybind11.get_include())") \
63
+ -I$(python3 -c "import numpy; print(numpy.get_include())") \
64
+ {os.path.join(os.getcwd(), 'cubic_cpp.cpp')} \
65
+ -o {os.path.join(os.getcwd(), 'cubic_cpp' + ext_suffix)}"""
66
+
67
+ print(f"Compile command: {compile_cmd}")
68
+ result = subprocess.run(compile_cmd, shell=True, capture_output=True, text=True)
69
+
70
+ if result.returncode == 0:
71
+ print("Compilation successful")
72
+ # Try to import again
73
+ sys.path.insert(0, os.getcwd()) # Ensure current directory is in path
74
+ import importlib.util
75
+ spec = importlib.util.spec_from_file_location("cubic_cpp", os.path.join(os.getcwd(), "cubic_cpp" + ext_suffix))
76
+ if spec:
77
+ cubic_cpp = importlib.util.module_from_spec(spec)
78
+ spec.loader.exec_module(cubic_cpp)
79
+ cpp_available = True
80
+ print(f"Successfully imported compiled module")
81
+ else:
82
+ raise ImportError(f"Failed to load module spec for cubic_cpp")
83
+ else:
84
+ print(f"Compilation failed: {result.stderr}")
85
+ raise RuntimeError(f"C++ compilation failed: {result.stderr}")
86
+ except Exception as compile_error:
87
+ print(f"Failed to compile C++ module: {compile_error}")
88
+ cpp_available = False
89
 
90
  def add_sqrt_support(expr_str):
91
  """Replace 'sqrt(' with 'sp.sqrt(' for sympy compatibility"""