Download api.py from Amoo0206/01: direct link, hf CLI and curl.
- Browser
- Download file 2.46 kB
-
https://huggingface.co/spaces/Amoo0206/01/resolve/main/api.py
- Command line
-
hf download hf://spaces/Amoo0206/01/api.py
-
curl -L -o api.py https://huggingface.co/spaces/Amoo0206/01/resolve/main/api.py
2.46 kB
| from flask import Flask, send_file, request, jsonify | |
| import subprocess | |
| import os | |
| import io | |
| app = Flask(__name__) | |
| DISPLAY = ":1" | |
| def health(): | |
| return jsonify({"status": "ok", "service": "desktop"}) | |
| def screenshot(): | |
| subprocess.run( | |
| ["scrot", "/tmp/screen.png", "--overwrite"], | |
| env={**os.environ, "DISPLAY": DISPLAY}, | |
| check=True, | |
| ) | |
| return send_file("/tmp/screen.png", mimetype="image/png") | |
| def open_url(): | |
| url = request.json.get("url", "") | |
| if not url: | |
| return jsonify({"error": "no url"}), 400 | |
| subprocess.Popen( | |
| [ | |
| "google-chrome", | |
| "--no-sandbox", | |
| "--disable-dev-shm-usage", | |
| "--disable-gpu", | |
| "--disable-software-rasterizer", | |
| "--memory-pressure-off", | |
| "--disable-crash-reporter", | |
| "--no-first-run", | |
| "--disable-notifications", | |
| url, | |
| ], | |
| env={**os.environ, "DISPLAY": DISPLAY}, | |
| ) | |
| return jsonify({"ok": True, "url": url}) | |
| def type_text(): | |
| text = request.json.get("text", "") | |
| subprocess.run( | |
| ["xdotool", "type", "--delay", "30", "--", text], | |
| env={**os.environ, "DISPLAY": DISPLAY}, | |
| ) | |
| return jsonify({"ok": True}) | |
| def click(): | |
| x = int(request.json.get("x", 0)) | |
| y = int(request.json.get("y", 0)) | |
| subprocess.run( | |
| ["xdotool", "mousemove", "--sync", str(x), str(y), "click", "1"], | |
| env={**os.environ, "DISPLAY": DISPLAY}, | |
| ) | |
| return jsonify({"ok": True, "x": x, "y": y}) | |
| def key_press(): | |
| key = request.json.get("key", "") | |
| subprocess.run( | |
| ["xdotool", "key", key], | |
| env={**os.environ, "DISPLAY": DISPLAY}, | |
| ) | |
| return jsonify({"ok": True, "key": key}) | |
| def scroll(): | |
| direction = request.json.get("direction", "down") | |
| amount = int(request.json.get("amount", 3)) | |
| button = "5" if direction == "down" else "4" | |
| for _ in range(amount): | |
| subprocess.run( | |
| ["xdotool", "click", button], | |
| env={**os.environ, "DISPLAY": DISPLAY}, | |
| ) | |
| return jsonify({"ok": True}) | |
| if __name__ == "__main__": | |
| app.run(host="0.0.0.0", port=8080, debug=False) | |