Spaces:
Paused
Paused
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import httpx
|
| 3 |
+
from PIL import Image
|
| 4 |
+
from io import BytesIO
|
| 5 |
+
import os
|
| 6 |
+
|
| 7 |
+
SCRAPFLY_API_KEY = os.getenv("SCRAPFLY_API_KEY") # Use HF Spaces secrets
|
| 8 |
+
|
| 9 |
+
client = httpx.Client(
|
| 10 |
+
timeout=60,
|
| 11 |
+
headers={'accept-encoding': 'gzip'},
|
| 12 |
+
params={"key": SCRAPFLY_API_KEY}
|
| 13 |
+
)
|
| 14 |
+
|
| 15 |
+
def take_screenshot(url, capture="fullpage"):
|
| 16 |
+
try:
|
| 17 |
+
response = client.get(
|
| 18 |
+
'https://api.scrapfly.io/screenshot',
|
| 19 |
+
params={"url": url, "capture": capture}
|
| 20 |
+
)
|
| 21 |
+
response.raise_for_status()
|
| 22 |
+
img = Image.open(BytesIO(response.content))
|
| 23 |
+
return img
|
| 24 |
+
except Exception as e:
|
| 25 |
+
return f"Error: {e}"
|
| 26 |
+
|
| 27 |
+
demo = gr.Interface(
|
| 28 |
+
fn=take_screenshot,
|
| 29 |
+
inputs=[
|
| 30 |
+
gr.Textbox(label="Enter URL", placeholder="https://example.com"),
|
| 31 |
+
gr.Radio(["fullpage", "viewport"], label="Capture Type", value="fullpage")
|
| 32 |
+
],
|
| 33 |
+
outputs=gr.Image(type="pil", label="Screenshot"),
|
| 34 |
+
title="Website Screenshot Tool",
|
| 35 |
+
description="Takes screenshots of any website using ScrapFly API."
|
| 36 |
+
)
|
| 37 |
+
|
| 38 |
+
if __name__ == "__main__":
|
| 39 |
+
demo.launch()
|