history008 commited on
Commit
231f68b
·
verified ·
1 Parent(s): 5e6fdc8

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +110 -0
app.py ADDED
@@ -0,0 +1,110 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # app.py
2
+ import os
3
+ import json
4
+ import requests
5
+ from flask import Flask, request, jsonify
6
+
7
+ app = Flask(__name__)
8
+
9
+ # ---- API KEYS ----
10
+ SKYVERN_API_KEY = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjQ5MDMwNTkxMjcsInN1YiI6Im9fNDM5ODczMDQ4OTgwNTgyMjQwIn0.IlKt9HFbDg1dqbv2qHxH5aRU4dwn4MFzxHXUNhzL5BA"
11
+ GEMINI_API_KEY = "AIzaSyB5DoxsabUkiUxfEVTcoMuzH-1hfJ0ZLH0" # replace with your actual Gemini key
12
+
13
+ # Skyvern endpoint
14
+ SKYVERN_BASE_URL = "https://api.skyvern.com/v1" # replace with correct Skyvern API base
15
+ # Gemini endpoint
16
+ GEMINI_BASE_URL = "https://api.openai.com/v1beta" # Gemini API base
17
+
18
+ def run_skyvern_task(task_instruction):
19
+ """
20
+ Sends a task to Skyvern API for execution.
21
+ Returns a dict with task status, result, and screenshot URL (if any).
22
+ """
23
+ headers = {
24
+ "Authorization": f"Bearer {SKYVERN_API_KEY}",
25
+ "Content-Type": "application/json"
26
+ }
27
+ payload = {
28
+ "instructions": task_instruction,
29
+ "returnArtifacts": True
30
+ }
31
+ try:
32
+ response = requests.post(f"{SKYVERN_BASE_URL}/tasks", headers=headers, json=payload)
33
+ data = response.json()
34
+ if response.status_code != 200:
35
+ return {"error": data, "success": False}
36
+ # Extract screenshot if available
37
+ screenshot_url = None
38
+ if "artifacts" in data and isinstance(data["artifacts"], list):
39
+ for artifact in data["artifacts"]:
40
+ if artifact.get("type") in ["screenshot", "image"]:
41
+ screenshot_url = artifact.get("url")
42
+ break
43
+ return {
44
+ "success": True,
45
+ "result": data.get("result") or "Task completed",
46
+ "screenshot": screenshot_url,
47
+ "raw": data
48
+ }
49
+ except Exception as e:
50
+ return {"error": str(e), "success": False}
51
+
52
+ def get_gemini_response(prompt_text):
53
+ """
54
+ Sends the user's natural language command to Gemini API to interpret instructions.
55
+ Returns the processed text for Skyvern task.
56
+ """
57
+ headers = {
58
+ "Authorization": f"Bearer {GEMINI_API_KEY}",
59
+ "Content-Type": "application/json"
60
+ }
61
+ payload = {
62
+ "model": "gemini-pro",
63
+ "input": prompt_text
64
+ }
65
+ try:
66
+ response = requests.post(f"{GEMINI_BASE_URL}/generateContent", headers=headers, json=payload)
67
+ data = response.json()
68
+ if response.status_code != 200:
69
+ return {"error": data, "success": False}
70
+ return {"success": True, "text": data.get("outputText") or prompt_text}
71
+ except Exception as e:
72
+ return {"error": str(e), "success": False}
73
+
74
+ @app.route("/execute", methods=["POST"])
75
+ def execute_command():
76
+ """
77
+ Accepts JSON payload: {"command": "user command in natural language"}
78
+ Processes it via Gemini for natural language understanding
79
+ Then sends task to Skyvern
80
+ Returns JSON with result and screenshot URL
81
+ """
82
+ data = request.get_json()
83
+ if not data or "command" not in data:
84
+ return jsonify({"error": "Command is required", "success": False}), 400
85
+
86
+ user_command = data["command"]
87
+
88
+ # 1. Process natural language via Gemini
89
+ gemini_response = get_gemini_response(user_command)
90
+ if not gemini_response.get("success"):
91
+ return jsonify({"error": gemini_response.get("error"), "success": False}), 500
92
+
93
+ processed_command = gemini_response.get("text")
94
+
95
+ # 2. Send processed command to Skyvern
96
+ skyvern_response = run_skyvern_task(processed_command)
97
+ if not skyvern_response.get("success"):
98
+ return jsonify({"error": skyvern_response.get("error"), "success": False}), 500
99
+
100
+ return jsonify({
101
+ "success": True,
102
+ "user_command": user_command,
103
+ "processed_command": processed_command,
104
+ "result": skyvern_response.get("result"),
105
+ "screenshot": skyvern_response.get("screenshot"),
106
+ "raw": skyvern_response.get("raw")
107
+ })
108
+
109
+ if __name__ == "__main__":
110
+ app.run(host="0.0.0.0", port=8000, debug=True)