File size: 2,751 Bytes
e789e15
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import re
import runpy
import os
import importlib.util
import subprocess
import sys


def install_missing_libraries(code):
    """
    Parses the Python code to identify missing libraries and installs them.
    """
    # Extract import statements from the code
    import_statements = re.findall(r"^\s*(?:import|from)\s+([a-zA-Z0-9_\.]+)", code, re.MULTILINE)
    missing_libraries = []

    for module in import_statements:
        # Check if the module is installed
        try:
            importlib.import_module(module.split('.')[0])  # Handle submodules like `pandas.core`
        except ImportError:
            missing_libraries.append(module.split('.')[0])

    # Install missing libraries
    if missing_libraries:
        for lib in set(missing_libraries):  # Remove duplicates
            try:
                print(f"Installing missing library: {lib}")
                subprocess.check_call([sys.executable, "-m", "pip", "install", lib])
            except Exception as e:
                print(f"Failed to install {lib}: {e}")
    else:
        print("All required libraries are already installed.")


def extract_python_code(response):
    """
    Extracts the Python code block from the LLM response.
    Assumes the code is enclosed within triple backticks (```).
    """
    code_block = re.search(r"```python\n(.*?)```", response, re.DOTALL)
    code_block1 = re.search(r"```\n(.*?)```", response, re.DOTALL)
    if code_block:
        return code_block.group(1)
    elif code_block1:
        return code_block1.group(1)
    else:
        raise ValueError("No Python code block found in the response.")


def execute_visualizations(code, temp_file="temp_visualization.py"):
    """
    Executes the extracted Python code in a separate file and
    returns the list of visualizations (figures).
    """
    try:
        # Check for missing libraries and install them
        install_missing_libraries(code)

        # Write the code to a temporary file
        with open(temp_file, "w") as file:
            file.write(code)

        # Dynamically load the module
        spec = importlib.util.spec_from_file_location("temp_visualization", temp_file)
        temp_module = importlib.util.module_from_spec(spec)
        spec.loader.exec_module(temp_module)

        # Call the create_visualizations function
        if hasattr(temp_module, "create_visualizations"):
            return temp_module.create_visualizations()
        else:
            raise AttributeError("No function 'create_visualizations' found in the provided code.")

    except Exception as e:
        return f"Error executing the code: {e}"

    finally:
        # Clean up the temporary file
        if os.path.exists(temp_file):
            os.remove(temp_file)