Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,6 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
|
| 3 |
-
from playwright.
|
| 4 |
import json
|
| 5 |
|
| 6 |
# Define model and inference parameters
|
|
@@ -22,41 +22,41 @@ def generate_actions(input_text):
|
|
| 22 |
actions = response.split("\n")
|
| 23 |
return actions
|
| 24 |
|
| 25 |
-
# Function to initialize browser and page
|
| 26 |
-
def initialize_browser():
|
| 27 |
-
with
|
| 28 |
-
browser = p.chromium.launch()
|
| 29 |
-
page = browser.new_page()
|
| 30 |
return browser, page
|
| 31 |
|
| 32 |
-
# Gradio interface
|
| 33 |
-
def run_agent(input_text):
|
| 34 |
-
with
|
| 35 |
-
browser, page = initialize_browser()
|
| 36 |
actions = generate_actions(input_text)
|
| 37 |
|
| 38 |
for action in actions:
|
| 39 |
if "open website" in action:
|
| 40 |
website = action.split(" ")[-1]
|
| 41 |
-
page.goto(website)
|
| 42 |
elif "click" in action:
|
| 43 |
selector = action.split(" ")[-1]
|
| 44 |
-
page.click(selector)
|
| 45 |
elif "type" in action:
|
| 46 |
text = action.split(" ")[-1]
|
| 47 |
-
page.type(text)
|
| 48 |
elif "submit" in action:
|
| 49 |
-
page.press("Enter")
|
| 50 |
else:
|
| 51 |
print(f"Action not recognized: {action}")
|
| 52 |
|
| 53 |
return f"Successfully executed actions based on: {input_text}"
|
| 54 |
|
| 55 |
iface = gr.Interface(
|
| 56 |
-
fn=run_agent,
|
| 57 |
inputs=gr.Textbox(label="Enter your request"),
|
| 58 |
outputs=gr.Textbox(label="Response"),
|
| 59 |
title="Automated Agent",
|
| 60 |
description="Enter a task or instruction for the agent to perform."
|
| 61 |
)
|
| 62 |
-
iface.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
|
| 3 |
+
from playwright.async_api import async_playwright
|
| 4 |
import json
|
| 5 |
|
| 6 |
# Define model and inference parameters
|
|
|
|
| 22 |
actions = response.split("\n")
|
| 23 |
return actions
|
| 24 |
|
| 25 |
+
# Function to initialize browser and page (now asynchronous)
|
| 26 |
+
async def initialize_browser():
|
| 27 |
+
async with async_playwright() as p:
|
| 28 |
+
browser = await p.chromium.launch()
|
| 29 |
+
page = await browser.new_page()
|
| 30 |
return browser, page
|
| 31 |
|
| 32 |
+
# Gradio interface (now using the asynchronous function)
|
| 33 |
+
async def run_agent(input_text):
|
| 34 |
+
async with async_playwright() as p:
|
| 35 |
+
browser, page = await initialize_browser()
|
| 36 |
actions = generate_actions(input_text)
|
| 37 |
|
| 38 |
for action in actions:
|
| 39 |
if "open website" in action:
|
| 40 |
website = action.split(" ")[-1]
|
| 41 |
+
await page.goto(website)
|
| 42 |
elif "click" in action:
|
| 43 |
selector = action.split(" ")[-1]
|
| 44 |
+
await page.click(selector)
|
| 45 |
elif "type" in action:
|
| 46 |
text = action.split(" ")[-1]
|
| 47 |
+
await page.type(text)
|
| 48 |
elif "submit" in action:
|
| 49 |
+
await page.press("Enter")
|
| 50 |
else:
|
| 51 |
print(f"Action not recognized: {action}")
|
| 52 |
|
| 53 |
return f"Successfully executed actions based on: {input_text}"
|
| 54 |
|
| 55 |
iface = gr.Interface(
|
| 56 |
+
fn=run_agent, # Pass the async function directly
|
| 57 |
inputs=gr.Textbox(label="Enter your request"),
|
| 58 |
outputs=gr.Textbox(label="Response"),
|
| 59 |
title="Automated Agent",
|
| 60 |
description="Enter a task or instruction for the agent to perform."
|
| 61 |
)
|
| 62 |
+
iface.launch(share=True) # Enable sharing
|