dhuynh95 commited on
Commit
4ff59a7
1 Parent(s): 356e569

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +4 -145
app.py CHANGED
@@ -4,163 +4,22 @@ import requests
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>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>
31
- <p><b>Note:</b> In this space, we use a Nous-Hermes-2-Mixtral-8x7B-DPO to generate the Selenium code required to perform users’ requested actions. While decent, this model might fail to generate the appropriate code (hence this initiative to collect a good dataset), which could simply crash, not perform anything, or perform the wrong action</p>.
32
- <p>For a more stable experience we recommend you to test our <a href="https://docs.lavague.ai/en/latest/docs/get-started/quick-tour/">Quick Tour</a> on the docs, with for instance a demo running on <a href="https://colab.research.google.com/github/lavague-ai/lavague/blob/main/docs/docs/get-started/quick-tour.ipynb">Gradio</a></p>.
33
  </div>
34
  """
35
 
36
- # action_engine = ActionEngine(llm, embedder)
37
-
38
- def exec_code_req(url, query, user_id):
39
- headers = {
40
- "X-User-ID": user_id, # Include the X-User-ID header for authentication
41
- "X-API-Key": os.environ['API_KEY']
42
- }
43
- try:
44
- response = requests.post(SERVER_URL + "/execute_req", json={"url": url, "query": query}, headers=headers)
45
- if response.status_code == 200:
46
- return response.json()
47
- else:
48
- return {"error": f"Failed with status code {response.status_code}"}
49
- except requests.RequestException as e:
50
- return {"error": str(e)}
51
-
52
- def get_screenshot_req(url, user_id):
53
- headers = {
54
- "X-User-ID": user_id, # Include the X-User-ID header for authentication
55
- "X-API-Key": os.environ['API_KEY']
56
- }
57
- try:
58
- response = requests.get(SERVER_URL + "/screenshot", params={"url": url}, headers=headers)
59
- if response.status_code == 200:
60
- return response.json()
61
- else:
62
- return {"error": f"Failed with status code {response.status_code}"}
63
- except requests.RequestException as e:
64
- return {"error": str(e)}
65
-
66
- def process_url(url, user_id):
67
- if user_id == "":
68
- user_id = str(uuid.uuid4())
69
- r = get_screenshot_req(url, user_id)
70
- f = open("screenshot.png", "wb")
71
- scr = base64.b64decode(r["result"])
72
- f.write(scr)
73
- return "screenshot.png", user_id
74
-
75
- def exec_code(code, source_nodes, full_code, url, query, user_id):
76
- html = ""
77
- url_base = url
78
- try:
79
- r = exec_code_req(url, query, user_id)
80
- url = r["url"]
81
- html = r["html"]
82
- code = r["code"]
83
- source_nodes = r["source_nodes"]
84
- err = r["err"]
85
- if r["result"] == True:
86
- output = "Successful code execution"
87
- status = """<p style="color: green; font-size: 20px; font-weight: bold;">Success!</p>"""
88
- else:
89
- output = f"Error in code execution: {err}"
90
- status = """<p style="color: red; font-size: 20px; font-weight: bold;">Failure! Open the Debug tab for more information</p>"""
91
- full_code += code
92
- try:
93
- 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"])})
94
- except Exception:
95
- pass
96
- except Exception as e:
97
- output = f"Error in code execution: {str(e)}"
98
- status = """<p style="color: red; font-size: 20px; font-weight: bold;">Failure! Open the Debug tab for more information</p>"""
99
- return output, code, html, status, full_code, url, source_nodes
100
-
101
- def update_image_display(img, url, user_id):
102
- r = get_screenshot_req(url, user_id)
103
- f = open("screenshot.png", "wb")
104
- scr = base64.b64decode(r["result"])
105
- f.write(scr)
106
- return "screenshot.png", url
107
-
108
- def show_processing_message(user_id):
109
- if user_id == "":
110
- user_id = str(uuid.uuid4())
111
- return "Processing...", user_id
112
-
113
- def create_demo(base_url, instructions):
114
  gr.HTML(piwik_header)
115
  with gr.Blocks() as demo:
116
  with gr.Tab("LaVague"):
117
  with gr.Row():
118
  gr.HTML(title)
119
- with gr.Row():
120
- user_id = gr.Textbox(value="", label="User ID", interactive=False, visible=False)
121
- with gr.Row():
122
- url_input = gr.Textbox(value=base_url, label="Enter URL and press 'Enter' to load the page.")
123
 
124
- with gr.Row():
125
- with gr.Column(scale=7):
126
- image_display = gr.Image(label="Browser", interactive=False)
127
-
128
- with gr.Column(scale=3):
129
- with gr.Accordion(label="Full code", open=False):
130
- full_code = gr.Code(value="", language="python", interactive=False)
131
- code_display = gr.Code(label="Generated code", language="python",
132
- lines=5, interactive=True)
133
-
134
- status_html = gr.HTML()
135
- with gr.Row():
136
- with gr.Column(scale=8):
137
- text_area = gr.Textbox(label="Enter instructions and press 'Enter' to generate code.")
138
- gr.Examples(examples=instructions, inputs=text_area)
139
- with gr.Tab("Debug"):
140
- with gr.Row():
141
- with gr.Column():
142
- log_display = gr.Textbox(interactive=False, lines=20)
143
- with gr.Column():
144
- source_display = gr.Code(language="html", label="Retrieved nodes", interactive=False, lines=20)
145
- with gr.Row():
146
- with gr.Accordion(label="Full HTML", open=False):
147
- full_html = gr.Code(language="html", label="Full HTML", interactive=False, lines=20)
148
-
149
- # Linking components
150
- url_input.submit(process_url, inputs=[url_input, user_id], outputs=[image_display, user_id], queue=False)
151
- text_area.submit(show_processing_message, inputs=[user_id], outputs=[status_html, user_id], queue=False).then(
152
- exec_code, inputs=[code_display, source_display, full_code, url_input, text_area, user_id],
153
- outputs=[log_display, code_display, full_html, status_html, full_code, url_input, source_display], queue=False
154
- ).then(
155
- update_image_display, inputs=[image_display, url_input, user_id], outputs=[image_display, url_input], queue=False
156
- )
157
- gr.HTML(piwik_footer)
158
  demo.launch(share=True, debug=True)
159
 
160
- base_url = "https://huggingface.co/"
161
-
162
- instructions = ["Click on the Models item on the menu",
163
- "Click on the search bar 'Filter by name', type 'The Stack', and press 'Enter'",
164
- "Scroll by 500 pixels",]
165
-
166
- create_demo(base_url, instructions)
 
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
 
16
+ def create_demo():
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
  gr.HTML(piwik_header)
18
  with gr.Blocks() as demo:
19
  with gr.Tab("LaVague"):
20
  with gr.Row():
21
  gr.HTML(title)
 
 
 
 
22
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23
  demo.launch(share=True, debug=True)
24
 
25
+ create_demo()