Spaces:
Runtime error
Runtime error
Delete App_Function_Libraries/Gradio_UI/Llamafile_tab.py
Browse files
App_Function_Libraries/Gradio_UI/Llamafile_tab.py
DELETED
|
@@ -1,312 +0,0 @@
|
|
| 1 |
-
# Llamafile_tab.py
|
| 2 |
-
# Description: Gradio interface for configuring and launching Llamafile with Local LLMs
|
| 3 |
-
|
| 4 |
-
# Imports
|
| 5 |
-
import os
|
| 6 |
-
import logging
|
| 7 |
-
from typing import Tuple, Optional
|
| 8 |
-
import gradio as gr
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
from App_Function_Libraries.Local_LLM.Local_LLM_Inference_Engine_Lib import (
|
| 12 |
-
download_llm_model,
|
| 13 |
-
llm_models,
|
| 14 |
-
start_llamafile,
|
| 15 |
-
get_gguf_llamafile_files
|
| 16 |
-
)
|
| 17 |
-
#
|
| 18 |
-
#######################################################################################################################
|
| 19 |
-
#
|
| 20 |
-
# Functions:
|
| 21 |
-
|
| 22 |
-
def create_chat_with_llamafile_tab():
|
| 23 |
-
# Function to update model path based on selection
|
| 24 |
-
def on_local_model_change(selected_model: str, search_directory: str) -> str:
|
| 25 |
-
if selected_model and isinstance(search_directory, str):
|
| 26 |
-
model_path = os.path.abspath(os.path.join(search_directory, selected_model))
|
| 27 |
-
logging.debug(f"Selected model path: {model_path}") # Debug print for selected model path
|
| 28 |
-
return model_path
|
| 29 |
-
return "Invalid selection or directory."
|
| 30 |
-
|
| 31 |
-
# Function to update the dropdown with available models
|
| 32 |
-
def update_dropdowns(search_directory: str) -> Tuple[dict, str]:
|
| 33 |
-
logging.debug(f"User-entered directory: {search_directory}") # Debug print for directory
|
| 34 |
-
if not os.path.isdir(search_directory):
|
| 35 |
-
logging.debug(f"Directory does not exist: {search_directory}") # Debug print for non-existing directory
|
| 36 |
-
return gr.update(choices=[], value=None), "Directory does not exist."
|
| 37 |
-
|
| 38 |
-
logging.debug(f"Directory exists: {search_directory}, scanning for files...") # Confirm directory exists
|
| 39 |
-
model_files = get_gguf_llamafile_files(search_directory)
|
| 40 |
-
|
| 41 |
-
if not model_files:
|
| 42 |
-
logging.debug(f"No model files found in {search_directory}") # Debug print for no files found
|
| 43 |
-
return gr.update(choices=[], value=None), "No model files found in the specified directory."
|
| 44 |
-
|
| 45 |
-
# Update the dropdown choices with the model files found
|
| 46 |
-
logging.debug(f"Models loaded from {search_directory}: {model_files}") # Debug: Print model files loaded
|
| 47 |
-
return gr.update(choices=model_files, value=None), f"Models loaded from {search_directory}."
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
def download_preset_model(selected_model: str) -> Tuple[str, str]:
|
| 52 |
-
"""
|
| 53 |
-
Downloads the selected preset model.
|
| 54 |
-
|
| 55 |
-
Args:
|
| 56 |
-
selected_model (str): The key of the selected preset model.
|
| 57 |
-
|
| 58 |
-
Returns:
|
| 59 |
-
Tuple[str, str]: Status message and the path to the downloaded model.
|
| 60 |
-
"""
|
| 61 |
-
model_info = llm_models.get(selected_model)
|
| 62 |
-
if not model_info:
|
| 63 |
-
return "Invalid model selection.", ""
|
| 64 |
-
|
| 65 |
-
try:
|
| 66 |
-
model_path = download_llm_model(
|
| 67 |
-
model_name=model_info["name"],
|
| 68 |
-
model_url=model_info["url"],
|
| 69 |
-
model_filename=model_info["filename"],
|
| 70 |
-
model_hash=model_info["hash"]
|
| 71 |
-
)
|
| 72 |
-
return f"Model '{model_info['name']}' downloaded successfully.", model_path
|
| 73 |
-
except Exception as e:
|
| 74 |
-
logging.error(f"Error downloading model: {e}")
|
| 75 |
-
return f"Failed to download model: {e}", ""
|
| 76 |
-
|
| 77 |
-
with gr.TabItem("Local LLM with Llamafile", visible=True):
|
| 78 |
-
gr.Markdown("# Settings for Llamafile")
|
| 79 |
-
|
| 80 |
-
with gr.Row():
|
| 81 |
-
with gr.Column():
|
| 82 |
-
am_noob = gr.Checkbox(label="Enable Sane Defaults", value=False, visible=True)
|
| 83 |
-
advanced_mode_toggle = gr.Checkbox(label="Advanced Mode - Show All Settings", value=False)
|
| 84 |
-
# Advanced Inputs
|
| 85 |
-
verbose_checked = gr.Checkbox(label="Enable Verbose Output", value=False, visible=False)
|
| 86 |
-
threads_checked = gr.Checkbox(label="Set CPU Threads", value=False, visible=False)
|
| 87 |
-
threads_value = gr.Number(label="Number of CPU Threads", value=None, precision=0, visible=False)
|
| 88 |
-
threads_batched_checked = gr.Checkbox(label="Enable Batched Inference", value=False, visible=False)
|
| 89 |
-
threads_batched_value = gr.Number(label="Batch Size for Inference", value=None, precision=0, visible=False)
|
| 90 |
-
model_alias_checked = gr.Checkbox(label="Set Model Alias", value=False, visible=False)
|
| 91 |
-
model_alias_value = gr.Textbox(label="Model Alias", value="", visible=False)
|
| 92 |
-
ctx_size_checked = gr.Checkbox(label="Set Prompt Context Size", value=False, visible=False)
|
| 93 |
-
ctx_size_value = gr.Number(label="Prompt Context Size", value=8124, precision=0, visible=False)
|
| 94 |
-
ngl_checked = gr.Checkbox(label="Enable GPU Layers", value=False, visible=True)
|
| 95 |
-
ngl_value = gr.Number(label="Number of GPU Layers", value=None, precision=0, visible=True)
|
| 96 |
-
batch_size_checked = gr.Checkbox(label="Set Batch Size", value=False, visible=False)
|
| 97 |
-
batch_size_value = gr.Number(label="Batch Size", value=512, visible=False)
|
| 98 |
-
memory_f32_checked = gr.Checkbox(label="Use 32-bit Floating Point", value=False, visible=False)
|
| 99 |
-
numa_checked = gr.Checkbox(label="Enable NUMA", value=False, visible=False)
|
| 100 |
-
server_timeout_value = gr.Number(label="Server Timeout", value=600, precision=0, visible=False)
|
| 101 |
-
host_checked = gr.Checkbox(label="Set IP to Listen On", value=False, visible=False)
|
| 102 |
-
host_value = gr.Textbox(label="Host IP Address", value="", visible=False)
|
| 103 |
-
port_checked = gr.Checkbox(label="Set Server Port", value=False, visible=False)
|
| 104 |
-
port_value = gr.Number(label="Port Number", value=8080, precision=0, visible=False)
|
| 105 |
-
api_key_checked = gr.Checkbox(label="Set API Key", value=False, visible=False)
|
| 106 |
-
api_key_value = gr.Textbox(label="API Key", value="", visible=False)
|
| 107 |
-
http_threads_checked = gr.Checkbox(label="Set HTTP Server Threads", value=False, visible=False)
|
| 108 |
-
http_threads_value = gr.Number(label="Number of HTTP Server Threads", value=None, precision=0, visible=False)
|
| 109 |
-
hf_repo_checked = gr.Checkbox(label="Use Huggingface Repo Model", value=False, visible=False)
|
| 110 |
-
hf_repo_value = gr.Textbox(label="Huggingface Repo Name", value="", visible=False)
|
| 111 |
-
hf_file_checked = gr.Checkbox(label="Set Huggingface Model File", value=False, visible=False)
|
| 112 |
-
hf_file_value = gr.Textbox(label="Huggingface Model File", value="", visible=False)
|
| 113 |
-
|
| 114 |
-
with gr.Column():
|
| 115 |
-
# Model Selection Section
|
| 116 |
-
gr.Markdown("## Model Selection")
|
| 117 |
-
|
| 118 |
-
# Option 1: Select from Local Filesystem
|
| 119 |
-
with gr.Row():
|
| 120 |
-
search_directory = gr.Textbox(label="Model Directory",
|
| 121 |
-
placeholder="Enter directory path(currently '.\Models')",
|
| 122 |
-
value=".\Models",
|
| 123 |
-
interactive=True)
|
| 124 |
-
|
| 125 |
-
# Initial population of local models
|
| 126 |
-
initial_dropdown_update, _ = update_dropdowns(".\Models")
|
| 127 |
-
refresh_button = gr.Button("Refresh Models")
|
| 128 |
-
local_model_dropdown = gr.Dropdown(label="Select Model from Directory", choices=[])
|
| 129 |
-
# Display selected model path
|
| 130 |
-
model_value = gr.Textbox(label="Selected Model File Path", value="", interactive=False)
|
| 131 |
-
|
| 132 |
-
# Option 2: Download Preset Models
|
| 133 |
-
gr.Markdown("## Download Preset Models")
|
| 134 |
-
|
| 135 |
-
preset_model_dropdown = gr.Dropdown(
|
| 136 |
-
label="Select a Preset Model",
|
| 137 |
-
choices=list(llm_models.keys()),
|
| 138 |
-
value=None,
|
| 139 |
-
interactive=True,
|
| 140 |
-
info="Choose a preset model to download."
|
| 141 |
-
)
|
| 142 |
-
download_preset_button = gr.Button("Download Selected Preset")
|
| 143 |
-
|
| 144 |
-
with gr.Row():
|
| 145 |
-
with gr.Column():
|
| 146 |
-
start_button = gr.Button("Start Llamafile")
|
| 147 |
-
stop_button = gr.Button("Stop Llamafile (doesn't work)")
|
| 148 |
-
output_display = gr.Markdown()
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
# Show/hide advanced inputs based on toggle
|
| 152 |
-
def update_visibility(show_advanced: bool):
|
| 153 |
-
components = [
|
| 154 |
-
verbose_checked, threads_checked, threads_value,
|
| 155 |
-
http_threads_checked, http_threads_value,
|
| 156 |
-
hf_repo_checked, hf_repo_value,
|
| 157 |
-
hf_file_checked, hf_file_value,
|
| 158 |
-
ctx_size_checked, ctx_size_value,
|
| 159 |
-
ngl_checked, ngl_value,
|
| 160 |
-
host_checked, host_value,
|
| 161 |
-
port_checked, port_value
|
| 162 |
-
]
|
| 163 |
-
return [gr.update(visible=show_advanced) for _ in components]
|
| 164 |
-
|
| 165 |
-
def on_start_button_click(
|
| 166 |
-
am_noob: bool,
|
| 167 |
-
verbose_checked: bool,
|
| 168 |
-
threads_checked: bool,
|
| 169 |
-
threads_value: Optional[int],
|
| 170 |
-
threads_batched_checked: bool,
|
| 171 |
-
threads_batched_value: Optional[int],
|
| 172 |
-
model_alias_checked: bool,
|
| 173 |
-
model_alias_value: str,
|
| 174 |
-
http_threads_checked: bool,
|
| 175 |
-
http_threads_value: Optional[int],
|
| 176 |
-
model_value: str,
|
| 177 |
-
hf_repo_checked: bool,
|
| 178 |
-
hf_repo_value: str,
|
| 179 |
-
hf_file_checked: bool,
|
| 180 |
-
hf_file_value: str,
|
| 181 |
-
ctx_size_checked: bool,
|
| 182 |
-
ctx_size_value: Optional[int],
|
| 183 |
-
ngl_checked: bool,
|
| 184 |
-
ngl_value: Optional[int],
|
| 185 |
-
batch_size_checked: bool,
|
| 186 |
-
batch_size_value: Optional[int],
|
| 187 |
-
memory_f32_checked: bool,
|
| 188 |
-
numa_checked: bool,
|
| 189 |
-
server_timeout_value: Optional[int],
|
| 190 |
-
host_checked: bool,
|
| 191 |
-
host_value: str,
|
| 192 |
-
port_checked: bool,
|
| 193 |
-
port_value: Optional[int],
|
| 194 |
-
api_key_checked: bool,
|
| 195 |
-
api_key_value: str
|
| 196 |
-
) -> str:
|
| 197 |
-
"""
|
| 198 |
-
Event handler for the Start Llamafile button.
|
| 199 |
-
"""
|
| 200 |
-
try:
|
| 201 |
-
result = start_llamafile(
|
| 202 |
-
am_noob,
|
| 203 |
-
verbose_checked,
|
| 204 |
-
threads_checked,
|
| 205 |
-
threads_value,
|
| 206 |
-
threads_batched_checked,
|
| 207 |
-
threads_batched_value,
|
| 208 |
-
model_alias_checked,
|
| 209 |
-
model_alias_value,
|
| 210 |
-
http_threads_checked,
|
| 211 |
-
http_threads_value,
|
| 212 |
-
model_value,
|
| 213 |
-
hf_repo_checked,
|
| 214 |
-
hf_repo_value,
|
| 215 |
-
hf_file_checked,
|
| 216 |
-
hf_file_value,
|
| 217 |
-
ctx_size_checked,
|
| 218 |
-
ctx_size_value,
|
| 219 |
-
ngl_checked,
|
| 220 |
-
ngl_value,
|
| 221 |
-
batch_size_checked,
|
| 222 |
-
batch_size_value,
|
| 223 |
-
memory_f32_checked,
|
| 224 |
-
numa_checked,
|
| 225 |
-
server_timeout_value,
|
| 226 |
-
host_checked,
|
| 227 |
-
host_value,
|
| 228 |
-
port_checked,
|
| 229 |
-
port_value,
|
| 230 |
-
api_key_checked,
|
| 231 |
-
api_key_value
|
| 232 |
-
)
|
| 233 |
-
return result
|
| 234 |
-
except Exception as e:
|
| 235 |
-
logging.error(f"Error starting Llamafile: {e}")
|
| 236 |
-
return f"Failed to start Llamafile: {e}"
|
| 237 |
-
|
| 238 |
-
advanced_mode_toggle.change(
|
| 239 |
-
fn=update_visibility,
|
| 240 |
-
inputs=[advanced_mode_toggle],
|
| 241 |
-
outputs=[
|
| 242 |
-
verbose_checked, threads_checked, threads_value,
|
| 243 |
-
http_threads_checked, http_threads_value,
|
| 244 |
-
hf_repo_checked, hf_repo_value,
|
| 245 |
-
hf_file_checked, hf_file_value,
|
| 246 |
-
ctx_size_checked, ctx_size_value,
|
| 247 |
-
ngl_checked, ngl_value,
|
| 248 |
-
host_checked, host_value,
|
| 249 |
-
port_checked, port_value
|
| 250 |
-
]
|
| 251 |
-
)
|
| 252 |
-
|
| 253 |
-
start_button.click(
|
| 254 |
-
fn=on_start_button_click,
|
| 255 |
-
inputs=[
|
| 256 |
-
am_noob,
|
| 257 |
-
verbose_checked,
|
| 258 |
-
threads_checked,
|
| 259 |
-
threads_value,
|
| 260 |
-
threads_batched_checked,
|
| 261 |
-
threads_batched_value,
|
| 262 |
-
model_alias_checked,
|
| 263 |
-
model_alias_value,
|
| 264 |
-
http_threads_checked,
|
| 265 |
-
http_threads_value,
|
| 266 |
-
model_value,
|
| 267 |
-
hf_repo_checked,
|
| 268 |
-
hf_repo_value,
|
| 269 |
-
hf_file_checked,
|
| 270 |
-
hf_file_value,
|
| 271 |
-
ctx_size_checked,
|
| 272 |
-
ctx_size_value,
|
| 273 |
-
ngl_checked,
|
| 274 |
-
ngl_value,
|
| 275 |
-
batch_size_checked,
|
| 276 |
-
batch_size_value,
|
| 277 |
-
memory_f32_checked,
|
| 278 |
-
numa_checked,
|
| 279 |
-
server_timeout_value,
|
| 280 |
-
host_checked,
|
| 281 |
-
host_value,
|
| 282 |
-
port_checked,
|
| 283 |
-
port_value,
|
| 284 |
-
api_key_checked,
|
| 285 |
-
api_key_value
|
| 286 |
-
],
|
| 287 |
-
outputs=output_display
|
| 288 |
-
)
|
| 289 |
-
|
| 290 |
-
download_preset_button.click(
|
| 291 |
-
fn=download_preset_model,
|
| 292 |
-
inputs=[preset_model_dropdown],
|
| 293 |
-
outputs=[output_display, model_value]
|
| 294 |
-
)
|
| 295 |
-
|
| 296 |
-
# Click event for refreshing models
|
| 297 |
-
refresh_button.click(
|
| 298 |
-
fn=update_dropdowns,
|
| 299 |
-
inputs=[search_directory], # Ensure that the directory path (string) is passed
|
| 300 |
-
outputs=[local_model_dropdown, output_display] # Update dropdown and status
|
| 301 |
-
)
|
| 302 |
-
|
| 303 |
-
# Event to update model_value when a model is selected from the dropdown
|
| 304 |
-
local_model_dropdown.change(
|
| 305 |
-
fn=on_local_model_change, # Function that calculates the model path
|
| 306 |
-
inputs=[local_model_dropdown, search_directory], # Inputs: selected model and directory
|
| 307 |
-
outputs=[model_value] # Output: Update the model_value textbox with the selected model path
|
| 308 |
-
)
|
| 309 |
-
|
| 310 |
-
#
|
| 311 |
-
#
|
| 312 |
-
#######################################################################################################################
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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