Skip to content

Commit

Permalink
UI-configurable ngrok
Browse files Browse the repository at this point in the history
  • Loading branch information
tnunamak committed Apr 2, 2024
1 parent 4a632c1 commit b014d48
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 16 deletions.
24 changes: 23 additions & 1 deletion selfie-ui/src/app/components/Settings/Settings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const Settings = () => {
type: "object",
required: ["method"],
properties: {
ngrok_enabled: { type: "boolean", title: "Enable ngrok", default: false },
method: {
type: "string",
title: "LLM provider",
Expand All @@ -29,14 +30,33 @@ const Settings = () => {
// verbose: { type: "boolean", title: "Verbose", default: false },
},
allOf: [
{
if: {
properties: {
ngrok_enabled: { const: true },
},
},
then: {
properties: {
ngrok_authtoken: {
type: "string",
title: "ngrok token",
},
ngrok_domain: {
type: "string",
title: "ngrok domain",
},
},
required: ["ngrok_authtoken"],
},
},
{
if: {
properties: {
method: {
title: "Local",
description: "Local",
const: "llama.cpp",
label: "ugh"
}
},
},
Expand Down Expand Up @@ -104,6 +124,8 @@ const Settings = () => {
console.log(models)

const uiSchema = {
'ui:order': ['gpu', 'ngrok_enabled', 'ngrok_authtoken', 'ngrok_domain', '*'],

method: {
"ui:widget": "radio",
},
Expand Down
29 changes: 14 additions & 15 deletions selfie/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def deserialize_args_from_env():

# Environment variable mappings with optional defaults
env_vars = {
'share': ('SELFIE_SHARE', to_bool),
'ngrok_enabled': ('SELFIE_NGROK_ENABLED', to_bool),
'api_port': ('SELFIE_API_PORT', int),
'gpu': ('SELFIE_GPU', to_bool),
'reload': ('SELFIE_RELOAD', to_bool),
Expand All @@ -53,10 +53,11 @@ def __getattr__(self, name):


def parse_args():
# TODO: Update these to sync with the configuration
parser = argparse.ArgumentParser(description="Run the selfie app.")
# Set all defaults to None, so that we can check if they were set by the user
# Defaults will be set in the configuration
parser.add_argument("--share", action="store_true", default=None, help="Share the API via ngrok")
parser.add_argument("--ngrok_enabled", action="store_true", default=None, help="Share the API via ngrok, requires token to be set")
parser.add_argument("--api_port", type=int, default=None, help="Specify the port to run on")
parser.add_argument("--gpu", default=None, action="store_true", help="Enable GPU support")
parser.add_argument("--reload", action="store_true", default=None, help="Enable hot-reloading")
Expand All @@ -76,22 +77,20 @@ def get_configured_app(shareable=False):
if 'verbose' in args and args.verbose:
logging.getLogger("selfie").setLevel(level=logging.DEBUG)

# TODO: Move these to the configuration
ngrok_auth_token = os.environ.get('NGROK_AUTHTOKEN', None)
ngrok_domain = os.environ.get('NGROK_DOMAIN', None)
logger.info("Creating app configuration")
app_config = create_app_config(**vars(args))

if shareable and args.share:
if ngrok_auth_token is None:
raise ValueError("NGROK_AUTHTOKEN environment variable is required to share the API. Visit https://dashboard.ngrok.com to get your token.")
if shareable and app_config.ngrok_enabled:
if app_config.ngrok_authtoken is None:
raise ValueError("ngrok_authtoken is required to share the API.")

listener = ngrok.forward(args.api_port, authtoken_from_env=True, domain=ngrok_domain)
listener = ngrok.forward(app_config.api_port, authtoken=app_config.ngrok_authtoken, domain=app_config.ngrok_domain)
logger.info(f"Application is available at {listener.url()}")
os.environ['SELFIE_HOST'] = listener.url()
del os.environ['SELFIE_API_PORT']

logger.info("Creating app configuration")

create_app_config(**vars(args))
# TODO: The idea was that these values could be used elsewhere in the app, but now that these values are synced with the database, this doesn't work. Figure out a way to make this work.
# os.environ["SELFIE_HOST"] = listener.url()
# del os.environ["SELFIE_API_PORT"] if "SELFIE_API_PORT" in os.environ else None
elif app_config.ngrok_enabled:
logger.warning("ngrok_enabled is set but ngrok_authtoken is not set. Disabling ngrok.")

# Ensure this import happens after configuration is set
from selfie.api import app
Expand Down
3 changes: 3 additions & 0 deletions selfie/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ class AppConfig(BaseModel):
embedding_chunk_size: int = Field(default=512, description="Embedding chunk size")
embedding_chunk_overlap: int = Field(default=50, description="Embedding chunk overlap")
environment_variables: dict = Field(default={}, description="Environment variables to load, used by e.g. litellm")
ngrok_enabled: bool = Field(default=False, description="Enable ngrok")
ngrok_authtoken: Optional[str] = Field(default=None, description="ngrok authentication token")
ngrok_domain: Optional[str] = Field(default=None, description="ngrok domain")

_runtime_overrides: dict = {}

Expand Down

0 comments on commit b014d48

Please sign in to comment.