@@ -1462,10 +1462,24 @@ def handle_data(self, data):
14621462 websearch_lastresponse = searchresults
14631463 return searchresults
14641464
1465- #################################################################
1466- ### A hacky simple HTTP server simulating a kobold api by Concedo
1467- ### we are intentionally NOT using flask, because we want MINIMAL dependencies
1468- #################################################################
1465+ def is_port_in_use (portNum ):
1466+ try :
1467+ import socket
1468+ with socket .socket (socket .AF_INET , socket .SOCK_STREAM ) as s :
1469+ return s .connect_ex (('localhost' , portNum )) == 0
1470+ except Exception :
1471+ return True
1472+
1473+ def is_ipv6_supported ():
1474+ try :
1475+ # Attempt to create an IPv6 socket
1476+ sock = socket .socket (socket .AF_INET6 , socket .SOCK_STREAM )
1477+ sock .setsockopt (socket .SOL_SOCKET , socket .SO_REUSEADDR , 1 )
1478+ sock .setsockopt (socket .IPPROTO_IPV6 , socket .IPV6_V6ONLY , 1 )
1479+ sock .close ()
1480+ return True
1481+ except Exception :
1482+ return False
14691483
14701484# Used to parse json for openai tool calls
14711485def extract_json_from_string (input_string ):
@@ -1720,6 +1734,26 @@ def transform_genparams(genparams, api_format):
17201734 genparams ["prompt" ] = ollamasysprompt + ollamabodyprompt
17211735 return genparams
17221736
1737+ def LaunchWebbrowser (target_url , failedmsg ):
1738+ try :
1739+ import webbrowser as wb
1740+ if wb .open (target_url , autoraise = True ):
1741+ return
1742+ raise RuntimeError ("Cannot open default browser" )
1743+ except Exception :
1744+ try :
1745+ import webbrowser as wb
1746+ if wb .get ('xdg-open' ).open (target_url , autoraise = True ):
1747+ return
1748+ raise RuntimeError ("Cannot open xdg-open browser" )
1749+ except Exception :
1750+ print (failedmsg )
1751+ print (f"Please manually open your browser to { target_url } " )
1752+
1753+ #################################################################
1754+ ### A hacky simple HTTP server simulating a kobold api by Concedo
1755+ ### we are intentionally NOT using flask, because we want MINIMAL dependencies
1756+ #################################################################
17231757class ServerRequestHandler (http .server .SimpleHTTPRequestHandler ):
17241758 sys_version = ""
17251759 server_version = "ConcedoLlamaForKoboldServer"
@@ -2672,25 +2706,6 @@ def end_headers(self, content_type=None):
26722706 self .send_header ('content-type' , content_type )
26732707 return super (ServerRequestHandler , self ).end_headers ()
26742708
2675- def is_port_in_use (portNum ):
2676- try :
2677- import socket
2678- with socket .socket (socket .AF_INET , socket .SOCK_STREAM ) as s :
2679- return s .connect_ex (('localhost' , portNum )) == 0
2680- except Exception :
2681- return True
2682-
2683- def is_ipv6_supported ():
2684- try :
2685- # Attempt to create an IPv6 socket
2686- sock = socket .socket (socket .AF_INET6 , socket .SOCK_STREAM )
2687- sock .setsockopt (socket .SOL_SOCKET , socket .SO_REUSEADDR , 1 )
2688- sock .setsockopt (socket .IPPROTO_IPV6 , socket .IPV6_V6ONLY , 1 )
2689- sock .close ()
2690- return True
2691- except Exception :
2692- return False
2693-
26942709def RunServerMultiThreaded (addr , port ):
26952710 global exitcounter , sslvalid
26962711 global embedded_kailite , embedded_kcpp_docs , embedded_kcpp_sdui
@@ -3971,23 +3986,13 @@ def load_config_gui(): #this is used to populate the GUI with a config file, whe
39713986 pass
39723987
39733988 def display_help ():
3974- try :
3975- import webbrowser as wb
3976- wb .open ("https://github.com/LostRuins/koboldcpp/wiki" )
3977- except Exception :
3978- print ("Cannot launch help in browser." )
3989+ LaunchWebbrowser ("https://github.com/LostRuins/koboldcpp/wiki" ,"Cannot launch help in browser." )
3990+
39793991 def display_help_models ():
3980- try :
3981- import webbrowser as wb
3982- wb .open ("https://github.com/LostRuins/koboldcpp/wiki#what-models-does-koboldcpp-support-what-architectures-are-supported" )
3983- except Exception :
3984- print ("Cannot launch help in browser." )
3992+ LaunchWebbrowser ("https://github.com/LostRuins/koboldcpp/wiki#what-models-does-koboldcpp-support-what-architectures-are-supported" ,"Cannot launch help in browser." )
3993+
39853994 def display_updates ():
3986- try :
3987- import webbrowser as wb
3988- wb .open ("https://github.com/LostRuins/koboldcpp/releases/latest" )
3989- except Exception :
3990- print ("Cannot launch updates in browser." )
3995+ LaunchWebbrowser ("https://github.com/LostRuins/koboldcpp/releases/latest" ,"Cannot launch updates in browser." )
39913996
39923997 ctk .CTkButton (tabs , text = "Launch" , fg_color = "#2f8d3c" , hover_color = "#2faa3c" , command = guilaunch , width = 80 , height = 35 ).grid (row = 1 ,column = 1 , stick = "se" , padx = 25 , pady = 5 )
39933998
@@ -4546,8 +4551,11 @@ def analyze_gguf_model(args,filename):
45464551
45474552def analyze_gguf_model_wrapper (filename = "" ):
45484553 if not filename or filename == "" :
4549- from tkinter .filedialog import askopenfilename
4550- filename = askopenfilename (title = "Select GGUF to analyze" )
4554+ try :
4555+ from tkinter .filedialog import askopenfilename
4556+ filename = askopenfilename (title = "Select GGUF to analyze" )
4557+ except Exception as e :
4558+ print (f"Cannot select file to analyze: { e } " )
45514559 if not filename or filename == "" or not os .path .exists (filename ):
45524560 print ("Selected GGUF file not found. Please select a valid GGUF file to analyze." )
45534561 return
@@ -5047,11 +5055,7 @@ def main(launch_args,start_server=True):
50475055 print (f"StableUI is available at { epurl } /sdui/" )
50485056
50495057 if args .launch :
5050- try :
5051- import webbrowser as wb
5052- wb .open (epurl )
5053- except Exception :
5054- print ("--launch was set, but could not launch web browser automatically." )
5058+ LaunchWebbrowser (epurl ,"--launch was set, but could not launch web browser automatically." )
50555059
50565060 if args .hordekey and args .hordekey != "" :
50575061 if args .hordeworkername and args .hordeworkername != "" :
0 commit comments