File size: 1,768 Bytes
8fb6e99
562ace5
 
 
93f2924
 
8fb6e99
562ace5
 
 
 
 
 
 
 
8fb6e99
562ace5
93f2924
562ace5
 
93f2924
 
 
562ace5
 
 
8fb6e99
562ace5
 
 
 
 
 
 
8fb6e99
 
562ace5
8fb6e99
afd3cf6
562ace5
93f2924
 
 
 
8fb6e99
93f2924
562ace5
8fb6e99
93f2924
8fb6e99
 
 
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
import gradio as gr
import asyncio
from playwright.async_api import async_playwright
import base64
from PIL import Image
import io

async def capture_screenshot(url):
    async with async_playwright() as p:
        browser = await p.chromium.launch()
        page = await browser.new_page()
        await page.goto(url)
        screenshot = await page.screenshot()
        await browser.close()
        return Image.open(io.BytesIO(screenshot))

async def open_link(url, index):
    try:
        screenshot = await capture_screenshot(url)
        return f"Request {index+1}: Successfully captured screenshot", screenshot
    except Exception as e:
        return f"Request {index+1}: Error: {str(e)}", None

async def open_links(link, count):
    tasks = [open_link(link, i) for i in range(count)]
    results = await asyncio.gather(*tasks)
    
    statuses = [result[0] for result in results]
    screenshots = [result[1] for result in results if result[1] is not None]
    
    return "\n".join(statuses), screenshots[0] if screenshots else None

def gradio_open_links(link, count):
    return asyncio.run(open_links(link, count))

iface = gr.Interface(
    fn=gradio_open_links,
    inputs=[
        gr.Textbox(label="Enter link", placeholder="https://example.com"),
        gr.Slider(minimum=1, maximum=5, step=1, value=1, label="Number of times to open")
    ],
    outputs=[
        gr.Textbox(label="Results"),
        gr.Image(label="Screenshot")
    ],
    title="Link Opener with Screenshot",
    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.",
    theme="huggingface",
    examples=[["https://www.example.com", 1]]
)

iface.launch()