Skip to content

Commit fc54833

Browse files
committed
Authenticated Sysinfo
1 parent 078d04e commit fc54833

File tree

5 files changed

+76
-17
lines changed

5 files changed

+76
-17
lines changed

javascript/settings.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,39 @@ onOptionsChanged(function() {
6969
});
7070
});
7171

72+
function downloadSysinfo() {
73+
const pad = (n) => String(n).padStart(2, '0');
74+
const now = new Date();
75+
const YY = now.getFullYear();
76+
const MM = pad(now.getMonth() + 1);
77+
const DD = pad(now.getDate());
78+
const HH = pad(now.getHours());
79+
const mm = pad(now.getMinutes());
80+
const link = document.createElement('a');
81+
link.download = `sysinfo-${YY}-${MM}-${DD}-${HH}-${mm}.json`;
82+
const sysinfo_textbox = gradioApp().querySelector('#internal-sysinfo-textbox textarea');
83+
const content = sysinfo_textbox.value;
84+
if (content.startsWith('file=')) {
85+
link.href = content;
86+
} else {
87+
const blob = new Blob([content], {type: 'application/json'});
88+
link.href = URL.createObjectURL(blob);
89+
}
90+
link.click();
91+
sysinfo_textbox.value = '';
92+
updateInput(sysinfo_textbox);
93+
}
94+
95+
function openTabSysinfo() {
96+
const sysinfo_textbox = gradioApp().querySelector('#internal-sysinfo-textbox textarea');
97+
const content = sysinfo_textbox.value;
98+
if (content.startsWith('file=')) {
99+
window.open(content, '_blank');
100+
} else {
101+
const blob = new Blob([content], {type: 'application/json'});
102+
const url = URL.createObjectURL(blob);
103+
window.open(url, '_blank');
104+
}
105+
sysinfo_textbox.value = '';
106+
updateInput(sysinfo_textbox);
107+
}

modules/api/api.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
from secrets import compare_digest
1818

1919
import modules.shared as shared
20-
from modules import sd_samplers, deepbooru, sd_hijack, images, scripts, ui, postprocessing, errors, restart, shared_items, script_callbacks, infotext_utils, sd_models, sd_schedulers
20+
from modules import sd_samplers, deepbooru, sd_hijack, images, scripts, ui, postprocessing, errors, restart, shared_items, script_callbacks, infotext_utils, sd_models, sd_schedulers, sysinfo
2121
from modules.api import models
2222
from modules.shared import opts
2323
from modules.processing import StableDiffusionProcessingTxt2Img, StableDiffusionProcessingImg2Img, process_images
@@ -33,6 +33,7 @@
3333
from contextlib import closing
3434
from modules.progress import create_task_id, add_task_to_queue, start_task, finish_task, current_task
3535

36+
3637
def script_name_to_index(name, scripts):
3738
try:
3839
return [script.title().lower() for script in scripts].index(name.lower())
@@ -244,6 +245,8 @@ def __init__(self, app: FastAPI, queue_lock: Lock):
244245
self.add_api_route("/sdapi/v1/scripts", self.get_scripts_list, methods=["GET"], response_model=models.ScriptsList)
245246
self.add_api_route("/sdapi/v1/script-info", self.get_script_info, methods=["GET"], response_model=list[models.ScriptInfo])
246247
self.add_api_route("/sdapi/v1/extensions", self.get_extensions_list, methods=["GET"], response_model=list[models.ExtensionItem])
248+
self.add_api_route("/internal/sysinfo", sysinfo.download_sysinfo, methods=["GET"])
249+
self.add_api_route("/internal/sysinfo-download", lambda: sysinfo.download_sysinfo(attachment=True), methods=["GET"])
247250

248251
if shared.cmd_opts.api_server_stop:
249252
self.add_api_route("/sdapi/v1/server-kill", self.kill_webui, methods=["POST"])

modules/sysinfo.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,3 +213,13 @@ def get_config():
213213
return json.load(f)
214214
except Exception as e:
215215
return str(e)
216+
217+
218+
def download_sysinfo(attachment=False):
219+
from fastapi.responses import PlainTextResponse
220+
from datetime import datetime
221+
222+
text = get()
223+
filename = f"sysinfo-{datetime.utcnow().strftime('%Y-%m-%d-%H-%M')}.json"
224+
225+
return PlainTextResponse(text, headers={'Content-Disposition': f'{"attachment" if attachment else "inline"}; filename="{filename}"'})

modules/ui.py

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import datetime
21
import mimetypes
32
import os
43
import sys
@@ -10,10 +9,10 @@
109
import gradio.utils
1110
import numpy as np
1211
from PIL import Image, PngImagePlugin # noqa: F401
13-
from modules.call_queue import wrap_gradio_gpu_call, wrap_queued_call, wrap_gradio_call, wrap_gradio_call_no_job # noqa: F401
12+
from modules.call_queue import wrap_gradio_gpu_call, wrap_queued_call, wrap_gradio_call, wrap_gradio_call_no_job # noqa: F401
1413

1514
from modules import gradio_extensons, sd_schedulers # noqa: F401
16-
from modules import sd_hijack, sd_models, script_callbacks, ui_extensions, deepbooru, extra_networks, ui_common, ui_postprocessing, progress, ui_loadsave, shared_items, ui_settings, timer, sysinfo, ui_checkpoint_merger, scripts, sd_samplers, processing, ui_extra_networks, ui_toprow, launch_utils
15+
from modules import sd_hijack, sd_models, script_callbacks, ui_extensions, deepbooru, extra_networks, ui_common, ui_postprocessing, progress, ui_loadsave, shared_items, ui_settings, timer, ui_checkpoint_merger, scripts, sd_samplers, processing, ui_extra_networks, ui_toprow, launch_utils
1716
from modules.ui_components import FormRow, FormGroup, ToolButton, FormHTML, InputAccordion, ResizeHandleRow
1817
from modules.paths import script_path
1918
from modules.ui_common import create_refresh_button
@@ -1223,16 +1222,5 @@ def quicksettings_hint():
12231222

12241223
app.add_api_route("/internal/profile-startup", lambda: timer.startup_record, methods=["GET"])
12251224

1226-
def download_sysinfo(attachment=False):
1227-
from fastapi.responses import PlainTextResponse
1228-
1229-
text = sysinfo.get()
1230-
filename = f"sysinfo-{datetime.datetime.utcnow().strftime('%Y-%m-%d-%H-%M')}.json"
1231-
1232-
return PlainTextResponse(text, headers={'Content-Disposition': f'{"attachment" if attachment else "inline"}; filename="{filename}"'})
1233-
1234-
app.add_api_route("/internal/sysinfo", download_sysinfo, methods=["GET"])
1235-
app.add_api_route("/internal/sysinfo-download", lambda: download_sysinfo(attachment=True), methods=["GET"])
1236-
12371225
import fastapi.staticfiles
12381226
app.mount("/webui-assets", fastapi.staticfiles.StaticFiles(directory=launch_utils.repo_dir('stable-diffusion-webui-assets')), name="webui-assets")

modules/ui_settings.py

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import gradio as gr
22

3-
from modules import ui_common, shared, script_callbacks, scripts, sd_models, sysinfo, timer, shared_items
3+
from modules import ui_common, shared, script_callbacks, scripts, sd_models, sysinfo, timer, shared_items, paths_internal, util
44
from modules.call_queue import wrap_gradio_call_no_job
55
from modules.options import options_section
66
from modules.shared import opts
77
from modules.ui_components import FormRow
88
from modules.ui_gradio_extensions import reload_javascript
99
from concurrent.futures import ThreadPoolExecutor, as_completed
10+
from pathlib import Path
1011

1112

1213
def get_value_for_setting(key):
@@ -170,7 +171,28 @@ def create_ui(self, loadsave, dummy_component):
170171
loadsave.create_ui()
171172

172173
with gr.TabItem("Sysinfo", id="sysinfo", elem_id="settings_tab_sysinfo"):
173-
gr.HTML('<a href="./internal/sysinfo-download" class="sysinfo_big_link" download>Download system info</a><br /><a href="./internal/sysinfo" target="_blank">(or open as text in a new page)</a>', elem_id="sysinfo_download")
174+
download_sysinfo = gr.Button(value='Download system info', elem_id="internal-download-sysinfo", visible=False)
175+
open_sysinfo = gr.Button(value='Open as text in a new page', elem_id="internal-open-sysinfo", visible=False)
176+
sysinfo_html = gr.HTML('''<a class="sysinfo_big_link" onclick="gradioApp().getElementById('internal-download-sysinfo').click();">Download system info</a><br/><a onclick="gradioApp().getElementById('internal-open-sysinfo').click();">(or open as text in a new page)</a>''', elem_id="sysinfo_download")
177+
sysinfo_textbox = gr.Textbox(label='Sysinfo textarea', elem_id="internal-sysinfo-textbox", visible=False)
178+
179+
def create_sysinfo():
180+
sysinfo_str = sysinfo.get()
181+
if len(sysinfo_utf8 := sysinfo_str.encode('utf8')) > 2 ** 20: # 1MB
182+
sysinfo_path = Path(paths_internal.script_path) / 'tmp' / 'sysinfo.json'
183+
sysinfo_path.parent.mkdir(parents=True, exist_ok=True)
184+
sysinfo_path.write_bytes(sysinfo_utf8)
185+
return gr.update(), gr.update(value=f'file={util.truncate_path(sysinfo_path)}')
186+
return gr.update(), gr.update(value=sysinfo_str)
187+
188+
download_sysinfo.click(
189+
fn=create_sysinfo, outputs=[sysinfo_html, sysinfo_textbox], show_progress=True).success(
190+
fn=None, _js='downloadSysinfo'
191+
)
192+
open_sysinfo.click(
193+
fn=create_sysinfo, outputs=[sysinfo_html, sysinfo_textbox], show_progress=True).success(
194+
fn=None, _js='openTabSysinfo'
195+
)
174196

175197
with gr.Row():
176198
with gr.Column(scale=1):

0 commit comments

Comments
 (0)