Update app.py
Browse files
app.py
CHANGED
|
@@ -1,64 +1,50 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import
|
| 3 |
-
|
| 4 |
-
import
|
| 5 |
-
import os
|
| 6 |
-
from selenium import webdriver
|
| 7 |
-
from selenium.webdriver.chrome.options import Options
|
| 8 |
from PIL import Image
|
| 9 |
import io
|
| 10 |
|
| 11 |
-
def capture_screenshot(url):
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
time.sleep(2) # Wait for the page to load
|
| 20 |
-
screenshot = driver.get_screenshot_as_png()
|
| 21 |
-
driver.quit()
|
| 22 |
-
|
| 23 |
-
return Image.open(io.BytesIO(screenshot))
|
| 24 |
|
| 25 |
-
def open_link(url, index):
|
| 26 |
-
start_time = time.time()
|
| 27 |
try:
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
status = f"Request {index+1}: Status {response.status_code}, Time: {elapsed_time:.2f}s, Size: {len(response.content)} bytes"
|
| 31 |
-
|
| 32 |
-
screenshot = capture_screenshot(url)
|
| 33 |
-
return status, screenshot
|
| 34 |
except Exception as e:
|
| 35 |
return f"Request {index+1}: Error: {str(e)}", None
|
| 36 |
|
| 37 |
-
def open_links(link, count):
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
with concurrent.futures.ThreadPoolExecutor(max_workers=5) as executor:
|
| 41 |
-
futures = [executor.submit(open_link, link, i) for i in range(count)]
|
| 42 |
-
for future in concurrent.futures.as_completed(futures):
|
| 43 |
-
status, screenshot = future.result()
|
| 44 |
-
results.append(status)
|
| 45 |
-
if screenshot:
|
| 46 |
-
screenshots.append(screenshot)
|
| 47 |
|
| 48 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
|
| 50 |
iface = gr.Interface(
|
| 51 |
-
fn=
|
| 52 |
inputs=[
|
| 53 |
gr.Textbox(label="Enter link", placeholder="https://example.com"),
|
| 54 |
-
gr.Slider(minimum=1, maximum=
|
| 55 |
],
|
| 56 |
outputs=[
|
| 57 |
gr.Textbox(label="Results"),
|
| 58 |
gr.Image(label="Screenshot")
|
| 59 |
],
|
| 60 |
title="Link Opener with Screenshot",
|
| 61 |
-
description="Enter a link and choose how many times to open it. The app will attempt to open the link,
|
| 62 |
theme="huggingface",
|
| 63 |
examples=[["https://www.example.com", 1]]
|
| 64 |
)
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import asyncio
|
| 3 |
+
from playwright.async_api import async_playwright
|
| 4 |
+
import base64
|
|
|
|
|
|
|
|
|
|
| 5 |
from PIL import Image
|
| 6 |
import io
|
| 7 |
|
| 8 |
+
async def capture_screenshot(url):
|
| 9 |
+
async with async_playwright() as p:
|
| 10 |
+
browser = await p.chromium.launch()
|
| 11 |
+
page = await browser.new_page()
|
| 12 |
+
await page.goto(url)
|
| 13 |
+
screenshot = await page.screenshot()
|
| 14 |
+
await browser.close()
|
| 15 |
+
return Image.open(io.BytesIO(screenshot))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
+
async def open_link(url, index):
|
|
|
|
| 18 |
try:
|
| 19 |
+
screenshot = await capture_screenshot(url)
|
| 20 |
+
return f"Request {index+1}: Successfully captured screenshot", screenshot
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
except Exception as e:
|
| 22 |
return f"Request {index+1}: Error: {str(e)}", None
|
| 23 |
|
| 24 |
+
async def open_links(link, count):
|
| 25 |
+
tasks = [open_link(link, i) for i in range(count)]
|
| 26 |
+
results = await asyncio.gather(*tasks)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
+
statuses = [result[0] for result in results]
|
| 29 |
+
screenshots = [result[1] for result in results if result[1] is not None]
|
| 30 |
+
|
| 31 |
+
return "\n".join(statuses), screenshots[0] if screenshots else None
|
| 32 |
+
|
| 33 |
+
def gradio_open_links(link, count):
|
| 34 |
+
return asyncio.run(open_links(link, count))
|
| 35 |
|
| 36 |
iface = gr.Interface(
|
| 37 |
+
fn=gradio_open_links,
|
| 38 |
inputs=[
|
| 39 |
gr.Textbox(label="Enter link", placeholder="https://example.com"),
|
| 40 |
+
gr.Slider(minimum=1, maximum=5, step=1, value=1, label="Number of times to open")
|
| 41 |
],
|
| 42 |
outputs=[
|
| 43 |
gr.Textbox(label="Results"),
|
| 44 |
gr.Image(label="Screenshot")
|
| 45 |
],
|
| 46 |
title="Link Opener with Screenshot",
|
| 47 |
+
description="Enter a link and choose how many times to open it. The app will attempt to open the link, capture a screenshot, and display the first successful screenshot.",
|
| 48 |
theme="huggingface",
|
| 49 |
examples=[["https://www.example.com", 1]]
|
| 50 |
)
|