forked from poe-platform/server-bot-quick-start
-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathfunction_exec.py
82 lines (69 loc) · 1.8 KB
/
function_exec.py
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
"""
Helper function to execute Python code
modal deploy function_exec.py
"""
import sys
from io import StringIO
from modal import App, Image
image = Image.debian_slim().pip_install(
"fastapi-poe==0.0.23",
"huggingface-hub==0.16.4",
"ipython",
"scipy",
"matplotlib",
"scikit-learn",
"pandas==1.3.2",
"ortools",
"torch",
"torchvision",
"tensorflow",
"spacy",
"transformers",
"opencv-python-headless",
"nltk",
"openai",
"requests",
"beautifulsoup4",
"newspaper3k",
"feedparser",
"sympy",
"tensorflow",
"cartopy",
"wordcloud",
"gensim",
"keras",
"librosa",
"XlsxWriter",
"docx2txt",
"markdownify",
"pdfminer.six",
"Pillow",
"opencv-python",
"sortedcontainers",
"intervaltree",
"geopandas",
"basemap",
"tiktoken",
"basemap-data-hires",
"yfinance",
"dill",
)
app = App("run-python-code-shared")
@app.function(image=image, timeout=30, container_idle_timeout=1200)
def execute_code(code):
import traitlets.config
from IPython.terminal.embed import InteractiveShellEmbed
config = traitlets.config.Config()
config.InteractiveShell.colors = "NoColor"
# config.PlainTextFormatter.max_width = 40 # not working
# config.InteractiveShell.width = 40 # not working
ipython = InteractiveShellEmbed(config=config)
# Redirect stdout temporarily to capture the output of the code snippet
old_stdout = sys.stdout
sys.stdout = StringIO()
# Execute the code with the silent parameter set to True
_ = ipython.run_cell(code, silent=True, store_history=False, shell_futures=False)
# Restore the original stdout and retrieve the captured output
captured_output = sys.stdout.getvalue()
sys.stdout = old_stdout
return captured_output