@@ -43,7 +43,7 @@ def get_missing_required_env_vars() -> List[str]:
43
43
44
44
return missing
45
45
46
- def prompt_for_config (use_ngrok : bool = None , port : int = None , ngrok_auth_token : str = None , force_reconfigure : bool = False ) -> Dict [str , Any ]:
46
+ def prompt_for_config (use_ngrok : bool = None , port : int = None , ngrok_auth_token : Optional [ str ] = None , force_reconfigure : bool = False ) -> Dict [str , Any ]:
47
47
"""
48
48
Interactive prompt for configuration
49
49
"""
@@ -59,105 +59,153 @@ def prompt_for_config(use_ngrok: bool = None, port: int = None, ngrok_auth_token
59
59
# Override with provided parameters
60
60
if use_ngrok is not None :
61
61
config ["use_ngrok" ] = use_ngrok
62
+ # Set environment variable for use_ngrok
63
+ os .environ ["LOCALLAB_USE_NGROK" ] = str (use_ngrok ).lower ()
64
+
62
65
if port is not None :
63
66
config ["port" ] = port
67
+ os .environ ["LOCALLAB_PORT" ] = str (port )
68
+
64
69
if ngrok_auth_token is not None :
65
70
config ["ngrok_auth_token" ] = ngrok_auth_token
71
+ os .environ ["NGROK_AUTHTOKEN" ] = ngrok_auth_token
66
72
67
73
# Determine if we're in Colab
68
74
in_colab = is_in_colab ()
69
75
70
- # If in Colab, use simplified configuration
71
- if in_colab :
72
- # Set default values for Colab environment
73
- config .setdefault ("port" , 8000 )
74
- config .setdefault ("use_ngrok" , True )
75
- config .setdefault ("model_id" , os .environ .get ("HUGGINGFACE_MODEL" , DEFAULT_MODEL ))
76
-
77
- # Use ngrok token from environment if available
78
- if os .environ .get ("NGROK_AUTH_TOKEN" ):
79
- config ["ngrok_auth_token" ] = os .environ .get ("NGROK_AUTH_TOKEN" )
80
- elif ngrok_auth_token :
81
- config ["ngrok_auth_token" ] = ngrok_auth_token
82
-
83
- # Set some reasonable defaults for Colab
84
- config .setdefault ("enable_quantization" , True )
85
- config .setdefault ("quantization_type" , "int8" )
86
- config .setdefault ("enable_attention_slicing" , True )
87
- config .setdefault ("enable_flash_attention" , True )
88
- config .setdefault ("enable_better_transformer" , True )
89
-
90
- return config
91
-
92
- # Check for GPU
93
- has_gpu = False
94
- gpu_memory = get_gpu_memory ()
95
- if gpu_memory :
96
- has_gpu = True
97
- total_gpu_memory , free_gpu_memory = gpu_memory
98
- click .echo (f"🎮 GPU detected with { free_gpu_memory } MB free of { total_gpu_memory } MB total" )
99
- else :
100
- click .echo ("⚠️ No GPU detected. Running on CPU will be significantly slower." )
101
-
102
- # Get system memory
103
- total_memory , free_memory = get_system_memory ()
104
- click .echo (f"💾 System memory: { free_memory } MB free of { total_memory } MB total" )
105
-
106
- # Check for missing required environment variables
107
- missing_vars = get_missing_required_env_vars ()
108
-
109
- # Check if we have all required configuration and not forcing reconfiguration
110
- has_model = "model_id" in config or os .environ .get ("HUGGINGFACE_MODEL" ) or os .environ .get ("DEFAULT_MODEL" )
111
- has_port = "port" in config or port is not None
112
- has_ngrok_config = not in_colab or not config .get ("use_ngrok" , use_ngrok ) or "ngrok_auth_token" in config or ngrok_auth_token is not None or os .environ .get ("NGROK_AUTH_TOKEN" )
113
-
114
- # If we have all required config and not forcing reconfiguration, return early
115
- if not force_reconfigure and has_model and has_port and has_ngrok_config and not missing_vars :
116
- # Ensure port is set in config
117
- if "port" not in config and port is not None :
118
- config ["port" ] = port
119
- # Ensure use_ngrok is set in config
120
- if "use_ngrok" not in config and use_ngrok is not None :
121
- config ["use_ngrok" ] = use_ngrok
122
- # Ensure ngrok_auth_token is set in config if needed
123
- if config .get ("use_ngrok" , False ) and "ngrok_auth_token" not in config and ngrok_auth_token is not None :
124
- config ["ngrok_auth_token" ] = ngrok_auth_token
125
-
126
- return config
127
-
76
+ # If in Colab, ensure ngrok is enabled by default
77
+ if in_colab and "use_ngrok" not in config :
78
+ config ["use_ngrok" ] = True
79
+ os .environ ["LOCALLAB_USE_NGROK" ] = "true"
80
+
128
81
click .echo ("\n 🚀 Welcome to LocalLab! Let's set up your server.\n " )
129
82
130
- # Always ask for model when reconfiguring or if not provided
83
+ # Basic Configuration
84
+ # ------------------
85
+ click .echo ("\n 📋 Basic Configuration" )
86
+ click .echo ("─────────────────────" )
87
+
88
+ # Model selection
131
89
model_id = click .prompt (
132
90
"📦 Which model would you like to use?" ,
133
91
default = config .get ("model_id" , DEFAULT_MODEL )
134
92
)
135
- os .environ ["HUGGINGFACE_MODEL" ] = model_id
136
93
config ["model_id" ] = model_id
137
94
138
- # Always ask for port when reconfiguring or if not provided
95
+ # Port configuration
139
96
port = click .prompt (
140
97
"🔌 Which port would you like to run on?" ,
141
98
default = config .get ("port" , 8000 ),
142
99
type = int
143
100
)
144
101
config ["port" ] = port
145
102
146
- # Ask about ngrok
103
+ # Model Optimization Settings
104
+ # -------------------------
105
+ click .echo ("\n ⚡ Model Optimization Settings" )
106
+ click .echo ("─────────────────────────────" )
107
+
108
+ config ["enable_quantization" ] = click .confirm (
109
+ "Enable model quantization?" ,
110
+ default = config .get ("enable_quantization" , ENABLE_QUANTIZATION )
111
+ )
112
+
113
+ if config ["enable_quantization" ]:
114
+ config ["quantization_type" ] = click .prompt (
115
+ "Quantization type (fp16/int8/int4)" ,
116
+ default = config .get ("quantization_type" , QUANTIZATION_TYPE ),
117
+ type = click .Choice (["fp16" , "int8" , "int4" ])
118
+ )
119
+
120
+ config ["enable_cpu_offloading" ] = click .confirm (
121
+ "Enable CPU offloading?" ,
122
+ default = config .get ("enable_cpu_offloading" , ENABLE_CPU_OFFLOADING )
123
+ )
124
+
125
+ config ["enable_attention_slicing" ] = click .confirm (
126
+ "Enable attention slicing?" ,
127
+ default = config .get ("enable_attention_slicing" , ENABLE_ATTENTION_SLICING )
128
+ )
129
+
130
+ config ["enable_flash_attention" ] = click .confirm (
131
+ "Enable flash attention?" ,
132
+ default = config .get ("enable_flash_attention" , ENABLE_FLASH_ATTENTION )
133
+ )
134
+
135
+ config ["enable_better_transformer" ] = click .confirm (
136
+ "Enable better transformer?" ,
137
+ default = config .get ("enable_bettertransformer" , ENABLE_BETTERTRANSFORMER )
138
+ )
139
+
140
+ # Advanced Settings
141
+ # ----------------
142
+ click .echo ("\n ⚙️ Advanced Settings" )
143
+ click .echo ("──────────────────" )
144
+
145
+ config ["model_timeout" ] = click .prompt (
146
+ "Model timeout (seconds)" ,
147
+ default = config .get ("model_timeout" , 3600 ),
148
+ type = int
149
+ )
150
+
151
+ # Cache Settings
152
+ # -------------
153
+ click .echo ("\n 💾 Cache Settings" )
154
+ click .echo ("────────────────" )
155
+
156
+ config ["enable_cache" ] = click .confirm (
157
+ "Enable response caching?" ,
158
+ default = config .get ("enable_cache" , True )
159
+ )
160
+
161
+ if config ["enable_cache" ]:
162
+ config ["cache_ttl" ] = click .prompt (
163
+ "Cache TTL (seconds)" ,
164
+ default = config .get ("cache_ttl" , 3600 ),
165
+ type = int
166
+ )
167
+
168
+ # Logging Settings
169
+ # ---------------
170
+ click .echo ("\n 📝 Logging Settings" )
171
+ click .echo ("──────────────────" )
172
+
173
+ config ["log_level" ] = click .prompt (
174
+ "Log level" ,
175
+ default = config .get ("log_level" , "INFO" ),
176
+ type = click .Choice (["DEBUG" , "INFO" , "WARNING" , "ERROR" , "CRITICAL" ])
177
+ )
178
+
179
+ config ["enable_file_logging" ] = click .confirm (
180
+ "Enable file logging?" ,
181
+ default = config .get ("enable_file_logging" , False )
182
+ )
183
+
184
+ if config ["enable_file_logging" ]:
185
+ config ["log_file" ] = click .prompt (
186
+ "Log file path" ,
187
+ default = config .get ("log_file" , "locallab.log" )
188
+ )
189
+
190
+ # Ngrok Configuration
191
+ # ------------------
192
+ click .echo ("\n 🌐 Ngrok Configuration" )
193
+ click .echo ("────────────────────" )
194
+
147
195
use_ngrok = click .confirm (
148
- "🌐 Do you want to enable public access via ngrok?" ,
196
+ "Enable public access via ngrok?" ,
149
197
default = config .get ("use_ngrok" , in_colab )
150
198
)
151
199
config ["use_ngrok" ] = use_ngrok
200
+ os .environ ["LOCALLAB_USE_NGROK" ] = str (use_ngrok ).lower ()
152
201
153
202
if use_ngrok :
154
- # Show current token if exists
155
203
current_token = config .get ("ngrok_auth_token" ) or get_env_var (NGROK_TOKEN_ENV )
156
204
if current_token :
157
205
click .echo (f"\n Current ngrok token: { current_token } " )
158
206
159
207
ngrok_auth_token = click .prompt (
160
- "🔑 Enter your ngrok auth token (get one at https://dashboard.ngrok.com/get-started/your-authtoken)" ,
208
+ "Enter your ngrok auth token (get one at https://dashboard.ngrok.com/get-started/your-authtoken)" ,
161
209
default = current_token ,
162
210
type = str ,
163
211
show_default = True
@@ -166,18 +214,26 @@ def prompt_for_config(use_ngrok: bool = None, port: int = None, ngrok_auth_token
166
214
if ngrok_auth_token :
167
215
token_str = str (ngrok_auth_token ).strip ()
168
216
config ["ngrok_auth_token" ] = token_str
169
- set_env_var (NGROK_TOKEN_ENV , token_str )
170
- click .echo (f"✅ Ngrok token saved: { token_str } " )
217
+ # Set both environment variables to ensure compatibility
218
+ os .environ ["NGROK_AUTHTOKEN" ] = token_str
219
+ os .environ ["LOCALLAB_NGROK_AUTH_TOKEN" ] = token_str
220
+
221
+ # Save immediately to ensure persistence
222
+ from .config import save_config
223
+ save_config (config )
224
+ click .echo (f"✅ Ngrok token saved and activated" )
225
+
226
+ # HuggingFace Token
227
+ # ----------------
228
+ click .echo ("\n 🤗 HuggingFace Token" )
229
+ click .echo ("──────────────────" )
171
230
172
- # Ask about HuggingFace token
173
231
current_hf_token = config .get ("huggingface_token" ) or get_env_var (HF_TOKEN_ENV )
174
232
if current_hf_token :
175
- click .echo (f"\n Current HuggingFace token: { current_hf_token } " )
233
+ click .echo (f"Current HuggingFace token: { current_hf_token } " )
176
234
177
235
if not current_hf_token or force_reconfigure :
178
- click .echo ("\n 🔑 HuggingFace Token Configuration" )
179
- click .echo ("───────────────────────────────" )
180
- click .echo ("A token is required to download models like microsoft/phi-2" )
236
+ click .echo ("\n A token is required to download models." )
181
237
click .echo ("Get your token from: https://huggingface.co/settings/tokens" )
182
238
183
239
hf_token = click .prompt (
@@ -190,16 +246,14 @@ def prompt_for_config(use_ngrok: bool = None, port: int = None, ngrok_auth_token
190
246
if hf_token :
191
247
if len (hf_token ) < 20 :
192
248
click .echo ("❌ Invalid token format. Token should be longer than 20 characters." )
193
- return config
194
-
195
- token_str = str (hf_token ).strip ()
196
- config ["huggingface_token" ] = token_str
197
- set_env_var (HF_TOKEN_ENV , token_str )
198
- click .echo (f"✅ HuggingFace token saved: { token_str } " )
199
-
200
- # Save immediately
201
- from .config import save_config
202
- save_config (config )
249
+ else :
250
+ token_str = str (hf_token ).strip ()
251
+ config ["huggingface_token" ] = token_str
252
+ set_env_var (HF_TOKEN_ENV , token_str )
253
+
254
+ # Save immediately
255
+ from .config import save_config
256
+ save_config (config )
203
257
else :
204
258
click .echo ("\n ⚠️ No token provided. Some models may not be accessible." )
205
259
0 commit comments