lvwerra HF Staff commited on
Commit
a4f3b3f
·
1 Parent(s): cbbc280

session fix + interactive mode

Browse files
Files changed (1) hide show
  1. app.py +88 -21
app.py CHANGED
@@ -2,30 +2,27 @@ import gradio as gr
2
  import os
3
  from e2b_desktop import Sandbox
4
 
5
-
6
  E2B_API_KEY = os.getenv("E2B_API_KEY")
7
- DEFAULT_MAX_TOKENS = 512
8
  SANDBOXES = {}
9
  TMP_DIR = './tmp/'
10
  if not os.path.exists(TMP_DIR):
11
  os.makedirs(TMP_DIR)
12
 
13
-
14
  custom_css = """
15
- /* Outer wrapper to handle centering without scrolling */
16
  .sandbox-outer-wrapper {
17
  display: flex;
18
  justify-content: center;
19
  width: 100%;
20
  padding: 20px 0;
21
- overflow: hidden; /* Changed from overflow-x: auto */
22
  }
23
 
24
  .sandbox-container {
25
  position: relative;
26
  width: 800px;
27
  height: 500px;
28
- flex-shrink: 0; /* Prevents container from shrinking */
29
  overflow: hidden;
30
  }
31
 
@@ -48,12 +45,56 @@ custom_css = """
48
  transform-origin: 0 0;
49
  transform: scale(0.392);
50
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51
  """
52
 
53
  html_template = """
54
  <div class="sandbox-outer-wrapper">
55
  <div class="sandbox-container">
56
  <img src="https://huggingface.co/datasets/lvwerra/admin/resolve/main/desktop_scaled.png" class="sandbox-background" />
 
 
57
  <iframe
58
  src="{stream_url}"
59
  class="sandbox-iframe"
@@ -62,19 +103,29 @@ html_template = """
62
  </div>
63
  </div>"""
64
 
65
-
66
- def update_placeholder_text(request: gr.Request):
67
-
68
- if request.session_hash not in SANDBOXES:
69
- SANDBOXES[request.session_hash] = Sandbox(api_key=E2B_API_KEY, resolution=(1024, 768), dpi=96)
70
- desktop = SANDBOXES[request.session_hash]
71
 
72
- #desktop = Sandbox(api_key=E2B_API_KEY, resolution=(1024, 768), dpi=96)
73
- desktop.stream.start(require_auth=True)
74
  auth_key = desktop.stream.get_auth_key()
75
- stream_url = desktop.stream.get_url(auth_key=auth_key)
76
 
77
- html_content = html_template.format(stream_url=stream_url)
 
 
 
 
 
 
 
 
 
 
 
 
78
  return html_content
79
 
80
  # Create a Gradio app with Blocks
@@ -83,7 +134,11 @@ with gr.Blocks(css=custom_css) as demo:
83
 
84
  # HTML output with simulated image and iframe
85
  html_output = gr.HTML(
86
- value=html_template,
 
 
 
 
87
  label="Output"
88
  )
89
 
@@ -93,18 +148,30 @@ with gr.Blocks(css=custom_css) as demo:
93
  label="Enter your command"
94
  )
95
 
 
 
 
 
 
 
96
  # Update button
97
  update_btn = gr.Button("Let's go!")
98
 
99
  # Connect the components
100
  update_btn.click(
101
- fn=update_placeholder_text,
102
- inputs=None,
 
 
 
 
 
 
 
103
  outputs=[html_output]
104
  )
105
 
106
- demo.load(update_placeholder_text, None, html_output)
107
-
108
  # Launch the app
109
  if __name__ == "__main__":
110
  demo.launch()
 
2
  import os
3
  from e2b_desktop import Sandbox
4
 
 
5
  E2B_API_KEY = os.getenv("E2B_API_KEY")
 
6
  SANDBOXES = {}
7
  TMP_DIR = './tmp/'
8
  if not os.path.exists(TMP_DIR):
9
  os.makedirs(TMP_DIR)
10
 
 
11
  custom_css = """
12
+ /* Your existing CSS */
13
  .sandbox-outer-wrapper {
14
  display: flex;
15
  justify-content: center;
16
  width: 100%;
17
  padding: 20px 0;
18
+ overflow: hidden;
19
  }
20
 
21
  .sandbox-container {
22
  position: relative;
23
  width: 800px;
24
  height: 500px;
25
+ flex-shrink: 0;
26
  overflow: hidden;
27
  }
28
 
 
45
  transform-origin: 0 0;
46
  transform: scale(0.392);
47
  }
48
+
49
+ /* Status indicator light */
50
+ .status-indicator {
51
+ position: absolute;
52
+ bottom: 25.5%;
53
+ left: 26.7%;
54
+ width: 15px;
55
+ height: 15px;
56
+ border-radius: 50%;
57
+ border: 2px solid black;
58
+ z-index: 100;
59
+ }
60
+
61
+ .status-text {
62
+ position: absolute;
63
+ bottom: 25.0%;
64
+ left: 28.6%;
65
+ font-size: 12px;
66
+ font-weight: bold;
67
+ color: black;
68
+ background-color: white;
69
+ padding: 0px 2px;
70
+ border-radius: 3px;
71
+ border: 2px solid black;
72
+ text-shadow: none;
73
+ z-index: 100;
74
+ }
75
+
76
+ .status-interactive {
77
+ background-color: #2ecc71;
78
+ animation: blink 2s infinite;
79
+ }
80
+
81
+ .status-view-only {
82
+ background-color: #e74c3c;
83
+ }
84
+
85
+ @keyframes blink {
86
+ 0% { background-color: rgba(46, 204, 113, 1); } /* Green at full opacity */
87
+ 50% { background-color: rgba(46, 204, 113, 0.4); } /* Green at 40% opacity */
88
+ 100% { background-color: rgba(46, 204, 113, 1); } /* Green at full opacity */
89
+ }
90
  """
91
 
92
  html_template = """
93
  <div class="sandbox-outer-wrapper">
94
  <div class="sandbox-container">
95
  <img src="https://huggingface.co/datasets/lvwerra/admin/resolve/main/desktop_scaled.png" class="sandbox-background" />
96
+ <div class="status-text">{status_text}</div>
97
+ <div class="status-indicator {status_class}"></div>
98
  <iframe
99
  src="{stream_url}"
100
  class="sandbox-iframe"
 
103
  </div>
104
  </div>"""
105
 
106
+ def update_html(interactive_mode, request: gr.Request):
107
+ if request.session_hash not in SANDBOXES: # str necessary to run locally when hash is None
108
+ print("No sandbox found, creating new one", request.session_hash)
109
+ desktop = Sandbox(api_key=E2B_API_KEY, resolution=(1024, 768), dpi=96)
110
+ desktop.stream.start(require_auth=True)
111
+ SANDBOXES[request.session_hash] = desktop
112
 
113
+ desktop = SANDBOXES[request.session_hash]
 
114
  auth_key = desktop.stream.get_auth_key()
 
115
 
116
+ # Add view_only parameter based on interactive_mode
117
+ base_url = desktop.stream.get_url(auth_key=auth_key)
118
+ stream_url = base_url if interactive_mode else f"{base_url}&view_only=true"
119
+
120
+ # Set status indicator class and text
121
+ status_class = "status-interactive" if interactive_mode else "status-view-only"
122
+ status_text = "Interactive" if interactive_mode else "View Only"
123
+
124
+ html_content = html_template.format(
125
+ stream_url=stream_url,
126
+ status_class=status_class,
127
+ status_text=status_text
128
+ )
129
  return html_content
130
 
131
  # Create a Gradio app with Blocks
 
134
 
135
  # HTML output with simulated image and iframe
136
  html_output = gr.HTML(
137
+ value=html_template.format(
138
+ stream_url="",
139
+ status_class="status-interactive",
140
+ status_text="Interactive"
141
+ ),
142
  label="Output"
143
  )
144
 
 
148
  label="Enter your command"
149
  )
150
 
151
+ # Interactive mode checkbox
152
+ interactive_mode = gr.Checkbox(
153
+ value=True,
154
+ label="Interactive Mode"
155
+ )
156
+
157
  # Update button
158
  update_btn = gr.Button("Let's go!")
159
 
160
  # Connect the components
161
  update_btn.click(
162
+ fn=update_html,
163
+ inputs=[interactive_mode],
164
+ outputs=[html_output]
165
+ )
166
+
167
+ # Also update when interactive mode changes
168
+ interactive_mode.change(
169
+ fn=update_html,
170
+ inputs=[interactive_mode],
171
  outputs=[html_output]
172
  )
173
 
174
+ demo.load(update_html, [interactive_mode], html_output)
 
175
  # Launch the app
176
  if __name__ == "__main__":
177
  demo.launch()