Skip to content

Commit 12002d8

Browse files
committed
very basic noscript mode
1 parent b6f952f commit 12002d8

File tree

1 file changed

+90
-33
lines changed

1 file changed

+90
-33
lines changed

koboldcpp.py

+90-33
Original file line numberDiff line numberDiff line change
@@ -630,6 +630,52 @@ async def handle_request(self, genparams, api_format, stream_flag):
630630
except Exception as e:
631631
print(e)
632632

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
633679

634680
def do_GET(self):
635681
global maxctx, maxhordelen, friendlymodelname, KcppVersion, totalgens, preloaded_story
@@ -644,6 +690,10 @@ def do_GET(self):
644690
else:
645691
response_body = self.embedded_kailite
646692

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+
647697
elif self.path.endswith(('/api/v1/model', '/api/latest/model')):
648698
response_body = (json.dumps({'result': friendlymodelname }).encode())
649699

@@ -1744,21 +1794,47 @@ def show_gui_msgbox(title,message):
17441794
except Exception as ex2:
17451795
pass
17461796

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+
17471827
#A very simple and stripped down embedded horde worker with no dependencies
17481828
def run_horde_worker(args, api_key, worker_name):
1749-
import urllib.request
17501829
from datetime import datetime
17511830
global friendlymodelname, maxhordectx, maxhordelen, exitcounter, punishcounter, modelbusy, session_starttime
17521831
epurl = f"http://localhost:{args.port}"
17531832
if args.host!="":
17541833
epurl = f"http://{args.host}:{args.port}"
17551834

1756-
def print_with_time(txt):
1757-
print(f"{datetime.now().strftime('[%H:%M:%S]')} " + txt)
1758-
17591835
def submit_completed_generation(url, jobid, sessionstart, submit_dict):
17601836
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)
17621838
if not reply:
17631839
punishcounter += 1
17641840
print_with_time(f"Error, Job submit failed.")
@@ -1780,31 +1856,12 @@ def submit_completed_generation(url, jobid, sessionstart, submit_dict):
17801856
if exitcounter >= 1:
17811857
exitcounter -= 1
17821858

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
18081865

18091866
current_id = None
18101867
current_payload = None
@@ -1816,7 +1873,7 @@ def make_url_request(url, data, method='POST'):
18161873
cluster = "https://horde.koboldai.net"
18171874
while exitcounter < 10:
18181875
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')
18201877
if readygo:
18211878
print_with_time(f"Embedded Horde Worker '{worker_name}' is started.")
18221879
break
@@ -1851,7 +1908,7 @@ def make_url_request(url, data, method='POST'):
18511908
"softprompts": [],
18521909
"bridge_agent": BRIDGE_AGENT,
18531910
}
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)
18551912
if not pop:
18561913
punishcounter += 1
18571914
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'):
18741931
#do gen
18751932
while exitcounter < 10:
18761933
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)
18781935
if current_generation:
18791936
break
18801937
else:

0 commit comments

Comments
 (0)