Samiran757 commited on
Commit
6e543a5
·
verified ·
1 Parent(s): 34d4987

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -0
app.py ADDED
@@ -0,0 +1,47 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI
2
+ import subprocess
3
+ from playwright.sync_api import sync_playwright
4
+ import base64
5
+ import os
6
+
7
+ app = FastAPI()
8
+
9
+ # -------- TERMINAL TOOL --------
10
+ @app.post("/terminal")
11
+ def terminal(cmd: str):
12
+ result = subprocess.run(
13
+ cmd,
14
+ shell=True,
15
+ capture_output=True,
16
+ text=True
17
+ )
18
+ return {
19
+ "stdout": result.stdout,
20
+ "stderr": result.stderr,
21
+ "code": result.returncode
22
+ }
23
+
24
+ # -------- BROWSER TOOL --------
25
+ @app.get("/browse")
26
+ def browse(url: str):
27
+ with sync_playwright() as p:
28
+ browser = p.chromium.launch(headless=True)
29
+ page = browser.new_page()
30
+ page.goto(url, timeout=60000)
31
+ screenshot = page.screenshot()
32
+ browser.close()
33
+
34
+ return {
35
+ "screenshot": base64.b64encode(screenshot).decode()
36
+ }
37
+
38
+ # -------- SCREENSHOT OF SYSTEM --------
39
+ @app.get("/screen")
40
+ def screen():
41
+ os.system("apt-get update && apt-get install -y scrot")
42
+ os.system("scrot screen.png")
43
+
44
+ with open("screen.png","rb") as f:
45
+ return {
46
+ "image": base64.b64encode(f.read()).decode()
47
+ }