@@ -630,6 +630,52 @@ async def handle_request(self, genparams, api_format, stream_flag):
630
630
except Exception as e :
631
631
print (e )
632
632
633
+ def noscript_webui (self , path ):
634
+ global modelbusy
635
+ import urllib .parse as urlparse
636
+ parsed_url = urlparse .urlparse (path )
637
+ parsed_dict = urlparse .parse_qs (parsed_url .query )
638
+ status = "Error"
639
+ reply = ""
640
+ prompt = parsed_dict ['prompt' ][0 ] if 'prompt' in parsed_dict else ""
641
+ max_length = parsed_dict ['max_length' ][0 ] if 'max_length' in parsed_dict else 100
642
+ temperature = parsed_dict ['temperature' ][0 ] if 'temperature' in parsed_dict else 0.7
643
+ top_k = parsed_dict ['top_k' ][0 ] if 'top_k' in parsed_dict else 100
644
+ top_p = parsed_dict ['top_p' ][0 ] if 'top_p' in parsed_dict else 0.9
645
+ rep_pen = parsed_dict ['rep_pen' ][0 ] if 'rep_pen' in parsed_dict else 1.1
646
+ gencommand = (parsed_dict ['generate' ][0 ] if 'generate' in parsed_dict else "" )== "Generate"
647
+
648
+ if prompt == "" or not gencommand or max_length <= 0 :
649
+ status = "Ready To Generate"
650
+ elif modelbusy .locked ():
651
+ status = "Model is busy, try again later."
652
+ else :
653
+ epurl = f"http://localhost:{ args .port } "
654
+ if args .host != "" :
655
+ epurl = f"http://{ args .host } :{ args .port } "
656
+ gen_payload = {"prompt" : prompt ,"max_length" : max_length ,"temperature" : temperature ,"prompt" : prompt ,"top_k" : top_k ,"top_p" : top_p ,"rep_pen" : rep_pen }
657
+ respjson = make_url_request (f'{ epurl } /api/v1/generate' , gen_payload )
658
+ reply = respjson ["results" ][0 ]["text" ]
659
+ status = "Generation Completed"
660
+
661
+ finalhtml = f'''
662
+ <!DOCTYPE html>
663
+ <html><head><title>KoboldCpp NoScript Mode</title></head><body>
664
+ <h2>KoboldCpp NoScript Mode</h2>
665
+ <p>KoboldCpp can be used without Javascript enabled, however this is not recommended.
666
+ <br>If you have Javascript, please use <a href="/">Kobold Lite WebUI</a> instead.</p><hr/>
667
+ <form action="/noscript">
668
+ Enter Prompt:<br>
669
+ <textarea name="prompt" cols="60" rows="7" placeholder="Enter Prompt Here">{ prompt + reply } </textarea>
670
+ <hr/>
671
+ { status } <br>
672
+ <hr/>
673
+ <input type="submit" name="generate" value="Generate">
674
+ </form>
675
+ </body>
676
+ </html>
677
+ '''
678
+ return finalhtml
633
679
634
680
def do_GET (self ):
635
681
global maxctx , maxhordelen , friendlymodelname , KcppVersion , totalgens , preloaded_story
@@ -644,6 +690,10 @@ def do_GET(self):
644
690
else :
645
691
response_body = self .embedded_kailite
646
692
693
+ elif self .path in ["/noscript" , "/noscript?" ] or self .path .startswith (('/noscript?' ,'noscript?' )): #it's possible for the root url to have ?params without /
694
+ content_type = 'text/html'
695
+ response_body = (self .noscript_webui (self .path )).encode ('utf-8' )
696
+
647
697
elif self .path .endswith (('/api/v1/model' , '/api/latest/model' )):
648
698
response_body = (json .dumps ({'result' : friendlymodelname }).encode ())
649
699
@@ -1744,21 +1794,47 @@ def show_gui_msgbox(title,message):
1744
1794
except Exception as ex2 :
1745
1795
pass
1746
1796
1797
+ def print_with_time (txt ):
1798
+ from datetime import datetime
1799
+ print (f"{ datetime .now ().strftime ('[%H:%M:%S]' )} " + txt )
1800
+
1801
+ def make_url_request (url , data , method = 'POST' , headers = {}):
1802
+ import urllib .request
1803
+ try :
1804
+ request = None
1805
+ if method == 'POST' :
1806
+ json_payload = json .dumps (data ).encode ('utf-8' )
1807
+ request = urllib .request .Request (url , data = json_payload , headers = headers , method = method )
1808
+ request .add_header ('content-type' , 'application/json' )
1809
+ else :
1810
+ request = urllib .request .Request (url , headers = headers , method = method )
1811
+ response_data = ""
1812
+ with urllib .request .urlopen (request ) as response :
1813
+ response_data = response .read ().decode ('utf-8' )
1814
+ json_response = json .loads (response_data )
1815
+ return json_response
1816
+ except urllib .error .HTTPError as e :
1817
+ try :
1818
+ errmsg = e .read ().decode ('utf-8' )
1819
+ print_with_time (f"Error: { e } - { errmsg } " )
1820
+ except Exception as e :
1821
+ print_with_time (f"Error: { e } " )
1822
+ return None
1823
+ except Exception as e :
1824
+ print_with_time (f"Error: { e } - { response_data } " )
1825
+ return None
1826
+
1747
1827
#A very simple and stripped down embedded horde worker with no dependencies
1748
1828
def run_horde_worker (args , api_key , worker_name ):
1749
- import urllib .request
1750
1829
from datetime import datetime
1751
1830
global friendlymodelname , maxhordectx , maxhordelen , exitcounter , punishcounter , modelbusy , session_starttime
1752
1831
epurl = f"http://localhost:{ args .port } "
1753
1832
if args .host != "" :
1754
1833
epurl = f"http://{ args .host } :{ args .port } "
1755
1834
1756
- def print_with_time (txt ):
1757
- print (f"{ datetime .now ().strftime ('[%H:%M:%S]' )} " + txt )
1758
-
1759
1835
def submit_completed_generation (url , jobid , sessionstart , submit_dict ):
1760
1836
global exitcounter , punishcounter , session_kudos_earned , session_jobs , rewardcounter
1761
- reply = make_url_request (url , submit_dict )
1837
+ reply = make_url_request_horde (url , submit_dict )
1762
1838
if not reply :
1763
1839
punishcounter += 1
1764
1840
print_with_time (f"Error, Job submit failed." )
@@ -1780,31 +1856,12 @@ def submit_completed_generation(url, jobid, sessionstart, submit_dict):
1780
1856
if exitcounter >= 1 :
1781
1857
exitcounter -= 1
1782
1858
1783
- def make_url_request (url , data , method = 'POST' ):
1784
- try :
1785
- request = None
1786
- headers = {"apikey" : api_key ,'User-Agent' :'KoboldCppEmbeddedWorkerV2' ,'Client-Agent' :'KoboldCppEmbedWorker:2' }
1787
- if method == 'POST' :
1788
- json_payload = json .dumps (data ).encode ('utf-8' )
1789
- request = urllib .request .Request (url , data = json_payload , headers = headers , method = method )
1790
- request .add_header ('content-type' , 'application/json' )
1791
- else :
1792
- request = urllib .request .Request (url , headers = headers , method = method )
1793
- response_data = ""
1794
- with urllib .request .urlopen (request ) as response :
1795
- response_data = response .read ().decode ('utf-8' )
1796
- json_response = json .loads (response_data )
1797
- return json_response
1798
- except urllib .error .HTTPError as e :
1799
- try :
1800
- errmsg = e .read ().decode ('utf-8' )
1801
- print_with_time (f"Error: { e } - { errmsg } , Make sure your Horde API key and worker name is valid." )
1802
- except Exception as e :
1803
- print_with_time (f"Error: { e } , Make sure your Horde API key and worker name is valid." )
1804
- return None
1805
- except Exception as e :
1806
- print_with_time (f"Error: { e } - { response_data } , Make sure your Horde API key and worker name is valid." )
1807
- return None
1859
+ def make_url_request_horde (url , data , method = 'POST' ):
1860
+ headers = headers = {"apikey" : api_key ,'User-Agent' :'KoboldCppEmbeddedWorkerV2' ,'Client-Agent' :'KoboldCppEmbedWorker:2' }
1861
+ ret = make_url_request (url , data , method , headers )
1862
+ if not ret :
1863
+ print ("Make sure your Horde API key and worker name is valid!" )
1864
+ return ret
1808
1865
1809
1866
current_id = None
1810
1867
current_payload = None
@@ -1816,7 +1873,7 @@ def make_url_request(url, data, method='POST'):
1816
1873
cluster = "https://horde.koboldai.net"
1817
1874
while exitcounter < 10 :
1818
1875
time .sleep (3 )
1819
- readygo = make_url_request (f'{ epurl } /api/v1/info/version' , None ,'GET' )
1876
+ readygo = make_url_request_horde (f'{ epurl } /api/v1/info/version' , None ,'GET' )
1820
1877
if readygo :
1821
1878
print_with_time (f"Embedded Horde Worker '{ worker_name } ' is started." )
1822
1879
break
@@ -1851,7 +1908,7 @@ def make_url_request(url, data, method='POST'):
1851
1908
"softprompts" : [],
1852
1909
"bridge_agent" : BRIDGE_AGENT ,
1853
1910
}
1854
- pop = make_url_request (f'{ cluster } /api/v2/generate/text/pop' ,gen_dict )
1911
+ pop = make_url_request_horde (f'{ cluster } /api/v2/generate/text/pop' ,gen_dict )
1855
1912
if not pop :
1856
1913
punishcounter += 1
1857
1914
print_with_time (f"Failed to fetch job from { cluster } . Waiting 10 seconds..." )
@@ -1874,7 +1931,7 @@ def make_url_request(url, data, method='POST'):
1874
1931
#do gen
1875
1932
while exitcounter < 10 :
1876
1933
if not modelbusy .locked ():
1877
- current_generation = make_url_request (f'{ epurl } /api/v1/generate' , current_payload )
1934
+ current_generation = make_url_request_horde (f'{ epurl } /api/v1/generate' , current_payload )
1878
1935
if current_generation :
1879
1936
break
1880
1937
else :
0 commit comments