Update tests.py
Browse files
tests.py
CHANGED
|
@@ -12,7 +12,7 @@ import json
|
|
| 12 |
import openpyxl
|
| 13 |
import shutil
|
| 14 |
from google import genai
|
| 15 |
-
|
| 16 |
client = genai.Client(api_key="AIzaSyDtP05TyoIy9j0uPL7_wLEhgQEE75AZQSc")
|
| 17 |
|
| 18 |
source_dir = "/app/uploads/temp"
|
|
@@ -210,10 +210,9 @@ def create_code_files(filename: str, code: str) -> dict:
|
|
| 210 |
return {"info":"task completed. The referenced code files were created successfully. "}
|
| 211 |
|
| 212 |
@mcp.tool()
|
| 213 |
-
def run_code(python_packages:str,filename: str, code: str,start_cmd:str) -> dict:
|
| 214 |
"""
|
| 215 |
Execute code in a controlled environment with package installation and file handling.
|
| 216 |
-
|
| 217 |
Args:
|
| 218 |
python_packages[Output an empty string ,if using any other language.]: Space-separated list of packages to install (e.g., "numpy matplotlib").
|
| 219 |
Preinstalled packages: gradio, XlsxWriter, openpyxl.
|
|
@@ -221,8 +220,7 @@ def run_code(python_packages:str,filename: str, code: str,start_cmd:str) -> dict
|
|
| 221 |
code: Full code to write to the file.
|
| 222 |
start_cmd: Command to execute the file (e.g., "python /app/code_interpreter/app.py"
|
| 223 |
or "bash /app/code_interpreter/app.py").
|
| 224 |
-
|
| 225 |
-
Notes:
|
| 226 |
- All user-uploaded files are in /app/code_interpreter/.
|
| 227 |
- After execution, embed a download link (or display images/gifs/videos directly in markdown format) in your response.
|
| 228 |
"""
|
|
@@ -244,9 +242,15 @@ def run_code(python_packages:str,filename: str, code: str,start_cmd:str) -> dict
|
|
| 244 |
f = open(os.path.join(destination_dir, filename), "w")
|
| 245 |
f.write(code)
|
| 246 |
f.close()
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 250 |
onlyfiles = glob.glob("/app/code_interpreter/*")
|
| 251 |
onlyfiles=list(set(onlyfiles)-set(files_list))
|
| 252 |
uploaded_filenames=[]
|
|
@@ -286,15 +290,13 @@ def run_code_files(start_cmd:str) -> dict:
|
|
| 286 |
@mcp.tool()
|
| 287 |
def run_shell_command(cmd:str) -> dict:
|
| 288 |
"""(cmd:Example- mkdir test.By default , the command is run inside the /app/code_interpreter/ directory.).Remember, the code_interpreter is running on **alpine linux** , so write commands accordingly.Eg-sudo does not work and is not required.."""
|
| 289 |
-
|
| 290 |
-
|
| 291 |
-
|
| 292 |
-
|
| 293 |
-
|
| 294 |
-
pass
|
| 295 |
-
time.sleep(1.5)
|
| 296 |
transfer_files()
|
| 297 |
-
return {"
|
| 298 |
|
| 299 |
|
| 300 |
|
|
@@ -426,14 +428,4 @@ def deepthinking3(query:str,info:str) -> dict:
|
|
| 426 |
|
| 427 |
if __name__ == "__main__":
|
| 428 |
# Initialize and run the server
|
| 429 |
-
mcp.run(transport='stdio')
|
| 430 |
-
|
| 431 |
-
|
| 432 |
-
# @mcp.tool()
|
| 433 |
-
# def run_website(start_cmd:str,port=8501) -> dict:
|
| 434 |
-
# """(start_cmd:streamlit run app.py).Always specify sandbox id.Specify port (int) if different from 8501."""
|
| 435 |
-
# output=sbx.commands.run(start_cmd,sandbox_id)
|
| 436 |
-
# url = sbx.get_host(port)
|
| 437 |
-
# info={"info":f"Your Application is live [here](https://{url})"}
|
| 438 |
-
|
| 439 |
-
# return info
|
|
|
|
| 12 |
import openpyxl
|
| 13 |
import shutil
|
| 14 |
from google import genai
|
| 15 |
+
import pexpect
|
| 16 |
client = genai.Client(api_key="AIzaSyDtP05TyoIy9j0uPL7_wLEhgQEE75AZQSc")
|
| 17 |
|
| 18 |
source_dir = "/app/uploads/temp"
|
|
|
|
| 210 |
return {"info":"task completed. The referenced code files were created successfully. "}
|
| 211 |
|
| 212 |
@mcp.tool()
|
| 213 |
+
def run_code(python_packages:str,filename: str, code: str,start_cmd:str,forever_cmd:bool) -> dict:
|
| 214 |
"""
|
| 215 |
Execute code in a controlled environment with package installation and file handling.
|
|
|
|
| 216 |
Args:
|
| 217 |
python_packages[Output an empty string ,if using any other language.]: Space-separated list of packages to install (e.g., "numpy matplotlib").
|
| 218 |
Preinstalled packages: gradio, XlsxWriter, openpyxl.
|
|
|
|
| 220 |
code: Full code to write to the file.
|
| 221 |
start_cmd: Command to execute the file (e.g., "python /app/code_interpreter/app.py"
|
| 222 |
or "bash /app/code_interpreter/app.py").
|
| 223 |
+
forever_cmd: If True, the command will run indefinitely.Set to True, when runnig a website/server. If False, the command will time out after 300 second and the result will be returned.
|
|
|
|
| 224 |
- All user-uploaded files are in /app/code_interpreter/.
|
| 225 |
- After execution, embed a download link (or display images/gifs/videos directly in markdown format) in your response.
|
| 226 |
"""
|
|
|
|
| 242 |
f = open(os.path.join(destination_dir, filename), "w")
|
| 243 |
f.write(code)
|
| 244 |
f.close()
|
| 245 |
+
if forever_cmd:
|
| 246 |
+
child=pexpect.spawn(command)
|
| 247 |
+
time.sleep(5)
|
| 248 |
+
stdout=str(child.readline().decode())
|
| 249 |
+
stderr=""
|
| 250 |
+
else:
|
| 251 |
+
run(start_cmd, 300)
|
| 252 |
+
while stderr=="" and stdout=="":
|
| 253 |
+
pass
|
| 254 |
onlyfiles = glob.glob("/app/code_interpreter/*")
|
| 255 |
onlyfiles=list(set(onlyfiles)-set(files_list))
|
| 256 |
uploaded_filenames=[]
|
|
|
|
| 290 |
@mcp.tool()
|
| 291 |
def run_shell_command(cmd:str) -> dict:
|
| 292 |
"""(cmd:Example- mkdir test.By default , the command is run inside the /app/code_interpreter/ directory.).Remember, the code_interpreter is running on **alpine linux** , so write commands accordingly.Eg-sudo does not work and is not required.."""
|
| 293 |
+
process = pexpect.spawn(cmd)
|
| 294 |
+
strLine=""
|
| 295 |
+
while not process.eof():
|
| 296 |
+
strLine = strLine+process.readline().decode()
|
| 297 |
+
output=strLine
|
|
|
|
|
|
|
| 298 |
transfer_files()
|
| 299 |
+
return {"output":output}
|
| 300 |
|
| 301 |
|
| 302 |
|
|
|
|
| 428 |
|
| 429 |
if __name__ == "__main__":
|
| 430 |
# Initialize and run the server
|
| 431 |
+
mcp.run(transport='stdio')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|