lvwerra HF staff commited on
Commit
5c9a3e1
·
verified ·
1 Parent(s): 033f69a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -10
app.py CHANGED
@@ -24,21 +24,24 @@ from utils import (
24
  E2B_API_KEY = os.environ["E2B_API_KEY"]
25
  HF_TOKEN = os.environ["HF_TOKEN"]
26
  DEFAULT_MAX_TOKENS = 512
 
27
 
28
  with open("ds-system-prompt.txt", "r") as f:
29
  DEFAULT_SYSTEM_PROMPT = f.read()
30
 
31
 
32
  def execute_jupyter_agent(
33
- sytem_prompt, user_input, max_new_tokens, model, files, message_history, sbx
34
  ):
 
 
 
 
35
  client = InferenceClient(api_key=HF_TOKEN)
36
 
37
  tokenizer = AutoTokenizer.from_pretrained(model)
38
  # model = "meta-llama/Llama-3.1-8B-Instruct"
39
 
40
-
41
-
42
  filenames = []
43
  if files is not None:
44
  for filepath in files:
@@ -67,11 +70,9 @@ def execute_jupyter_agent(
67
  yield notebook_html, message_history
68
 
69
 
70
- def clear(msg_state, sbx_state):
71
  msg_state = []
72
- sbx_state.kill()
73
- sbx_state = Sandbox(api_key=E2B_API_KEY)
74
- return update_notebook_display(create_base_notebook([])[0]), msg_state, sbx_state
75
 
76
 
77
  css = """
@@ -94,7 +95,7 @@ css = """
94
  # Create the interface
95
  with gr.Blocks() as demo:
96
  msg_state = gr.State(value=[])
97
- sbx_state = gr.State(value=Sandbox(api_key=E2B_API_KEY))
98
 
99
  html_output = gr.HTML(value=update_notebook_display(create_base_notebook([])[0]))
100
 
@@ -137,11 +138,11 @@ with gr.Blocks() as demo:
137
 
138
  generate_btn.click(
139
  fn=execute_jupyter_agent,
140
- inputs=[system_input, user_input, max_tokens, model, files, msg_state, sbx_state],
141
  outputs=[html_output, msg_state],
142
  )
143
 
144
- clear_btn.click(fn=clear, inputs=[msg_state, sbx_state], outputs=[html_output, msg_state, sbx_state])
145
 
146
  demo.load(
147
  fn=None,
 
24
  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()
31
 
32
 
33
  def execute_jupyter_agent(
34
+ sytem_prompt, user_input, max_new_tokens, model, files, message_history, request
35
  ):
36
+ if reqest.session_hash not in SANDBOXES:
37
+ SANDBOXES[reqest.session_hash] = Sandbox(api_key=E2B_API_KEY)
38
+ sbx = SANDBOXES[reqest.session_hash]
39
+
40
  client = InferenceClient(api_key=HF_TOKEN)
41
 
42
  tokenizer = AutoTokenizer.from_pretrained(model)
43
  # model = "meta-llama/Llama-3.1-8B-Instruct"
44
 
 
 
45
  filenames = []
46
  if files is not None:
47
  for filepath in files:
 
70
  yield notebook_html, message_history
71
 
72
 
73
+ def clear(msg_state):
74
  msg_state = []
75
+ return update_notebook_display(create_base_notebook([])[0]), msg_state
 
 
76
 
77
 
78
  css = """
 
95
  # Create the interface
96
  with gr.Blocks() as demo:
97
  msg_state = gr.State(value=[])
98
+ req = gr.Request()
99
 
100
  html_output = gr.HTML(value=update_notebook_display(create_base_notebook([])[0]))
101
 
 
138
 
139
  generate_btn.click(
140
  fn=execute_jupyter_agent,
141
+ inputs=[system_input, user_input, max_tokens, model, files, msg_state, req],
142
  outputs=[html_output, msg_state],
143
  )
144
 
145
+ clear_btn.click(fn=clear, inputs=[msg_state], outputs=[html_output, msg_state])
146
 
147
  demo.load(
148
  fn=None,