-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
153 lines (111 loc) · 3.96 KB
/
app.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import os
from dotenv import load_dotenv
from flask import Flask, render_template, jsonify, request
from database.database import init_db
import uuid
import docker
from ai import llm
load_dotenv()
db_uri = os.getenv("SQLALCHEMY_APIS_DATABASE_URI")
validate_user_prompt = os.getenv("VALIDATE_USER_PROMPT")
app = Flask(__name__)
db = init_db(db_uri, app)
async def process_user_prompt(service, token, prompt):
try:
endpoints = await llm.get_task_endpoints(
service=service, prompt=prompt, return_best_only=False
)
auth_info = await llm.get_auth_info(
description=prompt, service=service, use_llm=False
)
code = await llm.generate_task_code(
description=prompt,
auth_details=auth_info,
endpoints=endpoints,
token=token,
)
response = {"code": code, "endpoints": endpoints}
return response, None
except Exception as e:
return None, {"error": e}
# def process_user_prompt(service, token, prompt):
# try:
# generate_service_call(prompt, service, token)
# TODO: ask the llm if this request makes sense
# split_in_tasks = llm_get_prompt_tasks(service, prompt)
# if len(tasks) == 0:
# return None, f"No task for the given prompt {prompt}"
# endpoints = []
# for task in tasks:
# llm_get_endpoints = llm_get_task_endpoints(service, task)
# endpoints += llm_get_endpoints
# code = llm_generate_script(service, token, prompt, endpoints)
# return code, None
# except Exception as e:
# print(e)
# return None, "Error processing user prompt"
# Filter relevant endpoints from the database
@app.route("/")
def home():
return render_template("index.html")
@app.route("/faq")
def faq():
return render_template("faq.html") # Serve the FAQ content on this route
@app.route("/gen-script", methods=["POST"])
async def chat():
data = request.get_json()
if "service" not in data:
response = {"error": "Bad parameters: missing or invalid service"}
return jsonify(response), 400
if "token" not in data:
response = {"error": "Bad parameters: missing token"}
return jsonify(response), 400
if "prompt" not in data or len(data["prompt"]) == 0:
response = {"error": "Bad parameters: missing or invalid prompt"}
return jsonify(response), 400
service = data["service"]
token = data["token"]
prompt = data["prompt"]
response, error = await process_user_prompt(
prompt=prompt, service=service, token=token
)
# code, error = await generate_service_call(prompt, service, token)
if error is not None:
print(f"Error: {error}")
response = {"error": error}
return jsonify(response), 500
return jsonify(response)
@app.route("/run_code", methods=["POST"])
def run_code():
code = request.json.get("code")
scripts_folder = "user-scripts"
# Check if the directory exists
if not os.path.exists(scripts_folder):
# If not, create the directory
os.makedirs(scripts_folder)
# Generate a random file name
filename = f"{scripts_folder}/temp_script_{uuid.uuid4().hex}.py"
# Save the code to a file
with open(filename, "w") as file:
file.write(code)
try:
# Create a Docker client
client = docker.from_env()
# Run the script in a Docker container
result = client.containers.run(
"talk2apis-code-runner", # Use the new Docker image
f"python {filename}",
remove=True,
volumes={os.environ["HOST_PROJECT_PATH"]: {"bind": "/app", "mode": "rw"}},
)
# Capture standard output
output = result.decode("utf-8")
error = None
except Exception as e:
output = None
error = str(e)
return jsonify({"output": output, "error": error})
if __name__ == "__main__":
debug = os.getenv("DEBUG")
port = os.getenv("HTTP_PORT")
app.run(debug=debug, port=port, use_reloader=False)