console print edits for docker
frdel committed
Dec 7, 2024 at 21:51 UTC
884007cdb0064ef9d550dc99e6c3066719242da1
16 files changed
+79
-59
initialize.py
-4
@@ -84,16 +84,12 @@ def args_override(config):
84
# conversion based on type of config[key]
85
if isinstance(getattr(config, key), bool):
86
value = value.lower().strip() == "true"
87
- print("bool", value)
87
elif isinstance(getattr(config, key), int):
88
value = int(value)
90
- print("int", value)
89
elif isinstance(getattr(config, key), float):
90
value = float(value)
93
- print("float", value)
91
elif isinstance(getattr(config, key), str):
92
value = str(value)
96
- print("str", value)
93
else:
94
raise Exception(
95
f"Unsupported argument type of '{key}': {type(getattr(config, key))}"
preload.py
+3
-2
@@ -1,7 +1,8 @@
1
import asyncio
2
from python.helpers import runtime, whisper, settings
3
+from python.helpers.print_style import PrintStyle
4
4
-print("Running preload...")
5
+PrintStyle().print("Running preload...")
6
runtime.initialize()
7
8
@@ -14,7 +15,7 @@ async def preload():
15
16
return asyncio.gather(*tasks, return_exceptions=True)
17
except Exception as e:
17
- print(f"Error in preload: {e}")
18
+ PrintStyle().print(f"Error in preload: {e}")
19
20
21
# preload transcription model
prepare.py
+5
-3
@@ -1,8 +1,10 @@
1
from python.helpers import dotenv, runtime, settings
2
import string
3
import random
4
+from python.helpers.print_style import PrintStyle
5
5
-print("Preparing environment...")
6
+
7
+PrintStyle.standard("Preparing environment...")
8
9
try:
10
@@ -12,8 +14,8 @@ try:
14
root_pass = dotenv.get_dotenv_value(dotenv.KEY_ROOT_PASSWORD)
15
if not root_pass:
16
root_pass = "".join(random.choices(string.ascii_letters + string.digits, k=32))
15
- print("Changing root password...")
17
+ PrintStyle.standard("Changing root password...")
18
settings.set_root_password(root_pass)
19
20
except Exception as e:
19
- print(f"Error in preload: {e}")
21
+ PrintStyle.error(f"Error in preload: {e}")
python/helpers/attachment_manager.py
+4
-2
@@ -5,6 +5,8 @@ from PIL import Image
5
from typing import Dict, List, Optional, Tuple
6
from werkzeug.utils import secure_filename
7
8
+from python.helpers.print_style import PrintStyle
9
+
10
class AttachmentManager:
11
ALLOWED_EXTENSIONS = {
12
'image': {'jpg', 'jpeg', 'png', 'bmp'},
@@ -66,7 +68,7 @@ class AttachmentManager:
68
return file_path, metadata
69
70
except Exception as e:
69
- print(f"Error saving file {filename}: {e}")
71
+ PrintStyle.error(f"Error saving file {filename}: {e}")
72
return None, {} # type: ignore
73
74
def generate_image_preview(self, image_path: str, max_size: int = 800) -> Optional[str]:
@@ -86,6 +88,6 @@ class AttachmentManager:
88
# Convert to base64
89
return base64.b64encode(buffer.getvalue()).decode('utf-8')
90
except Exception as e:
89
- print(f"Error generating preview for {image_path}: {e}")
91
+ PrintStyle.error(f"Error generating preview for {image_path}: {e}")
92
return None
93
\ No newline at end of file
python/helpers/cloudflare_tunnel.py
+8
-7
@@ -4,6 +4,7 @@ import requests
4
import subprocess
5
import threading
6
from python.helpers import files
7
+from python.helpers.print_style import PrintStyle
8
9
class CloudflareTunnel:
10
def __init__(self, port: int):
@@ -41,7 +42,7 @@ class CloudflareTunnel:
42
download_url = f"{base_url}{download_file}"
43
download_path = files.get_abs_path(self.bin_dir, download_file)
44
44
- print(f"\nDownloading cloudflared from: {download_url}")
45
+ PrintStyle().print(f"\nDownloading cloudflared from: {download_url}")
46
response = requests.get(download_url, stream=True)
47
if response.status_code != 200:
48
raise RuntimeError(f"Failed to download cloudflared: {response.status_code}")
@@ -77,7 +78,7 @@ class CloudflareTunnel:
78
download_url = f"{base_url}{download_file}"
79
download_path = files.get_abs_path(self.bin_dir, download_file)
80
80
- print(f"\nDownloading cloudflared from: {download_url}")
81
+ PrintStyle().print(f"\nDownloading cloudflared from: {download_url}")
82
response = requests.get(download_url, stream=True)
83
if response.status_code != 200:
84
raise RuntimeError(f"Failed to download cloudflared: {response.status_code}")
@@ -113,9 +114,9 @@ class CloudflareTunnel:
114
start = line.find("https://")
115
end = line.find("trycloudflare.com") + len("trycloudflare.com")
116
self.tunnel_url = line[start:end].strip()
116
- print("\n=== Cloudflare Tunnel URL ===")
117
- print(f"URL: {self.tunnel_url}")
118
- print("============================\n")
117
+ PrintStyle().print("\n=== Cloudflare Tunnel URL ===")
118
+ PrintStyle().print(f"URL: {self.tunnel_url}")
119
+ PrintStyle().print("============================\n")
120
return
121
122
def start(self):
@@ -123,7 +124,7 @@ class CloudflareTunnel:
124
if not self.cloudflared_path:
125
self.download_cloudflared()
126
126
- print("\nStarting Cloudflare tunnel...")
127
+ PrintStyle().print("\nStarting Cloudflare tunnel...")
128
# Start tunnel process
129
self.tunnel_process = subprocess.Popen(
130
[
@@ -149,7 +150,7 @@ class CloudflareTunnel:
150
"""Stops the cloudflare tunnel"""
151
self._stop_event.set()
152
if self.tunnel_process:
152
- print("\nStopping Cloudflare tunnel...")
153
+ PrintStyle().print("\nStopping Cloudflare tunnel...")
154
self.tunnel_process.terminate()
155
self.tunnel_process.wait()
156
self.tunnel_process = None
python/helpers/docker.py
+6
-6
@@ -38,10 +38,10 @@ class DockerContainerManager:
38
try:
39
self.container.stop()
40
self.container.remove()
41
- print(f"Stopped and removed the container: {self.container.id}")
41
+ PrintStyle.standard(f"Stopped and removed the container: {self.container.id}")
42
if self.logger: self.logger.log(type="info", content=f"Stopped and removed the container: {self.container.id}")
43
except Exception as e:
44
- print(f"Failed to stop and remove the container: {e}")
44
+ PrintStyle.error(f"Failed to stop and remove the container: {e}")
45
if self.logger: self.logger.log(type="error", content=f"Failed to stop and remove the container: {e}")
46
47
def get_image_containers(self):
@@ -72,7 +72,7 @@ class DockerContainerManager:
72
73
if existing_container:
74
if existing_container.status != 'running':
75
- print(f"Starting existing container: {self.name} for safe code execution...")
75
+ PrintStyle.standard(f"Starting existing container: {self.name} for safe code execution...")
76
if self.logger: self.logger.log(type="info", content=f"Starting existing container: {self.name} for safe code execution...", temp=True)
77
78
existing_container.start()
@@ -81,9 +81,9 @@ class DockerContainerManager:
81
82
else:
83
self.container = existing_container
84
- # print(f"Container with name '{self.name}' is already running with ID: {existing_container.id}")
84
+ # PrintStyle.standard(f"Container with name '{self.name}' is already running with ID: {existing_container.id}")
85
else:
86
- print(f"Initializing docker container {self.name} for safe code execution...")
86
+ PrintStyle.standard(f"Initializing docker container {self.name} for safe code execution...")
87
if self.logger: self.logger.log(type="info", content=f"Initializing docker container {self.name} for safe code execution...", temp=True)
88
89
self.container = self.client.containers.run(
@@ -94,6 +94,6 @@ class DockerContainerManager:
94
volumes=self.volumes, # type: ignore
95
)
96
# atexit.register(self.cleanup_container)
97
- print(f"Started container with ID: {self.container.id}")
97
+ PrintStyle.standard(f"Started container with ID: {self.container.id}")
98
if self.logger: self.logger.log(type="info", content=f"Started container with ID: {self.container.id}")
99
time.sleep(5) # this helps to get SSH ready
python/helpers/dotenv.py
+3
-2
@@ -1,5 +1,6 @@
1
import os
2
import re
3
+from typing import Any
4
5
from .files import get_abs_path
6
from dotenv import load_dotenv as _load_dotenv
@@ -16,9 +17,9 @@ def load_dotenv():
17
def get_dotenv_file_path():
18
return get_abs_path(".env")
19
19
-def get_dotenv_value(key: str):
20
+def get_dotenv_value(key: str, default: Any = None):
21
# load_dotenv()
21
- return os.getenv(key)
22
+ return os.getenv(key, default)
23
24
def save_dotenv_value(key: str, value: str):
25
if value is None:
python/helpers/file_browser.py
+5
-4
@@ -8,6 +8,7 @@ from werkzeug.utils import secure_filename
8
from datetime import datetime
9
10
from python.helpers import files, runtime
11
+from python.helpers.print_style import PrintStyle
12
13
class FileBrowser:
14
ALLOWED_EXTENSIONS = {
@@ -58,13 +59,13 @@ class FileBrowser:
59
else:
60
failed.append(file.filename)
61
except Exception as e:
61
- print(f"Error saving file {file.filename}: {e}")
62
+ PrintStyle.error(f"Error saving file {file.filename}: {e}")
63
failed.append(file.filename)
64
65
return successful, failed
66
67
except Exception as e:
67
- print(f"Error in save_files: {e}")
68
+ PrintStyle.error(f"Error in save_files: {e}")
69
return successful, failed
70
71
def delete_file(self, file_path: str) -> bool:
@@ -85,7 +86,7 @@ class FileBrowser:
86
return False
87
88
except Exception as e:
88
- print(f"Error deleting {file_path}: {e}")
89
+ PrintStyle.error(f"Error deleting {file_path}: {e}")
90
return False
91
92
def _is_allowed_file(self, filename: str, file) -> bool:
@@ -160,7 +161,7 @@ class FileBrowser:
161
}
162
163
except Exception as e:
163
- print(f"Error reading directory: {e}")
164
+ PrintStyle.error(f"Error reading directory: {e}")
165
return {"entries": [], "current_path": "", "parent_path": ""}
166
167
def get_full_path(self, file_path: str, allow_dir: bool = False) -> str:
python/helpers/knowledge_import.py
+4
-3
@@ -13,6 +13,7 @@ from langchain_community.document_loaders import (
13
)
14
from python.helpers import files
15
from python.helpers.log import LogItem
16
+from python.helpers.print_style import PrintStyle
17
18
text_loader_kwargs = {"autodetect_encoding": True}
19
@@ -70,7 +71,7 @@ def load_knowledge(
71
kn_files = [f for f in kn_files if os.path.isfile(f)]
72
73
if kn_files:
73
- print(
74
+ PrintStyle.standard(
75
f"Found {len(kn_files)} knowledge files in {knowledge_dir}, processing..."
76
)
77
if log_item:
@@ -108,7 +109,7 @@ def load_knowledge(
109
doc.metadata = {**doc.metadata, **metadata}
110
cnt_files += 1
111
cnt_docs += len(file_data["documents"])
111
- # print(f"Imported {len(file_data['documents'])} documents from {file_path}")
112
+ # PrintStyle.standard(f"Imported {len(file_data['documents'])} documents from {file_path}")
113
114
# Update the index
115
index[file_key] = file_data # type: ignore
@@ -118,7 +119,7 @@ def load_knowledge(
119
if not file_data.get("state", ""):
120
index[file_key]["state"] = "removed"
121
121
- print(f"Processed {cnt_docs} documents from {cnt_files} files.")
122
+ PrintStyle.standard(f"Processed {cnt_docs} documents from {cnt_files} files.")
123
if log_item:
124
log_item.stream(
125
progress=f"\nProcessed {cnt_docs} documents from {cnt_files} files."
python/helpers/memory.py
+4
-2
@@ -13,6 +13,8 @@ from langchain_community.vectorstores.utils import (
13
import os, json
14
15
import numpy as np
16
+
17
+from python.helpers.print_style import PrintStyle
18
from . import files
19
from langchain_core.documents import Document
20
import uuid
@@ -78,7 +80,7 @@ class Memory:
80
in_memory=False,
81
) -> MyFaiss:
82
81
- print("Initializing VectorDB...")
83
+ PrintStyle.standard("Initializing VectorDB...")
84
85
if log_item:
86
log_item.stream(progress="\nInitializing VectorDB")
@@ -312,7 +314,7 @@ class Memory:
314
try:
315
return eval(condition, {}, data)
316
except Exception as e:
315
- # print(f"Error evaluating condition: {e}")
317
+ # PrintStyle.error(f"Error evaluating condition: {e}")
318
return False
319
320
return comparator
python/helpers/print_style.py
+4
@@ -117,6 +117,10 @@ class PrintStyle:
117
lines = sys.stdin.readlines()
118
return bool(lines) and not lines[-1].strip()
119
120
+ @staticmethod
121
+ def standard(text:str):
122
+ PrintStyle().print(text)
123
+
124
@staticmethod
125
def hint(text:str):
126
PrintStyle(font_color="#6C3483", padding=True).print("Hint: "+text)
python/helpers/process.py
+3
-2
@@ -1,6 +1,7 @@
1
import os
2
import sys
3
from python.helpers import runtime
4
+from python.helpers.print_style import PrintStyle
5
6
_server = None
7
@@ -26,10 +27,10 @@ def reload():
27
restart_process()
28
29
def restart_process():
29
- print("Restarting process...")
30
+ PrintStyle.standard("Restarting process...")
31
python = sys.executable
32
os.execv(python, [python] + sys.argv)
33
34
def exit_process():
34
- print("Exiting process...")
35
+ PrintStyle.standard("Exiting process...")
36
sys.exit(0)
\ No newline at end of file
python/helpers/shell_ssh.py
+2
-1
@@ -4,6 +4,7 @@ import time
4
import re
5
from typing import Tuple
6
from python.helpers.log import Log
7
+from python.helpers.print_style import PrintStyle
8
from python.helpers.strings import calculate_valid_match_lengths
9
10
@@ -51,7 +52,7 @@ class SSHInteractiveSession:
52
except Exception as e:
53
errors += 1
54
if errors < 3:
54
- print(f"SSH Connection attempt {errors}...")
55
+ PrintStyle.standard(f"SSH Connection attempt {errors}...")
56
self.logger.log(
57
type="info",
58
content=f"SSH Connection attempt {errors}...",
python/helpers/whisper.py
+2
-1
@@ -4,6 +4,7 @@ import whisper
4
import tempfile
5
import asyncio
6
from python.helpers import runtime, rfc, settings
7
+from python.helpers.print_style import PrintStyle
8
9
# Suppress FutureWarning from torch.load
10
warnings.filterwarnings("ignore", category=FutureWarning)
@@ -28,7 +29,7 @@ async def _preload(model_name:str):
29
try:
30
is_updating_model = True
31
if not _model or _model_name != model_name:
31
- print(f"Loading Whisper model: {model_name}")
32
+ PrintStyle.standard(f"Loading Whisper model: {model_name}")
33
_model = whisper.load_model(model_name)
34
_model_name = model_name
35
finally:
run_cli.py
+2
-2
@@ -96,7 +96,7 @@ def timeout_input(prompt, timeout=10):
96
97
def run():
98
global context
99
- print("Initializing framework...")
99
+ PrintStyle.standard("Initializing framework...")
100
101
#load env vars
102
load_dotenv()
@@ -112,5 +112,5 @@ def run():
112
asyncio.run(chat(context))
113
114
if __name__ == "__main__":
115
- print("\n\n!!! run_cli.py is now discontinued. run_ui.py serves as both UI and API endpoint !!!\n\n")
115
+ PrintStyle.standard("\n\n!!! run_cli.py is now discontinued. run_ui.py serves as both UI and API endpoint !!!\n\n")
116
run()
\ No newline at end of file
run_ui.py
+24
-18
@@ -3,12 +3,13 @@ import os
3
import threading
4
from flask import Flask, request, Response
5
from flask_basicauth import BasicAuth
6
-from python.helpers import files, git
6
+from python.helpers import errors, files, git
7
from python.helpers.files import get_abs_path
8
from python.helpers import persist_chat, runtime, dotenv, process
9
from python.helpers.cloudflare_tunnel import CloudflareTunnel
10
from python.helpers.extract_tools import load_classes_from_folder
11
from python.helpers.api import ApiHandler
12
+from python.helpers.print_style import PrintStyle
13
14
15
# initialize the internal Flask server
@@ -54,7 +55,7 @@ async def serve_index():
55
56
57
def run():
57
- print("Initializing framework...")
58
+ PrintStyle().print("Initializing framework...")
59
60
# Suppress only request logs but keep the startup messages
61
from werkzeug.serving import WSGIRequestHandler
@@ -65,25 +66,29 @@ def run():
66
pass # Override to suppress request logging
67
68
# Get configuration from environment
68
- port = runtime.get_arg("port") or int(os.environ.get("WEB_UI_PORT", 0)) or 5000
69
- host = runtime.get_arg("host") or os.environ.get("WEB_UI_HOST") or "localhost"
70
- use_cloudflare = (
71
- runtime.get_arg("cloudflare_tunnel")
72
- or os.environ.get("USE_CLOUDFLARE", "false").lower() == "true"
73
- )
69
+ port = runtime.get_arg("port") or int(dotenv.get_dotenv_value("WEB_UI_PORT", 0)) or 5000
70
+ host = runtime.get_arg("host") or dotenv.get_dotenv_value("WEB_UI_HOST") or "localhost"
71
+ use_cloudflare = (runtime.get_arg("cloudflare_tunnel")
72
+ or dotenv.get_dotenv_value("USE_CLOUDFLARE", "false").lower()) == "true"
73
+
74
75
- # Initialize and start Cloudflare tunnel if enabled
75
tunnel = None
77
- if use_cloudflare and port:
78
- try:
79
- tunnel = CloudflareTunnel(port)
80
- tunnel.start()
81
- except Exception as e:
82
- print(f"Failed to start Cloudflare tunnel: {e}")
83
- print("Continuing without tunnel...")
76
85
- # initialize contexts from persisted chats
86
- persist_chat.load_tmp_chats()
77
+ try:
78
+ # Initialize and start Cloudflare tunnel if enabled
79
+ if use_cloudflare and port:
80
+ try:
81
+ tunnel = CloudflareTunnel(port)
82
+ tunnel.start()
83
+ except Exception as e:
84
+ PrintStyle().error(f"Failed to start Cloudflare tunnel: {e}")
85
+ PrintStyle().print("Continuing without tunnel...")
86
+
87
+ # initialize contexts from persisted chats
88
+ persist_chat.load_tmp_chats()
89
+
90
+ except Exception as e:
91
+ PrintStyle().error(errors.format_error(e))
92
93
server = None
94
@@ -123,4 +128,5 @@ def run():
128
# run the internal server
129
if __name__ == "__main__":
130
runtime.initialize()
131
+ dotenv.load_dotenv()
132
run()