forked from BandarLabs/clickclickclick
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.py
74 lines (60 loc) · 2.48 KB
/
api.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
from clickclickclick.planner.task import execute_with_timeout, execute_task
from clickclickclick.executor.osx import MacExecutor
from clickclickclick.executor.android import AndroidExecutor
from clickclickclick.planner.gemini import GeminiPlanner
from clickclickclick.finder.gemini import GeminiFinder
from clickclickclick.planner.openai import ChatGPTPlanner
from clickclickclick.finder.local_ollama import OllamaFinder
from clickclickclick.finder.openai import OpenAIFinder
from clickclickclick.planner.local_ollama import OllamaPlanner
from clickclickclick.config import get_config
import uvicorn
app = FastAPI()
class TaskRequest(BaseModel):
task_prompt: str
platform: str = "android"
planner_model: str = "openai"
finder_model: str = "gemini"
@app.post("/execute")
def execute_task_api(request: TaskRequest):
task_prompt = request.task_prompt
platform = request.platform
planner_model = request.planner_model
finder_model = request.finder_model
c = get_config(platform, planner_model, finder_model)
if platform == "osx":
executor = MacExecutor()
elif platform == "android":
executor = AndroidExecutor()
else:
raise HTTPException(status_code=400, detail=f"Unsupported platform: {platform}")
if planner_model == "openai":
executor.screenshot_as_base64 = True
planner = ChatGPTPlanner(c)
elif planner_model == "gemini":
executor.screenshot_as_tempfile = True
planner = GeminiPlanner(c)
elif planner_model == "ollama":
executor.screenshot_as_tempfile = True
planner = OllamaPlanner(c, executor)
else:
raise HTTPException(status_code=400, detail=f"Unsupported planner model: {planner_model}")
if finder_model == "openai":
finder = OpenAIFinder(c, executor)
elif finder_model == "gemini":
finder = GeminiFinder(c, executor)
elif finder_model == "ollama":
finder = OllamaFinder("llama3.2-vision", executor)
else:
raise HTTPException(status_code=400, detail=f"Unsupported finder model: {finder_model}")
result = execute_with_timeout(
execute_task, c.TASK_TIMEOUT_IN_SECONDS, task_prompt, executor, planner, finder, c
)
if result is not None:
return {"result": result}
else:
raise HTTPException(status_code=500, detail="Task execution failed")
if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=8000)