Repository navigation
Expand file tree
/
Copy pathapp.py
More file actions
298 lines (247 loc) · 8.41 KB
/
Copy pathapp.py
File metadata and controls
298 lines (247 loc) · 8.41 KB
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
from flask import (
Flask,
render_template,
jsonify,
request,
send_from_directory,
redirect,
make_response,
)
import requests
import threading
import queue
from ollamaClient import OllamaClient
from dotenv import load_dotenv
import os
import hashlib
from uuid import uuid4
from base64 import b64encode
from itsdangerous import URLSafeSerializer
import markdown
# ===================== INITIALIZATION =====================
# Initialize Flask app and Ollama client
app = Flask(__name__)
ai = OllamaClient()
# Queue for handling asynchronous chat requests
request_queue: queue.Queue = queue.Queue()
auth_portal_api_url = ""
no_of_auth_reqs = 0
no_of_reqs = 0
# Load environment variables from .env file
load_dotenv("./config.env")
# ===================== HELPER FUNCTIONS =====================
def get_config(key, default=None):
"""
Retrieve a configuration value from environment variables.
Exits the program if the key is not found and no default is provided.
"""
value = os.getenv(key, default)
if value is None:
print(f"Error: The {key} is not set in the environment.")
exit(1) # Exit with non-zero status to indicate an error occurred
return value
# ===================== CONFIGURATION =====================
# Load configuration from environment variables
config = {
"host": get_config("SIMPLE_AI_UI_SERVER_HOST"),
"port": int(get_config("SIMPLE_AI_UI_SERVER_PORT")),
"users": {
pair.split(":")[0]: pair.split(":")[1]
for pair in get_config("SIMPLE_AI_UI_AUTH_USERS").split(",")
if ":" in pair
},
"limit_of_auth_requests": int(get_config("SIMPLE_AI_UI_AUTH_NO_OF_USERS_PER_DAY")),
"limit_of_requests": int(get_config("SIMPLE_AI_UI_API_LIMIT_OF_REQS_PER_DAY")),
}
# ===================== AUTHENTICATION AND SECURITY =====================
def auth(username, password):
"""
Authenticate a user by checking their username and password.
Returns "OK-12" if authentication is successful, otherwise an error code.
"""
if username in config["users"]:
if hashing(config["users"][username]) == hashing(password):
return "OK-12"
else:
return "BAD-2"
return "BAD-12"
def hashing(text):
"""
Hash a given text using SHA-256.
"""
hash_object = hashlib.sha256(text.encode("utf-8"))
return hash_object.hexdigest()
def middleware_check(type):
"""
Middleware to check if the user is authenticated.
Redirects to the login page or returns an error if not authenticated.
"""
global no_of_auth_reqs
try:
auth_cookies = request.cookies.get("auth", "")
except ValueError:
auth_cookies = ""
if auth_cookies == "":
if type == "api":
return jsonify(
{
"error": "Auth error, You are not logged in!\n Login via <a href='/login'>LOGIN PAGE</a>"
}
)
else:
return redirect("/login")
else:
no_of_auth_reqs += 1
return None
def middleware_init(username):
"""
Initialize middleware by generating a token for the authenticated user.
"""
global no_of_auth_reqs
data = {
"device": request.headers.get("User-Agent"),
"user": username,
"ID": str(uuid4()),
"number": no_of_auth_reqs,
}
token, _ = server_encode_tokenizer(data)
return token
def server_encode_tokenizer(data):
"""
Encode data into a token using a URL-safe serializer.
"""
key = str(uuid4())
with open(".keys", "a") as f:
f.write(key + "\n")
auth_s = URLSafeSerializer(
key + "-abujuni.dev", "Hi I am Junaid, Support my project"
)
token = auth_s.dumps(data)
return token, auth_s
def server_decode_tokenizer(token, auth_s):
"""
Decode a token back into data using a URL-safe serializer.
"""
data = auth_s.loads(token)
return data
# ===================== BACKGROUND WORKER THREAD =====================
def process_requests() -> None:
"""
Worker thread function to process queued chat requests.
"""
while True:
task = request_queue.get()
if task is None:
break # Exit when a termination signal is received
request_data, response_queue = task
prompt = request_data.get("prompt")
model = request_data.get("model")
if not prompt or not model:
response_queue.put({"error": "Invalid request data"})
else:
response = ai.chat(model=model, prompt=prompt)
response_queue.put({"response": response})
# Start background worker thread
threading.Thread(target=process_requests, daemon=True).start()
# ===================== ROUTES =====================
# ============= FRONTEND ROUTES =============
@app.route("/")
def index():
"""
Render the index page after checking authentication.
"""
middleware_response = middleware_check(type="html")
if middleware_response:
return middleware_response
return render_template("index.html"), 200
@app.route("/static/<path:filename>")
def serve_static(filename: str):
"""
Serve static files from the 'static' directory.
"""
return send_from_directory("static", filename), 200
@app.errorhandler(Exception)
def handle_error(error: Exception):
"""
Handle errors and render appropriate error pages.
"""
if hasattr(error, "code") and error.code == 404:
return render_template("404.html"), 404
return render_template("error.html", error=str(error)), 500
# ============= SECURITY ROUTES =============
@app.route("/login", methods=["GET", "POST"])
def api_auth():
"""
Handle user login. Authenticates the user and sets an auth cookie if successful.
"""
if request.method == "POST":
username = request.form.get("username")
password = request.form.get("password")
auth_result = auth(username, password)
if auth_result == "OK-12":
token = middleware_init(username)
response = make_response(redirect("/"))
response.set_cookie("auth", token)
return response
else:
return render_template("login.html", error="Invalid username or password")
return render_template("login.html")
# ============= API ROUTES =============
@app.route("/api/connection")
def api_connection():
"""
Check the connection to the Ollama server.
"""
middleware_response = middleware_check(type="api")
if middleware_response:
return middleware_response
try:
res = requests.get("http://localhost:11434", timeout=5)
status_code = res.status_code
except (requests.exceptions.ConnectionError, requests.exceptions.Timeout):
app.logger.error("Unable to connect to server")
status_code = 404
return (
jsonify(
{
"host": "localhost",
"port": 11434,
"status": "OK" if status_code == 200 else "ERROR",
}
),
status_code,
)
@app.route("/api/connection/stats")
def api_connection_stats():
"""
Retrieve statistics about available and active models from the Ollama client.
"""
middleware_response = middleware_check(type="api")
if middleware_response:
return middleware_response
models = ai.list_models()
active_models = ai.list_active_models()
if not models and not active_models:
return jsonify({"error": "No models found"}), 404
return jsonify({"available": models, "active": active_models}), 200
@app.route("/api/chat", methods=["POST"])
def api_chat():
"""
Handle chat requests by processing the prompt and model, and returning the AI's response.
"""
middleware_response = middleware_check(type="api")
if middleware_response:
return middleware_response
data = request.json
if not data or "prompt" not in data or "model" not in data:
return jsonify({"error": "Invalid request data"}), 400
response_queue = queue.Queue()
request_queue.put((data, response_queue))
response = response_queue.get()
if "response" not in response:
return jsonify({"error": "Invalid response from the AI"}), 500
html_response = markdown.markdown(response["response"])
return jsonify({"response": html_response}), 200
# ===================== MAIN EXECUTION =====================
if __name__ == "__main__":
app.run(host=config.get("host"), port=config.get("port"))