Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -5,6 +5,7 @@ from huggingface_hub import InferenceClient
|
|
5 |
from e2b_code_interpreter import Sandbox
|
6 |
from pathlib import Path
|
7 |
from transformers import AutoTokenizer
|
|
|
8 |
|
9 |
if not get_space():
|
10 |
try:
|
@@ -25,6 +26,9 @@ E2B_API_KEY = os.environ["E2B_API_KEY"]
|
|
25 |
HF_TOKEN = os.environ["HF_TOKEN"]
|
26 |
DEFAULT_MAX_TOKENS = 512
|
27 |
SANDBOXES = {}
|
|
|
|
|
|
|
28 |
|
29 |
with open("ds-system-prompt.txt", "r") as f:
|
30 |
DEFAULT_SYSTEM_PROMPT = f.read()
|
@@ -36,7 +40,11 @@ def execute_jupyter_agent(
|
|
36 |
if request.session_hash not in SANDBOXES:
|
37 |
SANDBOXES[request.session_hash] = Sandbox(api_key=E2B_API_KEY)
|
38 |
sbx = SANDBOXES[request.session_hash]
|
39 |
-
|
|
|
|
|
|
|
|
|
40 |
client = InferenceClient(api_key=HF_TOKEN)
|
41 |
|
42 |
tokenizer = AutoTokenizer.from_pretrained(model)
|
@@ -63,12 +71,16 @@ def execute_jupyter_agent(
|
|
63 |
|
64 |
print("history:", message_history)
|
65 |
|
66 |
-
for notebook_html, messages in run_interactive_notebook(
|
67 |
client, model, tokenizer, message_history, sbx, max_new_tokens=max_new_tokens
|
68 |
):
|
69 |
message_history = messages
|
70 |
-
|
71 |
-
|
|
|
|
|
|
|
|
|
72 |
|
73 |
def clear(msg_state):
|
74 |
msg_state = []
|
@@ -97,7 +109,7 @@ with gr.Blocks() as demo:
|
|
97 |
msg_state = gr.State(value=[])
|
98 |
|
99 |
html_output = gr.HTML(value=update_notebook_display(create_base_notebook([])[0]))
|
100 |
-
|
101 |
user_input = gr.Textbox(
|
102 |
value="Solve the Lotka-Volterra equation and plot the results.", lines=3
|
103 |
)
|
@@ -139,7 +151,7 @@ with gr.Blocks() as demo:
|
|
139 |
generate_btn.click(
|
140 |
fn=execute_jupyter_agent,
|
141 |
inputs=[system_input, user_input, max_tokens, model, files, msg_state],
|
142 |
-
outputs=[html_output, msg_state],
|
143 |
)
|
144 |
|
145 |
clear_btn.click(fn=clear, inputs=[msg_state], outputs=[html_output, msg_state])
|
|
|
5 |
from e2b_code_interpreter import Sandbox
|
6 |
from pathlib import Path
|
7 |
from transformers import AutoTokenizer
|
8 |
+
import json
|
9 |
|
10 |
if not get_space():
|
11 |
try:
|
|
|
26 |
HF_TOKEN = os.environ["HF_TOKEN"]
|
27 |
DEFAULT_MAX_TOKENS = 512
|
28 |
SANDBOXES = {}
|
29 |
+
TMP_DIR = './tmp/'
|
30 |
+
if not os.path.exists(TMP_DIR):
|
31 |
+
os.makedirs(TMP_DIR)
|
32 |
|
33 |
with open("ds-system-prompt.txt", "r") as f:
|
34 |
DEFAULT_SYSTEM_PROMPT = f.read()
|
|
|
40 |
if request.session_hash not in SANDBOXES:
|
41 |
SANDBOXES[request.session_hash] = Sandbox(api_key=E2B_API_KEY)
|
42 |
sbx = SANDBOXES[request.session_hash]
|
43 |
+
|
44 |
+
save_dir = os.path.join(TMP_DIR, request.session_hash)
|
45 |
+
os.makedirs(save_dir, exist_ok=True)
|
46 |
+
save_dir = os.path.join(save_dir, 'jupyter-agent.ipynb')
|
47 |
+
|
48 |
client = InferenceClient(api_key=HF_TOKEN)
|
49 |
|
50 |
tokenizer = AutoTokenizer.from_pretrained(model)
|
|
|
71 |
|
72 |
print("history:", message_history)
|
73 |
|
74 |
+
for notebook_html, notebook_data, messages in run_interactive_notebook(
|
75 |
client, model, tokenizer, message_history, sbx, max_new_tokens=max_new_tokens
|
76 |
):
|
77 |
message_history = messages
|
78 |
+
|
79 |
+
yield notebook_html, message_history, None
|
80 |
+
|
81 |
+
with open(save_dir, 'w', encoding='utf-8') as f:
|
82 |
+
json.dump(notebook_data, f, indent=2)
|
83 |
+
yield notebook_html, message_history, save_dir
|
84 |
|
85 |
def clear(msg_state):
|
86 |
msg_state = []
|
|
|
109 |
msg_state = gr.State(value=[])
|
110 |
|
111 |
html_output = gr.HTML(value=update_notebook_display(create_base_notebook([])[0]))
|
112 |
+
file = gr.File()
|
113 |
user_input = gr.Textbox(
|
114 |
value="Solve the Lotka-Volterra equation and plot the results.", lines=3
|
115 |
)
|
|
|
151 |
generate_btn.click(
|
152 |
fn=execute_jupyter_agent,
|
153 |
inputs=[system_input, user_input, max_tokens, model, files, msg_state],
|
154 |
+
outputs=[html_output, msg_state, file],
|
155 |
)
|
156 |
|
157 |
clear_btn.click(fn=clear, inputs=[msg_state], outputs=[html_output, msg_state])
|