browser-gradio / app.py
broadfield-dev's picture
Update app.py
d74e8cc verified
raw
history blame
9.06 kB
import os
os.system("playwright install")
# app.py (Final, Working Async Version with Bugfixes)
import gradio as gr
from playwright.async_api import async_playwright, Error as PlaywrightError
from bs4 import BeautifulSoup
import urllib.parse
import os
from itertools import cycle
import uuid
# --- 1. GLOBAL RESOURCES & STATE ---
# These are initialized on the first request to be compatible with Spaces.
P = None
BROWSER = None
REVOLVER = None
LIVE_CONTEXTS = {} # { tab_id: { "context": PlaywrightContext, "page": PlaywrightPage } }
APP_STARTED = False
# --- 2. PLAIN DATA STATE CLASSES (Unchanged) ---
class TabState:
def __init__(self, tab_id, proxy_used="Direct Connection"):
self.id, self.url, self.title = tab_id, "about:blank", "New Tab"
self.parsed_text, self.links, self.proxy_used = "Welcome!", [], proxy_used
class BrowserState:
def __init__(self): self.tabs, self.active_tab_id = [], None
def get_active_tab(self): return next((t for t in self.tabs if t.id == self.active_tab_id), None)
class CredentialRevolver:
def __init__(self, proxy_string: str):
self.proxies = self._parse_proxies(proxy_string)
if self.proxies: self.proxy_cycler = cycle(self.proxies)
else: self.proxy_cycler = None
def _parse_proxies(self, proxy_string: str):
proxies = [];
for line in proxy_string.strip().splitlines():
try: parsed = urllib.parse.urlparse(f"//{line.strip()}"); server = f"{parsed.scheme or 'http'}://{parsed.hostname}:{parsed.port}"; proxies.append({"server": server, "username": parsed.username, "password": parsed.password})
except: pass
return proxies
def get_next(self): return next(self.proxy_cycler) if self.proxy_cycler else None
def count(self): return len(self.proxies)
# --- 3. ASYNC LOGIC (Unchanged) ---
async def _fetch_and_update_tab_state(tab_state: TabState, url: str):
log = f"▶️ Navigating to {url}..."; live_page = LIVE_CONTEXTS[tab_state.id]["page"]
try:
await live_page.goto(url, wait_until='domcontentloaded', timeout=30000)
tab_state.url = live_page.url; tab_state.title = await live_page.title() or "No Title"
log += f"\n✅ Arrived at: {tab_state.url}"
html_content = await live_page.content(); soup = BeautifulSoup(html_content, 'lxml')
for script in soup(["script", "style", "nav", "footer"]): script.extract()
tab_state.parsed_text = soup.get_text(separator='\n', strip=True)
tab_state.links = [{'text': link.get_text(strip=True) or "[No Link Text]", 'url': urllib.parse.urljoin(tab_state.url, link['href'])} for link in soup.find_all('a', href=True) if link.get('href', '').startswith('http')]
log += f"\n🔗 Found {len(tab_state.links)} links."
except PlaywrightError as e:
error_message = str(e); tab_state.title = "Error"; tab_state.url = url
tab_state.parsed_text = f"❌ Failed to load page.\n\nError: {error_message}"
tab_state.links = []; log += f"\n❌ {error_message}"
return log
async def handle_action(browser_state: BrowserState, action: str, value=None):
log = ""; active_tab_state = browser_state.get_active_tab()
if action == "new_tab":
tab_id, proxy_config = str(uuid.uuid4()), REVOLVER.get_next()
context = await BROWSER.new_context(proxy=proxy_config)
page = await context.new_page()
LIVE_CONTEXTS[tab_id] = {"context": context, "page": page}
new_tab = TabState(tab_id, proxy_used=proxy_config['server'] if proxy_config else "Direct")
browser_state.tabs.append(new_tab); browser_state.active_tab_id = tab_id
log = await _fetch_and_update_tab_state(new_tab, "https://www.whatsmyip.org/")
elif action == "go" and active_tab_state:
url = value if (urllib.parse.urlparse(value).scheme and urllib.parse.urlparse(value).netloc) else f"https://duckduckgo.com/html/?q={urllib.parse.quote_plus(value)}"
log = await _fetch_and_update_tab_state(active_tab_state, url)
elif action == "click" and active_tab_state and value is not None:
try:
link_index = int(value)
if 0 <= link_index < len(active_tab_state.links): log = await _fetch_and_update_tab_state(active_tab_state, active_tab_state.links[link_index]['url'])
else: log = "Invalid link number."
except: log = "Please enter a valid number to click."
elif action == "close_tab" and active_tab_state:
if len(browser_state.tabs) > 1:
tab_to_close_id = browser_state.active_tab_id; tab_index = browser_state.tabs.index(active_tab_state)
browser_state.tabs.pop(tab_index); new_index = tab_index - 1 if tab_index > 0 else 0
browser_state.active_tab_id = browser_state.tabs[new_index].id
resources = LIVE_CONTEXTS.pop(tab_to_close_id); await resources['context'].close(); log = f"💣 Tab closed."
else: log = "Cannot close the last tab."
elif action == "switch_tab" and value is not None:
browser_state.active_tab_id = value; log = f"Switched to tab."
return browser_state, log
# --- 4. GRADIO UI AND EVENT HANDLING (WITH FIXES) ---
with gr.Blocks(theme=gr.themes.Soft(), title="Real Browser Demo") as demo:
browser_state = gr.State(BrowserState())
gr.Markdown("# 🛰️ Real Browser Demo (Final Working Version)")
# UI Layout is the same...
with gr.Row():
with gr.Column(scale=3):
url_textbox = gr.Textbox(label="URL or Search Term", interactive=True); go_btn = gr.Button("Go", variant="primary")
with gr.Accordion("Page Content (Text Only)", open=True): page_content = gr.Markdown("Loading...")
log_display = gr.Textbox(label="Status Log", interactive=False)
with gr.Column(scale=1):
with gr.Row(): new_tab_btn = gr.Button("➕ New Tab"); close_tab_btn = gr.Button("❌ Close Tab")
tab_selector = gr.Radio(choices=[], label="Active Tabs", interactive=True)
with gr.Accordion("Clickable Links", open=True):
links_display = gr.Markdown("...");
with gr.Row(): click_num_box = gr.Number(label="Link #", scale=1, minimum=0, step=1); click_btn = gr.Button("Click Link", scale=2)
# This order must be consistent
all_outputs = [page_content, url_textbox, links_display, tab_selector, log_display]
async def master_handler(current_state, action, value=None):
global APP_STARTED, P, BROWSER, REVOLVER
if not APP_STARTED:
print("🚀 First request received, starting up Playwright...");
P = await async_playwright().start(); BROWSER = await P.firefox.launch(headless=True)
proxy_list_str = os.getenv("PROXY_LIST", ""); REVOLVER = CredentialRevolver(proxy_list_str)
print(f"✅ Playwright started. {REVOLVER.count()} proxies loaded."); APP_STARTED = True
new_state, log = await handle_action(current_state, action, value)
ui_updates = update_ui_components(new_state)
# **THE CRITICAL FIX IS HERE:**
# We must return a tuple with a value for EACH output component, in the correct order.
return (
new_state, # 1. For the browser_state output
ui_updates[page_content], # 2.
ui_updates[url_textbox], # 3.
ui_updates[links_display], # 4.
ui_updates[tab_selector], # 5.
ui_updates[log_display] # 6.
)
# **THE SECOND CRITICAL FIX IS HERE:**
# Each event listener is a simple, separate async function that Gradio can correctly await.
async def on_load(state):
return await master_handler(state, "new_tab", None)
async def on_go_click(state, value):
return await master_handler(state, "go", value)
async def on_click_link(state, value):
return await master_handler(state, "click", value)
async def on_new_tab(state):
return await master_handler(state, "new_tab", None)
async def on_close_tab(state):
return await master_handler(state, "close_tab", None)
async def on_switch_tab(state, value):
return await master_handler(state, "switch_tab", value)
# Wire up the new, clean event handlers
demo.load(on_load, inputs=[browser_state], outputs=[browser_state, *all_outputs])
go_btn.click(on_go_click, [browser_state, url_textbox], [browser_state, *all_outputs], show_progress="full")
url_textbox.submit(on_go_click, [browser_state, url_textbox], [browser_state, *all_outputs], show_progress="full")
click_btn.click(on_click_link, [browser_state, click_num_box], [browser_state, *all_outputs], show_progress="full")
new_tab_btn.click(on_new_tab, [browser_state], [browser_state, *all_outputs], show_progress="full")
close_tab_btn.click(on_close_tab, [browser_state], [browser_state, *all_outputs])
tab_selector.input(on_switch_tab, [browser_state, tab_selector], [browser_state, *all_outputs])
demo.launch()