Update app.py
Browse files
app.py
CHANGED
|
@@ -1,34 +1,66 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import requests
|
| 3 |
import concurrent.futures
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
-
def
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
results = []
|
| 14 |
-
|
| 15 |
-
|
|
|
|
| 16 |
for future in concurrent.futures.as_completed(futures):
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
-
return "\n".join(results)
|
| 20 |
|
| 21 |
iface = gr.Interface(
|
| 22 |
fn=open_links,
|
| 23 |
inputs=[
|
| 24 |
gr.Textbox(label="Enter link", placeholder="https://example.com"),
|
| 25 |
-
gr.Slider(minimum=1, maximum=
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
],
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
description="Enter a link and choose how many times to open it. The app will attempt to open the link and return the status for each attempt.",
|
| 30 |
theme="huggingface",
|
| 31 |
-
examples=[["https://www.example.com",
|
| 32 |
)
|
| 33 |
|
| 34 |
iface.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import requests
|
| 3 |
import concurrent.futures
|
| 4 |
+
import time
|
| 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 |
+
chrome_options = Options()
|
| 13 |
+
chrome_options.add_argument("--headless")
|
| 14 |
+
chrome_options.add_argument("--no-sandbox")
|
| 15 |
+
chrome_options.add_argument("--disable-dev-shm-usage")
|
| 16 |
+
|
| 17 |
+
driver = webdriver.Chrome(options=chrome_options)
|
| 18 |
+
driver.get(url)
|
| 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 |
+
response = requests.get(url, timeout=10)
|
| 29 |
+
elapsed_time = time.time() - start_time
|
| 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 |
results = []
|
| 39 |
+
screenshots = []
|
| 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 |
+
return "\n".join(results), screenshots[0] if screenshots else None
|
| 49 |
|
| 50 |
iface = gr.Interface(
|
| 51 |
fn=open_links,
|
| 52 |
inputs=[
|
| 53 |
gr.Textbox(label="Enter link", placeholder="https://example.com"),
|
| 54 |
+
gr.Slider(minimum=1, maximum=10, step=1, value=1, label="Number of times to open")
|
| 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, return the status, response time, content size for each attempt, and display a screenshot of the first successful attempt.",
|
|
|
|
| 62 |
theme="huggingface",
|
| 63 |
+
examples=[["https://www.example.com", 1]]
|
| 64 |
)
|
| 65 |
|
| 66 |
iface.launch()
|