JoFrost commited on
Commit
e40d2f9
1 Parent(s): be2352b

chore: restore demo temporarily

Browse files
Files changed (1) hide show
  1. app.py +153 -9
app.py CHANGED
@@ -4,17 +4,161 @@ import requests
4
  import uuid
5
  import os
6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
  title = """
8
  <div align="center">
9
  <h1>🌊 Welcome to LaVague</h1>
10
- <p>This space provides a managed instance of <a href="https://github.com/lavague-ai/LaVague">LaVague</a>, an open-source Large Action Model (LAM) framework allowing users to write queries in natural language to interact with a website by automatically generating Selenium code.</p>
11
- <p><b> ⚠️Update 03/30/2024:</b> After a few days of activity, we have decided to close this Space. We noticed several prompt injection attempts on our server that could crash this space. Though we provided an initial sandboxing, the effort to have a usable and safe environment for users would be too much</p>.
12
- <p>However, you can easily run for free LaVague on a <a href="https://colab.research.google.com/github/lavague-ai/lavague/blob/main/docs/docs/get-started/quick-tour.ipynb">Google Colab</a> or run in on your machine with our <a href="https://docs.lavague.ai/en/latest/docs/get-started/quick-tour/">Quick Tour</a></p>.
13
  </div>
14
  """
15
- with gr.Blocks() as demo:
16
- with gr.Tab("LaVague"):
17
- with gr.Row():
18
- gr.HTML(title)
19
-
20
- demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
  import uuid
5
  import os
6
 
7
+ SERVER_URL = "https://lavague.mithrilsecurity.io"
8
+
9
+ piwik_header = '''
10
+ <!-- Google Tag Manager -->
11
+ <script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
12
+ new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
13
+ j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
14
+ 'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
15
+ })(window,document,'script','dataLayer','GTM-TVD93MF');</script>
16
+ <!-- End Google Tag Manager -->
17
+ <html lang="en">
18
+ '''
19
+
20
+ piwik_footer = '''
21
+ <!-- Google Tag Manager (noscript) -->
22
+ <noscript><iframe src="https://www.googletagmanager.com/ns.html?id=GTM-TVD93MF"
23
+ height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
24
+ <!-- End Google Tag Manager (noscript) -->
25
+ '''
26
+
27
  title = """
28
  <div align="center">
29
  <h1>🌊 Welcome to LaVague</h1>
30
+ <p>Redefining internet surfing by transforming natural language instructions into seamless browser interactions.</p>
 
 
31
  </div>
32
  """
33
+
34
+ # action_engine = ActionEngine(llm, embedder)
35
+
36
+ def exec_code_req(url, query, user_id):
37
+ headers = {
38
+ "X-User-ID": user_id, # Include the X-User-ID header for authentication
39
+ "X-API-Key": os.environ['API_KEY']
40
+ }
41
+ try:
42
+ response = requests.post(SERVER_URL + "/execute_req", json={"url": url, "query": query}, headers=headers)
43
+ if response.status_code == 200:
44
+ return response.json()
45
+ else:
46
+ return {"error": f"Failed with status code {response.status_code}"}
47
+ except requests.RequestException as e:
48
+ return {"error": str(e)}
49
+
50
+ def get_screenshot_req(url, user_id):
51
+ headers = {
52
+ "X-User-ID": user_id, # Include the X-User-ID header for authentication
53
+ "X-API-Key": os.environ['API_KEY']
54
+ }
55
+ try:
56
+ response = requests.get(SERVER_URL + "/screenshot", params={"url": url}, headers=headers)
57
+ if response.status_code == 200:
58
+ return response.json()
59
+ else:
60
+ return {"error": f"Failed with status code {response.status_code}"}
61
+ except requests.RequestException as e:
62
+ return {"error": str(e)}
63
+
64
+ def process_url(url, user_id):
65
+ if user_id == "":
66
+ user_id = str(uuid.uuid4())
67
+ r = get_screenshot_req(url, user_id)
68
+ f = open("screenshot.png", "wb")
69
+ scr = base64.b64decode(r["result"])
70
+ f.write(scr)
71
+ return "screenshot.png", user_id
72
+
73
+ def exec_code(code, source_nodes, full_code, url, query, user_id):
74
+ html = ""
75
+ url_base = url
76
+ try:
77
+ r = exec_code_req(url, query, user_id)
78
+ url = r["url"]
79
+ html = r["html"]
80
+ code = r["code"]
81
+ source_nodes = r["source_nodes"]
82
+ err = r["err"]
83
+ if r["result"] == True:
84
+ output = "Successful code execution"
85
+ status = """<p style="color: green; font-size: 20px; font-weight: bold;">Success!</p>"""
86
+ else:
87
+ output = f"Error in code execution: {err}"
88
+ status = """<p style="color: red; font-size: 20px; font-weight: bold;">Failure! Open the Debug tab for more information</p>"""
89
+ full_code += code
90
+ try:
91
+ tel = requests.post('https://telemetrylavague.mithrilsecurity.io/send_data', json={"code_produced": code, "llm": "NousResearch/Nous-Hermes-2-Mixtral-8x7B-DPO", "screenshot": "", "url": url_base, "html_code": "", "query": query, "nodes": "", "user_id": "", "origin": "HF-Space", "success": str(r["result"])})
92
+ except Exception:
93
+ pass
94
+ except Exception as e:
95
+ output = f"Error in code execution: {str(e)}"
96
+ status = """<p style="color: red; font-size: 20px; font-weight: bold;">Failure! Open the Debug tab for more information</p>"""
97
+ return output, code, html, status, full_code, url, source_nodes
98
+
99
+ def update_image_display(img, url, user_id):
100
+ r = get_screenshot_req(url, user_id)
101
+ f = open("screenshot.png", "wb")
102
+ scr = base64.b64decode(r["result"])
103
+ f.write(scr)
104
+ return "screenshot.png", url
105
+
106
+ def show_processing_message(user_id):
107
+ if user_id == "":
108
+ user_id = str(uuid.uuid4())
109
+ return "Processing...", user_id
110
+
111
+ def create_demo(base_url, instructions):
112
+ gr.HTML(piwik_header)
113
+ with gr.Blocks() as demo:
114
+ with gr.Tab("LaVague"):
115
+ with gr.Row():
116
+ gr.HTML(title)
117
+ with gr.Row():
118
+ user_id = gr.Textbox(value="", label="User ID", interactive=False, visible=False)
119
+ with gr.Row():
120
+ url_input = gr.Textbox(value=base_url, label="Enter URL and press 'Enter' to load the page.")
121
+
122
+ with gr.Row():
123
+ with gr.Column(scale=7):
124
+ image_display = gr.Image(label="Browser", interactive=False)
125
+
126
+ with gr.Column(scale=3):
127
+ with gr.Accordion(label="Full code", open=False):
128
+ full_code = gr.Code(value="", language="python", interactive=False)
129
+ code_display = gr.Code(label="Generated code", language="python",
130
+ lines=5, interactive=True)
131
+
132
+ status_html = gr.HTML()
133
+ with gr.Row():
134
+ with gr.Column(scale=8):
135
+ text_area = gr.Textbox(label="Enter instructions and press 'Enter' to generate code.")
136
+ gr.Examples(examples=instructions, inputs=text_area)
137
+ with gr.Tab("Debug"):
138
+ with gr.Row():
139
+ with gr.Column():
140
+ log_display = gr.Textbox(interactive=False, lines=20)
141
+ with gr.Column():
142
+ source_display = gr.Code(language="html", label="Retrieved nodes", interactive=False, lines=20)
143
+ with gr.Row():
144
+ with gr.Accordion(label="Full HTML", open=False):
145
+ full_html = gr.Code(language="html", label="Full HTML", interactive=False, lines=20)
146
+
147
+ # Linking components
148
+ url_input.submit(process_url, inputs=[url_input, user_id], outputs=[image_display, user_id], queue=False)
149
+ text_area.submit(show_processing_message, inputs=[user_id], outputs=[status_html, user_id], queue=False).then(
150
+ exec_code, inputs=[code_display, source_display, full_code, url_input, text_area, user_id],
151
+ outputs=[log_display, code_display, full_html, status_html, full_code, url_input, source_display], queue=False
152
+ ).then(
153
+ update_image_display, inputs=[image_display, url_input, user_id], outputs=[image_display, url_input], queue=False
154
+ )
155
+ gr.HTML(piwik_footer)
156
+ demo.launch(share=True, debug=True)
157
+
158
+ base_url = "https://huggingface.co/"
159
+
160
+ instructions = ["Click on the Models item on the menu",
161
+ "Click on the search bar 'Filter by name', type 'The Stack', and press 'Enter'",
162
+ "Scroll by 500 pixels",]
163
+
164
+ create_demo(base_url, instructions)