Update tests.py
Browse files
tests.py
CHANGED
|
@@ -13,6 +13,7 @@ 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"
|
|
@@ -26,8 +27,6 @@ os.environ["OPENROUTER_API_KEY"] = "sk-or-v1-019ff564f86e6d14b2a78a78be1fb88724e
|
|
| 26 |
mcp = FastMCP("code_sandbox")
|
| 27 |
data={}
|
| 28 |
result=""
|
| 29 |
-
stdout=""
|
| 30 |
-
stderr=""
|
| 31 |
import requests
|
| 32 |
import os
|
| 33 |
from bs4 import BeautifulSoup # For parsing HTML
|
|
@@ -140,17 +139,29 @@ chat_id = "5075390513"
|
|
| 140 |
from requests_futures.sessions import FuturesSession
|
| 141 |
session = FuturesSession()
|
| 142 |
|
| 143 |
-
def run(cmd, timeout_sec):
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 154 |
|
| 155 |
@mcp.tool()
|
| 156 |
def analyse_audio(audiopath,query) -> dict:
|
|
@@ -221,13 +232,12 @@ def run_code(python_packages:str,filename: str, code: str,start_cmd:str,forever_
|
|
| 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 |
"""
|
| 227 |
|
| 228 |
global files_list
|
| 229 |
-
global stdout
|
| 230 |
-
global stderr
|
| 231 |
package_names = python_packages.strip()
|
| 232 |
command="pip install"
|
| 233 |
if not package_names:
|
|
@@ -242,15 +252,8 @@ def run_code(python_packages:str,filename: str, code: str,start_cmd:str,forever_
|
|
| 242 |
f = open(os.path.join(destination_dir, filename), "w")
|
| 243 |
f.write(code)
|
| 244 |
f.close()
|
| 245 |
-
|
| 246 |
-
|
| 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=[]
|
|
@@ -261,19 +264,14 @@ def run_code(python_packages:str,filename: str, code: str,start_cmd:str,forever_
|
|
| 261 |
except:
|
| 262 |
pass
|
| 263 |
files_list=onlyfiles
|
| 264 |
-
return {"
|
| 265 |
|
| 266 |
|
| 267 |
@mcp.tool()
|
| 268 |
def run_code_files(start_cmd:str) -> dict:
|
| 269 |
"""(start_cmd:Example- python /app/code_interpreter/app.py or bash /app/code_interpreter/app.py).The files must be inside the /app/code_interpreter directory."""
|
| 270 |
global files_list
|
| 271 |
-
|
| 272 |
-
global stderr
|
| 273 |
-
run(start_cmd, 300)
|
| 274 |
-
while stderr=="" and stdout=="":
|
| 275 |
-
pass
|
| 276 |
-
time.sleep(1.5)
|
| 277 |
onlyfiles = glob.glob("/app/code_interpreter/*")
|
| 278 |
onlyfiles=list(set(onlyfiles)-set(files_list))
|
| 279 |
uploaded_filenames=[]
|
|
@@ -284,19 +282,16 @@ def run_code_files(start_cmd:str) -> dict:
|
|
| 284 |
except:
|
| 285 |
pass
|
| 286 |
files_list=onlyfiles
|
| 287 |
-
return {"
|
| 288 |
|
| 289 |
|
| 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 |
-
|
| 294 |
-
|
| 295 |
-
while not process.eof():
|
| 296 |
-
strLine = strLine+process.readline().decode()
|
| 297 |
-
output=strLine
|
| 298 |
transfer_files()
|
| 299 |
-
return {"output":
|
| 300 |
|
| 301 |
|
| 302 |
|
|
@@ -309,13 +304,11 @@ def install_python_packages(python_packages:str) -> dict:
|
|
| 309 |
if not package_names:
|
| 310 |
return
|
| 311 |
|
| 312 |
-
run(
|
| 313 |
f"{command} --break-system-packages {package_names}", timeout_sec=300
|
| 314 |
)
|
| 315 |
-
|
| 316 |
-
|
| 317 |
-
time.sleep(2)
|
| 318 |
-
return {"stdout":stdout,"stderr":stderr,"info":"Ran package installation command"}
|
| 319 |
|
| 320 |
@mcp.tool()
|
| 321 |
def get_youtube_transcript(videoid:str) -> dict:
|
|
@@ -428,4 +421,5 @@ def deepthinking3(query:str,info:str) -> dict:
|
|
| 428 |
|
| 429 |
if __name__ == "__main__":
|
| 430 |
# Initialize and run the server
|
| 431 |
-
mcp.run(transport='stdio')
|
|
|
|
|
|
| 13 |
import shutil
|
| 14 |
from google import genai
|
| 15 |
import pexpect
|
| 16 |
+
|
| 17 |
client = genai.Client(api_key="AIzaSyDtP05TyoIy9j0uPL7_wLEhgQEE75AZQSc")
|
| 18 |
|
| 19 |
source_dir = "/app/uploads/temp"
|
|
|
|
| 27 |
mcp = FastMCP("code_sandbox")
|
| 28 |
data={}
|
| 29 |
result=""
|
|
|
|
|
|
|
| 30 |
import requests
|
| 31 |
import os
|
| 32 |
from bs4 import BeautifulSoup # For parsing HTML
|
|
|
|
| 139 |
from requests_futures.sessions import FuturesSession
|
| 140 |
session = FuturesSession()
|
| 141 |
|
| 142 |
+
def run(cmd, timeout_sec,forever_cmd=False):
|
| 143 |
+
t=time.time()
|
| 144 |
+
child = pexpect.spawn("bash")
|
| 145 |
+
output=""
|
| 146 |
+
command="cd /app/code_interpreter/ && "+cmd
|
| 147 |
+
|
| 148 |
+
child.sendline('PROMPT_COMMAND="echo END"')
|
| 149 |
+
child.readline().decode()
|
| 150 |
+
child.readline().decode()
|
| 151 |
+
|
| 152 |
+
child.sendline(command)
|
| 153 |
+
|
| 154 |
+
while (not child.eof() ) and (time.time()-t<90):
|
| 155 |
+
x=child.readline().decode()
|
| 156 |
+
output=output+x
|
| 157 |
+
print(x)
|
| 158 |
+
if "END" in x :
|
| 159 |
+
output=output.replace("END","")
|
| 160 |
+
child.close()
|
| 161 |
+
break
|
| 162 |
+
if forever_cmd:
|
| 163 |
+
break
|
| 164 |
+
return output
|
| 165 |
|
| 166 |
@mcp.tool()
|
| 167 |
def analyse_audio(audiopath,query) -> dict:
|
|
|
|
| 232 |
start_cmd: Command to execute the file (e.g., "python /app/code_interpreter/app.py"
|
| 233 |
or "bash /app/code_interpreter/app.py").
|
| 234 |
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.
|
| 235 |
+
Notes:
|
| 236 |
- All user-uploaded files are in /app/code_interpreter/.
|
| 237 |
- After execution, embed a download link (or display images/gifs/videos directly in markdown format) in your response.
|
| 238 |
"""
|
| 239 |
|
| 240 |
global files_list
|
|
|
|
|
|
|
| 241 |
package_names = python_packages.strip()
|
| 242 |
command="pip install"
|
| 243 |
if not package_names:
|
|
|
|
| 252 |
f = open(os.path.join(destination_dir, filename), "w")
|
| 253 |
f.write(code)
|
| 254 |
f.close()
|
| 255 |
+
stdot=run(start_cmd, 300,forever_cmd)
|
| 256 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 257 |
onlyfiles = glob.glob("/app/code_interpreter/*")
|
| 258 |
onlyfiles=list(set(onlyfiles)-set(files_list))
|
| 259 |
uploaded_filenames=[]
|
|
|
|
| 264 |
except:
|
| 265 |
pass
|
| 266 |
files_list=onlyfiles
|
| 267 |
+
return {"output":stdot,"Files_download_link":uploaded_filenames}
|
| 268 |
|
| 269 |
|
| 270 |
@mcp.tool()
|
| 271 |
def run_code_files(start_cmd:str) -> dict:
|
| 272 |
"""(start_cmd:Example- python /app/code_interpreter/app.py or bash /app/code_interpreter/app.py).The files must be inside the /app/code_interpreter directory."""
|
| 273 |
global files_list
|
| 274 |
+
stdot=run(start_cmd, 300)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 275 |
onlyfiles = glob.glob("/app/code_interpreter/*")
|
| 276 |
onlyfiles=list(set(onlyfiles)-set(files_list))
|
| 277 |
uploaded_filenames=[]
|
|
|
|
| 282 |
except:
|
| 283 |
pass
|
| 284 |
files_list=onlyfiles
|
| 285 |
+
return {"output":stdot,"Files_download_link":uploaded_filenames}
|
| 286 |
|
| 287 |
|
| 288 |
@mcp.tool()
|
| 289 |
def run_shell_command(cmd:str) -> dict:
|
| 290 |
"""(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.."""
|
| 291 |
+
global stdout
|
| 292 |
+
run(cmd, 300)
|
|
|
|
|
|
|
|
|
|
| 293 |
transfer_files()
|
| 294 |
+
return {"output":stdout}
|
| 295 |
|
| 296 |
|
| 297 |
|
|
|
|
| 304 |
if not package_names:
|
| 305 |
return
|
| 306 |
|
| 307 |
+
stdot=run(
|
| 308 |
f"{command} --break-system-packages {package_names}", timeout_sec=300
|
| 309 |
)
|
| 310 |
+
|
| 311 |
+
return {"stdout":stdot,"info":"Ran package installation command"}
|
|
|
|
|
|
|
| 312 |
|
| 313 |
@mcp.tool()
|
| 314 |
def get_youtube_transcript(videoid:str) -> dict:
|
|
|
|
| 421 |
|
| 422 |
if __name__ == "__main__":
|
| 423 |
# Initialize and run the server
|
| 424 |
+
mcp.run(transport='stdio')
|
| 425 |
+
|