Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
@@ -5,17 +5,24 @@ import shutil
|
|
5 |
from fastapi import FastAPI, HTTPException, UploadFile, File
|
6 |
from fastapi.middleware.cors import CORSMiddleware
|
7 |
from pydantic import BaseModel
|
8 |
-
from jupyter_client.manager import KernelManager
|
9 |
-
from typing import Dict
|
10 |
from datetime import datetime, timedelta
|
11 |
import psutil
|
12 |
-
|
|
|
|
|
|
|
|
|
13 |
|
14 |
app = FastAPI()
|
15 |
|
16 |
# Define root directory for all session folders
|
17 |
root_dir = os.path.abspath(os.path.dirname(__file__))
|
18 |
|
|
|
|
|
|
|
19 |
# Middleware for CORS
|
20 |
app.add_middleware(
|
21 |
CORSMiddleware,
|
@@ -39,13 +46,14 @@ TIMEOUT_DURATION = 600 # 10 minutes
|
|
39 |
|
40 |
# Function to create a new kernel and session directory
|
41 |
async def create_kernel(session_token: str):
|
42 |
-
# Create session directory
|
43 |
session_dir = os.path.join(root_dir, "output", session_token)
|
44 |
-
|
45 |
-
os.makedirs(session_dir, mode=0o777, exist_ok=True)
|
46 |
|
|
|
47 |
km = KernelManager()
|
48 |
-
km.
|
|
|
|
|
49 |
kernel_managers[session_token] = km
|
50 |
last_access_times[session_token] = datetime.now()
|
51 |
|
@@ -64,34 +72,23 @@ async def kill_kernel(session_token: str):
|
|
64 |
@app.post("/upload/{session_token}")
|
65 |
async def upload_files(session_token: str, files: List[UploadFile] = File(...)):
|
66 |
session_dir = os.path.join(root_dir, "output", session_token)
|
67 |
-
|
68 |
-
# Create session directory if it doesn't exist
|
69 |
-
if not os.path.exists(session_dir):
|
70 |
-
os.makedirs(session_dir)
|
71 |
|
72 |
uploaded_files = []
|
73 |
for file in files:
|
74 |
-
# Save the uploaded file
|
75 |
file_path = os.path.join(session_dir, file.filename)
|
76 |
with open(file_path, "wb") as buffer:
|
77 |
shutil.copyfileobj(file.file, buffer)
|
78 |
uploaded_files.append(file.filename)
|
|
|
79 |
|
80 |
-
print("uploaded_files",uploaded_files)
|
81 |
return {"filenames": uploaded_files, "status": "success"}
|
82 |
|
83 |
# Function to execute code in a kernel
|
84 |
async def execute_code(session_token: str, code: str):
|
85 |
-
setup_code = """
|
86 |
-
%matplotlib inline
|
87 |
-
import os
|
88 |
-
os.chdir('{session_dir}')
|
89 |
-
""".format(session_dir=os.path.join(root_dir, "output", session_token).replace('\\', '\\\\'))
|
90 |
-
|
91 |
session_dir = os.path.join(root_dir, "output", session_token)
|
92 |
-
|
93 |
-
if not os.path.exists(session_dir):
|
94 |
-
os.makedirs(session_dir)
|
95 |
|
96 |
if session_token not in kernel_managers:
|
97 |
await create_kernel(session_token)
|
@@ -100,14 +97,18 @@ os.chdir('{session_dir}')
|
|
100 |
kc = km.client()
|
101 |
|
102 |
try:
|
103 |
-
#
|
104 |
-
|
105 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
106 |
|
107 |
-
# Execute setup code to set kernel's working directory
|
108 |
-
kc.execute_interactive(setup_code, store_history=False)
|
109 |
# Execute the provided code
|
110 |
-
kc.execute(code
|
111 |
|
112 |
output = []
|
113 |
timeout = datetime.now() + timedelta(seconds=TIMEOUT_DURATION)
|
@@ -153,6 +154,12 @@ async def check_idle_kernels():
|
|
153 |
|
154 |
@app.on_event("startup")
|
155 |
async def startup_event():
|
|
|
|
|
|
|
|
|
|
|
|
|
156 |
asyncio.create_task(check_idle_kernels())
|
157 |
|
158 |
@app.post("/execute")
|
@@ -169,7 +176,6 @@ async def get_info():
|
|
169 |
cpu_usage = psutil.cpu_percent(interval=1)
|
170 |
ram_usage = psutil.virtual_memory().percent
|
171 |
|
172 |
-
# Return the information
|
173 |
return {
|
174 |
"active_kernels": active_kernels,
|
175 |
"cpu_usage_percent": cpu_usage,
|
@@ -177,4 +183,4 @@ async def get_info():
|
|
177 |
}
|
178 |
|
179 |
if __name__ == "__main__":
|
180 |
-
uvicorn.run(app, host="0.0.0.0", port=7860)
|
|
|
5 |
from fastapi import FastAPI, HTTPException, UploadFile, File
|
6 |
from fastapi.middleware.cors import CORSMiddleware
|
7 |
from pydantic import BaseModel
|
8 |
+
from jupyter_client.manager import KernelManager
|
9 |
+
from typing import Dict, List
|
10 |
from datetime import datetime, timedelta
|
11 |
import psutil
|
12 |
+
|
13 |
+
# Set environment variables for Jupyter
|
14 |
+
os.environ['JUPYTER_RUNTIME_DIR'] = '/app/kernels'
|
15 |
+
os.environ['IPYTHONDIR'] = '/app/ipython'
|
16 |
+
os.environ['MPLCONFIGDIR'] = '/app/matplotlib'
|
17 |
|
18 |
app = FastAPI()
|
19 |
|
20 |
# Define root directory for all session folders
|
21 |
root_dir = os.path.abspath(os.path.dirname(__file__))
|
22 |
|
23 |
+
# Ensure base output directory exists
|
24 |
+
os.makedirs(os.path.join(root_dir, "output"), mode=0o777, exist_ok=True)
|
25 |
+
|
26 |
# Middleware for CORS
|
27 |
app.add_middleware(
|
28 |
CORSMiddleware,
|
|
|
46 |
|
47 |
# Function to create a new kernel and session directory
|
48 |
async def create_kernel(session_token: str):
|
|
|
49 |
session_dir = os.path.join(root_dir, "output", session_token)
|
50 |
+
os.makedirs(session_dir, mode=0o777, exist_ok=True)
|
|
|
51 |
|
52 |
+
# Create a kernel with specific working directory
|
53 |
km = KernelManager()
|
54 |
+
km.kernel_cmd = ['python', '-c', f'import os; os.chdir("{session_dir}"); from ipykernel.kernelapp import main; main()']
|
55 |
+
km.start_kernel(cwd=session_dir)
|
56 |
+
|
57 |
kernel_managers[session_token] = km
|
58 |
last_access_times[session_token] = datetime.now()
|
59 |
|
|
|
72 |
@app.post("/upload/{session_token}")
|
73 |
async def upload_files(session_token: str, files: List[UploadFile] = File(...)):
|
74 |
session_dir = os.path.join(root_dir, "output", session_token)
|
75 |
+
os.makedirs(session_dir, mode=0o777, exist_ok=True)
|
|
|
|
|
|
|
76 |
|
77 |
uploaded_files = []
|
78 |
for file in files:
|
|
|
79 |
file_path = os.path.join(session_dir, file.filename)
|
80 |
with open(file_path, "wb") as buffer:
|
81 |
shutil.copyfileobj(file.file, buffer)
|
82 |
uploaded_files.append(file.filename)
|
83 |
+
os.chmod(file_path, 0o777) # Give full permissions to uploaded file
|
84 |
|
85 |
+
print("uploaded_files", uploaded_files)
|
86 |
return {"filenames": uploaded_files, "status": "success"}
|
87 |
|
88 |
# Function to execute code in a kernel
|
89 |
async def execute_code(session_token: str, code: str):
|
|
|
|
|
|
|
|
|
|
|
|
|
90 |
session_dir = os.path.join(root_dir, "output", session_token)
|
91 |
+
os.makedirs(session_dir, mode=0o777, exist_ok=True)
|
|
|
|
|
92 |
|
93 |
if session_token not in kernel_managers:
|
94 |
await create_kernel(session_token)
|
|
|
97 |
kc = km.client()
|
98 |
|
99 |
try:
|
100 |
+
# Setup code to ensure correct working directory
|
101 |
+
setup_code = f"""
|
102 |
+
import os
|
103 |
+
os.chdir(r'{session_dir}')
|
104 |
+
print('Current working directory:', os.getcwd())
|
105 |
+
"""
|
106 |
+
# Execute setup code first
|
107 |
+
kc.execute(setup_code)
|
108 |
+
msg = kc.get_shell_msg() # Wait for setup completion
|
109 |
|
|
|
|
|
110 |
# Execute the provided code
|
111 |
+
kc.execute(code)
|
112 |
|
113 |
output = []
|
114 |
timeout = datetime.now() + timedelta(seconds=TIMEOUT_DURATION)
|
|
|
154 |
|
155 |
@app.on_event("startup")
|
156 |
async def startup_event():
|
157 |
+
# Ensure all required directories exist with proper permissions
|
158 |
+
os.makedirs('/app/kernels', mode=0o777, exist_ok=True)
|
159 |
+
os.makedirs('/app/ipython', mode=0o777, exist_ok=True)
|
160 |
+
os.makedirs('/app/matplotlib', mode=0o777, exist_ok=True)
|
161 |
+
os.makedirs(os.path.join(root_dir, "output"), mode=0o777, exist_ok=True)
|
162 |
+
|
163 |
asyncio.create_task(check_idle_kernels())
|
164 |
|
165 |
@app.post("/execute")
|
|
|
176 |
cpu_usage = psutil.cpu_percent(interval=1)
|
177 |
ram_usage = psutil.virtual_memory().percent
|
178 |
|
|
|
179 |
return {
|
180 |
"active_kernels": active_kernels,
|
181 |
"cpu_usage_percent": cpu_usage,
|
|
|
183 |
}
|
184 |
|
185 |
if __name__ == "__main__":
|
186 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|