soiz commited on
Commit
602cdfd
·
verified ·
1 Parent(s): 5a6b765

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -9
app.py CHANGED
@@ -1,9 +1,11 @@
1
- import gradio as gr
2
  from selenium import webdriver
3
  from selenium.common.exceptions import WebDriverException
4
  from PIL import Image
5
  from io import BytesIO
6
 
 
 
7
  def take_screenshot(url):
8
  options = webdriver.ChromeOptions()
9
  options.add_argument('--headless')
@@ -17,6 +19,7 @@ def take_screenshot(url):
17
  wd.implicitly_wait(10)
18
  screenshot = wd.get_screenshot_as_png()
19
  except WebDriverException as e:
 
20
  return Image.new('RGB', (1, 1))
21
  finally:
22
  if wd:
@@ -24,12 +27,21 @@ def take_screenshot(url):
24
 
25
  return Image.open(BytesIO(screenshot))
26
 
27
- iface = gr.Interface(
28
- fn=take_screenshot,
29
- inputs=gr.Textbox(label="Website URL", value="https://kargaranamir.github.io"),
30
- outputs=gr.Image(type="pil", label="Screenshot", height=360, width=540), # Adjust the image size here
31
- title="Website Screenshot",
32
- description="Take a screenshot of a website."
33
- )
 
 
 
 
 
 
 
 
34
 
35
- iface.launch()
 
 
1
+ from flask import Flask, request, send_file
2
  from selenium import webdriver
3
  from selenium.common.exceptions import WebDriverException
4
  from PIL import Image
5
  from io import BytesIO
6
 
7
+ app = Flask(__name__)
8
+
9
  def take_screenshot(url):
10
  options = webdriver.ChromeOptions()
11
  options.add_argument('--headless')
 
19
  wd.implicitly_wait(10)
20
  screenshot = wd.get_screenshot_as_png()
21
  except WebDriverException as e:
22
+ # If there's an error, return a blank image as a placeholder
23
  return Image.new('RGB', (1, 1))
24
  finally:
25
  if wd:
 
27
 
28
  return Image.open(BytesIO(screenshot))
29
 
30
+ @app.route('/screenshot', methods=['GET'])
31
+ def screenshot():
32
+ url = request.args.get('url')
33
+ if not url:
34
+ return "URL parameter is required.", 400
35
+
36
+ # Take the screenshot of the provided URL
37
+ image = take_screenshot(url)
38
+
39
+ # Save the screenshot to an in-memory file and return as response
40
+ img_io = BytesIO()
41
+ image.save(img_io, 'PNG')
42
+ img_io.seek(0)
43
+
44
+ return send_file(img_io, mimetype='image/png')
45
 
46
+ if __name__ == "__main__":
47
+ app.run(host="0.0.0.0", port=7860)