2023-09-28 10:58:58 +08:00
|
|
|
import json
|
|
|
|
from uuid import uuid4
|
|
|
|
import uuid
|
|
|
|
|
|
|
|
import requests
|
|
|
|
from websockets.sync.client import connect as ws_connect_sync
|
|
|
|
from websocket import create_connection
|
|
|
|
from websockets.client import connect as ws_connect
|
|
|
|
|
|
|
|
import sys, os
|
|
|
|
src_dir = os.path.join(
|
|
|
|
os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
|
|
|
)
|
|
|
|
sys.path.append(src_dir)
|
|
|
|
|
|
|
|
|
2024-01-26 14:03:25 +08:00
|
|
|
from configs.model_config import JUPYTER_WORK_PATH
|
|
|
|
from coagent.sandbox.pycodebox import PyCodeBox
|
2023-11-07 19:44:47 +08:00
|
|
|
import requests
|
2023-09-28 10:58:58 +08:00
|
|
|
|
|
|
|
|
|
|
|
# import subprocess
|
2024-01-26 14:03:25 +08:00
|
|
|
# import time
|
|
|
|
# from loguru import logger
|
|
|
|
|
|
|
|
# class PyCodeBox:
|
|
|
|
|
|
|
|
# enter_status: bool = False
|
|
|
|
|
|
|
|
# def __init__(
|
|
|
|
# self,
|
|
|
|
# remote_url: str = "",
|
|
|
|
# remote_ip: str = "localhost",
|
|
|
|
# remote_port: str = "5050",
|
|
|
|
# token: str = "mytoken",
|
|
|
|
# do_code_exe: bool = False,
|
|
|
|
# do_remote: bool = False,
|
|
|
|
# do_check_net: bool = True,
|
|
|
|
# ):
|
|
|
|
# self.enter_status = True
|
|
|
|
# self.do_check_net = do_check_net
|
|
|
|
# self.start()
|
|
|
|
|
|
|
|
|
|
|
|
# def start(self, ):
|
|
|
|
# logger.debug("start!")
|
|
|
|
# jupyter = subprocess.Popen(
|
|
|
|
# [
|
|
|
|
# "jupyter", "notebook",
|
|
|
|
# "--NotebookApp.token=mytoken",
|
|
|
|
# "--port=5050",
|
|
|
|
# "--no-browser",
|
|
|
|
# "--ServerApp.disable_check_xsrf=True",
|
|
|
|
# ],
|
|
|
|
# stderr=subprocess.PIPE,
|
|
|
|
# # stdin=subprocess.PIPE,
|
|
|
|
# stdout=subprocess.PIPE,
|
|
|
|
# )
|
|
|
|
# logger.debug("start over")
|
|
|
|
|
|
|
|
# pycodebox = PyCodeBox()
|
|
|
|
|
|
|
|
# logger.debug("start to sleep")
|
|
|
|
# idx = 0
|
|
|
|
# while True:
|
|
|
|
# time.sleep(5)
|
|
|
|
# logger.debug(f"sleep {idx}")
|
|
|
|
# idx+=1
|
2023-09-28 10:58:58 +08:00
|
|
|
|
2023-11-07 19:44:47 +08:00
|
|
|
# # 测试1
|
|
|
|
# import time, psutil
|
|
|
|
# from loguru import logger
|
|
|
|
# import asyncio
|
2023-12-07 20:17:21 +08:00
|
|
|
pycodebox = PyCodeBox(remote_url="http://localhost:5050",
|
|
|
|
remote_ip="http://localhost",
|
|
|
|
remote_port="5050",
|
|
|
|
token="mytoken",
|
2024-01-26 14:03:25 +08:00
|
|
|
jupyter_work_path=JUPYTER_WORK_PATH,
|
2023-12-07 20:17:21 +08:00
|
|
|
do_code_exe=True,
|
2024-01-26 14:03:25 +08:00
|
|
|
do_remote=False,
|
|
|
|
use_stop=True,
|
|
|
|
do_check_net=False
|
|
|
|
)
|
2023-09-28 10:58:58 +08:00
|
|
|
|
|
|
|
|
2024-01-26 14:03:25 +08:00
|
|
|
reuslt = pycodebox.chat("```import os\nos.getcwd()```", do_code_exe=True)
|
2023-12-07 20:17:21 +08:00
|
|
|
print(reuslt)
|
2023-09-28 10:58:58 +08:00
|
|
|
|
2024-01-26 14:03:25 +08:00
|
|
|
reuslt = pycodebox.chat("print('hello world!')", do_code_exe=False)
|
|
|
|
print(reuslt)
|
2023-09-28 10:58:58 +08:00
|
|
|
|
2024-01-26 14:03:25 +08:00
|
|
|
|
2023-09-28 10:58:58 +08:00
|
|
|
# 测试2
|
|
|
|
# with PyCodeBox(remote_url="http://localhost:5050",
|
|
|
|
# remote_ip="http://localhost",
|
|
|
|
# remote_port="5050",
|
|
|
|
# token="mytoken",
|
|
|
|
# do_code_exe=True,
|
2024-01-26 14:03:25 +08:00
|
|
|
# do_remote=False) as codebox:
|
2023-09-28 10:58:58 +08:00
|
|
|
|
2024-01-26 14:03:25 +08:00
|
|
|
# result = codebox.run("'hello world!'")
|
|
|
|
# print(result)
|