mirror of
				https://github.com/KoboldAI/KoboldAI-Client.git
				synced 2025-06-05 21:59:24 +02:00 
			
		
		
		
	Also allow downloading stories as plaintext
This commit is contained in:
		
							
								
								
									
										25
									
								
								aiserver.py
									
									
									
									
									
								
							
							
						
						
									
										25
									
								
								aiserver.py
									
									
									
									
									
								
							| @@ -370,7 +370,7 @@ log.setLevel(logging.ERROR) | |||||||
|  |  | ||||||
| # Start flask & SocketIO | # Start flask & SocketIO | ||||||
| print("{0}Initializing Flask... {1}".format(colors.PURPLE, colors.END), end="") | print("{0}Initializing Flask... {1}".format(colors.PURPLE, colors.END), end="") | ||||||
| from flask import Flask, render_template, Response | from flask import Flask, render_template, Response, request | ||||||
| from flask_socketio import SocketIO, emit | from flask_socketio import SocketIO, emit | ||||||
| app = Flask(__name__) | app = Flask(__name__) | ||||||
| app.config['SECRET KEY'] = 'secret!' | app.config['SECRET KEY'] = 'secret!' | ||||||
| @@ -501,6 +501,26 @@ def index(): | |||||||
|     return render_template('index.html') |     return render_template('index.html') | ||||||
| @app.route('/download') | @app.route('/download') | ||||||
| def download(): | def download(): | ||||||
|  |     save_format = request.args.get("format", "json").strip().lower() | ||||||
|  |  | ||||||
|  |     if(save_format == "plaintext"): | ||||||
|  |         ln = len(vars.actions) | ||||||
|  |  | ||||||
|  |         if(ln > 0): | ||||||
|  |             chunks = collections.deque() | ||||||
|  |             for key in reversed(vars.actions): | ||||||
|  |                 chunk = vars.actions[key] | ||||||
|  |                 chunks.appendleft(chunk) | ||||||
|  |  | ||||||
|  |         if(ln > 0): | ||||||
|  |             txt = vars.prompt + "".join(chunks) | ||||||
|  |         elif(ln == 0): | ||||||
|  |             txt = vars.prompt | ||||||
|  |          | ||||||
|  |         save = Response(txt) | ||||||
|  |         save.headers.set('Content-Disposition', 'attachment', filename='%s.txt' % path.basename(vars.savedir)) | ||||||
|  |         return(save) | ||||||
|  |  | ||||||
|     # Build json to write |     # Build json to write | ||||||
|     js = {} |     js = {} | ||||||
|     js["gamestarted"] = vars.gamestarted |     js["gamestarted"] = vars.gamestarted | ||||||
| @@ -520,6 +540,7 @@ def download(): | |||||||
|                 "selective": wi["selective"], |                 "selective": wi["selective"], | ||||||
|                 "constant": wi["constant"] |                 "constant": wi["constant"] | ||||||
|             }) |             }) | ||||||
|  |      | ||||||
|     save = Response(json.dumps(js, indent=3)) |     save = Response(json.dumps(js, indent=3)) | ||||||
|     save.headers.set('Content-Disposition', 'attachment', filename='%s.json' % path.basename(vars.savedir)) |     save.headers.set('Content-Disposition', 'attachment', filename='%s.json' % path.basename(vars.savedir)) | ||||||
|     return(save) |     return(save) | ||||||
| @@ -1964,11 +1985,9 @@ def saveRequest(savpath): | |||||||
|  |  | ||||||
|         if(ln > 0): |         if(ln > 0): | ||||||
|             chunks = collections.deque() |             chunks = collections.deque() | ||||||
|             i = 0 |  | ||||||
|             for key in reversed(vars.actions): |             for key in reversed(vars.actions): | ||||||
|                 chunk = vars.actions[key] |                 chunk = vars.actions[key] | ||||||
|                 chunks.appendleft(chunk) |                 chunks.appendleft(chunk) | ||||||
|                 i += 1 |  | ||||||
|  |  | ||||||
|         if(ln > 0): |         if(ln > 0): | ||||||
|             txt = vars.prompt + "".join(chunks) |             txt = vars.prompt + "".join(chunks) | ||||||
|   | |||||||
| @@ -839,6 +839,7 @@ $(document).ready(function(){ | |||||||
| 	button_saveas     = $('#btn_saveas'); | 	button_saveas     = $('#btn_saveas'); | ||||||
| 	button_savetofile = $('#btn_savetofile'); | 	button_savetofile = $('#btn_savetofile'); | ||||||
| 	button_download   = $('#btn_download'); | 	button_download   = $('#btn_download'); | ||||||
|  | 	button_downloadtxt= $('#btn_downloadtxt'); | ||||||
| 	button_load       = $('#btn_load'); | 	button_load       = $('#btn_load'); | ||||||
| 	button_loadfrfile = $('#btn_loadfromfile'); | 	button_loadfrfile = $('#btn_loadfromfile'); | ||||||
| 	button_import     = $("#btn_import"); | 	button_import     = $("#btn_import"); | ||||||
| @@ -1273,6 +1274,10 @@ $(document).ready(function(){ | |||||||
| 		window.open("/download", "_blank"); | 		window.open("/download", "_blank"); | ||||||
| 	}); | 	}); | ||||||
|  |  | ||||||
|  | 	button_downloadtxt.on("click", function(ev) { | ||||||
|  | 		window.open("/download?format=plaintext", "_blank"); | ||||||
|  | 	}); | ||||||
|  | 	 | ||||||
| 	button_load.on("click", function(ev) { | 	button_load.on("click", function(ev) { | ||||||
| 		socket.send({'cmd': 'loadlistrequest', 'data': ''}); | 		socket.send({'cmd': 'loadlistrequest', 'data': ''}); | ||||||
| 	}); | 	}); | ||||||
|   | |||||||
| @@ -41,7 +41,8 @@ | |||||||
| 										<a class="dropdown-item" href="#" id="btn_save">Save</a> | 										<a class="dropdown-item" href="#" id="btn_save">Save</a> | ||||||
| 										<a class="dropdown-item" href="#" id="btn_saveas">Save As</a> | 										<a class="dropdown-item" href="#" id="btn_saveas">Save As</a> | ||||||
| 										<a class="dropdown-item" href="#" id="btn_savetofile">Save To File...</a> | 										<a class="dropdown-item" href="#" id="btn_savetofile">Save To File...</a> | ||||||
| 										<a class="dropdown-item" href="#" id="btn_download">Download Current Story</a> | 										<a class="dropdown-item" href="#" id="btn_download">Download Story as JSON</a> | ||||||
|  | 										<a class="dropdown-item" href="#" id="btn_downloadtxt">Download Story as Plaintext</a> | ||||||
| 									</div> | 									</div> | ||||||
| 								</li> | 								</li> | ||||||
| 								<li class="nav-item dropdown"> | 								<li class="nav-item dropdown"> | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 Gnome Ann
					Gnome Ann