-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
run_ui.py
132 lines (107 loc) · 4.31 KB
/
run_ui.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
from functools import wraps
import os
import threading
from flask import Flask, request, Response
from flask_basicauth import BasicAuth
from python.helpers import errors, files, git
from python.helpers.files import get_abs_path
from python.helpers import persist_chat, runtime, dotenv, process
from python.helpers.cloudflare_tunnel import CloudflareTunnel
from python.helpers.extract_tools import load_classes_from_folder
from python.helpers.api import ApiHandler
from python.helpers.print_style import PrintStyle
# initialize the internal Flask server
app = Flask("app", static_folder=get_abs_path("./webui"), static_url_path="/")
app.config["JSON_SORT_KEYS"] = False # Disable key sorting in jsonify
lock = threading.Lock()
# Set up basic authentication
basic_auth = BasicAuth(app)
# require authentication for handlers
def requires_auth(f):
@wraps(f)
async def decorated(*args, **kwargs):
user = dotenv.get_dotenv_value("AUTH_LOGIN")
password = dotenv.get_dotenv_value("AUTH_PASSWORD")
if user and password:
auth = request.authorization
if not auth or not (auth.username == user and auth.password == password):
return Response(
"Could not verify your access level for that URL.\n"
"You have to login with proper credentials",
401,
{"WWW-Authenticate": 'Basic realm="Login Required"'},
)
return await f(*args, **kwargs)
return decorated
# handle default address, load index
@app.route("/", methods=["GET"])
@requires_auth
async def serve_index():
gitinfo = git.get_git_info()
return files.read_file(
"./webui/index.html",
version_no=gitinfo["version"],
version_time=gitinfo["commit_time"],
)
def run():
PrintStyle().print("Initializing framework...")
# Suppress only request logs but keep the startup messages
from werkzeug.serving import WSGIRequestHandler
from werkzeug.serving import make_server
class NoRequestLoggingWSGIRequestHandler(WSGIRequestHandler):
def log_request(self, code="-", size="-"):
pass # Override to suppress request logging
# Get configuration from environment
port = runtime.get_arg("port") or int(dotenv.get_dotenv_value("WEB_UI_PORT", 0)) or 5000
host = runtime.get_arg("host") or dotenv.get_dotenv_value("WEB_UI_HOST") or "localhost"
use_cloudflare = (runtime.get_arg("cloudflare_tunnel")
or dotenv.get_dotenv_value("USE_CLOUDFLARE", "false").lower()) == "true"
tunnel = None
try:
# Initialize and start Cloudflare tunnel if enabled
if use_cloudflare and port:
try:
tunnel = CloudflareTunnel(port)
tunnel.start()
except Exception as e:
PrintStyle().error(f"Failed to start Cloudflare tunnel: {e}")
PrintStyle().print("Continuing without tunnel...")
# initialize contexts from persisted chats
persist_chat.load_tmp_chats()
except Exception as e:
PrintStyle().error(errors.format_error(e))
server = None
def register_api_handler(app, handler: type[ApiHandler]):
name = handler.__module__.split(".")[-1]
instance = handler(app, lock)
@requires_auth
async def handle_request():
return await instance.handle_request(request=request)
app.add_url_rule(
f"/{name}",
f"/{name}",
handle_request,
methods=["POST", "GET"],
)
# initialize and register API handlers
handlers = load_classes_from_folder("python/api", "*.py", ApiHandler)
for handler in handlers:
register_api_handler(app, handler)
try:
server = make_server(host=host, port=port, app=app, request_handler=NoRequestLoggingWSGIRequestHandler, threaded=True)
process.set_server(server)
server.log_startup()
server.serve_forever()
# Run Flask app
# app.run(
# request_handler=NoRequestLoggingWSGIRequestHandler, port=port, host=host
# )
finally:
# Clean up tunnel if it was started
if tunnel:
tunnel.stop()
# run the internal server
if __name__ == "__main__":
runtime.initialize()
dotenv.load_dotenv()
run()