File size: 1,485 Bytes
571ff03
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from __future__ import annotations

import os
import base64
import mimetypes
import argparse
from pathlib import Path
from typing import List, Dict, Any, Optional
from dotenv import load_dotenv
from huggingface_hub import InferenceClient
import io
from contextlib import redirect_stdout

load_dotenv()

class PythonAgent:
    def __init__(self):
        # print("Python code execution agent initialized.")
        pass
    
    def __call__(self, file_path: str) -> str:
        return self.execute_python_file(file_path)


    def execute_python_file(self, file_path: str) -> str:
        import io
        from contextlib import redirect_stdout

        with open(file_path, "r") as f_in:
            code_to_exec = f_in.read()

        # important: real module-like globals with __name__="__main__"
        sandbox_globals = {
            "__name__": "__main__",
            "__file__": file_path,
        }

        buf = io.StringIO()
        with redirect_stdout(buf):
            try:
                exec(code_to_exec, sandbox_globals)
            except Exception as e:
                print(f"An error occurred: {e}")

        raw_response = buf.getvalue()
        # the response is the last line
        response = raw_response.strip().splitlines()[-1]
        return response

# path_python_file = r"downloaded\f918266a-b3e0-4914-865d-4faa564f1aef.py"
# response = execute_python_file(path_python_file)
# print(response)