Spaces:
Running
Running
File size: 6,821 Bytes
3be9f56 8042b49 2a0eb38 3be9f56 e40d2f9 3be9f56 e40d2f9 3be9f56 e40d2f9 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
import gradio as gr
import base64
import requests
import uuid
import os
SERVER_URL = "https://lavague.mithrilsecurity.io"
piwik_header = '''
<!-- Google Tag Manager -->
<script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
})(window,document,'script','dataLayer','GTM-TVD93MF');</script>
<!-- End Google Tag Manager -->
<html lang="en">
'''
piwik_footer = '''
<!-- Google Tag Manager (noscript) -->
<noscript><iframe src="https://www.googletagmanager.com/ns.html?id=GTM-TVD93MF"
height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
<!-- End Google Tag Manager (noscript) -->
'''
title = """
<div align="center">
<h1>🌊 Welcome to LaVague</h1>
<p>Redefining internet surfing by transforming natural language instructions into seamless browser interactions.</p>
</div>
"""
# action_engine = ActionEngine(llm, embedder)
def exec_code_req(url, query, user_id):
headers = {
"X-User-ID": user_id, # Include the X-User-ID header for authentication
"X-API-Key": os.environ['API_KEY']
}
try:
response = requests.post(SERVER_URL + "/execute_req", json={"url": url, "query": query}, headers=headers)
if response.status_code == 200:
return response.json()
else:
return {"error": f"Failed with status code {response.status_code}"}
except requests.RequestException as e:
return {"error": str(e)}
def get_screenshot_req(url, user_id):
headers = {
"X-User-ID": user_id, # Include the X-User-ID header for authentication
"X-API-Key": os.environ['API_KEY']
}
try:
response = requests.get(SERVER_URL + "/screenshot", params={"url": url}, headers=headers)
if response.status_code == 200:
return response.json()
else:
return {"error": f"Failed with status code {response.status_code}"}
except requests.RequestException as e:
return {"error": str(e)}
def process_url(url, user_id):
if user_id == "":
user_id = str(uuid.uuid4())
r = get_screenshot_req(url, user_id)
f = open("screenshot.png", "wb")
scr = base64.b64decode(r["result"])
f.write(scr)
return "screenshot.png", user_id
def exec_code(code, source_nodes, full_code, url, query, user_id):
html = ""
url_base = url
try:
r = exec_code_req(url, query, user_id)
url = r["url"]
html = r["html"]
code = r["code"]
source_nodes = r["source_nodes"]
err = r["err"]
if r["result"] == True:
output = "Successful code execution"
status = """<p style="color: green; font-size: 20px; font-weight: bold;">Success!</p>"""
else:
output = f"Error in code execution: {err}"
status = """<p style="color: red; font-size: 20px; font-weight: bold;">Failure! Open the Debug tab for more information</p>"""
full_code += code
try:
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"])})
except Exception:
pass
except Exception as e:
output = f"Error in code execution: {str(e)}"
status = """<p style="color: red; font-size: 20px; font-weight: bold;">Failure! Open the Debug tab for more information</p>"""
return output, code, html, status, full_code, url, source_nodes
def update_image_display(img, url, user_id):
r = get_screenshot_req(url, user_id)
f = open("screenshot.png", "wb")
scr = base64.b64decode(r["result"])
f.write(scr)
return "screenshot.png", url
def show_processing_message(user_id):
if user_id == "":
user_id = str(uuid.uuid4())
return "Processing...", user_id
def create_demo(base_url, instructions):
gr.HTML(piwik_header)
with gr.Blocks() as demo:
with gr.Tab("LaVague"):
with gr.Row():
gr.HTML(title)
with gr.Row():
user_id = gr.Textbox(value="", label="User ID", interactive=False, visible=False)
with gr.Row():
url_input = gr.Textbox(value=base_url, label="Enter URL and press 'Enter' to load the page.")
with gr.Row():
with gr.Column(scale=7):
image_display = gr.Image(label="Browser", interactive=False)
with gr.Column(scale=3):
with gr.Accordion(label="Full code", open=False):
full_code = gr.Code(value="", language="python", interactive=False)
code_display = gr.Code(label="Generated code", language="python",
lines=5, interactive=True)
status_html = gr.HTML()
with gr.Row():
with gr.Column(scale=8):
text_area = gr.Textbox(label="Enter instructions and press 'Enter' to generate code.")
gr.Examples(examples=instructions, inputs=text_area)
with gr.Tab("Debug"):
with gr.Row():
with gr.Column():
log_display = gr.Textbox(interactive=False, lines=20)
with gr.Column():
source_display = gr.Code(language="html", label="Retrieved nodes", interactive=False, lines=20)
with gr.Row():
with gr.Accordion(label="Full HTML", open=False):
full_html = gr.Code(language="html", label="Full HTML", interactive=False, lines=20)
# Linking components
url_input.submit(process_url, inputs=[url_input, user_id], outputs=[image_display, user_id], queue=False)
text_area.submit(show_processing_message, inputs=[user_id], outputs=[status_html, user_id], queue=False).then(
exec_code, inputs=[code_display, source_display, full_code, url_input, text_area, user_id],
outputs=[log_display, code_display, full_html, status_html, full_code, url_input, source_display], queue=False
).then(
update_image_display, inputs=[image_display, url_input, user_id], outputs=[image_display, url_input], queue=False
)
gr.HTML(piwik_footer)
demo.launch(share=True, debug=True)
base_url = "https://huggingface.co/"
instructions = ["Click on the Models item on the menu",
"Click on the search bar 'Filter by name', type 'The Stack', and press 'Enter'",
"Scroll by 500 pixels",]
create_demo(base_url, instructions) |