diff --git a/01OS/01OS/server/server.py b/01OS/01OS/server/server.py index f53df33..6313bc7 100644 --- a/01OS/01OS/server/server.py +++ b/01OS/01OS/server/server.py @@ -22,6 +22,19 @@ from ..utils.accumulator import Accumulator from .teach import teach from .utils.logs import setup_logging from .utils.logs import logger + +from ..utils.print_markdown import print_markdown + +markdown = """ +○ + +*Starting...* +""" +print("") +print_markdown(markdown) +print("") + + setup_logging() accumulator = Accumulator() @@ -365,8 +378,28 @@ import os import platform from importlib import import_module +# these will be overwritten +HOST = '' +PORT = 0 + +@app.on_event("startup") +async def startup_event(): + server_url = f"{HOST}:{PORT}" + print("") + print_markdown(f"\n*Ready.*\n") + print("") + +@app.on_event("shutdown") +async def shutdown_event(): + print_markdown("*Server is shutting down*") + async def main(server_host, server_port, llm_service, model, llm_supports_vision, llm_supports_functions, context_window, max_tokens, temperature, tts_service, stt_service): - + + global HOST + global PORT + PORT = server_port + HOST = server_host + # Setup services application_directory = user_data_dir('01') services_directory = os.path.join(application_directory, 'services') diff --git a/01OS/01OS/server/tunnel.py b/01OS/01OS/server/tunnel.py new file mode 100644 index 0000000..25f3e52 --- /dev/null +++ b/01OS/01OS/server/tunnel.py @@ -0,0 +1,57 @@ +import os +import subprocess +import re +import shutil +import time +from ..utils.print_markdown import print_markdown + +def create_tunnel(tunnel_method='bore', server_host='localhost', server_port=8000): + print_markdown(f"Exposing server to the internet...") + + if tunnel_method == "bore": + try: + output = subprocess.check_output('command -v bore', shell=True) + except subprocess.CalledProcessError: + print("The bore-cli command is not available. Please run 'cargo install bore-cli'.") + print("For more information, see https://github.com/ekzhang/bore") + exit(1) + + time.sleep(6) + output = subprocess.check_output(f'bore local {server_port} --to bore.pub', shell=True) + + for line in output.split('\n'): + if "listening at bore.pub:" in line: + remote_port = re.search('bore.pub:([0-9]*)', line).group(1) + print_markdown(f"Your server is being hosted at the following URL: bore.pub:{remote_port}") + break + + + + + elif tunnel_method == "localtunnel": + if not subprocess.call('command -v lt', shell=True): + print("The 'lt' command is not available.") + print("Please ensure you have Node.js installed, then run 'npm install -g localtunnel'.") + print("For more information, see https://github.com/localtunnel/localtunnel") + exit(1) + else: + output = subprocess.check_output(f'npx localtunnel --port {server_port}', shell=True) + for line in output.split('\n'): + if "your url is: https://" in line: + remote_url = re.search('https://([a-zA-Z0-9.-]*)', line).group(0).replace('https://', '') + print(f"Your server is being hosted at the following URL: {remote_url}") + break + + elif tunnel_method == "ngrok": + if not subprocess.call('command -v ngrok', shell=True): + print("The ngrok command is not available.") + print("Please install ngrok using the instructions at https://ngrok.com/docs/getting-started/") + exit(1) + else: + output = subprocess.check_output(f'ngrok http {server_port} --log stdout', shell=True) + for line in output.split('\n'): + if "started tunnel" in line: + remote_url = re.search('https://([a-zA-Z0-9.-]*)', line).group(0).replace('https://', '') + print(f"Your server is being hosted at the following URL: {remote_url}") + break + diff --git a/01OS/01OS/start.py b/01OS/01OS/start.py index f656ce7..906c41b 100644 --- a/01OS/01OS/start.py +++ b/01OS/01OS/start.py @@ -4,9 +4,9 @@ import platform import concurrent.futures import threading import os -import signal import importlib - +from .server.tunnel import create_tunnel +import signal app = typer.Typer() @app.command() @@ -63,9 +63,7 @@ def run( server_thread.start() if expose: - #tunnel_thread = threading.Thread(target=tunnel_service, args=[server_port]) - #tunnel_thread.start() - tunnel_thread = threading.Thread(target=os.system, args=("./tunnel.sh",)) + tunnel_thread = threading.Thread(target=create_tunnel, args=[tunnel_service, server_host, server_port]) tunnel_thread.start() if client: diff --git a/01OS/01OS/utils/print_markdown.py b/01OS/01OS/utils/print_markdown.py new file mode 100644 index 0000000..9fbbda8 --- /dev/null +++ b/01OS/01OS/utils/print_markdown.py @@ -0,0 +1,9 @@ +from rich.console import Console +from rich.markdown import Markdown + +def print_markdown(markdown_text): + console = Console() + md = Markdown(markdown_text) + print("") + console.print(md) + print("") \ No newline at end of file diff --git a/01OS/poetry.lock b/01OS/poetry.lock index f3f4ce8..9293658 100644 --- a/01OS/poetry.lock +++ b/01OS/poetry.lock @@ -7177,13 +7177,13 @@ rsa = ["oauthlib[signedtoken] (>=3.0.0)"] [[package]] name = "rich" -version = "13.7.0" +version = "13.7.1" description = "Render rich text, tables, progress bars, syntax highlighting, markdown and more to the terminal" optional = false python-versions = ">=3.7.0" files = [ - {file = "rich-13.7.0-py3-none-any.whl", hash = "sha256:6da14c108c4866ee9520bbffa71f6fe3962e193b7da68720583850cd4548e235"}, - {file = "rich-13.7.0.tar.gz", hash = "sha256:5cb5123b5cf9ee70584244246816e9114227e0b98ad9176eede6ad54bf5403fa"}, + {file = "rich-13.7.1-py3-none-any.whl", hash = "sha256:4edbae314f59eb482f54e9e30bf00d33350aaa94f4bfcd4e9e3110e64d0d7222"}, + {file = "rich-13.7.1.tar.gz", hash = "sha256:9be308cb1fe2f1f57d67ce99e95af38a1e2bc71ad9813b0e247cf7ffbcc3a432"}, ] [package.dependencies] @@ -8400,4 +8400,4 @@ testing = ["big-O", "jaraco.functools", "jaraco.itertools", "more-itertools", "p [metadata] lock-version = "2.0" python-versions = ">=3.9,<3.12" -content-hash = "f582fa2573961a7bca4df34f7bf62bcbda856e57697f5e3daad6603ce2bc0589" +content-hash = "292b410066ed60d734cb028bcb67827fdd349ec1515d06cc13edc90393003bc5" diff --git a/01OS/pyproject.toml b/01OS/pyproject.toml index 9b3e4d9..b4cb497 100644 --- a/01OS/pyproject.toml +++ b/01OS/pyproject.toml @@ -29,6 +29,7 @@ open-interpreter = {version = "0.2.1rc1", extras = ["os"]} psutil = "^5.9.8" typer = "^0.9.0" platformdirs = "^4.2.0" +rich = "^13.7.1" [build-system] requires = ["poetry-core"]